18
$\begingroup$

What is smoothing and how can I do it?

I have an array in Matlab which is the magnitude spectrum of a speech signal (the magnitude of 128 points of FFT). How do I smooth this using a moving average? From what I understand, I should take a window size of a certain number of elements, take average, and this becomes the new 1st element. Then shift the window to the right by one element, take average which becomes the 2nd element, and so on. Is that really how it works? I am not sure myself since if I do that, in my final result I will have less than 128 elements. So how does it work and how does it help to smooth the data points? Or is there any other way I can do smoothing of data?

EDIT: link to followup question

$\endgroup$
2
  • $\begingroup$ for a spectrum you probably want to average together (in the time dimension) multiple spectra rather than a running average along the frequency axis of a single spectrum $\endgroup$
    – endolith
    Oct 16, 2012 at 1:04
  • $\begingroup$ @endolith both are valid techniques. Averaging in the frequency domain (sometimes called a Danielle Periodogram) is the same as windowing in the time domain. Averaging multiple periodograms ("spectra") is an attempt to mimic the ensemble averaging required of the true Periodogram (this is called the Welch Periodogram). Also, as a matter of semantics, I would argue that "smoothing" is non-causual low-pass filtering. See Kalman filtering vs Kalman smoothing, Wiener filtering v Wiener smoothing, etc. There is a nontrivial distinction and it's implementation dependent. $\endgroup$
    – Bryan
    Dec 12, 2012 at 19:18

5 Answers 5

24
$\begingroup$

Smoothing can be done in many ways, but in very basic and general terms it means that you even out a signal, by mixing its elements with their neighbors. You smear/blur the signal a bit in order to get rid of noise. For example, a very simple smoothing technique would be, to recalculate every signal element f(t) to as 0.8 of the original value, plus 0.1 of each of its neighbors:

f'(t) = 0.1*f(t-1) + 0.8*f(t) + 0.1*f(t+1)

Note how the multiplication factors, or weights, add up to one. So if the signal is fairly constant, smoothing doesn't change it much. But if the signal contained a sudden jerky change, then the contribution from its neighbors will help to clear up that noise a bit.

The weights you use in this recalculation function can be called a kernel. A one-dimensional Gaussian function or any other basic kernel should do in your case.

Nice example of one particular kind of smoothing:

Above: unsmoothed signal
Below: smoothed signal

enter image description here

Examples of a few kernels:

enter image description here

$\endgroup$
4
  • $\begingroup$ so is this a weighted moving average? Is this called having a window size of 3? What about the 1st and the last element? And how would this be modified if I have an array of 128 elements and I want to use a window of 16 or 32 elements? $\endgroup$
    – user13267
    Oct 15, 2012 at 6:54
  • $\begingroup$ @user13267: Yes, you could say a smoothing kernel is a weighted moving average. If you use a uniform kernel (see second image), it's just a plain moving average. You're right about window size. For dealing with the edges, there are three basic approaches: 1) zero-padding your data, 2) repeating the last value, 3) mirroring the signal. In all cases you make some pretend data so that your kernel doesn't fall off into nothingness. $\endgroup$
    – Junuxx
    Oct 15, 2012 at 7:00
  • $\begingroup$ wouldn't zero padding count as falling into nothingness? At the end of the moving average process my new "averaged" data set should have the same number of data as the original shouldn't it? then if I zero pad it at the beginning or the end, or repeat the last data, won't it bias the average value at the edges of the array? And how would mirroring the signal help in terms of number of data terms? Is there any simple tutorial for this anywhere which shows how the process takes place for, say, 32 data points and a window size of 4 or 5? $\endgroup$
    – user13267
    Oct 15, 2012 at 8:21
  • 2
    $\begingroup$ If you want your smoothed dataset to have the same length as the original dataset, you have to "make up" data at the endpoints. Any choice you make for how to create that data biases the average in some way. Treating the out-of-bounds data as a mirror of the real dataset (i.e. assuming that sample N+1 is the same as N-1, N+2 = N-2, etc.) will retain the frequency spectrum characteristics of the end parts of the signal, whereas assuming a zero-or-non-zero repeat will make it appear that all frequencies are rolling off at the ends. $\endgroup$ Oct 15, 2012 at 18:17
8
$\begingroup$

In addition to the nice answer of Junuxx I would like to drop a few notes.

  • Smoothing is related to filtering (unfortunately quite vague Wikipedia article) - you should pick the smoother based on it's properties.

  • One of my favorites is the median filter. This is an example of a non-linear filter. It has some interesting properties, it preserves "edges" and is quite robust under large noise.

  • If you have a model how your signal behaves a Kalman filter is worth a look. Its smoothing is actually a Bayesian maximum likelihood estimation of the signal based on observations.

$\endgroup$
0
4
$\begingroup$

You can find the entire matlab code for smoothing moving average filter for varying lengths of taps. www.gaussianwaves.com/2010/11/moving-average-filter-ma-filter-2/

$\endgroup$
4
$\begingroup$

Smoothing implies using information from neighboring samples in order to change the relationship between neighboring samples. For finite vectors, at the ends, there is no neighboring information to one side. Your choices are: don't smooth/filter the ends, accept a shorter resulting smoothed vector, make up data and smooth with that (depends on the accuracy/usefulness of any predictions off the ends), or maybe using different asymmetric smoothing kernels at the ends (which ends up shortening the information content in the signal anyway).

$\endgroup$
1
$\begingroup$

Others have mentioned how you do smoothing, I'd like to mention why smoothing works.

If you properly oversample your signal, it will vary relatively little from one sample to the next (sample = timepoints, pixels, etc), and it is expected to have an overall smooth appearance. In other words, your signal contains few high frequencies, i.e. signal components that vary at a rate similar to your sampling rate.

Yet, measurements are often corrupted by noise. In a first approximation, we usually consider the noise to follow a Gaussian distribution with mean zero and a certain standard deviation that is simply added on top of the signal.

To reduce noise in our signal, we commonly make the following four assumptions: noise is random, is not correlated among samples, has a mean of zero, and the signal is sufficiently oversampled. With these assumptions, we can use a sliding average filter.

Consider, for example, three consecutive samples. Since the signal is highly oversampled, the underlying signal can be considered to change linearly, which means that the average of the signal across the three samples would equal the true signal at the middle sample. In contrast, the noise has mean zero and is uncorrelated, which means that its average should tend to zero. Thus, we can apply a three-sample sliding average filter, where we replace each sample with the average between itself and its two adjacent neighbors.

Of course, the larger we make the window, the more the noise will average out to zero, but the less our assumption of linearity of the true signal holds. Thus, we have to make a trade-off. One way to attempt to get the best of both worlds is to use a weighted average, where we give farther away samples smaller weights, so that we average noise effects from larger ranges, while not weighting true signal too much where it deviates from our linearity assumption.

How you should put the weights depends on the noise, the signal, and computational efficiency, and, of course, the trade-off between getting rid of the noise and cutting into the signal.

Note that there has been a lot of work done in the last few years to allow us to relax some of the four assumptions, for example by designing smoothing schemes with variable filter windows (anisotropic diffusion), or schemes that don't really use windows at all (nonlocal means).

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.