如何将快速转换数据绘制为Python中频率的函数?
我是Scipy的新手,仍在学习Numpy阵列中的构造数据。有人可以帮助我解决以下内容:
我的数据阵列由10列和110行组成。我无法获得正确数量的FREQ点。
我要:
- 将数组形状匹配到最终使用Scipy的
rfftfreq
- pad zeros与我的数据绘制频率点,然后计算快速傅立叶变换,
这是我的代码:
我将dataframe转换为numpy array并提取<<代码> x 和y
数据
xdata=np.array(combine.iloc[:,0:20:2]) #both x and y data shapes are (110,10) #the data corresponds to positions in mm
ydata=np.array(combine.iloc[:,1:20:2])
我计算FFT:
fftdata=fft(ydata) # array of 110 by 10
fftlen = len(fftdata) # size is 1 and value 110
time = (2*fftlen*xdata*0.0075e-3)/c/1e-12 # the numbers are constants #time axis is generated from x data with different delay position of the laser beam
timestep=abs(xdata[fftlen-1]-xdata[0])/(fftlen-1)/0.1499 #sampling rate
sample_size=fftdata.size # size is 1 and value of 1100
freq = rfftfreq(sample_size, d=timestep)
# shape is (551,) - (N/2)nyquist points
plt.plot(freq,fftdata[0:(fftlen//2+1)])
但是,我遇到了此错误,
Error : x and y must have same first dimension, but have shapes (551,) and (56, 10)
我知道由于不同而无法绘制这些错误形状。但是我想将FFT应用于整个数组(110,10),而不是将单个文件加载到不同的numpy数组。如果任何人也可以帮助我将零填充到xdata
和ydata
之前,我将非常感谢它,以改善频域分辨率。
I am new to Scipy and still learning about structuring data in the NumPy array. Can someone please help me resolve the following :
My data array consists of 10 columns and 110 rows. I am unable to get the right number of freq points.
I want to:
- Match the array shapes to ultimately plot frequency points using scipy's
rfftfreq
- Pad zeros to my data before computing th fast fourier transform
Here is my code:
I am converting the dataframe to numpy array and extracting x
and y
data
xdata=np.array(combine.iloc[:,0:20:2]) #both x and y data shapes are (110,10) #the data corresponds to positions in mm
ydata=np.array(combine.iloc[:,1:20:2])
I compute the fft:
fftdata=fft(ydata) # array of 110 by 10
fftlen = len(fftdata) # size is 1 and value 110
time = (2*fftlen*xdata*0.0075e-3)/c/1e-12 # the numbers are constants #time axis is generated from x data with different delay position of the laser beam
timestep=abs(xdata[fftlen-1]-xdata[0])/(fftlen-1)/0.1499 #sampling rate
sample_size=fftdata.size # size is 1 and value of 1100
freq = rfftfreq(sample_size, d=timestep)
# shape is (551,) - (N/2)nyquist points
plt.plot(freq,fftdata[0:(fftlen//2+1)])
But, I am getting this error
Error : x and y must have same first dimension, but have shapes (551,) and (56, 10)
I understand that these cannot be plotted due to different shapes. But I would like to apply FFT to the whole of my array (110,10) instead of loading individual files to different NumPy arrays. I would very much appreciate it if anyone could also help me with padding zeros to xdata
and ydata
before computing the FFT to improve the frequency domain resolution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Numpy文档中的示例是您所需要的:
但是,当您应用
rfftfreq
时,您只会得到正频率:因此,如果您想绘制某些东西,则只需占据相应的一半值(在傅立叶变换幅度中例如)。
要执行零件,您可以只使用
np.pad
在数组的开头和结束时增加了2个零值。可以多个选项。检查此 https://numpy.org/doc/doc/doc/stable/reference/生成/numpy.pad.html
The example in numpy documentation is all you need :
but when you apply
rfftfreq
, you get only positive frequencies:So if you want to plot something you take only the corresponding half of values (in the fourier transform magnitude for example).
To perform zero-padding, you can just use
np.pad
This added 2 zero values in the beginning and the end of the array. Multiple options are possible. Check this https://numpy.org/doc/stable/reference/generated/numpy.pad.html