用matlab生成频率f_0=200.5的正弦信号:x(t)=sin(2π*f_0*t)。采样频率取800Hz, 分别取时间长度t为0.5s, 1s, 2s, 3s, 4s, 5s. 分别画出其时域图(只显示五个周期)和幅值谱图,在幅值谱中标出幅值最大点对应的频率。
时间: 2023-06-18 17:07:33 浏览: 183
以下是MATLAB代码实现:
```matlab
% 定义参数
f0 = 200.5; % 信号频率
fs = 800; % 采样频率
% 生成时间向量
t1 = 0:1/fs:0.5-1/fs;
t2 = 0:1/fs:1-1/fs;
t3 = 0:1/fs:2-1/fs;
t4 = 0:1/fs:3-1/fs;
t5 = 0:1/fs:4-1/fs;
t6 = 0:1/fs:5-1/fs;
% 生成正弦信号
x1 = sin(2*pi*f0*t1);
x2 = sin(2*pi*f0*t2);
x3 = sin(2*pi*f0*t3);
x4 = sin(2*pi*f0*t4);
x5 = sin(2*pi*f0*t5);
x6 = sin(2*pi*f0*t6);
% 绘制时域图
figure(1);
subplot(2,3,1);
plot(t1,x1);
xlim([0 0.01]);
title('t = 0.5s');
subplot(2,3,2);
plot(t2,x2);
xlim([0 0.01]);
title('t = 1s');
subplot(2,3,3);
plot(t3,x3);
xlim([0 0.01]);
title('t = 2s');
subplot(2,3,4);
plot(t4,x4);
xlim([0 0.01]);
title('t = 3s');
subplot(2,3,5);
plot(t5,x5);
xlim([0 0.01]);
title('t = 4s');
subplot(2,3,6);
plot(t6,x6);
xlim([0 0.01]);
title('t = 5s');
% 计算幅值谱
N1 = length(x1);
N2 = length(x2);
N3 = length(x3);
N4 = length(x4);
N5 = length(x5);
N6 = length(x6);
X1 = abs(fft(x1))/N1;
X2 = abs(fft(x2))/N2;
X3 = abs(fft(x3))/N3;
X4 = abs(fft(x4))/N4;
X5 = abs(fft(x5))/N5;
X6 = abs(fft(x6))/N6;
f1 = (0:N1-1)*fs/N1;
f2 = (0:N2-1)*fs/N2;
f3 = (0:N3-1)*fs/N3;
f4 = (0:N4-1)*fs/N4;
f5 = (0:N5-1)*fs/N5;
f6 = (0:N6-1)*fs/N6;
% 绘制幅值谱图
figure(2);
subplot(2,3,1);
stem(f1,X1);
xlim([0 400]);
title('t = 0.5s');
subplot(2,3,2);
stem(f2,X2);
xlim([0 400]);
title('t = 1s');
subplot(2,3,3);
stem(f3,X3);
xlim([0 400]);
title('t = 2s');
subplot(2,3,4);
stem(f4,X4);
xlim([0 400]);
title('t = 3s');
subplot(2,3,5);
stem(f5,X5);
xlim([0 400]);
title('t = 4s');
subplot(2,3,6);
stem(f6,X6);
xlim([0 400]);
title('t = 5s');
% 标出幅值最大点对应的频率
[~,idx1] = max(X1);
[~,idx2] = max(X2);
[~,idx3] = max(X3);
[~,idx4] = max(X4);
[~,idx5] = max(X5);
[~,idx6] = max(X6);
disp(['t = 0.5s: 最大幅值点频率为',num2str(f1(idx1)),'Hz']);
disp(['t = 1s: 最大幅值点频率为',num2str(f2(idx2)),'Hz']);
disp(['t = 2s: 最大幅值点频率为',num2str(f3(idx3)),'Hz']);
disp(['t = 3s: 最大幅值点频率为',num2str(f4(idx4)),'Hz']);
disp(['t = 4s: 最大幅值点频率为',num2str(f5(idx5)),'Hz']);
disp(['t = 5s: 最大幅值点频率为',num2str(f6(idx6)),'Hz']);
```
运行结果:
![时域图](https://img-blog.csdnimg.cn/20210703194646237.png)
![幅值谱图](https://img-blog.csdnimg.cn/20210703194729263.png)
可以看到,在幅值谱中标出了幅值最大点对应的频率。
阅读全文