Convolution is often used for smoothing noisy data; a typical use will keep the 
'same' length of data and may look like this:

>    convol = 2**-np.linspace(-2,2,100)**2; 
>    y2 = np.convolve(y,convol/np.sum(convol), mode='same') ## simple smoothing
>    ax.plot(x, y2, label="simple smoothing", color='g')

However, when the smoothed curve has some nonzero background value at its 
edges, this convolution mode internally pads it with zeros, resulting in the 
curve looking like moustache of Frank Zappa.

I made an example plot illustrating this here: 
https://www.fzu.cz/~dominecf/misc/numpy_smoothing_example.png. Such a result, 
i.e. the green curve, is not publication ready!

1) One way around is to np.pad(..., mode='edge'), then convolve & properly 
truncate the curve back to its original length. This is not a correct approach, 
however, as it makes the curve edges smooth, but their actual shape becomes 
very sensitive to the pointwise noise. Moreover, it artificially removes the 
curve's slope at its edges.

2) Another way around is to generate an auxiliary "Zappa's moustache" by 
applying the same convolution to a fresh array of np.ones_like(y). Then one can 
normalize the convolved curve by this auxiliary function. This has only one 
downside of keeping the curve more noisy at its edges, which however appears 
more scientifically honest to me - at the dataset edges one simply has less 
means to filter out noise.

>    convol = 2**-np.linspace(-2,2,100)**2; 
>    norm = np.convolve(np.ones_like(y),convol/np.sum(convol), mode='same')
>    y2 = np.convolve(y,convol/np.sum(convol), mode='same')/norm ## simple 
> smoothing
>    ax.plot(x, y2, label="approach 2", color='k')

In my experimental work, I am missing this behaviour of np.convolve in a single 
function. I suggest this option should be accessible numpy under the 
mode="normalized" option. (Actually I believe this could have been set as 
default, but this would break compatibility.) 

Thanks for consideration,
Filip
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to