gpt4 book ai didi

python - numpy.fft.fft 的功率谱

转载 作者:太空狗 更新时间:2023-10-29 21:03:32 46 4
gpt4 key购买 nike

无论我如何更改数据,我通过下面的代码绘制的数字都只是零附近的一个峰值。我的数据只是一列,记录了某种信号的每个时间点。 time_step 是我应该根据数据中两个相邻点的间隔来定义的值吗?

data=np.loadtxt("timesequence",delimiter=",",usecols=(0,),unpack=True)

ps = np.abs(np.fft.fft(data))**2
time_step = 1

freqs = np.fft.fftfreq(data.size, time_step)
idx = np.argsort(freqs)

pl.plot(freqs[idx], ps[idx])
pl.show()

最佳答案

正如其他人暗示的那样,您的信号必须具有较大的非零分量。 0 (DC) 处的峰值表示信号的平均值。这是从傅里叶变换本身推导出来的。此余弦函数 cos(0)*ps(0) 表示信号平均值的度量。其他傅里叶变换分量是变化幅度的余弦波,其显示这些值的频率内容。

请注意,平稳信号不会有大的直流分量,因为它们已经是零均值信号。如果您不想要大的直流分量,那么您应该计算信号的平均值并从中减去值。无论您的数据是 0,...,999 还是 1,...,1000,甚至是 1000,...,2000,您都会在 0Hz 处获得峰值。唯一的区别是峰值的幅度,因为它测量的是平均值。

data1 = arange(1000)
data2 = arange(1000)+1000
dataTransformed3 = data - mean(data)
data4 = numpy.zeros(1000)
data4[::10] = 1 #simulate a photon counter where a 1 indicates a photon came in at time indexed by array.
# we could assume that the sample rate was 10 Hz for example
ps1 = np.abs(np.fft.fft(data))**2
ps2 = np.abs(np.fft.fft(data))**2
ps3 = np.abs(np.fft.fft(dataTransformed))**2

figure()
plot(ps1) #shows the peak at 0 Hz
figure()
plot(ps2) #shows the peak at 0 Hz
figure()
plot(ps3) #shows the peak at 1 Hz this is because we removed the mean value but since
#the function is a step function the next largest component is the 1 Hz cosine wave.
#notice the order of magnitude difference in the two plots.

关于python - numpy.fft.fft 的功率谱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20074930/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com