BER
clear;
N = 10^6; % Number of bits
rng(100); % Initialize random number generator
% randn('state',200); % Deprecated, replaced by rng
% Transmitter
ip = rand(1,N) > 0.5; % Generate random bits (0 or 1)
s = 2*ip - 1; % BPSK modulation: 0 -> -1, 1 -> +1
% Noise
n = 1/sqrt(2) * (randn(1,N) + 1j*randn(1,N)); % AWGN with 0 dB noise variance
Eb_N0_dB = -3:10; % Range of Eb/N0 values
nErr = zeros(1,length(Eb_N0_dB)); % To store number of errors
% Simulation loop
for ii = 1:length(Eb_N0_dB)
% Add noise based on current Eb/N0
y = s + 10^(-Eb_N0_dB(ii)/20) * n;
% Receiver - Hard decision decoding
ipHat = real(y) > 0;
% Count errors
nErr(ii) = sum(ip ~= ipHat);
end
% BER Calculation
simBer = nErr / N; % Simulated BER
theoryBer = 0.5 * erfc(sqrt(10.^(Eb_N0_dB / 10))); % Theoretical BER for BPSK
% Plotting
figure;
semilogy(Eb_N0_dB, theoryBer, 'b.-'); hold on;
semilogy(Eb_N0_dB, simBer, 'mx-');
axis([-3 10 10^-5 0.5]);
grid on;
legend('Theoretical', 'Simulated');
xlabel('Eb/N0 (dB)');
ylabel('Bit Error Rate (BER)');
title('BER Performance of BPSK over AWGN Channel');
%