在python中绘制功率谱 [英] Plotting power spectrum in python

查看:1081
本文介绍了在python中绘制功率谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含301个值的数组,这些值是从具有301帧的影片剪辑中收集的.这意味着1帧中有1个值.影片剪辑以30 fps的速度运行,因此实际上是10秒长

I have an array with 301 values, which were gathered from a movie clip with 301 frames. This means 1 value from 1 frame. The movie clip is running at 30 fps, so is in fact 10 sec long

现在,我想获得此信号"的功率谱(带有正确的Axis).我试过了:

Now I would like to get the power spectrum of this "signal" ( with the right Axis). I tried:

 X = fft(S_[:,2]);
 pl.plot(abs(X))
 pl.show()

我也尝试过:

 X = fft(S_[:,2]);
 pl.plot(abs(X)**2)
 pl.show()

尽管我认为这不是真正的频谱.

Though I don't think this is the real spectrum.

信号:

频谱:

功率谱:

有人可以为此提供一些帮助吗? 我想要一个以Hz为单位的图.

Can anyone provide some help with this ? I would like to have a plot in Hz.

推荐答案

Numpy具有便捷功能,np.fft.fftfreq用于计算与FFT组件相关的频率:

Numpy has a convenience function, np.fft.fftfreq to compute the frequencies associated with FFT components:

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(301) - 0.5
ps = np.abs(np.fft.fft(data))**2

time_step = 1 / 30
freqs = np.fft.fftfreq(data.size, time_step)
idx = np.argsort(freqs)

plt.plot(freqs[idx], ps[idx])

请注意,您遇到的最大频率不是30 Hz,而是

Note that the largest frequency you see in your case is not 30 Hz, but

In [7]: max(freqs)
Out[7]: 14.950166112956811

您永远不会在功率谱中看到采样频率.如果您有偶数个样本,那么您的情况将达到奈奎斯特频率,在您的情况下为15 Hz(尽管numpy会将其计算为-15).

You never see the sampling frequency in a power spectrum. If you had had an even number of samples, then you would have reached the Nyquist frequency, 15 Hz in your case (although numpy would have calculated it as -15).

这篇关于在python中绘制功率谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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