请写出完成BPSK和QPSK调制在AWGN信道下的SNR-BER误码率性能评估的matlab代码
时间: 2024-03-09 11:51:16 浏览: 276
以下是BPSK和QPSK调制在AWGN信道下的SNR-BER误码率性能评估的Matlab代码,其中SNR范围设置为-5到15 dB,步长为1dB:
```
%% BPSK and QPSK Modulation in AWGN Channel with SNR-BER Performance Evaluation
clear all;
close all;
clc;
%% Generating message signal
msg_length = 10^4; % Message signal length
msg = randi([0 1],msg_length,1); % Random binary message signal
%% BPSK Modulation
bpsk = 1 - 2*msg; % BPSK modulation
bpsk_noise = zeros(msg_length, 21); % Pre-allocating memory for noise signal
bpsk_ber = zeros(1,21); % Pre-allocating memory for BER values
%% QPSK Modulation
qpsk = zeros(msg_length/2,2); % Pre-allocating memory for QPSK modulated signal
qpsk_noise = zeros(msg_length/2,21); % Pre-allocating memory for noise signal
qpsk_ber = zeros(1,21); % Pre-allocating memory for BER values
for i = 1:21 % SNR range
EbN0dB(i) = -5 + (i-1); % Eb/N0 in dB
EbN0(i) = 10^(EbN0dB(i)/10); % Eb/N0 in linear scale
sigma(i) = sqrt(1/(2*EbN0(i))); % Standard deviation of noise
%% BPSK Modulation with AWGN
bpsk_noise(:,i) = bpsk + sigma(i)*randn(msg_length,1); % BPSK signal with AWGN
bpsk_demod = sign(bpsk_noise(:,i)); % BPSK demodulation
bpsk_error = find(bpsk ~= bpsk_demod); % Finding the errors
bpsk_ber(i) = length(bpsk_error)/msg_length; % Calculating the BER
%% QPSK Modulation with AWGN
for j = 1:2:msg_length % QPSK modulation
if msg(j) == 0 && msg(j+1) == 0
qpsk((j+1)/2,:) = [1 1];
elseif msg(j) == 0 && msg(j+1) == 1
qpsk((j+1)/2,:) = [-1 1];
elseif msg(j) == 1 && msg(j+1) == 0
qpsk((j+1)/2,:) = [-1 -1];
else
qpsk((j+1)/2,:) = [1 -1];
end
end
qpsk_noise(:,i) = qpsk + sigma(i)*randn(msg_length/2,2); % QPSK signal with AWGN
qpsk_demod = zeros(msg_length,1); % Pre-allocating memory for QPSK demodulated signal
for k = 1:2:msg_length % QPSK demodulation
if qpsk_noise((k+1)/2,1) >= 0
qpsk_demod(k) = 0;
else
qpsk_demod(k) = 1;
end
if qpsk_noise((k+1)/2,2) >= 0
qpsk_demod(k+1) = 0;
else
qpsk_demod(k+1) = 1;
end
end
qpsk_error = find(msg ~= qpsk_demod); % Finding the errors
qpsk_ber(i) = length(qpsk_error)/msg_length; % Calculating the BER
end
%% Plotting the BER vs SNR
figure(1)
semilogy(EbN0dB,bpsk_ber,'-o','LineWidth',2);
hold on
semilogy(EbN0dB,qpsk_ber,'-s','LineWidth',2);
grid on
axis([-5 15 10^-5 1])
xlabel('Eb/N0 (dB)');
ylabel('Bit Error Rate (BER)');
legend('BPSK','QPSK');
title('SNR-BER Performance of BPSK and QPSK Modulation in AWGN Channel');
```
注意:这段代码仅供参考,实际应用中需要根据具体情况进行更改。
阅读全文