image thumbnail
from Implementation of 16-QAM algorithm in the presence of AWGN channel by Umair Nadeem
This algorithm implements the 16-QAM modulation and demodulation in the presence of AWGN channel

qam.m


                        %%% 16-QAM Modulator and Demodulator Implementation with an AWGN Channel %%%
                       

clc;
clear;
close all;


    %%%% Input Signal %%%%
    % Input is a stream of binary digits of integers 0-15
dec=[0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1]; 
dl=length(dec);




	%%%% Serial To Parallel and 2-to-4 Level Converter %%%%
    sp=[];
    o1=[];
    o2=[];
    clear i;
    for i=1:4:64;
        sp=[dec(1,i:i+3)];  % Serial to Parallel 4-bit Register
        I=sp(1,1);          % Separation of I and Q bits
        Id=sp(1,2);
        Q=sp(1,3);
        Qd=sp(1,4);
    
        if I==0             % Assigning Amplitude levels for I-channel
            if Id==0
                o1=[o1 -3]; % if input is 00, output is -3
            else
                o1=[o1 -1]; % if input is 01, output is -1
            end
        
        else
            if Id==0
                o1=[o1 1]; % if input is 10, output is 1
            else
                o1=[o1 3]; % if input is 11, output is 3
            end
        end
    
        if Q==0             % Assigning Amplitude levels for Q-channel
            if Qd==0
                o2=[o2 -3]; % if input is 00, output is -3
            else
                o2=[o2 -1]; % if input is 01, output is -1
            end
        
        else
            if Qd==0
                o2=[o2 1]; % if input is 10, output is 1
            else
                o2=[o2 3]; % if input is 11, output is 3
            end
        end
    
    clear sp, clear I, clear Id, clear Q, clear Qd; 
    end


    
    %%%% Oscillator and Balanced Modulator %%%%
    fc=500;              % Carrier Frequency
    fs=20000;            % Sampling Frequency
    t=1:100;             % Duration of Signal
    s=[];
    clear i;
    for i=1:1:16;        % Modulation (multiplication with carrier signals cos and sin)
        Ac=o1(i);
        As=o2(i);
        s1=Ac*cos(2*pi*(fc/fs)*t);
        s2=As*sin(2*pi*(fc/fs)*t);
        s=[s (s1+s2)];
    end
    figure(1)
    plot(s)     % 's' demotes the transmitted signal
    title('16-QAM Modulated Signal','FontSize',14)
    Xlim([0 1610])
    
       
    
    
    
    %%%% AWGN Addition While Passing Through The Channel %%%%
    r=awgn(s,10);       % Transmitted Signal passes through AWGN Channel
    figure(2)           % Received Signal SNR is 10 db
    plot(r)             % received signal is denoted by 'r'
    title('Transmitted Signal With Additive Noise After Passing Through The Channel','FontSize',14);
    
    
    
    %%%% COHERENT DEMODULATION %%%%
    ss1=[];
    ss2=[];
    rs1=2*cos(2*pi*(fc/fs)*t);  % Sin and Cos generation by Local Oscillator
    rs2=2*sin(2*pi*(fc/fs)*t);
    clear i;
    for i=1:100:1600;           % Demodulation of Received Signal
    ss1=[ss1 rs1.*r(1,i:i+99)]; % for I-channel
    ss2=[ss2 rs2.*r(1,i:i+99)]; % for Q-channel
    end
    
    
    
    %%%% Low-Pass Filtering %%%%
                          
    wn=2*(fc/fs);                 % Cut-off Frequency is 500Hz
    [b1,a1]=butter(1,wn,'low');   % ButterWorth filter of Order 1
    [b2,a2]=butter(1,wn,'low');
    fo1=filter(b1,a1,ss1);        % Filtering
    fo2=filter(b2,a2,ss2);
    figure(3);
    plot(fo1,'linewidth',1.5)
    title('I-Channel After Passing Through Low Pass ButterWorth Filter of Order 2','FontSize',14);
    figure(4);
    plot(fo2,'linewidth',1.5)
    title('Q-Channel After Passing Through Low Pass ButterWorth Filter of Order 2','FontSize',14);
     
    
    
    %%%% Thresholding and Phase Detection %%%%
    ro1=[];
    ro2=[];
    clear i;
    for i=1:100:1600;           % Calculating Average values over an interval of 100 bits for I-channel 
            ff1=fo1(1,i:i+99);
            l1=length(ff1);
            sum1=sum(ff1);
            av1=sum1/l1;
            ro1=[ro1 av1];        
    end
    clear i;
    for i=1:100:1600;           % Calculating Average values over an interval of 100 bits for Q-channel
            ff2=fo2(1,i:i+99);
            l2=length(ff2);
            sum2=sum(ff2);
            av2=sum2/l2;
            ro2=[ro2 av2];        
    end
    
    
    
    %%%% Amplitude Detection and Generation of Demodulated Data %%%%
    oo1=round(ro1);
    oo2=round(ro2);
    op=[];
    clear i;
    
        % Re-assigning bits with respect to amplitude levels
        
    for i=1:16;             % Demodulation by Amplitude Detection
        if oo1(i)==-3;
            op=[op 0 0];    % if amplitude of I-channel is -3, output is 00
            if oo2(i)==-3; 
                op=[op 0 0];   % if amplitude of Q-channel is -3 output is 00
            else if oo2(i)==-1;   
                    op=[op 0 1];   % if amplitude of Q-channel is -1 output is 01
                else if oo2(i)==1;     
                        op=[op 1 0];   % if amplitude of Q-channel is 1 output is 10
                    else
                        op=[op 1 1];    % if amplitude of Q-channel is 3 output is 11    
                    end
                end
            end
            
            
        else if oo1(i)==-1;    % if amplitude of I-channel is -1, output is 01
                op=[op 0 1];
                if oo2(i)==-3;
                     op=[op 0 0];   % if amplitude of Q-channel is -3 output is 00
                  else if oo2(i)==-1;
                            op=[op 0 1];   % if amplitude of Q-channel is -1 output is 01
                     else if oo2(i)==1;
                             op=[op 1 0];   % if amplitude of Q-channel is 1 output is 10
                         else
                             op=[op 1 1];   % if amplitude of Q-channel is 3 output is 11
                         end
                      end
                 end
                
                
            else if oo1(i)==1;
                    op=[op 1 0];    % if amplitude of I-channel is 1, output is 10
                    if oo2(i)==-3;
                       op=[op 0 0];   % if amplitude of Q-channel is -3 output is 00
                     else if oo2(i)==-1;
                          op=[op 0 1];   % if amplitude of Q-channel is -1 output is 01
                         else if oo2(i)==1;
                                  op=[op 1 0];   % if amplitude of Q-channel is 1 output is 10
                             else
                              op=[op 1 1];   % if amplitude of Q-channel is 3 output is 11
                             end
                        end
                    end
                    
                    
                else
                    op=[op 1 1];       % if amplitude of I-channel is 3, output is 11 
                    if oo2(i)==-3;
                         op=[op 0 0];   % if amplitude of Q-channel is -3 output is 00
                     else if oo2(i)==-1;
                          op=[op 0 1];  % if amplitude of Q-channel is -1 output is 01
                           else if oo2(i)==1;
                               op=[op 1 0];    % if amplitude of Q-channel is 1 output is 10
                               else
                                 op=[op 1 1];  % if amplitude of Q-channel is 3 output is 11
                               end
                           end
                    end
                end
            end
        end
    end
    figure(5);
    subplot(2,1,1);
    plot(dec,'linewidth',2)
    title('Input Data Stream','FontSize',14);
    Xlim([0 65])
    Ylim([-0.5 1.5])
    subplot(2,1,2);
    plot(op,'linewidth',2)
    title('Demodulated Output Data (Same As Input)','FontSize',14);
    Ylim([-0.5 1.5])
    Xlim([0 65])
    
    
    
    %%%% Generation of Constellation Diagram For Transmitted Data and Received Data%%%%
    xo1=o1;               
    yo2=o2;
    xoo1=ro1;
    yoo2=ro2;
     
    figure(6);
    plot(xo1,yo2,'hk','linewidth',4);   % plot of original symbols
    hold on
    plot(xoo1,yoo2,'Or','linewidth',4); % plot of received symbols
    legend('=  Transmitted Data','=  Received Data');
    x=line([-5 5],[-2 -2],'linewidth',4);     % Horizontal Lines on Constellation Diagram
    text(-1.9,3.7,'x=-2','FontSize',14)
    x1=line([-5 5],[0 0],'linewidth',4);
    text(0.1,3.7,'x=0','FontSize',14)
    x2=line([-5 5],[2 2],'linewidth',4);
    text(2.1,3.7,'x=2','FontSize',14)
    y=line([-2 -2],[-15 15],'linewidth',4);   % Vertical Lines on Constellation Diagram
    text(-3.9,-1.8,'y=-2','FontSize',14)
    y1=line([0 0],[-15 15],'linewidth',4);
    text(-3.9,0.2,'y=0','FontSize',14)
    y2=line([2 2],[-15 15],'linewidth',4);
    text(-3.9,2.2,'y=2','FontSize',14)
    title('Constellation Diagram for Transmitted and Received Data','FontSize',14);
    Xlim([-4 4])
    Ylim([-4 4])
          
    

Contact us