Sunday, 13 December 2015

downsampling - Python's $tt resample$ vs $tt resample_poly$ vs $tt decimate$


I have multiple signals each measured with a sampling rate 300Hz. They are mostly periodic, although there are some parts of some of the signals, that seem to be random and do not follow the rest of the pattern(could be noise). I want to downsample this signal and I have found these three functions: decimate, resample_poly, and resample.


I do not know much about signal processing, so I was wondering whether someone could explain in simple terms what is the difference between the three functions and if possible suggest which would be the most appropriate to use for my case.


Note that I would like to apply machine learning methods to classify new signals to 2 categories. I need to downsample to decrease the time it takes to classify signals.



Answer



Downsampling and upsampling are operations that change the sampling rate of a signal. Each one of them is composed of two steps, changing the sampling rate and filtering. Usually, the amount of change is expressed as a ratio.


When downsampling, we are trying to take the signal from some $Fs$ to some $Fs_n < Fs$. The key problem with doing this is that we are effectively reducing the available bandwidth. Therefore, if we were to leave any frequency $f > \frac{Fs_n}{2}$ within the resampled signal, we would get aliasing.


Aliasing is a byproduct of sampling and it distorts the original signal. In DSP, we are trying to minimise it.


Therefore, when Downsampling, we first apply a low pass filter to the signal at its $Fs$ rate. This filter suppresses all frequencies that might cause aliasing. After applying the filter, we apply decimation. Decimation is a very simple process by which we are throwing away samples. For example, to downsample by $\frac{1}{2}$ you throw away every other sample (i.e. keep 0,2,4,6,8,...).



When upsampling, we are trying to take the signal from some $Fs$ to some $Fs_n > Fs$. The key problem now is not that we can't fit the amount of original data to the new bandwidth but the exact opposite. We now do not have enough data.


Sampling works by examining the value of a continuous signal at discrete times, specifically $Fs$ times per second. But, we don't really know what exactly does the original signal do in between those discrete instances we are examining its value.


Therefore, when upsampling, we first upsample, which is an operation that inserts silence in between the samples and then apply interpolation to make up the signal values during the silence bits. Interpolation is usually another low pass filter.


Obviously, we can use both of these operations to change the $Fs$ by any given ratio. But, no matter what this ratio is we first upsample and then downsample (with each upsample, downsample step composed of their own steps mentioned above). Otherwise, you are throwing away information.


So, having said this:




  1. Decimate does downsampling.





  2. Resample does upsampling / downsampling and the associated filtering, entirely in the frequency domain, using the Fourier Transform. Because it is using the Fourier Transform, a key assumption is that the signal that is fed in its input is periodic. In other words, if you supplied a recording of the word "Blue is a co", the Fourier Transform assumes that in reality, the signal sounds like "Blue is a coBlue is a coBlue is a coBlue is a coBlue is a coBlue is a coBlue is a co"...to infinity. This explains the Note towards the end of that page.




  3. Resample_poly does upsampling / downsampling and the associated filtering, using a polyphase filter](https://en.wikipedia.org/wiki/Polyphase_quadrature_filter). (Notice the series of operations applied as mentioned in the documentation).




2 and 3 are essentially the same thing with different techniques for reasons of computation speed. In the techniques mentioned, filtering and decimation and upsampling and interpolation are performed in one step because they are inherent to the technique.


As you can see, changing the sampling rate involves throwing away information or "diluting" the information that was already captured in some signal $x$ during its acquisition at some $Fs$. This is why I asked you what are you going to do after the decimation because under certain conditions, your classification/ clustering might be affected if those tiny little characteristics of signals that differentiate them from each other are smoothed out.


By what you are mentioning, if your highest non-negligible frequency component is at 50Hz, this means that (theoretically), you can reduce your $Fs$ by $\frac{1}{3}$. In practice, try not to get too close to a lowest $Fs$ of 100Hz. Maybe leave some lee-way, say 120Hz.


Hope this helps.



No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...