如何将快速转换数据绘制为Python中频率的函数?

发布于 2025-02-13 15:40:53 字数 1391 浏览 0 评论 0原文

我是Scipy的新手,仍在学习Numpy阵列中的构造数据。有人可以帮助我解决以下内容:

我的数据阵列由10列和110行组成。我无法获得正确数量的FREQ点。

我要:

  1. 将数组形状匹配到最终使用Scipy的rfftfreq
  2. 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数组。如果任何人也可以帮助我将零填充到xdataydata之前,我将非常感谢它,以改善频域分辨率。

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:

  1. Match the array shapes to ultimately plot frequency points using scipy's rfftfreq
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2025-02-20 15:40:53

Numpy文档中的示例是您所需要的:

import numpy as np

# Let's say here is your signal
signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5, -3, 4], dtype=float)

fourier = np.fft.rfft(signal)
n = signal.size
sample_rate = 100
freq = np.fft.fftfreq(n, d=1./sample_rate)
>> freq
 array([  0.,  10.,  20., ..., -30., -20., -10.])

但是,当您应用rfftfreq时,您只会得到正频率:

freq = np.fft.rfftfreq(n, d=1./sample_rate)

因此,如果您想绘制某些东西,则只需占据相应的一半值(在傅立叶变换幅度中例如)。

>> freq
 array([  0.,  10.,  20., 40., 50.])

要执行零件,您可以只使用np.pad

np.pad(signal, (2,2), 'constant', constant_values=(0,0))

在数组的开头和结束时增加了2个零值。可以多个选项。检查此 https://numpy.org/doc/doc/doc/stable/reference/生成/numpy.pad.html

The example in numpy documentation is all you need :

import numpy as np

# Let's say here is your signal
signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5, -3, 4], dtype=float)

fourier = np.fft.rfft(signal)
n = signal.size
sample_rate = 100
freq = np.fft.fftfreq(n, d=1./sample_rate)
>> freq
 array([  0.,  10.,  20., ..., -30., -20., -10.])

but when you apply rfftfreq, you get only positive frequencies:

freq = np.fft.rfftfreq(n, d=1./sample_rate)

So if you want to plot something you take only the corresponding half of values (in the fourier transform magnitude for example).

>> freq
 array([  0.,  10.,  20., 40., 50.])

To perform zero-padding, you can just use np.pad

np.pad(signal, (2,2), 'constant', constant_values=(0,0))

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文