Pages

Thursday 28 February 2013

CDMA (Matlab 2012a)

Program:

clc;
clear all;
code_length = input('Enter Code Length = ');
data_stream = input('Enter Data Bit Stream = ');
%Generating Walsh Code
code = [-1 -1; -1 +1];   
[r1 c1] = size(code);
disp('Walsh code generated = ');
while r1<code_length
    code = [code,code;code,-1*code];
    [r1 c1] = size(code);
    disp(code);
end
code_length = length(code); %length (number of bits) of code
Y = size(data_stream);             
N = Y(1); %number of unique senders / bit streams
I = Y(2); %number of bits per stream
T = []; %sum of all transmitted and encoded data on channel
data_received = []; %vector of reconstructed bits at receiver
%show data bits and codes
disp('Data bit stream to be transmitted = '),disp(data_stream);
disp('Walsh codes used for transmission = '),disp(code);
%encode bits and transmit
G = zeros(I,code_length);
for n = 1:N
    Z = zeros(I,code_length);
    for i = 1:I
        for m = 1:code_length
            Z(i,m) = [data_stream(n,i)*code(n,m)];       
        end
    end
    G = G + Z;
end
%show channel traffic
for i = 1:I
    T = [ T G(i,:) ];
end
disp('Resulting traffic on the channel = '),disp(T);
%decode and reconstruct
for n = 1:N
    tot = zeros(1,I);
    R   = zeros(I,code_length);
    for i = 1:I
        for m = 1:code_length
            R(i,m) = G(i,m) * code (n,m);
            tot(i) = tot(i) + R (i,m);
         end
    end
    data_received = [data_received ; tot / code_length];
end
disp('Reconstructed data at the receiver = '),disp(data_received);


Output:

Enter Code Length = 4
Enter Data Bit Stream = [ 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 ];


Walsh code generated =
    -1    -1    -1    -1
    -1     1    -1     1
    -1    -1     1     1
    -1     1     1    -1

Data bit stream to be transmitted =
     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

Walsh codes used for transmission =
    -1    -1    -1    -1
    -1     1    -1     1
    -1    -1     1     1
    -1     1     1    -1

Resulting traffic on the channel =
  Columns 1 through 19

    -2    -2     2    -2     0     0     4     0    -2     2    -2    -2     0     4     0     0     0     0    -4

  Columns 20 through 32

     0     0    -4     0     0     2    -2     2     2     2     2    -2     2

Reconstructed data at the receiver =
     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

Tuesday 26 February 2013

DFT & IDFT Without Using Function (Matlab 2012a)

Program:

clc;
clear all;
N = input('Number of DFT points = ');
xn = input('Enter the sequence xn = ');  %Get the sequence from user
ln = length(xn);       %find the length of the sequence
xn = [xn zeros(1,N-ln)];
xk = zeros(1,N);  %initialize an array of same size as that of input sequence
ixk = zeros(1,N); %initialize an array of same size as that of input sequence
%code block to find the DFT of the sequence
for k = 0:N-1
    for n = 0:N-1
        xk(k+1) = xk(k+1)+(xn(n+1)*exp((-1i)*2*pi*k*n/N));
    end
end

%code block to plot the input sequence
t = 0:N-1;
subplot(2,2,1);
stem(t,xn);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('Input Sequence ---->');
grid on;
magnitude = abs(xk); %Find the magnitudes of individual DFT points
disp('DFT Sequence = ');
disp(magnitude);

%code block to plot the DFT sequence
t = 0:N-1;
subplot(2,2,2);
stem(t,magnitude);
ylabel('Amplitude ---->');
xlabel('K ---->');
title('DFT Sequence ---->');
grid on;
phase = angle(xk); %Find the phases of individual DFT points
disp('Phase = ');
disp(phase);
 %code block to plot the Phase Response
t = 0:N-1;
subplot(2,2,3);
stem(t,phase);
ylabel('Phase ---->');
xlabel('K ---->');
title('Phase Response');
grid on;
% Code block to find the IDFT of the sequence
for n = 0:N-1
    for k = 0:N-1
        ixk(n+1) = ixk(n+1)+(xk(k+1)*exp(1i*2*pi*k*n/N));
    end
end
ixk = ixk./N;
%code block to plot the IDFT sequence
t = 0:N-1;
subplot(2,2,4);
stem(t,ixk);
disp('IDFT Sequence = ');
disp(ixk);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('IDFT sequence ---->');
grid on;



Output:

Number of DFT points = 4
Enter the sequence xn = [1 1 0 0]
DFT Sequence =
    2.0000    1.4142    0.0000    1.4142

Phase =
         0   -0.7854   -1.5708    0.7854

IDFT Sequence =
   1.0000 - 0.0000i   1.0000            -0.0000 + 0.0000i   0.0000 + 0.0000i



