Monday, June 14, 2021

Simulating Fourier Transform

 Using Julia and Python

Discrete Fourier Transform:  

It is a method to converting a signal from Time Domain to Frequency Domain
More the details can be read here
We have used the Fast Fourier Transform, as it is was the primary purpose of the project. The algorithm is implemented using Python (which can be found in the same repository). This module is called in the Julia console, and used.

Now when FFT is applied in Real-time, we take the sample and concatenate it to the last of the set of N-Points (here 64). Then, the Fourier Transform of the whole set is obtained and displayed in Real-time. The function below, does the same. It cuts first (oldest sample) and makes the list back to the standard size of DFT (here 64-point)

PyCall and Plots:

As mentioned above, the FFT calculation is taken care by python class which can be found here (or in my github repository mentioned at the end). Rest of it can be handled by using Julia, as it is easier and faster to generate animations in Julia when compared to Python. Moreover, Julia also gives us a privilege to call Python libraries. 

It is to be noted that the FFT.py class that we have prepared is not added to PATH and is also not a inbuilt function to be used, hence we have push the class/file into the PATH and then try importing it. The way to do it is shown in the code and is almost self explainatory.

Once we have our fft() method ready, we then try to initialize an array of 64 elements with zeros and at every loop, we pass a value into the calculator. This would give us the output in for of an array of 64 - complex elements. Now, we have two components that can be inferred, and I have chosen to plot the magnitude of the spectrum, hence the absolute value - abs().

We try to provide a random integer between 0-3 to the system and this is what we tend to generate. It can be seen that, the FFT calculator, almost instantaneously, produce the equivalent fourier transform result and this can be visualized as shown.


 

Moving Average Filter:

 A signal is generated of time duration 2 seconds, with sampling frequency 1kHz. 2 signals of 12f (Hz) and 8f (Hz) where f=10, are superposed. This Signal is introduced to a noise of a very small energy, whose Fourier Transform is demonstrated.This set of data given to the FFT calculator sample by sample and simulataneously, the FFT of the set is calculated. However, for the first few samples, the set is initially zero-padded to make it to 64-point, hence it takes some while to get to a stable situation. It can be seen that there are spikes at frequency 80Hz and 120Hz, which clearly tells us the presence of signal of that frequency in the input signal and the amplitude to the spike corresponds to the proportional contribution of that component to the input signal. Since input keeps changing at every instant, the equivalent Fourier transform is obtained at every instance and it keeps varing too.


Due to the noise in the input, It becomes very difficult to comprehend the Fourier Transform. This can be reduced, by reducing the noise in the input signal. This can be done by using Moving Average Filter. This concept helps to reduce the noise, by averaging out the sample with the preceding inputs which reduced the small abrupt changes caused due to noise. More on Moving Average Filters can be seen here

In the example shown, we have chosen the MAF to be of 5-points.
We can notice that the noise is considerably reduced, which makes the Fourier Transform more stable and easy to comprehend but the catch is that, The amplitude of the signal reduces.

Github repo

No comments:

Post a Comment