Theoritical and Practical BER COMPARISON of different M-ary QAM

 % Theoritical and Practical BER COMPARISON of different M-ary QAM

% ========================================%

% User Defined Functions used in this File are

% s2p --> Serial to Parallel Conversion

% p2s --> Parallel to Serial Conversion

% QAM_T ---> Theoritical M-QAM BER Calculation

% ========================================%

clc;

clear all;

close all;

bits = 1.e6;

msg  = round(rand(1,bits));                                                             % Generating Random Bit Stream

M = [4 16 64 256];                                                                          % M-ary

for j=1:4

% ============================= Tx ===========================%

%% 16-QAM Modulation

bits_sym = log2(M(j));                                                     % Bits per Symbol for M-ary

sym_tx = s2p(msg,bits_sym);                                          % Serial to Parallel Conversion for QAM

H_Tx = modem.qammod('M', M(j), 'PHASEOFFSET', 0, 'SYMBOLORDER', 'BINARY', ...

                                                    'INPUTTYPE', 'BIT');                           % Generate Handle for Modulation

qam_tx = modulate(H_Tx,sym_tx);                                               % Modulation

%======================== CHANNEL========================%

Eb_No = -11:1:11;                                                           % Eb/No in DB

SNR = Eb_No + 10*log10(log2(M(j)));                         % Finding out SNR from Eb/No

for i=1:length(Eb_No)

 qam_n = awgn(qam_tx,SNR(i),'measured');                      % Adding AWGN

    % ======================== Rx ============================%

%% QAM De-Modulation

qam_rx = p2s(qam_n);

H_Rx = modem.qamdemod('M', M(j), 'PHASEOFFSET', 0, 'SYMBOLORDER', 'BINARY', ...

                 'OUTPUTTYPE', 'BIT');                                     % Generate Handle for Modulation

sym_rx = demodulate(H_Rx,qam_rx);                                % Modulating data to QAM

%% Symbols to Msg Conversion

msg_rx = p2s(sym_rx);

msg_rx = msg_rx(1,1:length(msg));


[ErrBits BER(i,j)] = symerr(msg_rx,msg);         % Calculating BER by comparisson of Rx and Tx msg

BER_T (i,j) = QAM_T(Eb_No(i),M(j));                              % Theoritical BER calculation using formula

end

end

%%  Plot of  BER vs Eb/No

BER

BER_T

semilogy(Eb_No,BER(:,1),'r-*',Eb_No,BER_T(:,1),'g-o',Eb_No,BER(:,2),'b-v',Eb_No,BER_T(:,2),'k-x',Eb_No,BER(:,3),'c-*',Eb_No,BER_T(:,3),'m-o',Eb_No,BER(:,4),'y-x',Eb_No,BER_T(:,4),'k-*');    

legend('M=4','M_T=4','M=16','M_T=16','M=64','M_T=64','M=256','M_T=256');

title('Theoritical and Practical BER COMPARISON of different M-ary QAM');

ylabel('BER');

xlabel('Eb/No (dB)');

grid;

-------------------------------------------------------------------------------------------------------------------------

%% Function Serial to Parallel I/P msg and No. of Parallel Channels

function p_data = s2p(s_data,N)

    l = length(s_data);

    mode = mod(l,N);

    if mode ~= 0

        z_add = zeros(1,N-mode);

        data = [s_data z_add];

    else

        data = s_data;

    end

    M = length(data)/N;

    p_data = reshape(data,N,M);

end

--------------------------------------------------------------------------------------------------------------------------

%% Function Parallel to Serial

function s_data = p2s(data)

    data1 = data(:);

    s_data = data1.';

end

--------------------------------------------------------------------------------------------------------------------------

%% Function Serial to Parallel I/P msg and No. of Parallel Channels

function BER = QAM_T(Eb_No,M)

EbNo=10.^(Eb_No/10);

k=log2(M);

x=sqrt(3*k*EbNo/(M-1));

Pb=(4/k)*(1-1/sqrt(M))*(1/2)*erfc(x/sqrt(2));

BER = 1-(1-Pb)^2;

end

---------------------------------------------------------------------------------------------------------------------------

Result