Waveform:

Block Convolution Using Overlap Add Method (Matlab 2012a)

Program:

clc;
clear all;
x = input('Enter the sequence x(n) = ');
h = input('Enter the sequence h(n) = ');
n1 = length(x);
n2 = length(h);
N = n1+n2-1;
y = zeros(1,N);
h1 = [h zeros(1,n2-1)];
n3 = length(h1);
y = zeros(1,N+n3-n2);
H = fft(h1);
for i = 1:n2:n1
if i<=(n1+n2-1)
x1 = [x(i:i+n3-n2) zeros(1,n3-n2)];
else
x1 = [x(i:n1) zeros(1,n3-n2)];
end
x2 = fft(x1);
x3 = x2.*H;
x4 = round(ifft(x3));
if (i==1)
    y(1:n3) = x4(1:n3);
else
y(i:i+n3-1) = y(i:i+n3-1)+x4(1:n3);
end
end
subplot(3,1,1);
stem(x(1:n1));
grid on;
title('Input Sequence x(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,2);
stem(h(1:n2));
grid on;
title('Input Sequence h(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,3);
disp('Block Convolution Using Overlap Add Method = ');
disp(y(1:N));
stem(y(1:N));
grid on;
title('Block Convolution Using Overlap Add Method');
xlabel('Time --->');
ylabel('Amplitude --->');


Output:

Enter the sequence x(n) = [1 2 -1 2 3 -2 -3 -1 1 1 2 -1]
Enter the sequence h(n) = [1 2 3 -1]
Block Convolution Using Overlap Add Method =
     1     4     6     5     2    11     0   -16    -8     3     8     5     3    -5     1


Waveform:

 

Block Convolution Using Overlap Save Method (Matlab 2012a)

Program:

clc;
clear all;
x = input('Enter the sequence x(n) = ');
h = input('Enter the sequence h(n) = ');
n1 = length(x);
n2 = length(h);
N = n1+n2-1;
h1 = [h zeros(1,N-n1)];
n3 = length(h1);
y = zeros(1,N);
x1 = [zeros(1,n3-n2) x zeros(1,n3)];
H = fft(h1);
for i = 1:n2:N
y1 = x1(i:i+(2*(n3-n2)));
y2 = fft(y1);
y3 = y2.*H;
y4 = round(ifft(y3));
y(i:(i+n3-n2)) = y4(n2:n3);
end
subplot(3,1,1);
stem(x(1:n1));
grid on;
title('Input Sequence x(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,2);
stem(h(1:n2));
grid on;
title('Input Sequence h(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,3);
disp('Block Convolution Using Overlap Save Method = ');
disp(y(1:N));
stem(y(1:N));
grid on;
title('Block Convolution Using Overlap Save Method');
xlabel('Time --->');
ylabel('Amplitude --->');


Output:

Enter the sequence x(n) = [1 2 -1 2 3 -2 -3 -1 1 1 2 -1]
Enter the sequence h(n) = [1 2 3 -1]
Block Convolution Using Overlap Save Method =
     1     4     6     5     2    11     0   -16    -8     3     8     5     3    -5     1
 

Waveform:

Friday 22 February 2013

Auto Correlation Without Using Function (Matlab2012a)

Program:

clc;
clear all;
x = input('Enter the sequence = ');
N = length(x);
a = x;
y = x;
z = x;
for i = 1:N-1
    for i = N-1:-1:1
        x(i+1) = x(i);
    end
    x(1) = 0;
    z = [x;z;x];
end
k = z*y';
m = k';
subplot(2,1,1);
stem(a);
title('Input Sequence');
ylabel('Amplitude---->');
xlabel('n---->');
grid on;
subplot(2,1,2);
stem(m);
title('Autocorrelation Sequence');
ylabel('Amplitude---->');
xlabel('n---->');
grid on;
disp('Auto Correlation Sequence = ');
disp(m);


Output:

Enter the sequence = [1 2 3]
Auto Correlation Sequence =
     3     8    14     8     3


Waveform:

Cross Correlation Using Function (Matlab 2012a)

Program:

clc;
clear all;
x1 = input('Enter the first sequence = ');
x2 = input('Enter the second sequence = ');
y = xcorr(x1,x2);
subplot(3,1,1);
stem(x1);
title('First Sequence');
ylabel('Amplitude---->');
xlabel('n---->');
grid on;
subplot(3,1,2);
stem(x2);
title('Second Sequence');
ylabel('Amplitude---->');
xlabel('n---->');
grid on;
subplot(3,1,3);
stem(x2);
title('Cross Correlation Sequence');
ylabel('Amplitude---->');
xlabel('n---->');
grid on;
disp('Cross Correlation Sequence = ');
disp(y);


Output:

Enter the first sequence = [1 2 3 4]
Enter the second sequence = [4 3 2 1]
Cross Correlation Sequence =
    1.0000    4.0000   10.0000   20.0000   25.0000   24.0000   16.0000


Waveform:

 

Autocorrelation Sequence Using Function (Matlab 2012a)

Program:

clc;
clear all;
x = input('Enter the sequence = ');
y = xcorr(x,x);
subplot(2,1,1);
stem(x);
title('Input Sequence');
ylabel('Amplitude---->');
xlabel('n---->');
grid on;
subplot(2,1,2);
stem(y);
title('Autocorrelation Sequence');
ylabel('Amplitude---->');
xlabel('n---->');
grid on;
disp('Auto Correlation Sequence = ');
disp(y);


Output:

Enter the sequence = [1 2 3 4]
Auto Correlation Sequence =
    4.0000   11.0000   20.0000   30.0000   20.0000   11.0000    4.0000


Waveform:

 

Thursday 21 February 2013

Circular Convolution (Matlab 2012a)

Program:

clc;
clear all;
x1 = input('Enter the first sequence = ');
x2 = input('Enter the second sequence = ');
l1 = length(x1);
l2 = length(x2);
n1 = max(l1,l2);
l = l1-l2;
if(l>=0)
x2 = [x2,zeros(1,l)];
else
    x1 = [x1,zeros(1,-l)];
end%circular shifting and convolution
for n = 1:n1
    y(n) = 0;
    for i = 1:n1
        j = n-i+1;
        if(j<=0)
            j = n1+j;
        end
        y(n) = [y(n)+x1(i)*x2(j)];
    end
end%plot the inputs and output
n = 0:n1-1;
subplot(3,1,1);
stem(n,x1);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('First Sequence');
subplot(3,1,2);
stem(n,x2);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Second Sequence');
subplot(3,1,3);
disp('Convolved Sequence = ');
disp(y);
stem(n,y);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Convolved Output');



Output:

Enter the first sequence  = [1 2 3 4]
Enter the second sequence  = [1 2 3]

Convolved Sequence =
    18    16    10    16 


Waveforms:

Linear Convolution Without Using Function (Matlab 2012a)

Program:

clc;
clear all;
x1 = input('Enter the first sequence x1(n) = ');
t1 = input('Enter the starting time of first sequence t1 = ');
x2 = input('Enter the second sequence x2(n) = ');
t2 = input('Enter the starting time of second sequence t2 = ');
l1 = length(x1);
l2 = length(x2);
ln = l1+l2-1;
X1 = [x1,zeros(1,l2)];
X2 = [x2,zeros(1,l1)];
for i = 1:l2+l1-1
y(i) = 0;
for j = 1:l1
if(i-j+1>0)
y(i) = y(i)+X1(j)*X2(i-j+1);
else
end
end
end
a = t1+l1-1;
t = t1:a;
subplot(3,1,1);
stem(t,x1);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('First Sequence');
a = t2+l2-1;
t = t2:a;
subplot(3,1,2);
stem(t,x2);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Second Sequence');
tn = t1+t2;
a = tn+ln-1;
t = tn:a;
subplot(3,1,3);
disp('Convolved Sequence = ');
disp(y);
stem(t,y);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Convolved Output');


Output: 

Enter the first sequence x1(n) = [1 2 1 0]
Enter the starting time of first sequence t1 = 0
Enter the second sequence x2(n) = [1 0 1]
Enter the starting time of second sequence t2 = 0
Convolved Sequence =
     1     2     2     2     1     0


Waveforms:

Linear Convolution Using Function (Matlab 2012a)

Program


clc;
clear all;
x1 = input('Enter the first sequence x1(n) = ');
t1 = input('Enter the starting time of first sequence t1 = ');
x2 = input('Enter the second sequence x2(n) = ');
t2 = input('Enter the starting time of second sequence t2 = ');
l1 = length(x1);
l2 = length(x2);
ln = l1+l2-1;
y = conv(x1,x2);
a = t1+l1-1;
t = t1:a;
subplot(3,1,1);
stem(t,x1);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('First Sequence');
a = t2+l2-1;
t = t2:a;
subplot(3,1,2);
stem(t,x2);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Second Sequence');
tn = t1+t2;
a = tn+ln-1;
t = tn:a;
subplot(3,1,3);
disp('Convolved Sequence = ');
disp(y);
stem(t,y);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Convolved Output');



Output:

Enter the first sequence x1(n) = [1 2 1 0]
Enter the starting time of first sequence t1 = 0
Enter the second sequence x2(n) = [1 0 1]
Enter the starting time of second sequence t2 = 0
Convolved Sequence =
     1     2     2     2     1     0


Waveforms: