写一个绘制三条BER曲线MATLAB代码
时间: 2023-12-06 17:45:36 浏览: 111
以下是绘制三条BER曲线的MATLAB代码:
```matlab
% 设置参数
SNR_dB = 0:10; % 信噪比范围
SNR = 10.^(SNR_dB/10); % 转换为线性信噪比
M = 4; % 调制方式
L = 1000000; % 发送符号数
% 生成随机符号序列
tx = randi([0 M-1], 1, L);
% 调制
if M == 2
mod_tx = pskmod(tx, M);
else
mod_tx = qammod(tx, M);
end
% 添加高斯白噪声
rx = awgn(mod_tx, SNR_dB(end), 'measured');
% 解调
if M == 2
demod_rx = pskdemod(rx, M);
else
demod_rx = qamdemod(rx, M);
end
% 计算误比特率
ber = zeros(1, length(SNR));
for i = 1:length(SNR)
if M == 2
[~, ber(i)] = biterr(tx, demod_rx);
else
[~, ber(i)] = biterr(de2bi(tx), de2bi(demod_rx));
end
end
% 绘制BER曲线
semilogy(SNR_dB, ber, 'b-o');
hold on;
% 重复上述步骤,改变调制方式和SNR值,绘制另外两条曲线
M = 16;
tx = randi([0 M-1], 1, L);
if M == 2
mod_tx = pskmod(tx, M);
else
mod_tx = qammod(tx, M);
end
rx = awgn(mod_tx, SNR_dB(end), 'measured');
if M == 2
demod_rx = pskdemod(rx, M);
else
demod_rx = qamdemod(rx, M);
end
ber = zeros(1, length(SNR));
for i = 1:length(SNR)
if M == 2
[~, ber(i)] = biterr(tx, demod_rx);
else
[~, ber(i)] = biterr(de2bi(tx), de2bi(demod_rx));
end
end
semilogy(SNR_dB, ber, 'r-*');
M = 64;
tx = randi([0 M-1], 1, L);
if M == 2
mod_tx = pskmod(tx, M);
else
mod_tx = qammod(tx, M);
end
rx = awgn(mod_tx, SNR_dB(end), 'measured');
if M == 2
demod_rx = pskdemod(rx, M);
else
demod_rx = qamdemod(rx, M);
end
ber = zeros(1, length(SNR));
for i = 1:length(SNR)
if M == 2
[~, ber(i)] = biterr(tx, demod_rx);
else
[~, ber(i)] = biterr(de2bi(tx), de2bi(demod_rx));
end
end
semilogy(SNR_dB, ber, 'g-d');
% 添加图例和标签
legend('QPSK', '16-QAM', '64-QAM');
xlabel('SNR (dB)');
ylabel('BER');
grid on;
```
这段代码将生成三条不同调制方式下的BER曲线(QPSK, 16-QAM, 64-QAM)并将它们绘制在同一张图上。在绘制图像之前,需要设置一些参数,例如信噪比范围、发送符号数、调制方式等。随机生成符号序列并通过AWGN信道添加噪声。然后解调接收信号并计算误比特率,最后绘制三条BER曲线。
阅读全文