so what is FFT or the fourier transform.
what is the use of the transform
ok an example would be a couple of data points in a digital world
and you want to transform the data.
so why we need to transform the data
consider a couple of digital data for sound wave and you want to do some manipulation on the data ,sine it resembles a sine wave
we plot the data points as a sin wave
your x axis would be time
y axis would be your data
The theory here is sound wave has a mix of signals so sin wave of
different frequencies ,since we have collected data over time
we have to transform time data to freq data
in the freq domain our
x axis would be angular frequency
y axis would be amplitude.
you can find the link for the FFT and inverse FFT for a sin wave
https://github.com/naveez-alagarsamy/matplotlib/blob/main/fourier.ipynb
if u cant acess the link use the below code to run locally
import matplotlib.pyplot as plt import numpy as np from numpy.fft import fft, ifft sample_rate = 8 # 8 samples in 1 second sample_interval = 2*np.pi/sample_rate x = np.arange(0, 2*np.pi, sample_interval) y = np.sin(x) #adding another sin wave with freq =4 freq = 4 y += np.sin(freq*2*np.pi*x) y_hat = np.arange(len(y)) print(y_hat) #y = np.sin(2x) plt.plot(x, y) plt.show() print(fft(y)) print(np.abs(fft(y))) #fourier transform X = fft(y) #angular freq which is = (2 * pi * n/sample_interval * sample rate ) w = (2* np.pi* y_hat)/(sample_interval * sample_rate ) print(np.abs(w)) plt.figure(figsize = (12, 6)) plt.subplot(121) plt.stem(np.abs(w), np.abs(X), 'b', \ markerfmt=" ", basefmt="-b") plt.xlabel('Freq (Hz)') plt.ylabel('Amplitude ') plt.xlim(0, 10) plt.subplot(122) plt.plot(x, ifft(X), 'r') plt.xlabel('Time') plt.ylabel('Amplitude') plt.tight_layout() plt.show()
Comments
Post a Comment