Convolution (conv) with same size output

You can apply the following function to the result of DSP.conv() in order to mimic the behaviour of the parameter “save” from scipy.stats.fftconvolve and center the result of the convolution.

function centered(arr, input_arr_shape)
    # new_arr_shape is the size of the input array during convolution with DSP.conv(input_arr1, input_arr2)
    # Get the current shape of the array i.e. the result of DSP.conv()
    currshape = size(arr)
    
    # Calculate start and end indices for slicing
    startind = (currshape .- input_arr_shape) .÷ 2 .+ 1
    endind = startind .+ input_arr_shape .- 1
    
    # Create the slice objects
    slices = Tuple(startind[i]:endind[i] for i in 1:length(endind))
    
    # Return the centered portion of the array
    return arr[slices...]
end
1 Like