0% found this document useful (0 votes)
10 views

EEE_4702_Lab_1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

EEE_4702_Lab_1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Islamic University of Technology (IUT)

Organization of Islamic Cooperation (OIC)


Department of Electrical and Electronic Engineering

Date & Year: ……………… Section: ……... Group: ……….………… Std. ID: ………………

Course: EEE 4702 (Digital Signal Processing Lab)


Experiment no.: 01
Name of the experiment: The Study of basic concepts of digital signals by simulation in
MATLAB

A. Generation basic discrete signals

• Unit impulse sequence


• Unit step sequence
• Ramp sequence
• Exponential sequence
• Sinusoidal sequence
• Cosine sequence.

Procedure:
1. Get the number of samples
2. Generate the unit impulse, unite step by using ‘ONES’ and ‘ZEROS’ matrix commands
3. Generate ramp, sine, cosine, exponential signals using the corresponding general formula
4. Plot the graph

Codes:

1. Unit Impulse Signal 2. Unit Step Sequence [u(n)-u(n-N)]

clc; n=input ('enter the N value ');


clear all; t=0: 1: n-1;
close all; y=ones (1,n) ;
t = -3:1:3; stem(t,y);
y = [zeros(1,3), ones(1,1), zeros(1,3)]; ylabel ('amplitude ---->');
stem(t,y); xlabel('(b)n ---->');
ylabel('Amplitude------>'); title (' unit step sequence ');
xlabel('(a)n ------>');
title ('Unit Impulse Signal');

3. Ramp sequence 4. Exponential sequence

n=input('enter the length of ramp sequence ') ; n=input('enter the length of exponential
t=0:n-1 ; sequence') ;
stem(t,t) ; t=0:n ;
ylabel('amplitude ---->') ; a=input('enter the value of a ') ;
xlabel('(c)n ---->') ; y=exp(a*t) ;
title(' ramp sequence ') ; stem(t,y) ;
ylabel('amplitude ---->') ;
xlabel('(d)n ---->') ;
title(' exponential sequence ') ;
5. Sine sequence 6. Cosine sequence
t=0:0.05:pi ; t=0:0.05:pi ;
y=sin(2*pi*t) ; y=cos(2*pi*t) ;
stem(t,y) ; stem(t,y) ;
ylabel('amplitude ---->') ; ylabel('amplitude ---->') ;
xlabel('(e)n ---->') ; xlabel('(f)n ---->') ;
title(' sine sequence ') ; title(' cosine sequence ') ;

Class task 1: Delay any two of the plots by 3 samples and advance any of the other two by 2
samples.

B. Basic operations on discrete signals:


1. Scaling: This operation involves the multiplication of each sample in a sequence by a scalar factor.

The example below is the scaled version of a generated step sequence

Codes:

clc;
clear all;
close all;
x1=input('enter the scaling value value ')
n=input('enter the N value ') ;
t=0:1:n-1 ;
x=ones(1,n) ;
y=x1*x;
subplot(2,2,1);
stem(t,x) ;
ylabel('amplitude ---->') ;
xlabel('(b)n ---->') ;
title(' unit step sequence ') ;
subplot(2,2,2);
stem(y);
title('scaled sequence');
2. Addition: This is a sample-by-sample addition given by and the length of x1(n) and x2(n) must be the
same.

Code: OUTPUT:
clear all; Enter the first sequence: [2 3 1 4 5]
close all; Enter the second sequence: [1 -1 0 1 -1]
x1=0; Result: 3 2 1 5 4
y1=0;
x=input('ENTER THE FIRST SEQUENCE: ');
subplot(3,1,1);
stem(x);
title('X');
y=input('ENTER THE SECOND SEQUENCE:
');
subplot(3,1,2);
stem(y);
title('Y');
l1= length(x);
l2=length(y);
l3=l1-l2;
if l3>0
y= [y,zeros(1,l3)];
elseif l3<0
x=[x,zeros(1,abs(l3));
else
disp('doable')
end
z=x+y;
disp(z)
subplot(3,1,3);
stem(z);
title('Z=X+Y');

3. Subtraction:

Code: OUTPUT:
clc; ENTER THE FIRST SEQUENCE: [2 4 6 8]
clear all; ENTER THE SECOND SEQUENCE:[1 3 5 7]
close all; subtracted sequence 2 4 5 5 -5 -7
n11=input('enter the lower boundary of the
first sequence:');
n12=input('enter the upper boundary of the
first sequence:');
n1=-n11:n12;
x=input('ENTER THE FIRST SEQUENCE:');
n21=input('enter the lower boundary of the
second sequence:');
n22=input('enter the upper boundary of the
second sequence:');
n2=n21:n22;
y=input('ENTER THE SECOND
SEQUENCE:');
subplot(3,1,1);
stem(n1,x);
xlabel ('time')
ylabel ('amplitude')
title('FIRST SEQUENCE') ;
axis([-4 4 -5 5]);
subplot(3,1,2);
stem(n2,y);
xlabel ('time')
ylabel ('amplitude')
title('SECOND SEQUENCE');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1
) , max ( n2 ) );
% finding the duration of output signal
s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1
) )==1 ) )=x;
% signal x with the duration of output signal
'sub'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max (
n2 ))==1) )=y;
% signal y with the duration of output signal
'sub'
sub=s1 - s2;
% subtraction
disp('subtracted sequence')
disp(sub)
subplot(3,1,3)
stem(n3,sub)
xlabel ('time')
ylabel ('amplitude')

