
Hi, Coming from IDL, I've always wondered why there was no 'smooth' function in SciPy, seems like a standard/common operation. But I found an implementation in the Cookbook: http://wiki.scipy.org/Cookbook/SignalSmooth that does what I would expect such a function to do. But I found an indexing bug and a few usability and coding style improvement to the Cookbook. My version of the function is in attachment. Any reason why this function is not part of Scipy? Thanks, Nicolas

On 6/18/15 09:06 , Nicolas Petitclerc wrote:
Hi, Coming from IDL, I've always wondered why there was no 'smooth' function in SciPy, seems like a standard/common operation. But I found an implementation in the Cookbook: http://wiki.scipy.org/Cookbook/SignalSmooth that does what I would expect such a function to do.
But I found an indexing bug and a few usability and coding style improvement to the Cookbook. My version of the function is in attachment.
Any reason why this function is not part of Scipy?
Thanks, Nicolas
You may also be interested in this: https://pypi.python.org/pypi/scikits.datasmooth/0.61 Scipy has some other smoothing capabilities in interpolate (smoothing splines) and signal (Savitzky-Golay filter). I'd like to see these smoothing routines collected in one place, whether that is inside scipy or a separate package. Perhaps a high-level smoothing function could be written that performs subcalls to various routines. Regards, Jonathan

"Signal smoothing" is just another name for lowpass filtering. Construct your filter and pass it to scipy.signal.lfilter.
My point exactly, you shouldn't have to "construct your filter" to do a simple smoothing. I think it would be great to have a wrapper function, where you just have to select the smoothing method and window size. Nic On Thu, Jun 18, 2015 at 7:18 PM, Sturla Molden <sturla.molden@gmail.com> wrote:
Nicolas Petitclerc <npetitclerc@gmail.com> wrote:
Any reason why this function is not part of Scipy?
"Signal smoothing" is just another name for lowpass filtering. Construct your filter and pass it to scipy.signal.lfilter.
Sturla
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev

Nicolas Petitclerc <npetitclerc@gmail.com> wrote:
My point exactly, you shouldn't have to "construct your filter" to do a simple smoothing. I think it would be great to have a wrapper function, where you just have to select the smoothing method and window size.
There is one function to get the window and another to apply it to the signal. I don't really understand the problem. You save one function call. Why? Sturla

Sturla Molden <sturla.molden@gmail.com> wrote:
There is one function to get the window and another to apply it to the signal. I don't really understand the problem. You save one function call. Why?
But sure, if it is of any value I would gladly make such a function and post a PR. It would only be a few lines of Python, excluding the docstring. Sturla

On Fri, Jun 19, 2015 at 8:35 PM Sturla Molden <sturla.molden@gmail.com> wrote:
Sturla Molden <sturla.molden@gmail.com> wrote:
There is one function to get the window and another to apply it to the signal. I don't really understand the problem. You save one function call. Why?
But sure, if it is of any value I would gladly make such a function and post a PR. It would only be a few lines of Python, excluding the docstring.
Well, the thing is, signal smoothing is not really a well-defined operation. There are all sorts of criteria one could use - low-pass filtering with various filter bandpasses, say, or median smoothing, for example, if you're worried about outliers, or some kind of weighted smoothing if your data points have different uncertainties, or spline-based smoothing if you have continuity requirements and don't mind it being a global operation, or... scipy provides the tools to do all of these, but expects the user to know what kind of smoothing they want and how to implement it. An actual smoothing toolkit would presumably provide some kind of uniform interface and guidelines on which one to use when. Anne

Anne Archibald <archibald@astron.nl> wrote:
Well, the thing is, signal smoothing is not really a well-defined operation.
As you say signal smoothing can mean many things. Signal smoothing often means using a linear lowpass filter that is well-behaved in the time domain. Examples are moving average, Gaussian filter, RC filter (aka single-pole recursive filter), or convolution with a simple window function (e.g. Hamming or von Hann). A signal smoother is usually implemented with zero phase (cf. scipy.signal.filtfilt). Signal smoothing also often means Savitzky-Golay filtering. Another thing is that we should be careful not to implement things that are "too simple". It can mean that we are allowing people who don't know what they are doing to shoot themselves in the leg. Presumably anyone who uses scipy.signal should know how to smooth a signal or blur an image. Otherwise there are many excellent textbooks on DSP. Sturla

