0

I want to perform FFT on data array that I have extracted from MPU6050 sensor connected to Arduino UNO using Python

Please find the data sample below

0.13,0.04,1.03

0.14,0.01,1.02

0.15,-0.04,1.05

0.16,0.02,1.05

0.14,0.01,1.02

0.16,-0.03,1.04

0.15,-0.00,1.04

0.14,0.03,1.02

0.14,0.01,1.03

0.17,0.02,1.05

0.15,0.03,1.03

0.14,0.00,1.02

0.17,-0.02,1.05

0.16,0.01,1.04

0.14,0.02,1.01

0.15,0.00,1.03

0.16,0.03,1.05

0.11,0.03,1.01

0.15,-0.01,1.03

0.16,0.01,1.05

0.14,0.02,1.03

0.13,0.01,1.02

0.15,0.02,1.05

0.13,0.00,1.03

0.08,0.01,1.03

0.09,-0.01,1.03

0.09,-0.02,1.03

0.07,0.01,1.03

0.06,0.00,1.05

0.04,0.00,1.04

0.01,0.01,1.02

0.03,-0.05,1.02

-0.03,-0.05,1.03

-0.05,-0.02,1.02

I have taken 1st column (X axis) and saved in an array

Reference:https://hackaday.io/project/12109-open-source-fft-spectrum-analyzer/details from this i took a part of FFT and the code is as below

from scipy.signal import filtfilt, iirfilter, butter, lfilter
from scipy import fftpack, arange
import numpy as np
import string
import matplotlib.pyplot as plt

sample_rate = 0.2

accx_list_MPU=[]

outputfile1='C:/Users/Meena/Desktop/SensorData.txt'

def fftfunction(array):


    n=len(array)
    print('The length is....',n)
    k=arange(n)
    fs=sample_rate/1.0
    T=n/fs
    freq=k/T
    freq=freq[range(n//2)]
    Y = fftpack.fft(array)/n
    Y = Y[range(n//2)]
    pyl.plot(freq, abs(Y))
    pyl.grid()
    ply.show()
with open(outputfile1) as f:
                string1=f.readlines()
                N1=len(string1)

                for i in range (10,N1):
                    if (i%2==0):
                                new_list=string1[i].split(',')

                                l=len(new_list)
                                if (l==3):

                                        accx_list_MPU.append(float(new_list[0]))
fftfunction(accx_list_MPU)

I have got the output of FFT as shown FFToutput

I do not understand if the graph is correct.. This is the first time im working with FFT and how do we relate it to data

This is what i got after the changes suggested:FFTnew

1 Answers1

0

Here's a little rework of your fftfunction:

def fftfunction(array):
    N = len(array)
    amp_spec = abs(fftpack.fft(array)) / N
    freq = np.linspace(0, 1, num=N, endpoint=False)

    plt.plot(freq, amp_spec, "o-", markerfacecolor="none")
    plt.xlim(0, 0.6)  # easy way to hide datapoints
    plt.margins(0.05, 0.05)

    plt.xlabel("Frequency $f/f_{sample}$")
    plt.ylabel("Amplitude spectrum")
    plt.minorticks_on()
    plt.grid(True, which="both")

fftfunction(X)

Specifically it removes the fs=sample_rate/1.0 part - shouldn't that be the inverse?

The plot then basically tells you how strong which frequency (relative to the sample frequency) was. Looking at your image, at f=0 you have your signal offset or mean value, which is around 0.12. For the rest of it, there's not much going on, no peaks whatsoever that indicate a certain frequency being overly present in the measurement data.

Jeronimo
  • 2,268
  • 2
  • 13
  • 28
  • I have tried to use the function as you suggested.. i got the graph as shown with the name FFTnew –  Apr 30 '18 at 12:06