4. Multiplication: This is a sample-by-sample multiplication (or “dot” multiplication) given by

Code:
clc;
clear all;
close all;
n11=input('ENTER THE LOWER BOUNDARY OF THE FIRST SEQUENCE:');
n12=input('ENTER THE UPPER BOUNDARY OF THE FIRST SEQUENCE:');
n1=-n11:n12;
x=input('ENTER THE FIRST SEQUENCE:');
n21=input('ENTER THE LOWER BOUNDARY OF THE SECOND SEQUENCE:');
n22=input('ENTER THE UPPER BOUNDARY OF THE SECOND SEQUENCE:');
n2=n21:n22;
y=input('ENTER THE SECOND SEQUENCE:');
subplot(3,1,1);
stem(n1,x);
xlabel ('time')
ylabel ('amplitude')
title('FIRST SEQUENCE') ;
axis([-4 4 -5 5]);
subplot(3,1,2);
stem(n2,y);
xlabel ('time')
ylabel ('amplitude')
title('SECOND SEQUENCE');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) );
% finding the duration of output signal (out)

s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x;
% signal x with the duration of output signal 'mul'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y;
% signal y with the duration of output signal 'mul'
mul=s1 .* s2;
% multiplication
disp('MULTIPLIED SEQUENCE')
disp(mul)
subplot(3,1,3)
stem(n3,mul)
xlabel ('time')
ylabel ('amplitude')

OUTPUT:

ENTER THE FIRST SEQUENCE:[2 4 6 8]


ENTER THE SECOND SEQUENCE:[2 3 4 5]
MULTIPLIED SEQUENCE 0 0 12 24 0 0

5. Shifting

Code:

clc;
clear all;
close all;
n1=input('Enter the amount to be delayed');
n2=input('Enter the amount to be advanced');
n11=input('ENTER THE LOWER BOUNDARY OF THE FIRST SEQUENCE:');
n12=input('ENTER THE UPPER BOUNDARY OF THE FIRST SEQUENCE:');
n=n11:n12;
x=input('ENTER THE SEQUENCE');
subplot(3,1,1);
stem(n,x);
title('Signal x(n)');
m=n+n1;
y=x;
subplot(3,1,2);
stem(m,y);
title('Delayed signal x(n-n1)');
t=n-n2;
z=x;
subplot(3,1,3);
stem(t,z);
title('Advanced signal x(n+n2)');

OUTPUT:

Enter the amount to be delayed 3


Enter the amount to be advanced 4
ENTER THE SEQUENCE[1 2 3 4 5]
6. FOLDING or REVERSING:

Code:

clc;
clear all;
close all;
n11=input('ENTER THE LOWER BOUNDARY OF THE FIRST SEQUENCE:');
n12=input('ENTER THE UPPER BOUNDARY OF THE FIRST SEQUENCE:');

n=n11:n12;
x=input('ENTER THE SEQUENCE');
subplot(2,1,1)
stem(n,x);
axis([-3 3 -5 5]);
title('Signal x(n)');
c=fliplr(x);
y=fliplr(-n);
disp('FOLDED SEQUENCE')
disp(c)
subplot(2,1,2);
stem(y,c);
axis([-3 3 -5 5]);
title('Reversed Signal x(-n)') ;

OUTPUT:

ENTER THE SEQUENCE[1 -1 2 -3]


FOLDED SEQUENCE -3 2 -1 1
7. Correlation

a. Autocorrelation

Auto correlation function is a measure of similarity between a signal & its time delayed version. It is
represented with R(k). The auto correlation function of x(n) is given by

R11(k)=R(k)=

Algorithm:

Step I : Give input sequence x[n].

