简体   繁体   中英

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:

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 = []
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.

Python keeps telling me: 'Can't multiply sequence by non-int of type 'float'' What am I doing wrong here? 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.

nu is a list, so the piece of code 2*np.pi*nu*t[k] is giving you the error.Try something like:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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