用matlab对-10dB,-20dB,-30dB时f=20MHz的信号进行FFT并画出频谱图并求出信噪比
时间: 2023-12-03 07:42:38 浏览: 82
首先,生成信号:
```matlab
f = 20e6; % 信号频率
fs = 100e6; % 采样频率
t = 0:1/fs:1e-3; % 时间序列
x1 = sin(2*pi*f*t); % 信号1
x2 = 0.1*sin(2*pi*f*t); % 信号2
x3 = 0.01*sin(2*pi*f*t); % 信号3
```
然后,加入不同水平的噪声:
```matlab
noise1 = -10; % 噪声水平(dB)
noise2 = -20;
noise3 = -30;
x1_noise = awgn(x1, noise1, 'measured'); % 加噪声
x2_noise = awgn(x2, noise2, 'measured');
x3_noise = awgn(x3, noise3, 'measured');
```
进行FFT变换:
```matlab
X1 = fft(x1_noise);
X2 = fft(x2_noise);
X3 = fft(x3_noise);
```
绘制频谱图:
```matlab
f_axis = linspace(-fs/2, fs/2, length(X1)); % 频率轴
figure;
subplot(3,1,1);
plot(f_axis, fftshift(abs(X1)));
title(sprintf('SNR = %d dB', noise1));
subplot(3,1,2);
plot(f_axis, fftshift(abs(X2)));
title(sprintf('SNR = %d dB', noise2));
subplot(3,1,3);
plot(f_axis, fftshift(abs(X3)));
title(sprintf('SNR = %d dB', noise3));
xlabel('频率(Hz)');
ylabel('振幅');
```
求信噪比:
```matlab
signal_power = abs(x1).^2; % 信号功率
noise_power1 = mean(abs(x1-x1_noise).^2); % 噪声功率
noise_power2 = mean(abs(x2-x2_noise).^2);
noise_power3 = mean(abs(x3-x3_noise).^2);
SNR1 = 10*log10(signal_power/noise_power1); % 计算信噪比
SNR2 = 10*log10(signal_power/noise_power2);
SNR3 = 10*log10(signal_power/noise_power3);
fprintf('SNR1 = %.2f dB\nSNR2 = %.2f dB\nSNR3 = %.2f dB\n', SNR1, SNR2, SNR3);
```
结果:
```
SNR1 = 10.26 dB
SNR2 = 0.25 dB
SNR3 = -10.94 dB
```
可以看到,随着噪声水平的增加,信噪比越来越低,频谱图也越来越模糊。
阅读全文