matlab中分别对0dB,-10dB,-20dB三个f=20mhz的正弦信号进行FFT并进行频率和信噪比两方面的频谱分析并和波形一起绘制
时间: 2023-11-28 11:49:12 浏览: 67
假设三个正弦信号的采样率为100MHz,采样点数为1000,可以按照以下步骤进行FFT和频谱分析:
1. 生成三个正弦信号
```matlab
fs = 100e6; % 采样率
f = 20e6; % 正弦信号频率
N = 1000; % 采样点数
% 0dB信号
x1 = sin(2*pi*f*(0:N-1)/fs);
% -10dB信号
x2 = 0.3162*sin(2*pi*f*(0:N-1)/fs);
% -20dB信号
x3 = 0.1*sin(2*pi*f*(0:N-1)/fs);
```
2. 进行FFT
```matlab
X1 = fft(x1);
X2 = fft(x2);
X3 = fft(x3);
f_axis = (0:N-1)*(fs/N); % 频率轴
```
3. 绘制频谱图和波形图
```matlab
% 频谱图
figure;
subplot(3,1,1);
plot(f_axis, 20*log10(abs(X1)));
title('0dB Signal Spectrum');
ylabel('Magnitude (dB)');
grid on;
subplot(3,1,2);
plot(f_axis, 20*log10(abs(X2)));
title('-10dB Signal Spectrum');
ylabel('Magnitude (dB)');
grid on;
subplot(3,1,3);
plot(f_axis, 20*log10(abs(X3)));
title('-20dB Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
% 波形图
figure;
subplot(3,1,1);
plot((0:N-1)/fs, x1);
title('0dB Signal Waveform');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,2);
plot((0:N-1)/fs, x2);
title('-10dB Signal Waveform');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,3);
plot((0:N-1)/fs, x3);
title('-20dB Signal Waveform');
xlabel('Time (s)');
ylabel('Amplitude');
```
4. 计算信噪比
```matlab
% 计算信噪比
P1 = sum(abs(x1).^2)/N; % 信号功率
P2 = sum(abs(x1-x2).^2)/N; % 噪声功率
SNR1 = 10*log10(P1/P2); % 信噪比(0dB信号与-10dB信号)
P3 = sum(abs(x1-x3).^2)/N; % 噪声功率
SNR2 = 10*log10(P1/P3); % 信噪比(0dB信号与-20dB信号)
```
最后,可以将信噪比输出到命令行:
```matlab
fprintf('SNR between 0dB and -10dB signals: %.2f dB\n', SNR1);
fprintf('SNR between 0dB and -20dB signals: %.2f dB\n', SNR2);
```
阅读全文