6
$\begingroup$

First the simple questions: Is there an effect on the Nyquist frequency when I apply a moving average filter on the raw data before I downsample? And what does this do to aliased frequencies?

Background of my question, to help you answer in a way that I can understand it:

I am working with very large amounts of data from a production operation (flow, temperature, pressure, etc.). The data is collected at a ridiculously high sampling rate, and also the sampling and data are not fully reliable: sometimes values are missing, measurement artifacts cause spikes in the time trends, and often there is variation in the sampling frequency. This makes any analyses "unpleasant".

I started with downsampling by simply taking 1 min spaced values from the database, and I worked in Excel because it helped us try stuff out quickly. We gained experience with all the calculations we do, and we are now moving to different software. With this change, I also would like to take a better approach to determining a suitable sampling rate. I am completely untrained and inexperienced with DSP, so I read an introductory text. Now I know that I have to think about the Nyquist frequency. In a messy system like this, there are low frequencies that I would like to study, while there are also things happening at high frequencies (such as the aforementioned spikes) that I would rather filter out.

The new software has two options. The first is to simply return the value that happens to be in the database at the sampling rate. This is what I did to get to Excel. It has the downside that the missing values return a (NULL) result and handling these is a pain in the behind. The second is to return the average value over the length of each sampling period. I think this is equivalent to applying a moving average filter to the raw data, and then resampling at the same rate as the MA length. This method is more robust (returns (NULL) much less often) and mitigates the effect of artifacts.

I am ready to choose between the two downsampling methods, and to make a proper selection of suitable sampling rates rather than reducing the rate by trial and error until the results "don't look right". So, any insights would be appreciated.

Thank you for your time.

$\endgroup$
4
  • 5
    $\begingroup$ "ridiculously high": as a German engineer, I'd rather have numbers than humor ;) No, seriously, chances are what you consider to be a high rate is going to look very low to a lot of us (The signals I deal with in real time are typically more in the order of 10⁶ per second, not 1/60 sample per second, but complications arise if you need to keep a couple thousand of these low-rate streams coherent), and appropriate processing techniques would depend on the actual rate. $\endgroup$ Nov 23, 2022 at 9:08
  • 5
    $\begingroup$ as a French engineer, I'd rather have both numbers AND humor! $\endgroup$
    – Jdip
    Nov 23, 2022 at 10:21
  • $\begingroup$ The sampling rate to you seems low, I’m certain. They spacing is in the order of 10-100 microseconds for many signals. I am studying production runs that run continuously for up to about a week. A very fast change in the physical process is in the order of minutes. But tens of minutes or even up to an hour is typical. So there is no value in microsecond data.., $\endgroup$
    – W_vH
    Nov 23, 2022 at 10:29
  • 4
    $\begingroup$ As an American engineer living in the T**** era, I don't give a rat's ass about numbers and have no sense of humor. $\endgroup$ Nov 23, 2022 at 19:38

3 Answers 3

7
$\begingroup$

There is no effect on the Nyquist frequency, which is only dependent on the sample rate.

Decimating is the combination of low-pass filtering + downsampling (which is the term for discarding samples only).

Assuming an original sampling rate $f_s$ and decimation factor $K$, the low-pass filtering step is to make sure that there is no aliasing when decimating to the new frequency $f_s/K$. Per Nyquist, the ideal low-pass filter used should then pass the frequency content below $f_s/(2K)$ and filter out the frequency content above. Of course an ideal filter is un-realizable, but filter design is flexible and we can approximate these specs as close as we virtually want.

The method you mention does the low-pass filtering through a moving average filter of length $K$, and the down-sampling by keeping only every $K^{th}$ sample.

This might be ok for your purposes. However, the problem with using a moving average filter as the low-pass in a decimating routine is the high side-lobes past Nyquist that will result in aliasing in your decimated data. Moreover, any other length than $K$ will not work, making this filter completely rigid, as opposed to the flexibility offered by what comes next:

A better filter would be a FIR filter of order $N$ with $N$ large enough to satisfy your conditions.

For illustration purposes, let's look at a decimate-by-10 moving average filter (FIR filter with fixed length $K=10$, i.e order $N=9$) vs. three FIR filters with $N=30$, $N=60$ and $N=120$: enter image description here

Note the x-axis has units of normalized frequency (in rad/samples), so a value of $1$ maps to $f_s/2$.

Clearly the higher order filters have better stop band attenuation, which will be better to prevent aliasing. Since we're downsampling by $K=10$, the low-pass filter should keep frequency content below $0.1$ and filter out frequency content above $0.1$. Let's zoom in around there:

enter image description here

You can see the higher the FIR order, the closer to our ideal filter we get.

There are multiple ways to design low-pass filters (you could also use a lower-order IIR, which is what most software packages do), but that's a different topic.

Bottom line

  • Figure out what frequencies you're actually interested in (see @MarcusMuller's answer).
  • Deduce the decimation factor $K$: remember, after decimation, you'll be left with only frequency content below $f_s/(2K)$
  • Design an appropriate low-pass filter. If you go the FIR route, bear in mind that the larger $K$ is, the larger $N$ needs to be to be more frequency selective ($N = 2K$ is a good start). If $K$ is ridiculously high, you probably want to go the IIR route.
$\endgroup$
2
  • 1
    $\begingroup$ Very nice and well-illustrated! $\endgroup$ Nov 23, 2022 at 10:18
  • $\begingroup$ Thank you very much for this detailed explanation. I now have a lot to think about. $\endgroup$
    – W_vH
    Nov 23, 2022 at 21:05
3
$\begingroup$

Is there an effect on the Nyquist frequency when I apply a moving average filter on the raw data before I downsample?

Nyquist frequency only depends on sample rate. So, No. That was a quick and easy answer!

Now, looking at the rest of your question, I think you're looking for an anti-aliasing filter that you can use to low-pass filter your sampled signal before you downsample it. The moving average is one such filter, but it's probably not a good idea.

Which filter you'd choose, and also, how much you can downsample your signal and still have it contain meaningful information, depends on what the spectrum of the signal you're interested is.

We don't have that signal – but you do! So, maybe load your signal (python with numpy is a great tool for such tasks), and plot a Power Spectral Density (PSD) estimate (Python and scipy are nice). Look at the frequencies you need to preserve in your signal. Then use a filter designed to suppress anything else, and downsample as far as possible without aliasing.

$\endgroup$
1
$\begingroup$

You write "the sampling and data are not fully reliable: sometimes values are missing, measurement artefacts cause spikes". These are non linear effects which you won't eliminate with a linear filter (moving average etc), it will simply smear them around. I suggest you look at a median filter (which is non linear) to clean up your data by rejecting anomalies. You can then look at a low pass filter and down-sampling.
As an alternative - fix the real problem which is messing up your data!

$\endgroup$
1
  • $\begingroup$ If the anomalies are utterly meaningless, filter them out with a median filter and throw them away. If they're meaningful in some way (i.e. if they indicate the state of health of some sensor), then log when you throw one away, or how many you throw away in an hour, or whatever. I'm not sure how easy this would be to build into a median filter, but I'm sure there's a way to do it. $\endgroup$
    – TimWescott
    Nov 24, 2022 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.