Search code examples
pythonpandasnumpymatplotlibfft

Python : How I can draw FFT graph with Pandas DataFrame which is made by time and values


I am trying to make FFT graph which is derived from Pandas DataFrame.

It is my source code I tried with.

import numpy as np  
import matplotlib.pyplot as plt 
import pandas as pd
from scipy.fft import fftfreq 
plt.style.use("seaborn")

data = pd.read_csv("/Users/kyungyunlee/Desktop/ IRP reference/Data/PIXEL_DATA/1_piece.csv")

N = len(t)
t = data["time"].loc[data["time"] > 5].loc[data["time"] < 10]
s = data["y_value"].loc[data["time"] > 5].loc[data["time"] < 10]
print(len(s))

fft = np.fft.fft(s)
fftfreq = np.fft.fftfreq(len(s))

plt.subplot(1, 2, 1)
plt.xlabel("Frquency Domain")
plt.ylabel("Amplitude")
plt.plot(fftfreq, fft)
plt.subplot(1, 2, 2)
plt.plot(t, s)
plt.show()

And the picture below is the result of the source code. As you can see from the graph, the left graph is FFT and the right graph is the time and amplitude graph. In this situation, I can't understand why my FFT graph is like that. The graph is weird but I can't find what the problem is.

enter image description here

Please check this Pandas dataframe screenshot. very simple data consist with time(maybe ms) and values.

time,y_value
5.009026,614
5.035417,550
5.061302,554
5.08712,611
5.114184,613
5.140525,614
5.167711,573
5.19439,532
5.220309,596
5.247532,607
5.273929,608
5.300062,588
5.326553,529
5.352314,577
5.378559,602
5.404629,602
5.431329,597
5.459119,547
5.486477,556
5.512459,597
5.539668,594
5.567103,597
5.594013,564
5.621206,539
5.646212,586
5.671964,594
5.698939,594
5.726222,577
5.777665,574
5.804736,590
5.831811,590
5.858152,583
5.885826,543
5.912285,562
5.937549,587
5.991617,585
6.018168,555
6.044418,547
6.07098,581
6.097121,585
6.124821,585
6.151159,566
6.177994,536
6.205361,573
6.232069,582
6.25743,582
6.284097,573
6.31036,537
6.336849,564
6.363457,580
6.390022,580
6.417727,576
6.444151,549
6.471022,553
6.498445,576
6.551982,577
6.578571,557
6.60393,544
6.631363,571
6.657855,576
6.685089,576
6.711603,563
6.763428,565
6.789426,574
6.815717,574
6.841412,569
6.867886,543
6.867886,517
6.89452,558
6.921834,572
6.974582,570
7.00143,550
7.029219,550
7.055249,569
7.109767,570
7.137385,556
7.188917,565
7.215901,569
7.215901,543
7.243045,569
7.270299,561
7.32553,560

What I want to do is to draw FFT graph with this Data but I don't know why the code is not working.

I hope I can get some feedbacks. Thank you.


Solution

  • If you suppress the DC node and adjust the axes, the results seem pretty reasonable:

    import numpy as np  
    import matplotlib.pyplot as plt 
    import pandas as pd
    from scipy.fft import fftfreq 
    plt.style.use("seaborn")
    
    data = pd.read_csv("x.data")
    print(data)
    
    t = data["time"].loc[data["time"] > 5].loc[data["time"] < 10]
    s = data["y_value"].loc[data["time"] > 5].loc[data["time"] < 10]
    
    fft = np.fft.fft(s)
    fft[0] = 0
    fftfreq = np.fft.fftfreq(len(s))*len(s)/(t.max()-t.min())
    
    plt.subplot(1, 2, 1)
    plt.xlabel("Frquency Domain")
    plt.ylabel("Amplitude")
    plt.plot(fftfreq, fft)
    plt.subplot(1, 2, 2)
    plt.plot(t, s)
    plt.show()
    

    Output: enter image description here

    And if you plot the power spectrum (np.abs(fft)), you get: enter image description here