写出BPSK和QPSK在AWGN信道下的SNR对比的MATLAB代码
时间: 2023-06-19 19:06:09 浏览: 115
以下是BPSK和QPSK在AWGN信道下的SNR对比的MATLAB代码:
```matlab
% BPSK和QPSK在AWGN信道下的SNR对比
clear all;
close all;
% 设置参数
N = 100000; % 发送符号的数量
EbN0dB = 0:2:20; % 信噪比范围
EbN0 = 10.^(EbN0dB/10);
% 生成随机的二进制序列
data = randi([0,1],1,N);
% BPSK调制
bpsk_data = 1-2*data;
% QPSK调制
qpsk_data = 1/sqrt(2)*(1-2*data(1:2:end)) + 1i*1/sqrt(2)*(1-2*data(2:2:end));
% 计算理论误码率
Pb_bpsk = 0.5*erfc(sqrt(EbN0));
Pb_qpsk = 0.5*erfc(sqrt(EbN0/2));
% 产生高斯白噪声
for ii = 1:length(EbN0dB)
% 计算噪声功率
N0 = 1/EbN0(ii);
% 产生高斯白噪声
noise_bpsk = sqrt(N0/2)*(randn(1,N)+1i*randn(1,N));
noise_qpsk = sqrt(N0/2)*(randn(1,N/2)+1i*randn(1,N/2));
% 添加噪声
received_bpsk = bpsk_data + noise_bpsk;
received_qpsk = qpsk_data + noise_qpsk;
% BPSK解调
bpsk_demod = real(received_bpsk)<0;
% QPSK解调
qpsk_demod = zeros(1,N);
qpsk_demod(1:2:end) = real(received_qpsk)<0;
qpsk_demod(2:2:end) = imag(received_qpsk)<0;
% 计算实际误码率
Pe_bpsk(ii) = sum(bpsk_demod~=data)/N;
Pe_qpsk(ii) = sum(qpsk_demod~=data)/N;
end
% 绘制误码率曲线
figure;
semilogy(EbN0dB,Pb_bpsk,'r-','LineWidth',2);
hold on;
semilogy(EbN0dB,Pe_bpsk,'ro-','LineWidth',1);
semilogy(EbN0dB,Pb_qpsk,'b-','LineWidth',2);
semilogy(EbN0dB,Pe_qpsk,'bo-','LineWidth',1);
grid on;
xlabel('Eb/N0(dB)');
ylabel('误码率');
title('BPSK和QPSK在AWGN信道下的SNR对比');
legend('BPSK理论误码率','BPSK实际误码率','QPSK理论误码率','QPSK实际误码率');
```
阅读全文