【Python信号处理】FFT变换后的坐标变换以及绘图

假 设 : f 1 = 10 ; f 2 = 20 ; f 3 = 30 假设: f1 = 10; f2 = 20; f3 = 30 f1=10;f2=20;f3=30
y = s i n ( 2 ∗ p i ∗ f 1 ∗ t ) + 2 ∗ s i n ( 2 ∗ p i ∗ f 2 ∗ t ) + 3 ∗ s i n ( 2 ∗ p i ∗ f 3 ∗ t ) y = sin(2*pi*f1*t) + 2*sin(2*pi*f2*t) + 3*sin(2*pi*f3*t) y=sin(2pif1t)+2sin(2pif2t)+3sin(2pif3t)
F r e q u e n c y S a m p l e ( f s ) = 128 , L e n g t h ( T ) = 5 ; 注 意 此 处 的 采 样 点 数 要 满 足 奈 奎 斯 特 准 则 FrequencySample(fs) = 128,Length(T)=5;注意此处的采样点数要满足奈奎斯特准则 FrequencySample(fs)=128,Length(T)=5
S u m m a t i o n L e n g t h ( N ) = f s ∗ T = 128 ∗ 5 Summation Length(N) = fs*T = 128 * 5 SummationLength(N)=fsT=1285
FFT后的频域转化公式为
F i = ∑ 1 < = i < = N i N ∗ f s F_i = \sum\limits_{1<=i<=N} \dfrac{i}{N} * fs Fi=1<=i<=NNifs
FFT后的幅值转化公式为, Ai为傅里叶变换后的第i个值
A i = ∑ 1 < = i < = N ∣ A i ∣ 2 N A_i = \sum\limits_{1<=i<=N} |A_i| \dfrac{2}{N} Ai=1<=i<=NAiN2

双边傅里叶变换频率

from matplotlib.pyplot import plot, show, subplot
from numpy import pi, sin, linspace
from numpy.fft import fft, fftfreq


fs = 128
T = 5
f1 = 10
f2 = 20
f3 = 30
t = linspace(0, T, T*fs)
y = sin(2*pi*f1*t) + 2*sin(2*pi*f2*t) + 3*sin(2*pi*f3*t)
x = fftfreq((len(t))) * fs
subplot(121)
plot(t, y)
subplot(122)
plot(x, abs(fft(y)) / len(t) * 2)
show()

在这里插入图片描述

单边傅里叶变换频率

from matplotlib.pyplot import plot, show, subplot
from numpy import pi, sin, linspace
from numpy.fft import fft, fftfreq


fs = 128
T = 5
f1 = 10
f2 = 20
f3 = 30
t = linspace(0, T, T*fs)
y = sin(2*pi*f1*t) + 2*sin(2*pi*f2*t) + 3*sin(2*pi*f3*t)
x = fftfreq((len(t))) * fs
subplot(121)
plot(t, y)
subplot(122)
plot(x[:len(x)//2], abs(fft(y))[:len(x)//2] / len(t) * 2)
show()

在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python中的NumPy库来实现FFT(快速傅里叶变换)并绘制形。首先,导入NumPy和Matplotlib库,并定义一些参数和信号。可以参考以下代码实现FFT绘图: ```python import numpy as np from matplotlib import pyplot as plt # 定义参数和信号 pi = np.pi # 圆周率 fs = 1000 # 采样频率 dt = 1/fs # 采样周期 f1 = 10 # 信号的特征频率1 f2 = 100 # 信号的特征频率2 f3 = 200 # 信号的特征频率3 N = 2**12 # 离散信号的长度 tn = np.arange(0, dt*N, dt) # 时间序列 x = 2*np.cos(2*pi*f1*tn) + 2*np.cos(2*pi*f2*tn) + 2*np.cos(2*pi*f3*tn) # 生成离散信号 # FFT计算函数 def fft_calc(x, f_s, x_size, nfft): w = np.hanning(nfft) # 加汉宁窗 cnt = x_size // nfft # 计算数据长度可以覆盖几个窗口 if cnt == 0: # 用0在数据尾部补齐 x_pad = np.pad(x, (0, nfft - x_size)) else: x_pad = np.pad(x, (0, x_size - cnt * nfft)) cnt = len(x_pad) // nfft # 更新补齐的数据长度可以覆盖几个窗口 tmp = [] for i in range(cnt): # 窗与窗之间数据不重叠 p = np.fft.fft(w * x_pad[i * nfft:(i + 1) * nfft]) # 计算加窗的FFT并乘以调整系数 tmp.append(p) # 每个窗的结果 fft_result = np.mean(tmp, axis=0) # 将所有窗平均得到最终结果 amp = abs(fft_result)*2 / (nfft / 2) # 根据采样宽度计算幅值 amp /= 2 # 对直流分量额外调整 amp_half = amp[:int(len(amp) / 2) + 1 # 根据FFT特性,取一半频谱即可 freq = np.arange(int(len(amp) / 2) + 1) * f_s/nfft # 根据采样频率和采样点数,计算频率分辨率,并得到对应的频率坐标 return amp_half, freq # 使用FFT计算函数并绘图 nfft = fs # FFT的窗长 (amp, freq) = fft_calc(x, fs, N, nfft) # 绘制FFT结果 fig = plt.figure(figsize=(12, 8)) plt.plot(freq, amp, color="blue") plt.title('FFT result') plt.xlabel('Freq /Hz') plt.ylabel('urad/Hz') ax = plt.gca() plt.grid(which='both', axis='both') plt.show() ``` 以上代码将生成一个包含FFT结果形窗口,横轴为频率(Hz),纵轴为幅度(urad/Hz)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值