用fourier函数在Matlab上画出2sin(t)/t的图形及其傅立叶后的图形
时间: 2024-03-16 14:46:57 浏览: 15
可以使用MATLAB的fft函数和ifft函数进行傅里叶变换和反变换,具体步骤如下:
1. 定义函数 $f(t) = 2\sin(t)/t$,并设定采样时间和采样点数。这里我们采用采样时间为 $0.01$,采样点数为 $1024$。
```
t = -5:0.01:5;
f = 2*sin(t)./t;
```
2. 对函数进行傅里叶变换,并求取频谱幅值和频率。
```
F = fft(f);
P2 = abs(F/length(f));
P1 = P2(1:length(f)/2+1);
P1(2:end-1) = 2*P1(2:end-1);
frequencies = 0:length(f)/2;
```
3. 绘制函数的时域波形和频域幅值谱图。
```
subplot(2,1,1);
plot(t,f);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time domain waveform');
subplot(2,1,2);
plot(frequencies,P1);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Frequency domain spectrum');
```
4. 对函数进行反傅里叶变换,并绘制反变换后的时域波形。
```
f_recon = ifft(F);
subplot(2,1,1);
plot(t,f_recon);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time domain waveform (reconstructed)');
```
完整的MATLAB代码如下:
```
t = -5:0.01:5;
f = 2*sin(t)./t;
F = fft(f);
P2 = abs(F/length(f));
P1 = P2(1:length(f)/2+1);
P1(2:end-1) = 2*P1(2:end-1);
frequencies = 0:length(f)/2;
subplot(2,1,1);
plot(t,f);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time domain waveform');
subplot(2,1,2);
plot(frequencies,P1);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Frequency domain spectrum');
f_recon = ifft(F);
subplot(2,1,1);
plot(t,f_recon);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time domain waveform (reconstructed)');
```