簡體   English   中英

Python FFT:頻移

[英]Python FFT: freq shift

在對信號進行頻譜分析時,我遇到了一個奇怪的問題,即繪制的信號頻率發生了偏移(或加倍)。 這是一個顯示我的方法的簡單示例:以 100kHz 采樣 1kHz 正弦信號。 最后,信號倉出現在 2kHz 而不是 1kHz。

import numpy as np
import matplotlib.pyplot as plt

time_step = 1.0/100e3
t = np.arange(0, 2**14) * time_step

sig = np.sin(2*np.pi*1e3*t)

sig_fft = np.fft.rfft(sig)

#calculate the power spectral density
sig_psd = np.abs(sig_fft) ** 2 + 1

#create the frequencies
fftfreq = np.fft.fftfreq(len(sig_psd), d=time_step)

#filter out the positive freq
i = fftfreq > 0

plt.plot(fftfreq[i], 10*np.log10(sig_psd[i]))
plt.xscale("log")

您使用錯誤的 function 來計算頻率。 實信號的 FFT 變換具有負頻率分量,它們是正頻率分量的復共軛,即頻譜是 Hermitian 對稱的。 rfft()利用了這一事實,並沒有 output 負頻率,只有直流分量和正頻率。 因此, sig_psd比使用fft()而不是rfft()並且將其傳遞給fftfreq()有效地使頻率加倍時得到的短兩倍。

解決方案:改用rfftfreq()

import numpy as np
import matplotlib.pyplot as plt

time_step = 1.0/100e3
t = np.arange(0, 2**14) * time_step

sig = np.sin(2*np.pi*1e3*t)

sig_fft = np.fft.rfft(sig)

#calculate the power spectral density
sig_psd = np.abs(sig_fft) ** 2 + 1

#create the frequencies
fftfreq = np.fft.rfftfreq(len(sig), d=time_step)  # <-- note: len(sig), not len(sig_psd)

#filter out the positive freq
i = fftfreq > 0  # <-- note: Not really needed, this just removes the DC component

plt.plot(fftfreq[i], 10*np.log10(sig_psd[i]))
plt.xscale("log")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM