Showing posts with label vec2mat. Show all posts
Showing posts with label vec2mat. Show all posts

Wednesday, 21 May 2014

Implementing Spread Spectrum Communication in Matlab



DSSS  is  a  wireless  analog  encoding  scheme  based  on  a  technique   spread  spectrum.  
It represents each data bit by multiple bits in the transmitted signal using a chip code/spreading code. The spreading code spreads the signal across a wider frequency band in direct proportion to the length of the chip code. 

Task 1: Study and implement the  matlab functions and show the output:
Hadamard:
This MATLAB function returns the Hadamard matrix of order n.
Syntax:
Hadamard(n) % n n must be an integer and n, n/12 or n/20 must be a power of 2.
Command: Hadamard(4);
Output: 
     1     1     1     1
     1    -1     1    -1
     1     1    -1    -1
     1    -1    -1     1
randperm
It stands for random permutation
Syntax:
P = randperm(n); % returns a row vector containing a random permutation of the integer from 1 to n inclusive.
P= randperm(n,k) % return a row vector containing k unique integers selected randomly from 1 to n inclusive. 
Command: 
randperm(7);
output:  6     3     7     5     1     2     4
Command:
Randperm(7,3);
Output:
                     6     2     7

randint
It generates a matrix of uniformly distributed random integers.
Syntax:
Out= randint % generate either 0 or 1
Out = randint(n) % generate a matrix of order nxn containing randomly 0’s and 1’s.
Out = randint(n,m,range)% generates a matrix of order nxm, if rg is 0 it will generate matrix containing all zeros, otherwise it will have integer containing in the specified range.
Command:   randint(2,2,9)
                  Output:8     4
              4     2
biterr
It computes the number of bit errors and bit error rate(BER).
Syntax:
 [number, ratio]= biterr(x,y) % compare the unsigned binary representation of each element of x matrix with that of y matrix.
[number, ratio]= biterr(x,y,k) % it considers that elements of x and y have k bits.
[number, ratio]= biterr(x,y,k,flg) % flg can be ‘overall’ (element by element comparison), ‘rsow_wise’  (compare nth row of x with nth row of y) and ‘column_wise’. 
[number,ratio, individual]= biterr(….) % individual have dimension greater then that of x and y matrix. Each element of individual represent the difference of bits between the corresponding elements of x and y matrix and number of comparisons.
Command:
[number,ratio,individual] = biterr([0;0;0],randi([0 1],3,5));
Output:
number = 2              1              2              1              1       
                 ratio =  2/3            1/3            2/3            1/3            1/3     
                 individual =0              0              0              1              0       
                     1              0              1              0              1       
                     1              1              1              0              0       
bi2de
convert binary vector to decimal equivalent number.
Syntax:
Bin2de(v)
Command:   v=[1 0 1 0 1]
bi2de(v);
Output= 21
bin2gray
Converts binary integer to corresponding  Gray-encoded integer.
Syntax: 
Y= bin2gray(x,modulation,M)
or 
[y,map]= bin2gray(x,modulation,M) 
Code:
% To Gray encode a vector x with a 16-QAM Gray encoded
 % constellation and return its map, use:
 x=randi([0 5],1,100);
 [y,map] = bin2gray(x,'qam',16);
 % Obtain the symbols for 16-QAM
 hMod = modem.qammod('M', 16);
 symbols = hMod.Constellation;
 % Plot the constellation
 scatterplot(symbols);
 set(get(gca,'Children'),'Marker','d','MarkerFaceColor',...
 'auto'); hold on;
 % Label the constellation points according
 % to the Gray mapping
 for jj=1:16
 text(real(symbols(jj))-0.15,imag(symbols(jj))+0.15,...
 dec2base(map(jj),2,4));
 end
 set(gca,'yTick',(-4:2:4),'xTick',(-4:2:4),...
 'XLim',[-4 4],'YLim',...
 [-4 4],'Box','on','YGrid','on', 'XGrid','on');
Output:


de2bi
It converts decimal number to equivalent binary  vector.
Command: de2bi(3)
Output:  1 0 1
gray2bin
Convert Gray-encoded positive integers to corresponding Gray-decoded integers.
Syntax: 
Y= gray2bin(x,Modulation, M);
or
[Y,map]= gray2bin(x,Modulation,M);
Code:
% To Gray decode a vector x with a 16-QAM Gray encoded
 % constellation and return its map, use:
 x=randi([0 15],1,100);
 [y,map] = gray2bin(x,'qam',16);
 % Obtain the symbols for 16-QAM
 hMod = modem.qammod('M', 16);
 symbols = hMod.Constellation;
 % Plot the constellation
 scatterplot(symbols);
 set(get(gca,'Children'),'Marker','d','MarkerFaceColor','auto');
 hold on;
 % Label the constellation points according
 % to the Gray mapping
 for jj=1:16
  text(real(symbols(jj))-0.15,imag(symbols(jj))+0.15,...
  dec2base(map(jj),2,4));
 end
 set(gca,'yTick',(-4:2:4),'xTick',(-4:2:4),...
  'XLim',[-4 4],'YLim',...
   [-4 4],'Box','on','YGrid','on', 'XGrid','on');

Output:



vec2mat
It converts a vector into matrix.
Command: v=[1 2 3 4 5];
 vec2mat(v);
Output: 1    2    3
               4    5    0

Task 2:  Implement DSSS receiver in Matlab and show the complete results:
Matlab code: 
At transmitter side the data=[1 0 1 1 1 1] was encoded as coded=[1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1];  by  spread  spectrum technique.
Matlab function for receiver side.
coded=[1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1];
ct=hadamard(8);
p=8;
code=ct(p,1:end)';
data=[];
l=length(coded);
for i=1:8:l
    matrix=coded(1,i:i+7);
    original=(matrix*code)/length(code);
    if(original ==-1)
        original=0;
    else
        original=1;
    end
    data=[data original];
    end
Data:    1 0 1 1 1 1
Coded data received from transmitter: 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1
Decoded data at Receiver:  1 0 1 1 1 1
Task 3:  Implement Frequency Hopping Spread Spectrum (FHSS) Transmitter in Matlab and show the complete results.
Frequency-hopping spread spectrum (FHSS) is a method of transmitting radio signals by rapidly switching a carrier among many frequency channels, using a pseudorandom sequence known to both transmitter and receiver. It is used as a multiple access methodin the frequency-hopping code division multiple access (FH-CDMA) scheme
Matlab code for transmitter: 
      clc
clear all
close all 
data=[1 0 1 1 1 1]; 
size=length(data); 
M=2; 
NRZ_signal=[]; %unipolar NRZ signal 
k=[];
for (t=1:size) 
    if (data(t)==0)
        NRZ_signal(t)=-1;
    else
        NRZ_signal(t)=1;
    end
    k=[k NRZ_signal(t)];
end
rand_freq=[2 3 4 5 6 7 8 9];
chip=8;
r=randperm(chip);
t=[];
bpsk_datasig=[];
for b=1:length(k)
   f=rand_freq(r(b));
    fs=50;
    t1=b-1:1/fs:b;
    t2=0:1/fs:1;
    x=cos(2*pi*f*t2);
    y=sin(2*pi*f*t2);
    t=[t t1];
     if (k(b)==1) 
            a=x; 
        else
            a=y;
        end
 bpsk_datasig=[bpsk_datasig a];
end
plot([1:length(bpsk_datasig)], abs(fft(bpsk_datasig)));