matlab dsp
TRANSCRIPT
S.U.S. WOMEN ENGINEERING COLLEGETANGORI (MOHALI)
LAB FILE
Digital Signal Processing Using Mat Lab 7.1
Submitted to: Submitted by:Er.Aarti Hina Pradip Sangai 81003108013 6th Semester Branch-ECE
Hina Pradip Sangai 81003108013
List of Experiments
1. Introduction about Mat lab.
2. To develop sine and cosine function waveforms.
3. To develop elementary signals: Unit step signal, unit ramp signal, unit impulse signal, exponential signal.
4. Develop program modules based on operation on sequences like: Signal addition, Signal multiplication, Signal folding, Signal Shifting.
5. To develop program to perform linear convolution of two input Sequence x(n) and h(n). Plot x(n) and h(n) and result of linear convolution as y(n)on single plot and verify the result mathematically.
6. To obtain the impulse response of the system described by difference equation: y(n)-0.5y(n-1)=x(n)
7. Write a program to determine and plot poles and zeroes of rational Z-transform given as:- H(z)=(5+z-1)(3+z-1 ) - ------------------
(2+z-1)(4+z-1)
8. To develop program for computing DFT and IDFT.
9. To develop program for finding magnitude and phase response of LTI system described by difference equation : y(n)=0.8y(n-2)+x(n)-x(n-2)
10. To design an IIR filter with following specifications using filter design and analysis toolbox Response type: Low pass filter
Design method: IIR Butterworth filter Filter order: Minimum Match exactly: Stop band Frequency specifications: Units=KHz Fs=500 Fpass=100 Fstop=150 Magnitude specifications: Units=dB Apass=3 Astop=1
2
Hina Pradip Sangai 81003108013
EXPERIMENT-1INTRODUCTION ABOUT MATLAB
MATLAB® is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include:
Math and computation Algorithm development Data acquisition Modeling, simulation and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building
MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar noninteractive language such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries, embedding the state of the art in software for matrix computation.
Desktop Overview
Use desktop tools to manage your work in MATLAB. You can also use MATLAB functions to perform the equivalent of most of the features found in the desktop tools.
The following illustration shows the default configuration of the MATLAB desktop. You can modify the setup to meet your needs.
3
Hina Pradip Sangai 81003108013
The MATLAB System
The MATLAB system consists of five main parts:
Desktop Tools and Dvelopment Environment. This is the set of tools and facilities that help you use MATLAB functions and files. Many of these tools are graphical user interfaces. It includes the MATLAB desktop and Command Window, a command history, an editor and debugger, and browsers for viewing help, the workspace, files, and the search path.
The MATLAB Mathematical Function Library. This is a vast collection of computational algorithms ranging from elementary functions, like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms.
The MATLAB Language. This is a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. It allows both "programming in the small" to rapidly create quick and dirty throw-away programs, and "programming in the large" to create large and complex application programs.
Graphics. MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as annotating and printing these graphs. It includes high-level functions
4
Hina Pradip Sangai 81003108013
for two-dimensional and three-dimensional data visualization, image processing, animation, and presentation graphics. It also includes low-level functions that allow you to fully customize the appearance of graphics as well as to build complete graphical user interfaces on your MATLAB applications.
The MATLAB External Interfaces/API. This is a library that allows you to write C and Fortran programs that interact with MATLAB. It includes facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and writing MAT-files.
Programming:
Flow Control:MATLAB has several flow control constructs:
if, else, and elseif switch and case for while continue break try - catch return
Several special functions provide values of useful constants:
5
Hina Pradip Sangai 81003108013
Operators
Expressions use familiar arithmetic operators and precedence rules.
6
Hina Pradip Sangai 81003108013
EXPERIMENT-2
% Develop sine and cosine function waveforms t=linspace(0,10,100);y=sin(pi*t);plot(t,y);y2=cos(pi*t);hold onplot(t,y2,'red');xlabel('time');ylabel('amplitude');title('sine and cosine waveforms');
7
Hina Pradip Sangai 81003108013
WAVEFORMS:-
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
time
ampl
itude
sine and cosine waveforms
8
Hina Pradip Sangai 81003108013
EXPERIMENT-3
% Develop elementary signals: 1) Unit step signal 2) unit ramp signal 3) unit impulse signal 4) exponential signal.
t=-3:1:3;y=[zeros(1,3),ones(1,4)];subplot(2,2,1);stem(t,y);xlabel('time');ylabel('amplitude');title('unit step function');t1=-4:1:4;y1=t1;subplot(2,2,2);stem(t1,y1);xlabel('time');ylabel('amplitude');title('unit ramp function');t2=-3:1:3;y2=[zeros(1,3),1,zeros(1,3)];subplot(2,2,3);stem(t2,y2);xlabel('time');ylabel('amplitude');title('unit impulse function');t3=-5:1:5;y3=exp(t3);subplot(2,2,4);stem(t3,y3);xlabel('time');ylabel('amplitude');title('exponential function');
9
Hina Pradip Sangai 81003108013
WAVEFORMS:-
-4 -2 0 2 40
0.5
1
time
ampl
itude
unit step function
-4 -2 0 2 4-4
-2
0
2
4
timeam
plitu
de
unit ramp function
-4 -2 0 2 40
0.5
1
time
ampl
itude
unit impulse function
-5 0 50
50
100
150
time
ampl
itude
exponential function
10
Hina Pradip Sangai 81003108013
EXPERIMENT-4
%Develop program modules based on operation on sequences like: 1) Signal addition 2) Signal multiplication3) Signal folding 4) Signal shifting
t=0:1:5;x=[1,2,3,4,5,6];subplot(4,3,1);stem(t,x);xlabel('time');ylabel('amplitude');title('x sequence');t1=0:1:5;y=[0,-1,1,2,3,2];subplot(4,3,2);stem(t1,y);xlabel('time');ylabel('amplitude');title('y sequence');z=x+y;subplot(4,3,3);stem(z);xlabel('time');ylabel('amplitude');title('addition of x and y sequence');t2=0:1:5;x1=[1,2,3,4,5,6];subplot(4,3,4);stem(t2,x1);xlabel('time');ylabel('amplitude');title('x sequence');t3=0:1:5;y1=[0,1,1,2,3,2];subplot(4,3,5);stem(t3,y1);xlabel('time');ylabel('amplitude');title('y sequence');z1=x.*y;subplot(4,3,6);stem(z1);xlabel('time');ylabel('amplitude');title('multiplication of x and y sequence');t4=0:1:5;x2=[1,2,3,4,5,6];subplot(4,3,7);stem(t4,x2);
11
Hina Pradip Sangai 81003108013
xlabel('time');ylabel('amplitude');title('x sequence');z2=-x2;subplot(4,3,8);stem(t4,z2);xlabel('time');ylabel('amplitude');title('folding of x sequence');t5=0:1:5;t5=-t4;subplot(4,3,9);stem(t5,x2);xlabel('time');ylabel('amplitude');title('folding of x sequence');t6=0:1:5;x3=[1,2,3,4,5,6];subplot(4,3,10);stem(t6,x3);xlabel('time');ylabel('amplitude');title('x sequence');t7=0:1:6;t7=t6-1;subplot(4,3,11);stem(t7,x3);xlabel('time');ylabel('amplitude');title('shifting of x sequence');t8=0:1:6;t8=t6+1;subplot(4,3,12);stem(t8,x3);xlabel('time');ylabel('amplitude');title('shifting of x sequence');
12
Hina Pradip Sangai 81003108013
WAVEFORMS:-
0 50
5
10
time
ampl
itude
x sequence
0 5-5
0
5
timeam
plitu
de
y sequence
0 5 100
5
10
time
ampl
itude
addition of x and y sequence
0 50
5
10
time
ampl
itude
x sequence
0 50
2
4
time
ampl
itude
y sequence
0 5 10-20
0
20
time
ampl
itude
multiplication of x and y sequence
0 50
5
10
time
ampl
itude
x sequence
0 5-10
-5
0
time
ampl
itude
folding of x sequence
-4 -2 00
5
10
time
ampl
itude
folding of x sequence
0 50
5
10
time
ampl
itude
x sequence
-5 0 50
5
10
time
ampl
itude
shifting of x sequence
0 5 100
5
10
time
ampl
itude
shifting of x sequence
13
Hina Pradip Sangai 81003108013
EXPERIMENT-5
%To develop program to perform linear convolution of two input sequencex(n) and h(n). Plot x(n) and h(n) and result of linear convolution as y(n)on single plot and verify the result mathematically.
t=0:1:4;x=input('enter x(n) sequence');subplot(3,1,1);stem(t,x);xlabel('time');ylabel('amplitude');title('x sequence');t1=0:1:2;h=input('enter h(n) sequence');subplot(3,1,2);stem(t1,h);xlabel('time');ylabel('amplitude');title('h sequence');t2=0:1:6;y=conv(x,h);subplot(3,1,3);stem(t2,y);xlabel('time');ylabel('amplitude');title('convolution of x and h sequence');
Result on command window :
enter x(n) sequence[1 2 3 4 5]enter h(n) sequence[ 1 2 3]
14
Hina Pradip Sangai 81003108013
15
Hina Pradip Sangai 81003108013
WAVEFORMS:-
0 0.5 1 1.5 2 2.5 3 3.5 40
5
time
ampl
itude
x sequence
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20
2
4
time
ampl
itude
h sequence
0 1 2 3 4 5 60
20
40
time
ampl
itude
convolution of x and h sequence
16
Hina Pradip Sangai 81003108013
EXPERIMENT-6
%To obtain the impulse response of the system described by differenceequation y(n)-0.5y(n-1)=x(n)
x=[ones(1,1),zeros(1,9)]a=input('enter the coeff. of y(n)')b=input('enter the coeff. of x(n)')h=filter(b,a,x)n=0:9stem(n,h)title('impulse response of the system')xlabel('n')ylabel('h(n)')
Result on command window:-
enter the coeff. of y(n)[1 -0.5]
a =
1.0000 -0.5000
enter the coeff. of x(n)[1]
b =
1
h =
1.0000 0.5000 0.2500 0.1250 0.0625 0.0313 0.0156 0.0078 0.0039 0.0020
n =
0 1 2 3 4 5 6 7 8 9
17
Hina Pradip Sangai 81003108013
WAVEFORMS:-
0 1 2 3 4 5 6 7 8 90
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1impulse response of the system
n
h(n)
18
Hina Pradip Sangai 81003108013
EXPERIMENT-7
%Write a program to determine and plot poles and zeroes of rational z-transform given as H(z)= 15 z2 + 8 z + 1 H(z)=(5+z-1)(3+z-1)
--------------- or ----------- 8 z2 + 6 z + 1 (2+z-1)(4+z-1)
a=input('enter the coefficients of num.')b=input('enter the coefficients of den.')c=tf(a,b,1)[z p]=tf2zp(a,b)zplane(z,p)[r,p,k]=residuez(a,b)[num,den]=residuez(r,p,k)
Result on command window:-
enter the coefficients of num.[15 8 1]
a =
15 8 1
enter the coefficients of den.[8 6 1]
b =
8 6 1 1
Transfer function:15 z^2 + 8 z + 1----------------8 z^2 + 6 z + 1 Sampling time: 1
z =
-0.3333 -0.2000
p =
19
Hina Pradip Sangai 81003108013
-0.5000 -0.2500
r =
0.7500 0.1250
p =
-0.5000 -0.2500
k =
1
num =
1.8750 1.0000 0.1250
den =
1.0000 0.7500 0.1250
20
Hina Pradip Sangai 81003108013
POLE-ZERO PLOT:-
-1 -0.5 0 0.5 1
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Real Part
Imag
inar
y P
art
21
Hina Pradip Sangai 81003108013
EXPERIMENT-8
%To develop program for computing DFT and IDFT clc;x=input('ENTER A SEQUENCE TO FIND DFT');N=8;X=fft(x,N);a=ifft(X);
22
Hina Pradip Sangai 81003108013
Result on the command window:-
ENTER A SEQUENSE TO FIND DFT[1 2 3 4 5]
N =
8
X =
Columns 1 through 6
15.0000 -5.4142 - 7.2426i 3.0000 + 2.0000i -2.5858 - 1.2426i 3.0000 -2.5858 + 1.2426i
Columns 7 through 8
3.0000 - 2.0000i -5.4142 + 7.2426i
a =
1.0000 2.0000 3.0000 4.0000 5.0000 -0.0000 0 -0.0000
23
Hina Pradip Sangai 81003108013
EXPERIMENT-9
% To develop program for finding magnitude and phase response of LTI system described by difference equation y(n)=0.8y(n-2)+x(n)-x(n-2)
clc;b=input('enter the coeff. of x(n)');a=input('enter the coeff. of y(n)');N=200;[H,W]=freqz(b,a,N);subplot(2,1,1);absH=abs(H);angH=angle(H);plot(W,absH);xlabel('W');ylabel('abs H');title('LTI magnitude');subplot(2,1,2);plot(W,angH);xlabel('W');ylabel('angle H');title('LTI angle');
Result on command window:
enter the coeff. of x(n)[1 0 -1]enter the coeff. of y(n)[1 0 -0.81]
24
Hina Pradip Sangai 81003108013
WAVEFORMS:-
0 0.5 1 1.5 2 2.5 3 3.50
0.5
1
1.5
W
abs
H
LTI magnitude
0 0.5 1 1.5 2 2.5 3 3.5-2
-1
0
1
2
W
angl
e H
LTI angle
25
Hina Pradip Sangai 81003108013
EXPERIMENT-10
% To design an IIR filter with following specifications using filter design and analysis toolboxResponse type: Low pass filterDesign method: IIR Butterworth filterFilter order: MinimumMatch exactly: Stop bandFrequency specifications:Units=KHzFs=500Fpass=100Fstop=150Magnitude specifications:Units=dBApass=3Astop=16
26
Hina Pradip Sangai 81003108013
Current Filter Information:
Magnitude Resoponse:
27
Hina Pradip Sangai 81003108013
Phase Response:
Impulse Response:
28
Hina Pradip Sangai 81003108013
Step Response:
29
Hina Pradip Sangai 81003108013
Generated MATLAB Code:-
%EXP10 Returns a discrete-time filter object. %% M-File generated by MATLAB(R) 7.1 and the Signal Processing Toolbox 6.4.%% Generated on: 23-Apr-2009 13:55:23% % Butterworth Lowpass filter designed using FDESIGN.LOWPASS. % All frequency values are in kHz.Fs = 500; % Sampling Frequency Fpass = 100; % Passband FrequencyFstop = 150; % Stopband FrequencyApass = 3; % Passband Ripple (dB)Astop = 16; % Stopband Attenuation (dB)match = 'stopband'; % Band to match exactly % Construct an FDESIGN object and call its BUTTER method.h = fdesign.lowpass(Fpass, Fstop, Apass, Astop);Hd = butter(h, 'MatchExactly', match); % [EOF]
30