采样频率为1000,采样数据点为128,输入信号为sin(2*pi*50*t)+0.4*sin(3*2*pi*50*t)+0.3*sin(5*2*pi*50*t)+0.05*sin(7*2*pi*50*t)的加窗插值fft算法
时间: 2023-09-29 15:11:33 浏览: 57
首先,对输入信号进行加窗。选择汉宁窗:
```python
import numpy as np
N = 128
n = np.arange(0, N, 1)
w = 0.5 * (1 - np.cos(2 * np.pi * n / (N - 1))) # 汉宁窗
x = 0.4 * np.sin(2 * np.pi * 50 * n / 1000) + \
0.4 * np.sin(3 * 2 * np.pi * 50 * n / 1000) + \
0.3 * np.sin(5 * 2 * np.pi * 50 * n / 1000) + \
0.05 * np.sin(7 * 2 * np.pi * 50 * n / 1000)
xw = x * w
```
然后,进行插值,将输入信号从128个数据点插值为N个数据点:
```python
M = 1024 # 插值点数
y = np.zeros(M)
for i in range(N):
for j in range(M // N):
y[i * (M // N) + j] = xw[i]
```
接着,进行FFT变换:
```python
Y = np.fft.fft(y)
Y_mag = np.abs(Y)
Y_phase = np.angle(Y)
```
最后,绘制幅度谱和相位谱:
```python
import matplotlib.pyplot as plt
f = np.arange(0, M, 1) * 1000 / M
plt.plot(f, Y_mag)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.show()
plt.plot(f, Y_phase)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Phase')
plt.show()
```
幅度谱如下图所示:
![magnitude_spectrum](https://i.loli.net/2021/06/28/j9tH4iXYzvZMx5W.png)
相位谱如下图所示:
![phase_spectrum](https://i.loli.net/2021/06/28/3q8iLHrG7VwYv6R.png)
阅读全文