在FFT中如何将时间转换为频率

2024-06-01 21:09:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我是FFT新手,我试图通过手动测试来理解Python代码。 我测试的原因是一步一步地理解FFT

import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import fft, fftfreq

n = 1000 #Number of samples on x axis
Lx = 100 #Time period in seconds
omg = 2.0*np.pi/Lx #Frequency is in cycles per second. Multiplying by 2π gives the frequency in radians

x = np.linspace(0, Lx, n)# np.linspace(start = val, stop = val, num = val), here 0 to 100 with 1000 numbers

#A Sine wave with 1 wave with 1 amplitude
y = (1.0*np.sin(1.0*omg*x))

freqs = fftfreq(n)
#print('FFT Values freqs ',freqs[mask])

# Used to ignore half the values
mask = freqs > 0

# fft values
fft_vals = fft(y)

#Theoritical FFT
fft_theo = 2.0*np.abs(fft_vals/n)
#print('FFT Amplitude ',fft_theo[mask])
plt.figure(1)
plt.title('Original Signal')
plt.plot(x, y)
plt.xlabel('Time in seconds')
plt.ylabel('Amplitude')
plt.legend()

plt.figure(2)
plt.title('FFT values using Mask')
plt.plot(freqs[mask], fft_theo[mask], color='xkcd:salmon')
plt.xlabel('Frequency in Hz')
plt.ylabel('Amplitude')
plt.legend()

plt.show

以下是我对该计划的理解 输入为单一正弦波,振幅为1,x轴为时间,周期为100s 现在一个波的长度是2π除以1000 我理解了发送单个正弦波的输入,时间在x轴上,振幅在y轴上

使用fftfreq函数将此时间转换为频率。我不明白这个函数是如何转换的。 若要忽略负值,请将掩码变量指定为仅包含大于0的频率。 我也不明白为什么我们必须调用fft函数来转换振幅,因为振幅应该保持不变,只有时间才能转换成频率

然后计算理论值fft_theo=2.0*np.abs(fft_vals/n),使输出与预期一致

enter image description here

enter image description here

在输出中,我也不明白为什么频率为0时振幅为1

请帮助我用外行的术语理解FFT

谢谢, 阿莎


Tags: inimportfftnp时间pltmaskval
1条回答
网友
1楼 · 发布于 2024-06-01 21:09:44

FFT幅值不会将单个正弦信号的时间转换为频率

相反,它分解可能更有趣的波形

它将假定可能由大量正弦波之和组成的波形转换为一个阵列,该阵列包含与一组N/2个不同频率的正弦波相关的每个频率的数量

您在图表底部附近看到的峰值表示与极低频率(与图表的X范围相比)的单一强相关性。与更高频率的大量相关性都为零

相关问题 更多 >