Step II : Give impulse response sequence h (n)

Step III: Find auto correlation using the matlab command xcorr.

Step IV: Plot x[n], h[n], z[n].

Step V: Display the result


Code:

clc;
close all;
clear all;
% two input sequences
x=input('enter input sequence')
subplot(1,2,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input sequence');
% auto correlation of input sequence
z=xcorr(x,x);
disp('The values of z are = ');
disp(z);
subplot(1,2,2);
stem(z);
xlabel('n');
ylabel('z(n)');
title('auto correlation of input sequence');

Output:

enter input sequence [1 2 3 4]

z = [3.99, 11, 20, 30, 20, 11, 3.99]

b. Cross Correlation

This correlation function is a measure of similarity between a signal & a different one.

% Program for computing cross-correlation of the sequences x5[1, 2, 3, 4] and h5[4, 3, 2, 1]

clc;
clear all;
close all;
x=input('enter the 1st sequence');
h=input('enter the 2nd sequence');
y=crosscorr(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel('Amplitude --.');
xlabel(' (a) n --.');
title('input sequence');
subplot(3,1,2);
stem(h);
ylabel('Amplitude --.');
xlabel(' (b) n --.');
title('impulse sequence');
subplot(3,1,3);
stem(fliplr(y));
ylabel('Amplitude --.');
xlabel(' (c) n --.');
title('Cross correlated sequence');
disp('The resultant signal is');
fliplr(y)

OUTPUT:

enter the 1st sequence [1 2 3 4]

enter the 2nd sequence [4 3 2 1]

The resultant signal is Y=1.0000 4.0000 10.0000 20.↑0000 25.0000 24.0000 16.0000

8. Convolution

a) linear convolution using conv function

Convolution is a mathematical operation used to express the relation between input and output of an LTI
system. It relates input, output and impulse response of an LTI system as
y(n)=x(n)∗h(n)

Where y (n) = output of LTI

x (n) = input of LTI

h (n) = impulse response of LTI

Discrete Convolution is given by


y(n)=x(n)∗h(n)

Algorithm:

Step I: Give input sequence x[n].

Step II: Give impulse response sequence h(n)

Step III: Find the convolution y[n] using the matlab command conv.

Step IV: Plot x[n],h[n],y[n]

Code

clc;
clear all;
close all;
x1=input('Enter the first sequence x1(n) = ');
x2=input('Enter the second sequence x2(n) = ');
L=length(x1);
M=length(x2);
N=L+M-1;
yn=conv(x1,x2);
disp('The values of yn are= ');
disp(yn);
n1=0:L-1;
subplot(311);
stem(n1,x1);
grid on;
xlabel('n1--->');
ylabel('amplitude--->');
title('First sequence');
n2=0:M-1; subplot(312);
stem(n2,x2);
grid on;
xlabel('n2--->');
ylabel('amplitude--->');
title('Second sequence');
n3=0:N-1;
subplot(313);
stem(n3,yn);
grid on;
xlabel('n3--->');
ylabel('amplitude--->');
title('Convolved output');

Output:

Enter the first sequence x1(n) = [1 2 3 4 5]

Enter the second sequence x2(n) = [5 8 3 5 4 6]

The values of yn are= 5 18 34 55 80 81 59 59 44 30

b) circular convolution

Code:

clc;
clear all;
a = input('enter the sequence x(n) = ');
b = input('enter the sequence h(n) = ');
n1=length(a);
n2=length(b);
N=max(n1,n2);
x = [a zeros(1,(N-n1))];
for i = 1:N k = i; for j = 1:n2 H(i,j)=x(k)* b(j);
k = k-1; if (k == 0) k = N;
end
end
end
y=zeros(1,N);
M=H;
for j = 1:N
for i = 1:n2
y(j)=M(i,j)+y(j);
end
end
disp('The output sequence is y(n)= ');
disp(y);
stem(y);
title('Circular Convolution');
xlabel('n');
ylabel('y(n) ');
OUTPUT:

Enter the sequence x(n) = [1 2 4]


Enter the sequence h(n) = [1 2]
The output sequence is y(n)= 9 4 8

Assignments 1:
1. Give a detailed and comprehensive difference between convolution and correlation nothing
carefully their domains of application
2. Where are linear and circular convolution applied and why?
3. Carefully distinguish between a continuous, discrete, and digital signal.

Assignments 2:
Write short notes on the following topics:

1. Continuous time or discrete time signals


2. Periodic or aperiodic signals
3. Power or energy signals
4. Odd or even signals
5. Deterministic and non-deterministic signals
6. Real and imaginary signals

You might also like