Gunnar Kjemphol
Gunnar Kjemphol

Reputation: 53

numpy fft returns orginal frequency when plotting

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('signal180_single.csv', sep=',', header=None)
x = df.values
length = len(x)
# frequency 0.02s
fs = 50.0
t = np.arange(0, length/fs,1.0/fs)

xF = np.fft.fft(x)
N = len(xF)
xF = xF[0:N/2]

# plot frequencys from 0 to fs, with num = N/2
fr = np.linspace(0,fs,N/2)

plt.figure()
plt.subplot(211)
plt.plot(t,x)
plt.subplot(212)
plt.plot(fr, abs(xF))
plt.show()

I am writing 180000 floating point values into an array from file. The values are sampled at 50Hz, and contains a sinus of 2Hz.

Then I plot the frequency in the upper plot window. I want to plot the frequency specter of the frequency in the lower plot window, but I get the same values as in the upper plot window. Can anyone see where the error is?

When I plot the formula x = np.sin(10*t) + np.cos(3*t), I get the frequency’s. But not when I read the sinus from a file or array.

Upvotes: 2

Views: 507

Answers (1)

kwant
kwant

Reputation: 248

the line

x = df.values 

returns a 180000 x 1 array. Where every value is stored separately. In this case replace the line with:

x = df.values.ravel()

And your script will work.

Upvotes: 3

Related Questions