Use gaussian_filter
instead of fftconvolve
.
Compare the behavior of fftconvolve
(with mode='same'
) to gaussian_filter
(with mode='constant'
):
import numpy as np
from scipy.signal import fftconvolve
from scipy.ndimage import gaussian_filter
x = np.linspace(-3, 3, 51)
y = np.sin(x)
blurring_kernel = np.zeros_like(x)
blurring_kernel[25] = 1
blurring_kernel = gaussian_filter(blurring_kernel, sigma=3)
a = fftconvolve(y, blurring_kernel, mode='same')
b = gaussian_filter(y, sigma=3, mode='constant')
print max(abs((a - b))) # a and b are identical
"""
You have to assume *something* outside the boundary.
This is probably what you want:
"""
c = gaussian_filter(y, sigma=3, mode='reflect')
You have to assume something outside the boundary of your signal. fftconvolve
assumes zeros. gaussian_filter
lets you choose from several different assumptions, and I find one of these is usually closer to my needs than assuming zeros.
For a quick fix, you could use gaussian_filter
, or else pad your signal with something nonzero, to get the same effect at the boundary, perhaps using pad
.