Click here to Skip to main content
15,888,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So,we are working on a signal obtained from hand tremors by using a 3 axis accelerometer.We want to compute FFT of the signal to find out the dominating frequency.The dominating frequency obtained in matlab was upto 10 Hz which was expected.However, the result obtained in python are not as expected.Can someone please let us know the program for computation of FFT in python?

What I have tried:

Python
import csv


import numpy as np
#import scipy as sy
#import scipy.fftpack as syfp
#import pylab as pyl

with open('C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV') as csvfile:
    spamreader=csv.reader(csvfile,delimiter=' ',quotechar='|')
    for row in spamreader:
        print(', '.join(row))

#Read in data from file here
array=np.loadtxt("C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV")


from scipy.fftpack import fft
 # Number of sample points
N = len(array)
# sample spacing

T=1.0/N

x = np.linspace(0.0, 2*np.pi*N*T, N)
# y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(array)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()


program 2
Python
import csv
import numpy as np
import pylab as pyl

with open('C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV') as csvfile:
    spamreader=csv.reader(csvfile,delimiter=' ',quotechar='|')
    for row in spamreader:
        print(', '.join(row))
        
#Read in data from file here
array=np.loadtxt("C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV")

Ts=1.00E-03;
Fs=1/Ts;
Fn=Fs/2;


#np.isnan(array)=[]
#Ly1=array.shape
N=len(array)
T=np.linspace(0,1,N)*Ts

#pyl.plot(T,array)
from scipy.fftpack import fft
FF=fft(array/N);

#Fv=np.linspace(0,1,np.fix(N/2)+1)*Fn;
#Iv=len(Fv)
pyl.plot(T,FF)


we have tried the above 2 programs and many other versions of this program.
Posted
Updated 13-Mar-17 4:39am
v5
Comments
Richard MacCutchan 12-Mar-17 4:14am    
It is the same as in any language, it is a mathematical issue, not a programming one.

1 solution

Well, you don't specify what exactly you're seeing that's different... but I'll take a stab at a common problem. The result of an FFT has the DC frequency (i.e. 0) in the first bin, proceeded by the real frequency spectral bins, then proceeded by the negative spectral bins. Use a "shift" function to shift the zero bin to the middle and re-arrange the negative components to be left of zero for plotting:
fftshift - Rearranges the fft output, moving the zero frequency to the center of the spectrum[^]

The spacing of the spectral bins is essentially Fs/N, where Fs is the sampling frequency and N is the FFT size. The larger the FFT size, the tighter your bin spacing for a given sample rate (i.e. you get more frequency resolution). Due to computational complexity, typically N will be a power of 2... if you feed less samples than a power of 2 to an FFT library, they'll zero pad up to the closest power of 2.

As for the magnitude scaling of your graph, well... there's a handful of scaling options but 1/N and 1/sqrt(N) are common options.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900