在Python中绘制快速傅立叶变换 [英] Plotting a Fast Fourier Transform in Python

查看:82
本文介绍了在Python中绘制快速傅立叶变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问numpy和scipy,并希望为数据集创建一个简单的FFT.我有两个列表,一个是y值,另一个是这些y值的时间戳.

I have access to numpy and scipy and want to create a simple FFT of a dataset. I have two lists one that is y values and the other is timestamps for those y values.

将这些列表输入scipy或numpy方法并绘制所得FFT的最简单方法是什么?

What is the simplest way to feed these lists into a scipy or numpy method and plot the resulting FFT?

我看了一些示例,但是它们都依赖于创建一组具有一定数量的数据点,频率等的伪造数据,而并没有真正展示如何仅使用一组数据和数据来做到这一点.相应的时间戳.

I have looked up examples, but they all rely on creating a set of fake data with some certain number of data points, and frequency, etc. and doesn't really show how to do it with just a set of data and the corresponding timestamps.

我尝试了以下示例:

from scipy.fftpack import fft
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()

但是当我将fft的参数更改为我的数据集并对其进行绘图时,得到的结果非常奇怪,看来频率的缩放比例可能不正确.我不确定.

But when i change the argument of fft to my data set and plot it i get extremely odd results, it appears the scaling for the frequency may be off. i am unsure.

这是我尝试FFT的数据的粘贴框

Here is a pastebin of the data i am attempting to FFT

http://pastebin.com/0WhjjMkb http://pastebin.com/ksM4FvZS

当我在整个过程中执行一次ftf操作时,它的峰值只有零,而其他都没有

When i do an fft on the whole thing it just has a huge spike at zero and nothing else

这是我的代码:

## Perform FFT WITH SCIPY
signalFFT = fft(yInterp)

## Get Power Spectral Density
signalPSD = np.abs(signalFFT) ** 2

## Get frequencies corresponding to signal PSD
fftFreq = fftfreq(len(signalPSD), spacing)

## Get positive half of frequencies
i = fftfreq>0

##
plt.figurefigsize=(8,4));
plt.plot(fftFreq[i], 10*np.log10(signalPSD[i]));
#plt.xlim(0, 100);
plt.xlabel('Frequency Hz');
plt.ylabel('PSD (dB)')

间距等于xInterp[1]-xInterp[0]

推荐答案

因此,我在IPython笔记本中运行了功能上等效的代码形式:

So I run a functionally equivalent form of your code in an IPython notebook:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)

fig, ax = plt.subplots()
ax.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.show()

我得到了我认为非常合理的输出.

I get what I believe to be very reasonable output.

自从我在工科学校开始考虑信号处理以来,这个时间就比我承认的要长得多,但是峰值在50和80正是我所期望的.那是什么问题?

It's been longer than I care to admit since I was in engineering school thinking about signal processing, but spikes at 50 and 80 are exactly what I would expect. So what's the issue?

这里的问题是您没有定期数据.您应该始终检查输入到 any 算法中的数据,以确保它是合适的.

The problem here is that you don't have periodic data. You should always inspect the data that you feed into any algorithm to make sure that it's appropriate.

import pandas
import matplotlib.pyplot as plt
#import seaborn
%matplotlib inline

# the OP's data
x = pandas.read_csv('http://pastebin.com/raw.php?i=ksM4FvZS', skiprows=2, header=None).values
y = pandas.read_csv('http://pastebin.com/raw.php?i=0WhjjMkb', skiprows=2, header=None).values
fig, ax = plt.subplots()
ax.plot(x, y)

这篇关于在Python中绘制快速傅立叶变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