[SOLVED] need help for BER and SNR plot for QAM modulation and demodulation

Status
Not open for further replies.

mewthturn

Newbie level 3
Joined
Oct 20, 2013
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
24
MATLAB : need help for BER and SNR plot for QAM modulation and demodulation

i am currently creating a code where the signal vector will be segmented out to a certain amount of bits and i will add noise to it and demodulation and get the BER. But my BER vs SNR plot graph looks very wierd to how a normal BER vs SNR graph should look like. I would appreciate if someone can tell me what is wrong with my code or something I am lacking, thank you.

Here is the code:
Code:
%% QAM-[Modulation,Demodulation,Bit Error Rate]

%% (1)Setup

    a = 4;                                      % A variable to make sure it is in [2^]

    N = 4;                                      % Number of antennas in Receieve and Transmit ends

    M = 2^a;                                    % M-ary QAM in base 2 / Size of bit for signal constellation points

    k = log2(M);                                % Number of bits per symbol

    n = 20;                                    % Number of bits to process
   
    
   
    oo=1;

    snr=1:5;   % snr in dB
   

%% (2)Signal Source

% Create a binary data stream as a column vector.

    binary = randi([0 1],n,k);                 % Random binary data stream

    de = bi2de(binary,'left-msb');                         % Convert binary to decimal values


%% (4)Modulation

    qammodulation = modem.qammod(M);            % Modulation type with value 'M'
    sym = modulate(qammodulation,de);           % Modulate using h-type \ showing where the points are located

    
    %sym=step(zz1,de)                       for soft decision of generalqammod
    
    
    
    
%% (5)Normalization : setting Es and Eb to 1                       
% to obtain E==1 : integral of (abs(signal))^2 dt

    b = zeros(n,1);                             % Creating a vector with 'n' amount of rows

for o = 1:(length(binary))                  % For 'o' amount of loops running to length of binary
   
    b(o) = (1/(abs(sym(o))))*sym(o);            % Each index of 'b' takes in the value of the function 4qam
                                            % Modulated vector = b
end
    Es=1;Eb=1; % due to normalization

%% (6)Miscellaneous





% Standard distribution matrix of N antena

    H = (1/sqrt(2))*(randn(N) + 1i*randn(N));   % H to be a matrix of mean=0,variance=1 and complex
                                            % 1/sqrt(2) due to (var=1) + (var=1) and normalisation is absolute rms
                                           
    y = zeros(N,1);                             % y = output vector

    s = zeros(N,1);                             % s = signal vector

    zfnns = zeros(N,1);                         % new s with 0 noise interference with Gzf

    zfns = zeros(N,1);                          % new s with noise interference with Gzf

    nns = zeros(N,1);                           % new s with 0 noise interference and direct inverse

    ns = zeros(N,1);                            % new s with noise interference with direct inverse

    tx = zeros(N*k,1);                          % tx = transmit vector



  


%% (7)Loop for Segmentation of 'k' bits matrix into vector

    io = 1; jo = 1; ko = 1;

while (ko<=M)
   
    tx(ko) = binary(io,jo);

    ko = (ko + 1);

    jo = mod( (jo + 1),(k + 1) );

if jo == 0
   
   io = io + 1;
  
   jo = 1;
  
end

end
%% (8)Segmentation of Signal Vector(s) to wanted dimension with 'N' amount of Transmitted and Received antenna(s)

    x=zeros((length(snr)),(n/k));
    
    ja=1; 
while ja<=length(snr)
    
    i = 1;
    j = N;
    oo=1;
while((i<=(n-(N-1)))&&(j<=(n)))
  
    SNR=10^((snr(ja))/10);                       % Changing from Decibels
   
    No  = Es/SNR;                           % Noise power
   
    meanofnoise = 0;                        % Expected Value of Noise vector to be zero
   
    sigma_2 = No;                         % Variance not zero
   
    sigma = sqrt(sigma_2);                  % Standard deviation
   
   
    s(1:N)=b(i:j);                           % Segmentation of vectors out to wanted amount
                                
    i = i + N;   
   
    j = j+ N;                              
 
    y(1:N) = H*s ;                         % Output : y = H*s
  
    %adding y with noise
   
    noise = normrnd(meanofnoise,sigma,N,1)+1i*normrnd(meanofnoise,sigma,N,1);
   
    ynoise=y+noise;
   
    vec_length = length(noise);

    Cn = zeros(vec_length);

for r = 1:vec_length
   
    Cn(r,r) = var(noise);                    % Creating Covariance Matrix with diagonal element of noise variance
                                            % and has zero mean due to Stochastic Independent