I think it would be worth it, for the cases when you plot a 1D array, it looks very messy(noisy) and you just want to quickly see the general trend. A quick way to apply the most common methods: flat and Gaussian filters. A few more like Savitzky-Golay, lowess would be nice, and simple to do, but the idea would be to add convenience for the most basic operations. Nic On Fri, Jun 19, 2015 at 10:28 PM, Sturla Molden <sturla.molden@gmail.com> wrote:
Anne Archibald <archibald@astron.nl> wrote:
Well, the thing is, signal smoothing is not really a well-defined operation.
As you say signal smoothing can mean many things.
Signal smoothing often means using a linear lowpass filter that is well-behaved in the time domain. Examples are moving average, Gaussian filter, RC filter (aka single-pole recursive filter), or convolution with a simple window function (e.g. Hamming or von Hann). A signal smoother is usually implemented with zero phase (cf. scipy.signal.filtfilt). Signal smoothing also often means Savitzky-Golay filtering.
Another thing is that we should be careful not to implement things that are "too simple". It can mean that we are allowing people who don't know what they are doing to shoot themselves in the leg. Presumably anyone who uses scipy.signal should know how to smooth a signal or blur an image. Otherwise there are many excellent textbooks on DSP.
Sturla
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev

Nicolas Petitclerc <npetitclerc@gmail.com> wrote:
I think it would be worth it, for the cases when you plot a 1D array, it looks very messy(noisy) and you just want to quickly see the general trend. A quick way to apply the most common methods: flat and Gaussian filters. A few more like Savitzky-Golay, lowess would be nice, and simple to do, but the idea would be to add convenience for the most basic operations.
Lowess (aka loess) is a scatterplot smoother, not a signal smoother. It more properly belongs to the realm of statsmodels (which actually has it). Smoothing splines and kernel regression are other alternatives to lowess. I am not sure scipy.signal should implement a method to deal with unevenly sampled data. "Smoothing" is also sometimes used errorneously for denoising, which includes methods such as Wiener filtering and wavelet shrinkage. Sturla

I just like the old boxcar average (astronomical spectral data) and bypass scipy for that for stsci.convolve. The thing is that with real data it is often useful to bring out the signal in the noisy parts. Sometimes its just handy because of instrumental noise being present and you want to get rid of (most of ) it. By the time you need more sophisticated filtering, you usually have more control over your system/experiment or gotten enough understanding to know you can/should use this or that method. I think it depends a lot on the field or application you're using. Probably it makes more sense to write your own basic filter and use that consistently, rather then adding such functions to the toolkit. However, having good examples for different applications somewhere (with perhaps example filters) would be very useful. It still find the ones in the documentation a bit to far removed from my field (to my taste), and there is a lot that actually can be done with filtering. (I must admit that I do not look into textbooks anymore, they are just to difficult to get access to for my taste). On Sat, Jun 20, 2015 at 12:58 AM, Sturla Molden <sturla.molden@gmail.com> wrote:
Nicolas Petitclerc <npetitclerc@gmail.com> wrote:
I think it would be worth it, for the cases when you plot a 1D array, it looks very messy(noisy) and you just want to quickly see the general trend. A quick way to apply the most common methods: flat and Gaussian filters. A few more like Savitzky-Golay, lowess would be nice, and simple to do, but the idea would be to add convenience for the most basic operations.
Lowess (aka loess) is a scatterplot smoother, not a signal smoother. It more properly belongs to the realm of statsmodels (which actually has it). Smoothing splines and kernel regression are other alternatives to lowess. I am not sure scipy.signal should implement a method to deal with unevenly sampled data.
"Smoothing" is also sometimes used errorneously for denoising, which includes methods such as Wiener filtering and wavelet shrinkage.
Sturla
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev
-- * * * * * * * * http://www.mssl.ucl.ac.uk/~npmk/ * * * * Dr. N.P.M. Kuin (n.kuin@ucl.ac.uk) phone +44-(0)1483 (prefix) -204927 (work) mobile +44(0)7806985366 skype ID: npkuin Mullard Space Science Laboratory – University College London – Holmbury St Mary – Dorking – Surrey RH5 6NT– U.K.

On Sat, Jun 20, 2015 at 1:58 AM, Sturla Molden <sturla.molden@gmail.com> wrote:
Nicolas Petitclerc <npetitclerc@gmail.com> wrote:
I think it would be worth it, for the cases when you plot a 1D array, it looks very messy(noisy) and you just want to quickly see the general trend. A quick way to apply the most common methods: flat and Gaussian filters. A few more like Savitzky-Golay, lowess would be nice, and simple to do, but the idea would be to add convenience for the most basic operations.
Lowess (aka loess) is a scatterplot smoother, not a signal smoother. It more properly belongs to the realm of statsmodels (which actually has it). Smoothing splines and kernel regression are other alternatives to lowess. I am not sure scipy.signal should implement a method to deal with unevenly sampled data.
Note that Matlab manages to put a simple moving average, loes/lowess and Savitsky-Golay in a single function [1]. I tend to agree that this isn't the best idea for Scipy though, and that scipy.signal shouldn't deal with unevenly spaced data. Ralf [1] http://nl.mathworks.com/help/curvefit/smooth.html
participants (6)
-
Anne Archibald
-
Jonathan Stickel
-
Nicolas Petitclerc
-
Paul Kuin
-
Ralf Gommers
-
Sturla Molden