功率谱由numpy.fft.fft

que*_*ang 8 python signal-processing numpy fft

无论我如何更改数据,通过以下代码绘制的图形只是ZERO周围的峰值.我的数据只是一列,记录了某种信号的每个时间点.是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()
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 5

正如其他人所暗示的那样,您的信号必须具有较大的非零分量。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.
Run Code Online (Sandbox Code Playgroud)