简体   繁体   English

Python 中的快速傅立叶变换

[英]Fast Fourier Transform in Python

I am new to the fourier theory and I've seen very good tutorials on how to apply fft to a signal and plot it in order to see the frequencies it contains.我是傅立叶理论的新手,我看过关于如何将 fft 应用于信号并绘制它以查看它包含的频率的非常好的教程。 Somehow, all of them create a mix of sines as their data and i am having trouble adapting it to my real problem.不知何故,他们所有人都创建了混合正弦作为他们的数据,我无法将其适应我的真正问题。

I have 242 hourly observations with a daily periodicity, meaning that my period is 24. So I expect to have a peak around 24 on my fft plot.我有 242 小时的观测值,每天有一个周期,这意味着我的周期是 24。所以我希望在我的 fft 图中有一个大约 24 的峰值。

A sample of my data.csv is here: https://pastebin.com/1srKFpJQ我的 data.csv 示例在这里: https ://pastebin.com/1srKFpJQ

Data plotted:绘制的数据:

该系列

My code:我的代码:

data = pd.read_csv('data.csv',index_col=0)
data.index = pd.to_datetime(data.index)
data = data['max_open_files'].astype(float).values

N = data.shape[0] #number of elements
t = np.linspace(0, N * 3600, N) #converting hours to seconds
s = data

fft = np.fft.fft(s)
T = t[1] - t[0]

f = np.linspace(0, 1 / T, N)
plt.ylabel("Amplitude")
plt.xlabel("Frequency [Hz]")
plt.bar(f[:N // 2], np.abs(fft)[:N // 2] * 1 / N, width=1.5)  # 1 / N is a normalization factor
plt.show()

This outputs a very weird result where it seems I am getting the same value for every frequency.这输出了一个非常奇怪的结果,似乎我对每个频率都得到了相同的值。

结果

I suppose that the problems comes with the definition of N, t and T but I cannot find anything online that has helped me understand this clearly.我想问题出在 N、t 和 T 的定义上,但我在网上找不到任何可以帮助我清楚地理解这一点的内容。 Please help :)请帮忙 :)

EDIT1:编辑1:

With the code provided by charles answer I have a spike around 0 that seems very weird.使用查尔斯回答提供的代码,我在 0 附近有一个尖峰,这看起来很奇怪。 I have used rfft and rfftfreq instead to avoid having too much frequencies.我已经使用rfftrfftfreq来避免频率过多。

频率

I have read that this might be because of the DC component of the series, so after substracting the mean i get:我读过这可能是因为该系列的直流分量,所以在减去平均值后我得到:

减去直流分量频率

I am having trouble interpreting this, the spikes seem to happen periodically but the values in Hz don't let me obtain my 24 value (the overall frequency).我无法解释这一点,尖峰似乎周期性地发生,但以 Hz 为单位的值不允许我获得我的 24 值(总频率)。 Anybody knows how to interpret this ?有谁知道如何解释这个? What am I missing ?我错过了什么?

The problem you're seeing is because the bars are too wide, and you're only seeing one bar.您看到的问题是因为条形太宽了,而您只能看到一个条形。 You will have to change the width of the bars to 0.00001 or smaller to see them show up.您必须将条形的宽度更改为 0.00001 或更小才能看到它们。

Instead of using a bar chart, make your x axis using fftfreq = np.fft.fftfreq(len(s)) and then use the plot function, plt.plot(fftfreq, fft) :不要使用条形图,而是使用fftfreq = np.fft.fftfreq(len(s))制作 x 轴,然后使用绘图函数plt.plot(fftfreq, fft)

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

data = pd.read_csv('data.csv',index_col=0)
data.index = pd.to_datetime(data.index)
data = data['max_open_files'].astype(float).values

N = data.shape[0] #number of elements
t = np.linspace(0, N * 3600, N) #converting hours to seconds
s = data

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

T = t[1] - t[0]

f = np.linspace(0, 1 / T, N)
plt.ylabel("Amplitude")
plt.xlabel("Frequency [Hz]")
plt.plot(fftfreq,fft)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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