# How to smooth a signal?

The easiest way to smooth a signal is by moving window average.

A more advanced way is to use a Savitzky-Golay filter. From wikipedia:

The main advantage of this approach is that it tends to preserve features of the distribution such as relative maxima, minima and width, which are usually 'flattened' by other adjacent averaging techniques (like moving averages, for example).

There is also a whole range of window functions. As I understand this: any finite filter wil cause spectral leakage, but the moving window average is the worst. E.g. a Gaussian window is better in this respect.

Is flattening and spectral leakage the same?

When should one use Savitzky-Golay and when should one use Gauss, Hann, Hamming etc?

• Window functions are typically used when spectral analysis (i.e. in the frequency domain) of a signal is desired. Features like minima, maxima, and peak width are time-domain features. You should use the tool that is most appropriate for the job that you want to do (i.e. which domain you're concerned with). Commented Mar 26, 2012 at 14:37
• Thanks. DFT requires a periodic input so windowing in the context of spectral analysis is a way to get around this? But e.g. in image analysis convolving with a 2D gaussian is often used as a lowpass filter; so window functions are not only used for spectral analysis. The term windowing seems to imply this use whilst the term convolution seems to imply a lowpass filtering.
– Andy
Commented Mar 26, 2012 at 16:20
• I keep it easy by just always using S-G. Only if there's a special reason, such as I'm too lazy to implement it or look it up in a signal processing library, do I choose something different and simple. Commented Oct 31, 2012 at 5:03

When using a moving average filter, or rectangular convolution in the time domain, the time domain smoothing and so-called spectral leakage are nearly opposing effects. The smoothing is due to removing high frequency content, and the leakage is the name given for portions of this frequency content that end up not being removed. Since sharp peaks and transients can be partially composed of high frequency content, smoothing by this kind of convolution can diminish them.

Time-domain convolution windows other than rectangular may remove less of the very nearby frequencies, but better remove more of the far away frequency content (relative to some transition frequency, which is related approximately to the window width).

An adaptive polynomial regression will remove different spectral characteristics from a signal at different points in time, which could either expose or hide "important" elements of the signal, depending on the signal, and what you are looking for.

• Thanks. Your sentence about time domain smoothing and spectral leakage being nearly opposing effects clarified things for me. When smoothing one wants to remove high frequencies. Spectral leakage is a failure to do so; some higher frequencies (the sidelobes) are not filtered out. Removing higher frequency from a peak leads to a widening of the peak. This is something we notice in seismic data where only low frequencies can travel deep into the earth. The thing I do not understand is how can Savitzky-Golay both smooth and not flatten local peaks?
– Andy
Commented Mar 27, 2012 at 7:12
• This adaptive filter doesn't do both at the same time. It only "smooths" or removes high frequencies that can't be locally represented by a kth order polynomial. If the noise (or peak) can, then the poly matches the data and nothing is filtered out. Commented Mar 27, 2012 at 16:29

When using a window you are essentially giving more or less importance to some aspect of the input signal.

For example, if you have a speech synthesis model that generates words by taking sounds of letters and pasting them together,end to end, with out any modification this would be the equivalent of a rectangular window in the time domain. Now this might not sound as it should when played back, very choppy I would assume... But what if we knew how much of one letter we wanted to flow into another and used a shaped window before overlapping the tails of each letter, such as a gaussian.

(Please note this example is for illustration only, audio processing is not my specialty but it is something that can be understood easily)

Windows can be applied in the frequency or time domain, and in it really needs to be known what property of the signal you wish to enhance or remove before you know which to use but there are some guidelines and some trade offs for different types of windows. The link you provided to the windowing functions describes more in depth the trade-offs you face for choosing certain windows.