Course: EEE 2102 (Fundamental Electrical Engineering Lab)
Experiment no.: 02
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 = -[Link]; 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 ') ;
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=[Link]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. 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]
5. 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
6. 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