如何从信号的FFT中获得频率和相应的幅度?

问题描述 投票:0回答:1

我有以下

1.2Hz/9Hz/15Hz
信号

import numpy as np

total_time = 5
sampling_frequency = 100

t = np.linspace(0, total_time, total_time * sampling_frequency, endpoint=False)
signal = np.sin(2 * np.pi * 1.2 * t) + 0.5*np.cos(2 * np.pi * 9 * t) + 0.75*np.sin(2 * np.pi * 15.0 * t)
plt.plot(t, signal)

和离散 FFT

fft_spectrum = np.fft.rfft(signal)
fft_spectrum_abs = np.abs(fft_spectrum) * 2 / (total_time * sampling_frequency)

freq = np.fft.rfftfreq(signal.size, d=1./sampling_frequency)

plt.plot(freq, fft_spectrum_abs)
plt.xlabel("frequency, Hz")
plt.ylabel("amplitude, units")
plt.show()

如何获得FFT的频率和幅度?

我正在使用

[
    (amp, freq) for amp, freq in sorted(zip(fft_spectrum_abs, freq), 
    key=lambda pair: pair[0]) if amp > 0.1
]
# [(0.4999999999999984, 9.0), (0.749999999999999, 15.0), (1.0, 1.2000000000000002)]

这是正确的,但似乎有点黑客,有没有更好的方法来解决这个问题?

python numpy signal-processing fft
1个回答
0
投票

您可以使用

np.where
查找索引并过滤频率和幅度:

peaks_indices = np.where(fft_spectrum_abs > 0.1)[0]
freq_and_magnitudes = list(zip(freq[peaks_indices],
                               fft_spectrum_abs[peaks_indices]))
© www.soinside.com 2019 - 2024. All rights reserved.