在正弦波信号上加谐波,一直加到13次谐波,各次谐波的幅度为0.20,0.15,0.15,0.10,0.10,0.01,0.05,0.01,0.01,0.003,0.003,0.002,0.002次基波幅度,持续时间为1s,画出波形,并播放出这个信号;做fft,画出幅度谱。
时间: 2023-10-09 19:08:19 浏览: 132
下面是根据给定谐波幅度添加谐波、绘制波形和播放信号的修改代码:
```matlab
% 参数设置
fs = 8000; % 采样频率
f = 440; % 信号频率
a = 1; % 基波幅度
harmonic_amplitudes = [0.20, 0.15, 0.15, 0.10, 0.10, 0.01, 0.05, 0.01, 0.01, 0.003, 0.003, 0.002, 0.002]; % 各次谐波幅度
duration = 1; % 持续时间
% 生成时间轴
t = 0:1/fs:duration;
% 初始化信号
x_total = zeros(1, length(t));
% 添加谐波
for i = 1:length(harmonic_amplitudes)
harmonic_amplitude = harmonic_amplitudes(i) * a;
harmonic_frequency = (i+1) * f; % 谐波频率为基波频率的整数倍
x_harmonic = harmonic_amplitude * sin(2*pi*harmonic_frequency*t);
x_total = x_total + x_harmonic;
end
% 绘制波形
subplot(2, 1, 1);
plot(t, x_total);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sinusoidal Waveform with Harmonics');
grid on;
% 播放信号
sound(x_total, fs);
% 做FFT,并绘制幅度谱
subplot(2, 1, 2);
N = length(x_total);
frequencies = (0:fs/N:fs-fs/N);
X = abs(fft(x_total))/N;
plot(frequencies, X);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Amplitude Spectrum');
grid on;
```
这段代码会在 MATLAB 中生成一个带有13个谐波的正弦波信号的波形图和幅度谱,并通过扬声器播放出来。
阅读全文