简体   繁体   English

python列表上的NP.FFT

[英]NP.FFT on python list

Could you please advise me on the following: I gather data from an Arduino ADC and store the data in a list on a Raspberry Pi 4 with Python 3. The list is called 'dataList' and contains 1024 10 bits samples. 您能否在以下方面为我提供建议:我从Arduino ADC收集数据,并将数据存储在具有Python 3的Raspberry Pi 4上的列表中。该列表称为“ dataList”,包含1024个10位样本。 This all works fine: I can reproduce the sampled signal on the Raspberry. 一切正常:我可以在Raspberry上重现采样信号。

I would like to use the power spectrum of the acquired signal using numpy FFT. 我想通过numpy FFT使用采集信号的功率谱。 I tried the following: 我尝试了以下方法:

[see below] [见下文]

This should illustrate what I'm trying to do; 这应该可以说明我正在尝试做的事情; however this produces incoherent output. 但是,这会产生不连贯的输出。 The sampled signal has a frequency of about 300 Hz. 采样信号的频率约为300 Hz。 I would be very grateful for any hints in the right direction! 对于任何正确方向的提示,我将不胜感激!

def show_FFT(window):
   fft = np.fft.fft (dataList, 1024, -1, None)
   for X_value in range (0,512, 1):
       Y_value = fft ([X_value]
       gfxdraw.pixel (window, X_value, int(abs(Y_value), black)

As you mentioned in your question, you have a data set whith X starting from 0 to... but for numpy.fft.fft you must keep in mind that it is a discrete Fourier transform ( DFT ) which caculate the fft of equaly spaced samples and i must mntion that it must be a symetric range of dataset from -x to x. 正如您在问题中提到的那样,您有一个数据集,其中X的值从0开始到...,但是对于numpy.fft.fft,您必须记住,它是离散傅里叶变换( DFT ),用于计算等距的fft样本,我必须说明它必须是从-x到x的数据集的对称范围。 You can simply try it with a gausian finction and change the parameters as you wish and see what are the results... Since you didn''t give any data set here , I would refer you to a generl case with below code: 您可以简单地使用高斯函数进行尝试,并根据需要更改参数,然后查看结果是什么...由于您在此处未提供任何数据集,因此我将为您提供一个通用案例,其中包含以下代码:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
# create data from dataframes
x = np.random.rand(50) #unequaly spaced measurment
x.sort()
y = np.exp(-x*x) #measured signal

based on the answer here you can resample your data into equaly spaced points by: 根据此处的答案您可以通过以下方法将数据重新采样为等距的点:

f = interpolate.interp1d(x, y)
num = 500
xx = np.linspace(x[0], x[-1], num)
yy = f(xx)
plt.close('all')
plt.plot(x,y,'bo')
plt.plot(xx,yy, 'g.-')
plt.show()

enter image description here then you can make your x data symetric very simply by : 在此处输入图像描述,然后可以通过以下方法非常简单地使x数据对称:

x=xx
y=yy
xsample = x-((x.max()-x.min())/2)
xsample=xsample-(xsample.max()+xsample.min())/2
x=xsample

thne if you try fft you will get the corect results as: 如果尝试fft,将得到以下结果:

ysample =yy
ysample_fft = np.fft.fftshift(np.abs(np.fft.fft(ysample/ysample.max()))) / 
np.sqrt(len(ysample))
plt.plot(xsample,ysample_fft/ysample_fft.max(),'b--')
plt.show()

enter image description here 在此处输入图片说明

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

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