简体   繁体   English

在不使用内置FFT函数的情况下,使用Python查找功率谱

[英]Finding a power spectrum in Python, without using the inbuilt FFT function

I would like to find the a power spectrum with Python using the following formula: 我想使用以下公式使用Python查找功率谱:

Power spectrum 功率谱

I am trying to plot the array that should follow from this with this: 我正在尝试绘制与此有关的数组:

P = []
for k in range(0,int(N/2)):
    P.append((2/N)*(sum(x[k]*np.cos(2*np.pi*nu*t[k]))**2+(sum(x[k]*np.sin(2*np.pi*nu*t[k])))**2))

where nu are the frequencies: 其中nu是频率:

nu = []
for j in range(0, int(N/2), 2):
nu.append(j/T)

x and t come from a dataset, and N is just the total of all x. x和t来自数据集,而N只是所有x的总和。

Python keeps telling me: 'Can't multiply sequence by non-int of type 'float'' What am I doing wrong here? Python不断告诉我:“不能将序列乘以'float类型的非整数'。在这里,我在做什么错? I'm sure it's something to do with multiplying the wrong types of data with eachother. 我确定这与将错误类型的数据相互相乘有关。 But I'm not sure how to change this. 但我不确定如何更改此设置。

I know there is an inbuild FFT function, but I think it would be really instructive for me to get this function to work. 我知道有一个内置的FFT函数,但是我认为让该函数正常工作对我很有帮助。

nu is a list, so the piece of code 2*np.pi*nu*t[k] is giving you the error.Try something like: nu是一个列表,因此代码段2*np.pi*nu*t[k]给您错误。尝试类似的方法:

for i in nu:
    P.append((2/N)*(sum(x*np.cos(2*np.pi*nu[i]*t))**2+(sum(x*np.sin(2*np.pi*nu[i]*t)))**2))

This way you are summing the quantity (2/N)*(sum(x*np.cos(2*np.pi*nu[i]*t))**2+(sum(x*np.sin(2*np.pi*nu[i]*t)))**2) for every i. 这样,您求和的数量为(2/N)*(sum(x*np.cos(2*np.pi*nu[i]*t))**2+(sum(x*np.sin(2*np.pi*nu[i]*t)))**2)对于每个i (2/N)*(sum(x*np.cos(2*np.pi*nu[i]*t))**2+(sum(x*np.sin(2*np.pi*nu[i]*t)))**2)

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

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