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

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

问题描述

我可以访问 NumPy 和 SciPy,并且想要创建数据集的简单 FFT.我有两个列表,一个是 y 值,另一个是那些 y 值的时间戳.

I have access to NumPy and SciPy and want to create a simple FFT of a data set. 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 don'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, and 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/0WhjjMkbhttp://pastebin.com/ksM4FvZS

当我在整个过程中使用 fft() 时,它只是在零处有一个巨大的尖峰,没有别的.

When I use 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 notebook 中运行了一个功能等效形式的代码:

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?

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

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天全站免登陆