# Resampling time series to regular array, then downsampling (Butterworth)

Long time reader, first time poster. I have a few very simple questions that are troubling me and I am hoping that one of you guys can help me out.

Setup & Aim: I have a time series that I want to downsample, and I simply want to run a lowpass filter on it before doing so to avoid aliasing. I am using Python (SciPy) but it looks like MATLAB behaves similarly, neither are really relevant for these questions.

My original time series is sampled at $0.5\textrm{ ms}$ ($2000\textrm{ Hz}, f_{\rm Nyquist}=1000\textrm{ Hz}$) and I want to resample to $2\textrm{ ms}$ ($250\textrm{ Hz}, f_{\rm Nyquist}=250\textrm{ Hz}$), so I must apply an anti-alias filter that cuts off any frequencies $> 250\textrm{ Hz}$, and then downsample. So far, so good.

In Python, it looks like a Butterworth Filter is the way to go, which requires a normalised frequency $\omega_n$. My understanding is that in my case $\omega_n = 250\textrm{ Hz}/1000\textrm{ Hz} = 0.25$.

Now, what I don't understand and I cannot find any information on, is as follows:

• What if my original time series ($f_s=2000\textrm{ Hz}$) had been upsampled from $1\textrm{ ms}$ ($f_s=1000\textrm{ Hz}, f_{\rm Nyquist}=500\textrm{ Hz}$)? There is no extra information between $500\textrm{ Hz}$ and $1000\textrm{ Hz}$ but I don't necessarily know that and I apply a Butterworth Filter with $omega_n = 0.25$ (instead of $\omega_n = 0.5$ for $1\textrm{ ms}$ sampling). Is it an issue? Am I misunderstanding how a Butterworth filter works?

• My second question is something like "Why is this the preferred implementation of a low pass filter?" I am sure there are good reasons but I have used software in the past to just high cut filter my data knowing my new $f_{\rm Nyquist}$, so in my case. So in my case I would use something like $0-0-200-250\textrm{ Hz}$. Again, what am I missing? I know that maximum frequency that I want to keep.

Finally, one of the roots of my problem is that I sometimes have irregularly sampled data. I can run an interpolation to a regular time array but when doing this I tend to oversample, to avoid losing signal (This is where my first question comes in). Am I wasting my time? Should I just resample to the smallest time interval in my data?

• There are probably 3 distinct questions in this post. Are you set on using a Butterworth filter for this? For many applications, a linear phase FIR filter is preferable. Do you care about preserving the "shape" of your signal after filtering? – hops May 9 '17 at 22:00
• Thanks for the response. I'm not set on using a Butterworth filter. I had just assumed that this was the best approach based on search for examples of how other people have done low pass filtering in Python. When you say "shape", what do you mean? The low frequency "background" trend? If so, then yes that is important. – Stev May 11 '17 at 8:18
• Roughly speaking, I mean that the peaks and valleys of the signal remain at the same relative locations (time-shifted due to filter delay, but not altered in other ways). A signal is composed of a (possibly infinite) number of sinusoids each with a certain phase. If the phase of these sinusoids is modified according to a linear function of their frequency the output will have the same "shape" (this is the linear phase property). If not, the signal will appear to be distorted. Using firls as suggested by Dan Boschen's answer, results in a perfect linear phase filter, Butterworth is not perfect. – hops May 11 '17 at 13:01
• I realise now that Butterworth is not the way to go. The phase of the signal is important, so I will investigate using FIRLS mentioned below. Thanks again. – Stev May 12 '17 at 14:03

I am not sure why you concluded "A Butterworth Filter" is the way to go, and looks like you have come to the same question- what originally led you to that?

A Butterworth Filter implementation stems from copying analog filter techniques and from what I have learned with DSP is that you have the wonderful opportunity to NOT copy the analog. This is because analog design techniques have been derived from the mathematical limits of what standard R L and C components can give you, while in DSP you can more likely address the problem from an optimum mathematical perspective within the greater space of delays, adds and multiplies (to note and hopefully not confuse; delays, adds and multiplies are NOT strictly limited to digital implementations; for example SAW filters). This may be a good case in point if we look closer at why you would choose a Butterworth filter versus what optimized algorithms such as least squares (FIRLS in Matlab) can give you. The least squares algorithm will result in a linear phase FIR solution, with a stop band roll-off which is ideal for multi-rate resampling applications. fred harris has made a good case (I cannot find the specific reference) that for resampling applications, and FIR filter will always out-perform an IIR filter.

Further what is really great about FIRLS for multirate applications is that you can easily implement a multi-band filter, concentrating the rejection bands to be just at the alias locations, as in the example shown in the figure below showing the same filter design for either a decimation (DFIR) or interpolation (IFIR) design, in this case using a factor of 4:  To answer your question about upsampling: If the signal has been upsampled, this is typically done by inserting zeros (which creates all the aliases in frequency) followed by a low pass filter which will eliminate the aliases and in the process grow the zeros to their optimum interpolated value, so yes in this case, as long as the intepolation filter is present, you will not need an additional low pass filter.

I show this in the following figure showing that if you do the interpolation first in a rational rate conversion (both interpolate and decimate) you can combine the IFIR and DFIR designs as one filter. Also for resampling given its simplicity, consider using a CIC structure. If you are concerned about passband roll-off- the inverse Sinc compensator is quite simple (3 taps). Both are detailed in the following links:

Decimation concepts: Fast Integer 8 Hz 2nd Order LP for Microcontroller

Upsampling Spectrum: Upsampling and comb function

Inverse Sinc Filter: how to make CIC compensation filter