【发布时间】:2021-05-03 12:51:31
【问题描述】:
我试图通过它的光谱来确定探测器在时域中记录的总能量。 使用 Numpy 的 FFT 库执行快速傅里叶变换后的第一步是确认 Parseval's theorem。
根据定理,时域和频域的总能量必须相同。我有两个问题无法解决。
- 当我在 np.trapz() 集成期间没有为 x 轴使用正确的单位时,我可以确认该定理。一旦我使用我的实际采样点/频率,结果就会关闭。我不明白为什么会这样,我想知道是否可以应用规范化来解决此错误。
- 当我对信号应用直流偏移时,我无法确认定理(取消注释 f = np.sin(np.pi**t)* 行)。
下面是我的带有示例正弦函数的代码。
# Python code
import matplotlib.pyplot as plt
import numpy as np
# Create a Sine function
dt = 0.001 # Time steps
t = np.arange(0,10,dt) # Time array
f = np.sin(np.pi*t) # Sine function
# f = np.sin(np.pi*t)+1 # Sine function with DC offset
N = len(t) # Number of samples
# Energy of function in time domain
energy_t = np.trapz(abs(f)**2)
# Energy of function in frequency domain
FFT = np.sqrt(2) * np.fft.rfft(f) # only positive frequencies; correct magnitude due to discarding of negative frequencies
FFT[0] /= np.sqrt(2) # DC magnitude does not have to be corrected
FFT[-1] /= np.sqrt(2) # Nyquist frequency does not have to be corrected
frq = np.fft.rfftfreq(N,d=dt) # FFT frequenices
# Energy of function in frequency domain
energy_f = np.trapz(abs(FFT)**2) / N
print('Parsevals theorem fulfilled: ' + str(energy_t - energy_f))
# Parsevals theorem with proper sample points
energy_t = np.trapz(abs(f)**2, x=t)
energy_f = np.trapz(abs(FFT)**2, x=frq) / N
print('Parsevals theorem NOT fulfilled: ' + str(energy_t - energy_f))
【问题讨论】:
-
trapz只赋予第一个和最后一个样本一半的权重。使用sum或将第一个样本附加到末尾,因为频率和信号都应该是循环的。 -
两种方法都很好用,谢谢!
标签: python signal-processing fft