1

I have a problem with the results obtained from running sets of codes of numpy FFT functions in my python 3.7.

The problem is that the frequency vs amplitude and frequency vs Power spectrum density plots are not correct - judging from the shape and look of my raw data:

Raw Data

As can be seen from the raw data plot, I would expect the freq. vs amplitude and freq. vs PSD plots should not be looking like what i have now:

Current results

FYI, The length of my raw data is 4096 which was taken for 5.7 hours. So, that means one data point is taken in ~ 5 seconds.

How to fix my code to obtain the right results of my FFT analysis?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from numpy.fft import fft, fftfreq, ifft, rfft

file = "C:\work\Data_2019-01-03_16-19-49.txt"

df = pd.read_csv(file,skiprows = (0,2), delimiter='\t')

data1_name = "CH9CC05"
data1 = df[data1_name].values
n = data1.size
y= data1

fft_vals = fft(y,n)
freqs = fftfreq(n)
mask = freqs>=0
fft_theo = 1/n* np.abs(fft_vals)
psd = 1/n*(np.abs(fft_vals)**2)

plt.figure(1)
plt.plot(y, label= data1_name)
plt.title(data1_name+" Raw Data")

plt.figure(2)
plt.plot(freqs[mask], fft_theo[mask], 'r')
plt.title(data1_name+" Freq vs Amplitude")
plt.show()

plt.figure(3)
plt.plot(freqs[mask], psd[mask], 'r')
plt.title(data1_name+" Freq vs PSD")
plt.show()`
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    This is a scaling issue. The DC value (at frequency =0) is very large compared to everything else. Set that element to 0 (this is equivalent to subtracting the mean from the signal) to see other peaks in your plot. – Cris Luengo Jul 06 '19 at 17:01
  • Hi, thank you very much for your responds, Do you mean to subtract each data point from the mean? If i do this, won't my amplitude values be so small? Thank you – Muhammad Hisham Jul 07 '19 at 04:10
  • 1
    `y = data1 - np.mean(data1)`. This doesn't change the amplitude. This just removes the peak at `f=0`. All the other bins will be the same they are now. – Cris Luengo Jul 07 '19 at 05:12

0 Answers0