简体   繁体   中英

FFT in Python - wrong data?

on my Raspberry PI 4, I have a xlsx file which I scan with: df = pd.read_excel('') This is a dataset of a pressure sensor with 146651 rows x 1 columns. I want to perform a fft for this dataset, but if I plot the fft I get a curve exact the same to the time signal (moved on the x-axis???). So what is the problem?

import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack
import pandas as pd


# Import csv file
df = pd.read_excel('/home/pi/Downloads/test_11.12.19_500_neuHz.xlsx', skiprows=1)


print(df)

sig_fft = fftpack.fft(df)
power = np.abs(sig_fft)
print (power)
sample_freq = fftpack.fftfreq(df.size, 0.02)
plt.figure(figsize=(6,5))
plt.plot(sample_freq, power)


plt.show()

graph

Your input data must be in a single row. Currently, your FFT is applied to all rows individually, which means the output for any given row is the mean of the single cell signal, therefore, your output is the same as your input.

#data is one column
df = pd.DataFrame([4,5,4])
df
fft = fftpack.fft(df)
fft
# output = input => wrong

#data as one row
df = pd.DataFrame({'0':[4],'1':[5],'2':[4]})
df
fft = fftpack.fft(df)
fft
# right

Now I am performing the fft in another way. But how can I scale the frequency and the magnitude axis.

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy.fftpack import fft
import pandas as pd
import math
from tkinter import filedialog
from tkinter import *

#choose csv file
root = Tk()
root.filename = filedialog.askopenfilename ( initialdir = "/home/pi", title = "Datei auswählen", filetypes = (("Comma Seperated Values (CSV)", "*.csv"), ("Alle Dateien", "*.*")) )


# Import csv file
df = pd.read_csv(root.filename, delimiter = ';')
#convert Voltage to Bar
df_echt = df/0.01
#preparation for fft
df_neu = df.as_matrix()
time = df_neu[:,0]
voltage = df_neu[:,0]/df_neu[:,0].max()
df_tr = df_neu.T

#fft
amplitude = np.fft.rfft (voltage)
freq = np.fft.rfftfreq(len(time),np.diff(time)[0])

#plot time signal and fft
plt.plot( np.absolute(amplitude), lw = 0.5)
plt.figure (2)
plt.plot (df_echt)
plt.legend (df)
plt.show()

FFT graph

So how do I scale the axises?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM