Saturday, September 5, 2020

Understanding Short Term Fourier Transform (STFT)

 Hello ppl,

 In the previous post, we have seen about Fourier transform and its physical meaning. Now, what might be missing in the Fourier transform is that, if multiple frequency signals are present at different time window (Non – stationary signal may be the correct word for this), we don’t have a clear data from the frequency response. We don’t understand which part of the signal has the obtained particular frequency. What we do is a dot product of a signal with particular single frequency throughout. But the actual signal has two different frequencies at different time window. 

Let us understand the problem with a simple example. We can take two signals for comparison. The first signal is a concatenation of sine waves of three different frequencies (5 Hz, 10 Hz and 15 Hz) .i.e. Different frequencies at different time windows. (Non- stationary signal). The second signal is a sum of sine waves with three different frequencies .i.e. same frequencies at all points of time (Stationary signal). All the implementations can be seen in the Github code page here

We are using the FFT implementation for taking the Fourier transform. When plotting we can usually remove the negative frequencies. Now, if we take the Fourier transform of both signals, we will have,

 



From this, it is clear that, the Fourier transform doesn’t give us any detail about the time of presence of a particular frequency.

How to tackle this issue?

We can fix a window and calculate FT in that particular window.  This idea gives rise to Short Term Fourier Transform.

The mathematical representation of Short Term Fourier Transform can be given as follows:

Continuous – time STFT

Discrete – time STFT

Here,

x(n) – Signal

ω(n) – Sliding window

m – Denotes the location of the window

We can see that, in this form, only the window is added extra. Now, we need some more parameters other than the ones that are mentioned in the formula for practical implementation using scipy in python.

1)     Sampling frequency

2)     Length of the window

3)     Amount of overlap between the window (this determines the value of ‘m’)

 

Sampling frequency can be chosen as the number of samples per second. Mostly, there is no practical discrete signal. We discretize the analog signal to discrete signal. Now assume we take 256 samples per second, then this is our sampling frequency. 1/256 is our sampling time .i.e. The distance between two samples. (We choose our sampling frequency based on the maximum frequency present in the signal. By Nyquist criteria, we take more than twice the maximum frequency to be our sampling frequency. Here, in our example, we are dealing with frequencies of 5 Hz, 10 Hz and 15 Hz and we use sampling frequency as 256 which is good enough)

The next parameter is length of window, which we should specify in terms of number of samples. This usually is a trial and error term for an unknown signal. In our case, we know that our signal differs after every second (256 samples). So we fix that to be our length of window. The higher window length means high frequency resolution. This is because; we have large part of signal to assess the frequency present in it. (Say, the time period of a frequency (1/frequency) is 100 samples and our window length is just 25 samples, we may not find the presence of that particular frequency). Similarly, lesser the width of the window, higher is the time resolution.

The next parameter is amount of overlap. This is also a parameter to be chosen in a trial and error basis. Here I am choosing it to the maximum (255). This tells the step size of our window.

We are getting the scalogram (The plot of time, frequency and amplitude from STFT) as follows:

 

Here we can see clearly, even the time of the presence of a particular frequency.

 

Now, STFT helped in overcoming one problem but not completely.

Why??

Because, we need to make a trade-off between time and frequency resolution. Now to overcome this drawback we can use wavelet transform, which we can see in another post. The code for all the implementations in the post along with few more examples can be seen in the Github page

References

https://en.wikipedia.org/wiki/Short-time_Fourier_transform

http://ataspinar.com/2018/12/21/a-guide-for-using-the-wavelet-transform-in-machine-learning/

https://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html


No comments:

Post a Comment

Automatic segmentation based on thresholding

Hlo ppl,  Segmentation is one of the major preprocessing steps in analyzing the information present in the image. There are various methods ...