Friday, January 15, 2016

Fast Fourier Transform (FFT)

#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np

Ts = 0.004; # sampling interval in second
Fs=1/Ts;
data = np.genfromtxt('inputfft.txt')
t = data[:,0]  ## time col 1
y = data[:,1]  ## amplitude col 2
n = len(y) # length of the signal
k = np.arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n/2)] # one side frequency range

Y = np.fft.fft(y)/n # fft computing and normalization
Y = Y[range(n/2)]

fig, ax = plt.subplots(2, 1)
ax[0].plot(t,y)
ax[0].set_xlabel('Time (ms)')
ax[0].set_ylabel('Amplitude')
ax[1].plot(frq,abs(Y),'r') # plotting the spectrum
ax[1].set_xlabel('Freq (Hz)')
ax[1].set_ylabel('|Y(freq)|')
plt.axis((min(frq),max(frq),min(abs(Y)),max(abs(Y))))
plt.show()

No comments: