[Date] Digital Signal
Processing
Lab 2
HP
[COMPANY NAME]
Contents
1.0 Introduction................................................................................................................................2
2.0 First section: Basic Signals........................................................................................................2
3.0 Second section: Impulse Response & Step Response...............................................................6
4.0 Third section: Frequency Response & Input-Output Relationship..........................................10
5.0 Conclusion...............................................................................................................................14
1.0 Introduction
The MATLAB is use to visualize different types of signals in including discrete and continuous.
In this lab the implementation of discrete time systems and signals is done also the impulse
response and frequency response of the signals is visualize in this lab with the help of MATLAB.
In this lab different commands like stem and filters are used for analyzing the signals and
systems.
2.0 First section: Basic Signals
Part 1
In this section the signal is visualized as shown below.
x [ n ] =¿
This signals is the combination of the difference in unit step function and multiplied by the
sinusoid signal having some magnitude. The difference in unit step signal will form a rectangle
signal starting from 0 and ending at 63. The signal is the discrete signal and is made by first
initializing the vector starting from 0 to 63 so the stem command in the end is used to visualize
the signal as shown below.
MATLAB Code:
%Lab 2
% Section 1
n= 0:63 % limits of n ste by the unit step function
u[n]-u[n-64]
x(n+1)=(0.95.^n).*cos(pi/20*n) % function of signal
x[n]
y= x(n+1);
figure(1)
stem (y),xlim([1 65]),xlabel('n'),ylabel('x[n]'),
title('Section 1: Basic signal part 1')
Figure 1.0: Discrete Sinusoidal Graphs
The above graph shows that sinusoid signal starting from 0 and end at 63. This signals is the
combination of the difference in unit step function and multiplied by the sinusoid signal having
some magnitude.
Part 2
Now the in part 2 of the section 1 the ramp signal is generated and analyzed with the help of the
function after that the cosine signal is generated by five inputs as function and the behavior of
the cosine function is analyzed by determining the boundary of the cosine and this is done be
zeros command and determining the bounds appropriately to generate the unit-step.
MATLAB Code:
% ramp function
function x=framp(N)
% function x=framp(N)
% Generates an N-point ramp sequence
n=1:N;
x=n';
% End of Function%
%Lab 2
% Section 1
N=100;
x=framp(N)
figure(1)
stem (x'),xlabel('n'),ylabel('x[n]'), title('framp
function')
Figure 2.0: Ramp signal
In the above figure the ramp signal is generated by using the function called ramp and the vector
is generated having values that are increased linearly and the sequence of ramp is generated from
0 to 100 as shown in figure above.
The cosine signal is generated by five inputs as function and the behavior of the cosine function
is analyzed by determining the boundary of the cosine and this is done be zeros command and
determining the bounds appropriately to generate the unit-step. The five inputs include
magnitude, frequency, pi and the range of the cosine signal.
MATLAB Code:
% Cosine Fubction
function y = fcosine(A,w,phi,ni,nf)
y= A*cos(w*(ni:nf)+phi);
figure
figure(1)
stem (y),xlabel('n'),ylabel('y[n]=Acos(wn+phi)'),
title('fcosine function')
end
%Lab 2
% Section 1
%b
% Testing Function
A=4; w=pi/10; phi=pi/4; ni= -20; nf=20;
y = fcosine(A,w,phi,ni,nf);
Figure 3.0: Cosine signal
The above figure illustrates the sinusoidal signals made by fcoisne function by giving the five
inputs as shown above.
3.0 Second section: Impulse Response & Step Response
In this section the Impulse response and step response of the signal below is analyzed ad
observed by applying different inputs to signals. The input is represented by x[n] while output I
represented by y[n].
y [ n ] −1.85 cos ( 18π ) y [ n−1 ]+0.83 y [ n−2]=x [ n]+ 13 x [n−1]
The impulse response can be find by many ways like using the DTDT or first converting into
frequency response and then using IDFT but in MATLAB there is the built-in function called as
filter command that takes three terms (b, a, x) where b represents the numerator y[n] while a
represents the denominator terms and x is the input applied to visualize the signal. Initially the
unit impulse is given as input to check the behavior of the system for y ranging from -10 to 100
and then, x[n] was chosen to be the unit step sequence and the corresponding impulse response
plot was generated.
MATLAB Code:
% Defination of Delta Function%
function x = delta(n)
x=1.*(n==0)+0.*(n~=0);
end
%Lab 2
% Section 2
%These cofficents are used to find both the impulse
response and the unit
%step response
a=[1,1/3]; %cofficents of x[n]
b=[1,-1.85*cos(pi/18),0.83]; %cofficents of y[n]
n=-10:100; %limits of n
Filter command and Delta Input
MATLAB there is the built-in function called as filter command that takes three terms (b, a, x)
where b represents the numerator y[n] while a represents the denominator terms and x is the
input applied to visualize the signal. In the below code the unit impulse is given as input to check
the behavior of the system for y ranging from -10 to 100
MATLAB Code:
%Question 3: find the impulse response
x=delta(n);%input delta signal
y=filter (a,b,x); % filter function
figure(1)
stem(n,y),xlabel('n'),ylabel('y[n]'), title('Impulse
response y[n]=h[n]')
Figure 4.0: h[n] with unit impulse signal as the input
The above figure shows the impulse response of the difference equation by input as delta
function. The graph starts from 0 and end s at 100.
Filter command and Step Input
MATLAB there is the built-in function called as filter command that takes three terms (b, a, x)
where b represents the numerator y[n] while a represents the denominator terms and x is the
input applied to visualize the signal. In the below code the unit step is given as input to check the
behavior of the system for y ranging from -10 to 100
MATLAB Code:
%Defination of unit step Function
function x = unitstep(n)
x=1.*(n>=0)+0.*(n<0);
end
%Question 4: find the unit step response
q=unitstep(n);%input delta signal
y=filter (a,b,q); % filter function
figure(2)
stem(n,y),xlabel('n'),ylabel('y[n]'), title('Unit Step
response y[n]')
Figure 5.0: Impulse Response with Unit Step as the input
The above figure shows the impulse response of the difference equation by input as step
function. The graph starts from 0 and end s at 100.
4.0 Third section: Frequency Response & Input-Output Relationship
In this section the frequency response of the signal is observed and analyzed. The frequency
response would be calculated by applying the DTFT and manipulating the equation in such a
way that it would be in the form as shown below.
(e¿¿ jw )
Y ¿
X (e ¿¿ jw)¿
The freqz is the built-in command in MATLAB that takes three arguments (b, a, n) where b and
a are numerator and denominator while n is the number of frequency points and return the output
as vector and angular frequency. Initially using freqz command and for n=512 the magnitude and
phase response of H (e ¿¿ jw )¿ is obtained for range of 0 ≤ w ≤ π. After the H (e ¿¿ jw )¿ is
obtained than the input and output relation is found by finding impulse response h[n] and the
impulse response is plot. The phase and magnitude of frequency response are plot.
1
1+ e− jw
3
H ( e jw )=
π − jw
1−1.85 cos( )
18
e +0.83 e− j 2 w
%Lab 2
% Section 3
b=[1,1/3]; %numenators cofficents
a=[1,-1.85*cos(pi/18),0.83]; % denominator cofficents
n=[0:1:512]*(pi/512); %vector representing to obtain
512 samples
[h,w]=freqz(b,a,n);% freqz function returns the
frequency response
magnitude=abs(h); %magnitude of frequency response
phase=angle(h); %phase of frequency response
%plots of magnitude and phase of H(e^jw)
%the frequency in the plots has a pi-scale
figure
subplot(2,1,1);plot(n/pi,magnitude,'r');grid
xlabel('Fequency (w)');
ylabel('Magnitude')
title('Magnitude Response')
subplot(2,1,2);plot(n/pi,phase/pi);grid
xlabel('Fequency (w)');
ylabel('Phase')
title('Phase Response')
Figure 6.0: Magnitude and Phase plot
The above plot shows the magnitude and phase of transfer function given above. The abs and
angel command is used for magnitude and phase plot and the plot is of 512 frequency points.
the difference equation was obtained using theoretical formulas and the use of the equation
editor. The step are as follows.
1
1+ e− jw
jw 3
Using the input and output relation. H ( e )=
π − jw
1−1.85 cos
18
e +0.83 e ( )
− j2w
(e¿¿ jw )
1. We equate it to Y ¿
X (e ¿¿ jw)¿
(e ¿¿ jw )
Y ¿
1 − jω
1+ e
3
X (e ¿¿ jw)= ¿
π − jω
1−1.85 cos
18 ( )
e +0.83 e−2 jω
2. Cross-Multiply
1 − jω
Y ( e¿¿ jw )[1−1.85 cos ( 18π ) e − jω
+0.83 e−2 jω ]¿ = X (e¿¿ jw)[ 1+ e ]¿
3
3. Apply the inverse Discrete Fourier transform to both sides since the IDTFT is a linear
operator. We can also observe that we can use the table specifically the shifting
property
π
y[n] = F−1 ¿ y[n]-1.85 cos
18 ( )
y [ n−1 ] +0.83 y [n−2]
1
x[n]= F−1 ¿ x [n] + x [n−1]
3
4. Equating the resulting y[n]=x[n] and the difference equation is obtained
y [ n ]-1.85 cos ( 18π ) y [ n−1 ]+0.83 y [n−2] = x [n] + 13 x [n−1]
Impulse Response h[n]
The impulse response can be find by many ways like using the DTDT or first converting into
frequency response and then using IDFT but in MATLAB there is the built-in function called as
filter command that takes three terms (b, a, x) where b represents the numerator y[n] while a
represents the denominator terms and x is the input applied to visualize the signal. The unit
impulse is given as input to check the behavior of the system for y ranging from -10 to 100
MATLAB Code:
% Defination of Delta Function%
function x = delta(n)
x=1.*(n==0)+0.*(n~=0);
end
%These cofficents are used to find the impulse response
a=[1,1/3]; %cofficents of x[n]
b=[1,-1.85*cos(pi/18),0.83]; %cofficents of y[n]
n=-10:100; %limits of n
% find the impulse response
x=delta(n);%input delta signal
y=filter (a,b,x); % filter function
figure(1)
stem(n,y),xlabel('n'),ylabel('y[n]'), title('Impulse
response y[n]=h[n]')
Figure 7.0: h[n] with unit impulse signal as the input
The above figure shows the impulse response of the difference equation by input as delta
function. The graph starts from 0 and end s at 100.
5.0 Conclusion
In this lab MATLAB helps in visualizing different types of signals including discrete and
continuous. In this lab the implementation of discrete time systems and signals is done also the
impulse response and frequency response of the signals. The unit step and impulse response I
analyzed for different equations and built-in commands like filter and freqz are used and
analyzed for difference equation and transfer function systems.
Appendix
%Lab 2
% Section 1
n= 0:63 % limits of n ste by the unit step function
u[n]-u[n-64]
x(n+1)=(0.95.^n).*cos(pi/20*n) % function of signal
x[n]
y= x(n+1);
figure(1)
stem (y),xlim([1 65]),xlabel('n'),ylabel('x[n]'),
title('Section 1: Basic signal part 1')
% ramp function
function x=framp(N)
% function x=framp(N)
% Generates an N-point ramp sequence
n=1:N;
x=n';
% End of Function%
%Lab 2
% Section 1
N=100;
x=framp(N)
figure(1)
stem (x'),xlabel('n'),ylabel('x[n]'), title('framp
function')
% Cosine Fubction
function y = fcosine(A,w,phi,ni,nf)
y= A*cos(w*(ni:nf)+phi);
figure
figure(1)
stem (y),xlabel('n'),ylabel('y[n]=Acos(wn+phi)'),
title('fcosine function')
end
%Lab 2
% Section 1
%b
% Testing Function
A=4; w=pi/10; phi=pi/4; ni= -20; nf=20;
y = fcosine(A,w,phi,ni,nf);
% Defination of Delta Function%
function x = delta(n)
x=1.*(n==0)+0.*(n~=0);
end
%Lab 2
% Section 2
%These cofficents are used to find both the impulse
response and the unit
%step response
a=[1,1/3]; %cofficents of x[n]
b=[1,-1.85*cos(pi/18),0.83]; %cofficents of y[n]
n=-10:100; %limits of n
%Question 3: find the impulse response
x=delta(n);%input delta signal
y=filter (a,b,x); % filter function
figure(1)
stem(n,y),xlabel('n'),ylabel('y[n]'), title('Impulse
response y[n]=h[n]')
%Defination of unit step Function
function x = unitstep(n)
x=1.*(n>=0)+0.*(n<0);
end
%Question 4: find the unit step response
q=unitstep(n);%input delta signal
y=filter (a,b,q); % filter function
figure(2)
stem(n,y),xlabel('n'),ylabel('y[n]'), title('Unit Step
response y[n]')
%Lab 2
% Section 3
b=[1,1/3]; %numenators cofficents
a=[1,-1.85*cos(pi/18),0.83]; % denominator cofficents
n=[0:1:512]*(pi/512); %vector representing to obtain
512 samples
[h,w]=freqz(b,a,n);% freqz function returns the
frequency response
magnitude=abs(h); %magnitude of frequency response
phase=angle(h); %phase of frequency response
%plots of magnitude and phase of H(e^jw)
%the frequency in the plots has a pi-scale
figure
subplot(2,1,1);plot(n/pi,magnitude,'r');grid
xlabel('Fequency (w)');
ylabel('Magnitude')
title('Magnitude Response')
subplot(2,1,2);plot(n/pi,phase/pi);grid
xlabel('Fequency (w)');
ylabel('Phase')
title('Phase Response')
% Defination of Delta Function%
function x = delta(n)
x=1.*(n==0)+0.*(n~=0);
end
%These cofficents are used to find the impulse response
a=[1,1/3]; %cofficents of x[n]
b=[1,-1.85*cos(pi/18),0.83]; %cofficents of y[n]
n=-10:100; %limits of n
% find the impulse response
x=delta(n);%input delta signal
y=filter (a,b,x); % filter function
figure(1)
stem(n,y),xlabel('n'),ylabel('y[n]'), title('Impulse
response y[n]=h[n]')