end;                
   
   
    %% (9)Filter Matrix

    G = pinv(H);                                % Due to zero noise ; HG = I

    Gzf = inv(conj(H'))*(inv(Cn))*(inv(Cn));    % Zero Forcing Filter Matrix

    nns(1:N) = G*y;                         % noiseless inverse
   
    ns(1:N) = G*ynoise;                     % noise inverse
   
    zfnns(1:N) = Gzf*y;                     % noiseless Gzf
   
    zfns(1:N) = Gzf*ynoise;                 % noise Gzf
   
%% (10)Demodulation

% Demodulate signal using M-QAM.
    qamdemodulation = modem.qamdemod(qammodulation);
    desym = demodulate(qamdemodulation,ynoise);
    
% Decimal values to binary using toolbox

    dectobit = comm.IntegerToBit(k);
%
    rx = step(dectobit,desym);

%% (11)BER Computation of each atenna of 'N' amount
% Comparsion of [ 't' which is the amount/type of bits transmitted ] &
% & [ 'r' which is the amount/type of bits received ] to obtain the number of error(s) % the bit error rate.

    er = comm.ErrorRate;                       % Toolbox that creates an error rate calculator System object

    bit_error_rate = step(er,tx,rx);            % Computes the error

    BER = bit_error_rate(1);                   % Bit Error Rate per 'N' amount of atenna bits

    errors = bit_error_rate(2);                % Number of Errors per 'N' amount of atenna bits
   
    x(ja,oo:oo)=errors;


% alternate way%[nubmer_of_errors,bit_error_rate] = biterr(tx,rx);

oo=oo+1;

end

    ja=ja+1;

end

%% (12)Transmitted Signal
    ytx = s;

%% (13)Received Signal
    yrx = ynoise;
%% (14)Total Bit Error Rate

    c1=zeros(length(snr),1);
    m=length(snr)*(k*N);        %total bits per snr
                            %total no.of different snr *  no. of bits to represent 1 symbol * no. of atennas
    c=1;

while c<=length(snr)
   
    c1(c)=sum(x(c,:));          %total errors per snr

    c=c+1;

end

    ber=c1/m;                    %dividing total error by total bits gives BER


%% Scatter Plot

% Create scatter plot of noisy signal and transmitted signal on the same axes.

    h = scatterplot(yrx(1:N),1,0,'b.');

    hold on;

    scatterplot(ytx(1:N),1,0,'k*',h);

    title('Received Signal');

    legend('Received Signal','Signal Constellation, What was transmitted');

    axis([-5 5 -5 5]); % axis parameters.

    hold off;

%% BER vs SNR
    figure;
    loglog(snr,ber);
    title('log graph of SNR vs BER');
    xlabel('BER');
    ylabel('SNR');
 
Last edited by a moderator:

Re: MATLAB : need help for BER and SNR plot for QAM modulation and demodulation

edit to the post . i am getting a weird zig zag curve plot for my SNR vs BER graph. For demodulation of noiseless signal i get a straight line plot which i believe is correct?
please correct me if there is anything wrong

Here is the code
Code:
    %% QAM-[Modulation,Demodulation,Bit Error Rate]
    
    %% (1)Setup
    
        a = 4;                                      % A variable to make sure it is in [2^]
    
        N = 4;                                      % Number of antennas in Receieve and Transmit ends
    
        M = 2^a;                                    % M-ary QAM in base 2 / Size of bit for signal constellation points
    
        k = log2(M);                                % Number of bits per symbol
    
        n = 100;                                    % Number of bits to process
       
        
       %unknown=(0:(M-1));
       % zz1=comm.GeneralQAMModulator(unknown)
       % zz2=comm.GeneralQAMDemodulator(unknown)
       
       %zz2=comm.GeneralQAMDemodulator(M,'BitOutput',true,'DecisionMethod','Log-Likelihood ratio')
        oo=1;
    
        snr=1:8:160;   % snr in dB
       
    
    %% (2)Signal Source
    
    % Create a binary data stream as a column vector.
    
        binary = randi([0 1],n,k)  ;               % Random binary data stream
    
        de = bi2de(binary,'left-msb');                         % Convert binary to decimal values
    
    
    %% (4)Modulation
    
        qammodulation = modem.qammod(M);            % Modulation type with value 'M'
        
        
        
        %sym=step(zz1,de)                       for soft decision of generalqammod
        
        
        
        
        sym = modulate(qammodulation,de) ;          % Modulate using h-type \ showing where the points are located
    
    %% (5)Normalization : setting Es and Eb to 1                       
    % to obtain E==1 : integral of (abs(signal))^2 dt
    
        b = zeros(n,1);                             % Creating a vector with 'n' amount of rows
    
    for o = 1:(length(binary))                  % For 'o' amount of loops running to length of binary
       
        b(o) = (1/(abs(sym(o))))*sym(o);            % Each index of 'b' takes in the value of the function 4qam
                                                % Modulated vector = b
    end
        Es=1;Eb=1; % due to normalization
    
    %% (6)Miscellaneous
    
    
    
    
    
    % Standard distribution matrix of N antena
    
        H = (1/sqrt(2))*(randn(N) + 1i*randn(N));   % H to be a matrix of mean=0,variance=1 and complex
                                                % 1/sqrt(2) due to (var=1) + (var=1) and normalisation is absolute rms
                                               
        y = zeros(N,1);                             % y = output vector
    
        s = zeros(N,1);                             % s = signal vector
    
        zfnns = zeros(N,1);                         % new s with 0 noise interference with Gzf
    
        zfns = zeros(N,1);                          % new s with noise interference with Gzf
    
        nns = zeros(N,1);                           % new s with 0 noise interference and direct inverse
    
        ns = zeros(N,1);                            % new s with noise interference with direct inverse
    
        tx = zeros(N*k,1);                          % tx = transmit vector
    
    
    
      
    
    
    %% (7)Loop for Segmentation of 'k' bits matrix into vector
    
        io = 1; jo = 1; ko = 1;
    
    while (ko<=M)
       
        tx(ko) = binary(io,jo);
    
        ko = (ko + 1);
    
        jo = mod( (jo + 1),(k + 1) );
    
    if jo == 0
       
       io = io + 1;
      
       jo = 1;
      
    end
    
    end
    %% (8)Segmentation of Signal Vector(s) to wanted dimension with 'N' amount of Transmitted and Received antenna(s)
    
        x=zeros((length(snr)),(n/k));
        
        ja=1; 
    while ja<=length(snr)
    
        i = 1;
        j = N;
        oo=1;
    while((i<=(n-(N-1)))&&(j<=(n)))
      
        SNR=10^((snr(ja))/10) ;                      % Changing from Decibels
       
        No  = Es/SNR ;                          % Noise power
       
        meanofnoise = 0;                        % Expected Value of Noise vector to be zero
       
        sigma_2 = No;                         % Variance not zero
       
        sigma = sqrt(sigma_2) ;                 % Standard deviation
       
       
        s(1:N)=b(i:j);                           % Segmentation of vectors out to wanted amount
                                    
        i = i + N;   
       
        j = j+ N;                              
     
        y(1:N) = H*s;                          % Output : y = H*s
      
        %adding y with noise
       
        noise = normrnd(meanofnoise,sigma,N,1)+1i*normrnd(meanofnoise,sigma,N,1);
       
        ynoise=y+noise;
       
        vec_length = length(noise);
    
        Cn = zeros(vec_length);
    
    for r = 1:vec_length
       
        Cn(r,r) = var(noise);                    % Creating Covariance Matrix with diagonal element of noise variance
                                                % and has zero mean due to Stochastic Independent
    end;                
       
       
        %% (9)Filter Matrix
    
        G = pinv(H);                                % Due to zero noise ; HG = I
    
        Gzf = inv(conj(H'))*(inv(Cn))*(inv(Cn));    % Zero Forcing Filter Matrix
    
        nns(1:N) = G*y;                         % noiseless inverse
       
        ns(1:N) = G*ynoise;                     % noise inverse
       
        zfnns(1:N) = Gzf*y;                     % noiseless Gzf
       
        zfns(1:N) = Gzf*ynoise;                 % noise Gzf
       
    %% (10)Demodulation
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    % Demodulate signal using M-QAM.
        qamdemodulation = modem.qamdemod(qammodulation);
        desym = demodulate(qamdemodulation,zfns);
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        %desym=step(zz2,ynoise,(var(noise)) )                         %for soft decision of generalqammod
        
        
        
    % Decimal values to binary using toolbox
    
        dectobit = comm.IntegerToBit(k);
    %
        rx = step(dectobit,desym);
    
    %% (11)BER Computation of each atenna of 'N' amount
    % Comparsion of [ 't' which is the amount/type of bits transmitted ] &
    % & [ 'r' which is the amount/type of bits received ] to obtain the number of error(s) % the bit error rate.
    
        er = comm.ErrorRate;                       % Toolbox that creates an error rate calculator System object
    
        bit_error_rate = step(er,tx,rx);            % Computes the error
    
        BER = bit_error_rate(1);                   % Bit Error Rate per 'N' amount of atenna bits
    
        errors = bit_error_rate(2);                % Number of Errors per 'N' amount of atenna bits
       
        x(ja,oo:oo)=errors;
    
    
    % alternate way%[nubmer_of_errors,bit_error_rate] = biterr(tx,rx);
    
    oo=oo+1;
    
    end
    
        ja=ja+1;
    
    end
    
    %% (12)Transmitted Signal
        ytx = s;
    
    %% (13)Received Signal
        yrx = zfns;
    %% (14)Total Bit Error Rate
    
        c1=zeros(length(snr),1);
        m=length(snr)*(k*N);        %total bits per snr
                                %total no.of different snr *  no. of bits to represent 1 symbol * no. of atennas
        c=1;
    
    while c<=length(snr)
       
        c1(c)=sum(x(c,:));          %total errors per snr
    
        c=c+1;
    
    end
    
        ber=c1/m;                    %dividing total error by total bits gives BER
    
    
    %% Scatter Plot
    
    % Create scatter plot of noisy signal and transmitted signal on the same axes.
    
        h = scatterplot(yrx(1:N),1,0,'b.');
    
        hold on;
    
        scatterplot(ytx(1:N),1,0,'k*',h);
    
        title('Received Signal');
    
        legend('Received Signal','Signal Constellation, What was transmitted');
    
        axis([-5 5 -5 5]); % axis parameters.
    
        hold off;
    
    %% BER vs SNR
        figure;
        loglog(snr,ber);
        title('log graph of SNR vs BER');
        xlabel('SNR');
        ylabel('BER');
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…