完整程序:
clear; close all; clc;
c=0;
rate=1; %Code rate
N=10000000; %Number of bits
for EbNoc=0:1:10 %Ratio of bit energy to noise power spectral density
c=c+1;
X=randi([0 1],1,N); %Signal
MX=2*X-1; %BPSK modulation
SNR=EbNoc+10*log10(rate)-10*log10(0.5); %SNR equation
GX=awgn(MX,SNR,'measured'); %Adding Gaussian noise
DX=GX>0; %Detection
E=length(find(DX~=X)); %Error
EbNo(c)=EbNoc;
BER_Simulation(c)=E/N; %Simulation bit error rate
end
BER_Theoretical=berawgn(EbNo,'psk',2,'nondiff'); %Theoretical bit error rate
semilogy(EbNo,BER_Theoretical,'b-',EbNo,BER_Simulation,'r--');
grid on;
xlabel('Eb/No (dB)'); ylabel('误码率');
legend('理论','模拟');