DSP Edited
DSP Edited
Digital Signal
Processing Lab
FAMILIARIZATION OF MATLAB
Department of Electronics 2
CAS,calicut
Digital Signal
Processing Lab
MATLAB BASICS
MATLAB is an interactive program for doing matrix calculations and has now
grown to a high level mathematical language that can solve integrals and differential
equations numerically and plot a wide variety of two and three dimensional graphs.
A) Definition of Variables
Variables are assigned numerical values by typing the expression directly. The
answer will not be displayed when a semicolon is put at the end of an expression.
A variable can be assigned using a formula that utilizes these operators and
either numbers or previously defined variables. To determine the value of a previously
defined quantity, type the quantity by itself. If your expression does not fit on one line,
use an ellipsis (three or more periods at the end of the line) and continue on the next
line. There are several predefined variables which can be used at any time, in the same
manner as user defined variables:
i - sqrt(-1)
j - sqrt(-1)
pi - 3.1416...
There are also a number of predefined functions that can be used when defining
a variable. Some common functions that are used in this text are:
abs - magnitude of a number (absolute value for real numbers)
angle - angle of a complex number, in radians
cos - cosine function, assumes argument is in radians
sin - sine function, assumes argument is in radians
exp - exponential function(can be used on complex numbers).
B) Definition of Matrices
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1
matrices. Therefore, vector and matrix operations are as simple as common calculator
operations.
Department of Electronics 3
CAS,calicut
Digital Signal
Processing Lab
Vectors can be defined in two ways. The first method is used for arbitrary
elements.
v = [1 3 5 7];
creates a 1x4 vector with elements 1, 3, 5 and 7. Note that commas could have been
used in place of spaces to separate the elements. Additional elements can be added to
the vector,
v(5) = 8;
yields the vector v = [1 3 5 7 8]. Previously defined vectors can be used to define a new
vector.
The second method is used for creating vectors with equally spaced elements.
t = 0:0.1:10;
creates a 1x11 vector with the elements 0, .1, .2, .3,..., 10. Note that the middle number
defines the increment. If only two numbers are given, then the increment is set to a
default of 1.
k = 0:10;
creates a 1x11 vector with the elements 0, 1, 2, ..., 10.Matrices are defined by entering
the elements row by row.
M = [1 2 4; 3 6 8];
creates the matrix
M = [1 2 4]
[3 6 8]
C) General Information
MATLAB is case sensitive so "a" and "A" are two different names. Comment
statements are preceded by a "%". On-line help for MATLAB can be reached by typing
help for the full menu or typing help followed by a particular function name or M-file
name. For example, help cos gives help on the cosine function.
The number of digits displayed is not related to the accuracy. To change the
format of the display, type format short e for scientific notation with 5 decimal places,
format long e for scientific notation with 15 significant decimal places and format bank
for placing two significant digits to the right of the decimal.
The commands who and whos give the names of the variables that have been
defined in the workspace. The command length(x) returns the length of a vector x and
size(x) returns the dimension of the matrix x.
D) M-files
M-files are macros of MATLAB commands that are stored as ordinary text files
with the extension "m", that is filename.m. An M-file can be either a function with
input and output variables or a list of commands.
MATLAB M-files are most efficient when written in a way that utilizes matrix
or vector operations. Loops and if statements are available, but should be used
sparingly since they are computationally inefficient. An if statement can be used to
define conditional statements.
The allowable comparisons between expressions are >=, <=, <, >, ==, and ~=.
Suppose that you want to run an M-file with different values of a variable T. The
following command line within the M-file defines the value.
T = input ('Input the value of T: ')
Whatever comment is between the quotation marks are displayed to the screen
when the M-file is running, and the user must enter an appropriate value.
Department of Electronics 5
CAS,calicut
Digital Signal
Processing Lab
Double click on icon for MATLAB 6.5. Within about 30 seconds MATLAB
will open, a screen like the picture below appears.
• Command Window – This is where you can type commands and usually the
answers (or error messages) appear here too. You will see the cursor flickering
after the >> prompt. This means that MATLAB is waiting for further
instructions.
• Workspace – if you define new quantities (called variables) there names should
be listed here.
Department of Electronics 6
CAS,calicut
Digital Signal
Processing Lab
• Command History – This is past commands are remembered. If you want to re-
run a previous command or to edit it you can drag it from this window to the
command window to re-run it.
To begin to use MATLAB, click New:M-file from the File menu. This opens a
blank window as shown below.
The M-file is executed using the Run command under the Tools menu. The
output signal appears in Figure Window.
Department of Electronics 7
CAS,calicut
Digital Signal
Processing Lab
Department of Electronics 8
CAS,calicut
Digital Signal
Processing Lab
Department of Electronics 9
CAS,calicut
Digital Signal
Processing Lab
∂(n) =1 for n = 0
0 for n ≠ 0
u(n) =1 for n ≥ 0
0 for n < 0
Department of Electronics 11
CAS,calicut
Digital Signal
Processing Lab
3. Ramp signal
ur(n)=n for n ≥ 0
0 for n < 0
4. Exponential signal
If the parameter a is real, then x(n) is a real signal. We can express x(n) for
various values of the parameter a.
If the parameter a is complex valued, it can be expressed as
a = rejθ
Department of Electronics 12
CAS,calicut
Digital Signal
Processing Lab
5. Sine signal
ω ≡ 2π f
6. Cosine signal
ω ≡ 2π f
Department of Electronics 13
CAS,calicut
Digital Signal
Processing Lab
Aim
To write a program that plot unit step signal, unit impulse, ramp signals.
Program
clf;
%UNIT STEP SIGNAL
subplot(3,1,1);
x=[-10:10];
y=[zeros(1,10) 1 ones(1,10)];
stem(x,y);
xlabel(‘samples’);
ylable(‘amplitude’);
axis([-10,10,0,2]);
title(‘UNIT STEP’);
%
subplot(2,1,1)
x=[zeros(1,10) 1 zeros(1,10)];
n=[-10:10];
stem(n,x);
axis([-10,10,0,2]);
subplot(2,1,2)
x=[zeros(1,15) 1 zeros(1,5)];
n=[-10:10];
stem(n,x);
axis([-10,10,0,2]);
Department of Electronics 14
CAS,calicut
Digital Signal
Processing Lab
Output
Unit step
2
1.5
amplitude
0.5
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
samples
unit step shifted by 5 unit
2
1.5
amplitude
0.5
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
samples
Result
The unit step signal and delaying unit step signal are plotted.
Department of Electronics 15
CAS,calicut
Digital Signal
Processing Lab
Aim
To write a program that plot unit impulse signal and delayed unit impulse signal
Program
clf;
Department of Electronics 16
CAS,calicut
Digital Signal
Processing Lab
Output
Unit impulse
2
1.5
amplitude
0.5
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
samples
Unit impulse shifted by 5 unit
2
1.5
Amplitude
0.5
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
Samples
Result
The unit impulse signal and delayed unit impulse signal are plotted.
Department of Electronics 17
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
subplot(3,1,1);
n=[0:60];
stem(n,n);
axis([0,60,0,60]);
subplot(3,1,2);
y=[0:1:20 19:-1:0 -1:-1:-20 -19:1:0];
n=[0:80]
stem(n,y);
axis([0,80,-20,20]);
subplot(3,1,3);
y=[zeros(1,10) 1:1:10 9:-1:0 zeros(1,10) -1:-1:-10 -9:1:0 zeros(1,11)];
n=[0:70];
stem(n,y);
axis([0,70,-15,15]);
Department of Electronics 18
CAS,calicut
Digital Signal
Processing Lab
Output
Ramp signal
60
Amplitude
40
20
0
0 10 20 30 40 50 Samples60
Triangular wave
20
Amplitude
-20
0 10 20 30 40 50 60 70 80
Modified triangular wave Samples
10
Amplitude
-10
0 10 20 30 40 50 60 70
Samples
Result
The ramp signal, triangular wave and modified triangular wave are plotted.
Department of Electronics 19
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
subplot(2,1,1);
a=10;
f=0.02;
n=[0:80];
y=a*sin(2*3.14*f*n);
stem(n,y);
axis([0,80,-20,20]);
subplot(2,1,2);
a=10;
f=0.02;
n=[0:80];
y=a*cos(2*3.14*f*n);
stem(n,y);
axis([0,80,-20,20]);
Department of Electronics 20
CAS,calicut
Digital Signal
Processing Lab
Output
Sine wave
20
10
Amplitude
-10
-20
0 10 20 30 40 50 60 70 80
Samples
Cosine wave
20
10
Amplitude
-10
-20
0 10 20 30 40 50 60 70 80
Samples
Result
Department of Electronics 21
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
subplot(2,1,1);
a=0.8;
n=[0:15];
y=a.^n;
stem(n,y);
axis([0,15,0,1]);
subplot(2,1,2);
a=1.1;
n=[0:50];
y=a.^n;
stem(n,y);
axis([0,42,0,50]);
Department of Electronics 22
CAS,calicut
Digital Signal
Processing Lab
Output
0.8
Amplitude
0.6
0.4
0.2
0
0 5 10 Samples 15
Exponential signal with a=1.1
50
40
amplitude
30
20
10
0
0 5 10 15 20 25 30 35 40
Samples
Result
Department of Electronics 23
CAS,calicut
Digital Signal
Processing Lab
Aim
clf;
subplot(5,1,1);
a=10;
f=0.1;
n=[0:500];
ya=a*cos(2*3.14*f*n);
plot(n,ya);
axis([0,500,-20,20]);
subplot(5,1,2);
a=10;
f=0.01;
n=[0:500];
yb=a*cos(2*3.14*f*n);
plot(n,yb);
axis([0,500,-20,20])
subplot(5,1,3);
m=1;
am=10;
n=[0:500];
y=(1+(m/am).*yb).*ya;
plot(n,y);
axis([0,500,-20,20])
subplot(5,1,4);
m=2;
am=10;
n=[0:500];
y=(1+(m/am).*yb).*ya;
plot(n,y);
axis([0,500,-20,20])
subplot(5,1,5);
m=.5;
am=10;
n=[0:500];
y=(1+(m/am).*yb).*ya;
plot(n,y);
axis([0,500,-20,20])
Department of Electronics 24
CAS,calicut
Digital Signal
Processing Lab
Output
Carrier siginal
20
Amplitude
-20
Message siginal 0 50 100 150 200 250 300 350 400 450 500
20 Time
Amplitude
-20
AM wave m=1 0 50 100 150 200 250 300 350 400 450 500
20 Time
Amplitude
-20
AM wave m>1 0 50 100 150 200 250 300 350 400 450 500
20 Time
Amplitude
-20
0 50 100 150 200 250 300 350 400 450 500
AM wave m<1 20
Time
Amplitude
-20
0 50 100 150 200 250 300 350 400 450 500
Time
Result
Department of Electronics 25
CAS,calicut
Digital Signal
Processing Lab
Department of Electronics 26
CAS,calicut
Digital Signal
Processing Lab
y(n) = H[x(n)]
where H denotes the transformation (also called an operator) .
N M
y(n) = - ∑ ak y(n-k) + ∑ bk x(n-k) …… (1)
k =1 k =0
The integer N is called the order of the system. Here y(n-k) are past outputs,
x(n-k) are past inputs, x(n) is present input and ak and bk are constant coefficients .
The transfer function of the LTI system is defined as the ratio of z-transform of output
to z-transform of input. On taking z-transform of equation (1) we get
M −k
∑ bk z
Y (z) k =0
= ….. (2)
X (z) N
1+ ∑ ak z − k
k =1
The equation (2) is the transfer function of the LTI system. The numerator and
the denominator of the transfer function are polynomials of z. The roots of the
numerator polynomial are zeros of the LTI system and the roots of the denominator
polynomial are poles of the LTI system.
The discrete time systems are classified based on their characteristics. Some of
the classifications of discrete time systems are,
Department of Electronics 27
CAS,calicut
Digital Signal
Processing Lab
4. Stable and unstable systems.
5. Static and dynamic systems.
6. FIR and IIR systems.
7. Recursive and non recursive systems
A linear system is one that satisfies the superposition principle. The principle of
superposition requires that the response of the system to a weighted sum of the signals
is equal to the corresponding weighted sum of the responses of the system to each of
the individual input signals.
Definition:
for any arbitrary input sequences x1(n) and x2(n) and for any arbitrary constants a1 and
a2. If a relaxed system does not satisfy the superposition principle as given by the above
definition, the system is non linear.
Definition:
A system is said to be causal if the output of the system at any time n depends
only on the present input, past inputs and past outputs but does not depend on future
inputs and outputs.
If the system output at any time n depends on future inputs or outputs then the
system is called non causal system.
The causality refers to a system that is realizable in real time. It can be shown
that a LTI is causal if and only if the impulse response is zero for n<0, (i.e., h(n)=0 for
n<0).
Let x(n)=present input and y(n)=present output
Department of Electronics 28
CAS,calicut
Digital Signal
Processing Lab
In mathematical terms the output of a causal system satisfies the equation of the form
Definition:
A system can be tested for time invariance using the following procedure:
1. Delay the input signal by k units of time and determine the response of the
system for this delayed input signal. Let this response be y(n-k).
2. Delay the response of the system for unshifted input by k units of time. Let
this delayed response be y`(n-k).
3. Check whether y(n-k) = y`(n-k).If they are equal then the system is time
invariant. Otherwise the system is time variant.
Definition:
Department of Electronics 29
CAS,calicut
Digital Signal
Processing Lab
An arbitrary relaxed system is said to be Bounded Input-Bounded Output
(BIBO) stable if and only if every bounded input produces a bounded output.
If x(n) is bounded, there exists a constant Mx such that │x(n)│≤ Mx< ∞; for all
n. Similarly, if the output is bounded, there exists a constant My such that │y (n) │≤ My
< ∞; for all n.
∞
By convolution sum formula, y(n)= ∑
k =−∞
h(k) x(n-k) ….. (1)
If the input is bounded then │x(n-k)│ = Mx. Hence equation (2) can be written
as,
∞ ∞
│y(n)│ = ∑
k =−∞
│h(k)│ Mx = Mx ∑
k =−∞
│h(k)│ ….. (3)
Form equation (3) we can say that the output is bounded if the impulse response
of the system satisfies the condition
∞
∑
k =−∞
│h(k)│ < ∞
A discrete time system is static or memory less if its output at any instant n
depends at most on the input sample at the same time but not on past or future samples
of the input. In any other case the system is said to be dynamic or to have memory.
From equation (1) it can be concluded that the impulse response selects only N
samples of the input signal.
Department of Electronics 30
CAS,calicut
Digital Signal
Processing Lab
In effect, the system acts as a window that views only the most recent N input
signal samples in forming the output. It neglects or simply forgets all prior input
samples. Thus a FIR system has a finite memory of length N samples.
N −1
y(n)= ∑ bk x(n-k)
k =0
In IIR (Infinite duration Impulse Response) systems the impulse response has
finite number of samples. The convolution formula for IIR systems is given by,
∞
y(n)= ∑ h(k) x(n-k)
k =0
Since this weighted sum involves the present and all the past input samples, the
system has an infinite memory.
N M
y(n)= - ∑ ak y(n-k) + ∑ bk x(n-k)
k =1 k =1
A system whose output y (n) at time n depends on any number of past output
values is called a recursive system. The past outputs are y(n-1), y(n-2), y(n-3), …
Hence for recursive system, the output, y(n) is given by,
A system whose output depends only on the present and past input is called a
non-recursive system. Hence for non recursive system, the output, y(n) is given by
Department of Electronics 31
CAS,calicut
Digital Signal
Processing Lab
Program
clf;
subplot(4,1,1);
a=10;
f=0.02;
n=[0:80];
y1=a*sin(2*3.14*f*n);
plot(n,y1);
axis([0,80,-20,20]);
subplot(4,1,2);
a=10;
y2=a*rand(1,81);
plot(n,y2);
axis([0,80,-12,12]);
subplot(4,1,3);
y3=y1+y2;
plot(n,y3);
axis([0,80,-25,25]);
subplot(4,1,4);
xco=ones(1,15);
yco=1;
y4=filter(xco,yco,y3)/10
plot(n,y4);
axis([0,80,-30,30]);
Department of Electronics 32
CAS,calicut
Digital Signal
Processing Lab
Output
-20
0 10 20 30 40 50 60 70 Time 80
Input siginal Y2(n)
10
Amplitude
-10
0 10 20 30 40 50 60 70 Time 80
Y3(n)=Y1(n)+Y2(n)
20
Amplitude
-20
0 10 20 30 40 50 60 70 Time 80
Filtered siginal
20
Amplitude
-20
0 10 20 30 40 50 60 70 Time 80
Result
Department of Electronics 33
CAS,calicut
Digital Signal
Processing Lab
Aim
To write a program to realize the system described by the equation,
y(n)= x2(n) + x(n-1) ∗ x(n+1)
Program
clf;
n=[0:4];
x=[1 2 3 4 5];
subplot(5,1,1);
a=10;
f=0.02;
n=[0:6];
x1=[0 x 0];
stem(n,x1);
axis([-2,7,0,8]);
subplot(5,1,2);
a=10;
f=0.02;
n=[0:6];
x2=[0 0 x];
stem(n,x2)
axis([-2,7,0,8]);
subplot(5,1,3);
a=10;
f=0.02;
n=[0:6];
x3=[x 0 0];
stem(n,x3)
axis([-2,7,0,8]);
subplot(5,1,4);
a=10;
f=0.02;
n=[0:6];
x4=(x1).^2;
Department of Electronics 34
CAS,calicut
Digital Signal
Processing Lab
stem(n,x4)
axis([-2,7,0,25]);
subplot(5,1,5);
a=10;
f=0.02;
n=[0:6];
x5=x4+(x2.*x3);
stem(n,x5)
axis([-2,7,0,40]);
Output
X(n)
5
Amplitude
0
-2 -1 0 1 2 X(n-1) 3 4 5 6 7
Time
Amplitude
0
-2 -1 0 1 2 X(n+1) 3 4 5 6 7
Time
Amplitude
0
-2 -1 0 1 2 X2(n) 3 4 5 6 7
20
Amplitude
10
0
-2 -1 0 1 Y(n)=X2(n)+X(n-1)*X(n+1)
2 3 4 5 6 7
40
Amplitude
20
0
-2 -1 0 1 2 3 4 5 6 7
Time
Result
Department of Electronics 35
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
a=10;
f=0.02;
n=[0:80];
y1=a*sin(2*3.14*f*n);
xco=[1 -0.4 0.2 0.6];
yco=[1 -0.1 0.5];
y2=filter(xco,yco,y1)/5;
subplot(5,2,1);
a1=2;
y3=a1.*y2;
plot(n,y3);
axis([0,80,-20,20]);
subplot(5,2,2);
a=10;
y4=a*rand(1,81);
y5=filter(xco,yco,y4)/5;
a2=3;
y6=a2.*y5;
plot(n,y6);
axis([0,80,-20,20]);
subplot(5,1,3);
y7=y6+y3;
Department of Electronics 36
CAS,calicut
Digital Signal
Processing Lab
plot(n,y7);
axis([0,80,-20,20]);
subplot(5,2,4);
y8=[a1.*y1+a2.*y4];
plot(n,y8);
subplot(5,1,5);
y9=filter(xco,yco,y8)/5;
plot(n,y9);
axis([0,80,-20,20]);
Output
a1H[x1(n)] a2H[x2(n)]
20 20
Amplitude
Amplitude
0 0
-20 -20
0 20 40 60 Time80 0 20 40 60 80
Time
[a1x1(n)+a2x2(n)]
20
Amplitude
-20
0 20 40 60 80
Time a1H[x1(n)]+a2H[x2(n)]=LHS
50
Amplitude
-50
0 10 20 30 40 50 60 70 Time 80
H[a1x1(n)+a2x2(n)]=RHS
20
Amplitude
-20
0 10 20 30 40 50 60 70 80
Time
Result
Department of Electronics 37
CAS,calicut
Digital Signal
Processing Lab
Aim
To write a program to check the system described by the equation y(n)= x2(n) is
a linear system.
Program
clf;
subplot(4,1,1);
a=2;
f=0.02;
n=[0:80];
y1=a*sin(2*3.14*f*n);
a1=2;
y2=a1.*y1;
plot(n,y2);
axis([0,80,-20,20]);
subplot(4,1,2);
a=2;
y3=a*rand(1,81);
a2=3;
y4=a2.*y3;
plot(n,y4);
axis([0,80,-20,20]);
subplot(4,1,3);
y5=y2+y4;
y6=(y5).^2;
plot(n,y6);
Department of Electronics 38
CAS,calicut
Digital Signal
Processing Lab
axis([0,80,0,40]);
subplot(4,1,4);
y7=y1.^2;
y8=a1.*y7;
y9=y3.^2;
y10=a2.*y9;
y11=y8+y10;
plot(n,y11);
axis([0,80,0,40]);
Output
a1x1(n)
20
Amplitude
-20
0 10 20 30 40 50 60 70 Time 80
a2x2(n)
20
Amplitude
-20
0 10 20 30 40 50 60 70 Time 80
[a1x1(n)+a2x2(n)]2
40
Amplitude
20
0
0 10 20 30 40 50 60 70 Time 80
[a1x12(n)+a2x22(n)]
40
Amplitude
20
0
0 10 20 30 40 50 60 70 Time 80
Result
Department of Electronics 39
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
subplot(5,2,1);
a=15;
f=0.1;
n=[0:80];
y1=[0.5*sin(2*3.14*0.1*n)]-[0.3*cos(2*3.14*0.1*n)];
plot(n,y1);
axis([0,80,-1,1]);
subplot(5,2,2);
xco=[1 1 -0.5];
yco=[1 0.41 -0.21 -0.635];
y2=filter(xco,yco,y1);
plot(n,y2);
axis([0,80,-1,1]);
subplot(5,2,3);
d=20;
n1=[0:80+d]
Department of Electronics 40
CAS,calicut
Digital Signal
Processing Lab
y3=[zeros(1,d) y2];
plot(n1,y3);
axis([0,80,-1,1]);
subplot(5,2,4);
y4=[zeros(1,d) y1];
plot(n1,y4);
axis([0,80,-1,1]);
subplot(5,2,5);
y5=filter(xco,yco,y4);
plot(n1,y5);
axis([0,80,-1,1]);
Output
X(n) Y(n)
1 1
Amplitude
Amplitude
0 0
-1 -1
0 20 40 60 80 0 20 40 60Time 80
Time
Y(n-d) X(n-d)
1 1
Amplitude
Amplitude
0 0
-1 -1
0 20 40 60 80 0 20 40 60 Time 80
Time
-1
0 20 40 60 80
Time
Result
Department of Electronics 41
CAS,calicut
Digital Signal
Processing Lab
The given system is realized. The system is time invariant.
Aim
clf;
subplot(3,2,1);
n=[0:80];
x=[0.5*sin(2*3.14*0.01*n)];
plot(n,x);
axis([0,80,0,1]);
subplot(3,2,2);
n1=[0:82];
x1=[0 x 0];
x2=[0 0 x]
y1=(n1.*x1)+x2;
plot(n1,y1);
axis([0,80,0,20]);
subplot(3,2,3);
d=20;
n2=[0:82+d];
y2=[zeros(1,d) y1];
plot(n2,y2);
Department of Electronics 42
CAS,calicut
Digital Signal
Processing Lab
axis([0,80,0,20]);
subplot(3,2,4);
d=20;
n3=[0:82+d]
y3=[zeros(1,d) y1];
plot(n3,y3);
axis([0,80,0,20]);
subplot(3,2,5);
n=[0:80+d];
x3=[zeros(1,d) x];
n1=[0:82+d];
x1=[0 x3 0];
x2=[0 0 x3]
y4=(n1.*x1)+x2;
plot(n1,y4);
axis([0,80,0,25]);
Output
X(n) Y(n)
1 20
Amplitude
Amplitude
0.5 10
0 0
0 20 40 60 Time80 0 20 40 60 Time80
Y(n-d) X(n-d)
20 20
Amplitude
Amplitude
10 10
0 0
0 20 40 60 Time 80 0 20 40 60 80
Y(n,d) Time
20
Amplitude
10
0
80 60 40 20 0
Time
Result
Department of Electronics 43
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
n=[0:500];
xco=[1 0.8];
yco=[1 1 1.5 0.9];
h=impz(xco,yco,n);
stem(n,abs(h));
s=0;
for i=1:500
s=s+abs(h(i));
if abs(h(i))<10^(-6)
break
end
end
display(s);
Department of Electronics 44
CAS,calicut
Digital Signal
Processing Lab
Output
2.5
2
amplitude
1.5
0.5
0
0 50 100 150 200 250 300 350 400 450 500
samples
Department of Electronics 45
CAS,calicut
Digital Signal
Processing Lab
Result
The impulse response of the given system is plotted. Since ∑ h(n) ≈ ∞, the
system is unstable.
Aim
To write a program that check the following LTI system described by the
difference equation y(n) = x(n) - 0.8x(n-1) - y(n-1) - 0.9y(n-2) is stable.
Program
clf;
n=[0:500];
xco=[1 0.8];
yco=[1 1 0.9];
h=impz(xco,yco,n);
stem(n,abs(h));
s=0;
for i=1:500
s=s+abs(h(i));
if abs(h(i))<10^(-6)
break
end
end
display(s);
Department of Electronics 46
CAS,calicut
Digital Signal
Processing Lab
Output
Department of Electronics 47
CAS,calicut
Digital Signal
Processing Lab
Impulse Response of a Stable System
1
0.9
0.8
0.7
0.6
amplitude
0.5
0.4
0.3
0.2
0.1
0
0 50 100 150 200 250 300 350 400 450 500
samples
Result
The impulse response of the given system is plotted. Since ∑ h(n)< ∞, the
system is stable.
Department of Electronics 48
CAS,calicut
Digital Signal
Processing Lab
The Fourier Transform of a finite energy discrete time signal, x(n) is defined as
∞
ω
X(ω ) = ∑ x(n) e-j n
n =−∞
∞
∑ | x(n) | < ∞
n =−∞
The Fourier Transform X(ω ) of a signal x(n) represents the frequency of x(n).
By taking Fourier Transform, the signal x(n) is decomposed into its frequency
components. Hence X(ω ) is also called Signal spectrum.
The difference between the Fourier Transform of a discrete time signal and
analog signal are given below.
2. Since the analog signals are continuous the Fourier Transform of analog
signals involves integration but the Fourier Transform of discrete time
signals involves summation because the signals are discrete.
Department of Electronics 50
CAS,calicut
Digital Signal
Processing Lab
The inverse Fourier Transform of X(ω ) is defined as,
Π
F {x(ω )} = x(n) = π ∫
-1 ωn
X(ω ) ej dω
−Π
We also refer to x(n) and X(ω ) as a Fourier Transform pair and this relation is
expressed as
x(n) F
→ X(ω )
A more useful method of determining the values of x(n) follows directly from
the definition of the Fourier Transform.
ωn ω ω ω ω
X(ω ) = Σ x(n) e-j = . . .+ x(-2) ej2 + x(-1) ej + x(0) e0 +x(1) e-j + x(2) e-j2 + . . .
….. (1)
From the defining equation of X(ω ) we can say that, if X(ω ) can be expressed
as a series of complex exponentials, as shown in equation (1) then x(n) is simply the
ω
coefficient of e-j n.
Properties:
1. Time-shifting property
If F{x(n)} = X(ω )
ω
then F{x(n-k)} = e-j k X(ω )
Also
ωk
F{x(n+k)} = ej X(ω )
2. Time-reversal property
If F{x(n)} = X(ω )
then F{x(-n)} = X(-ω )
This means that if a signal is folded about the origin in time, its
magnitude spectrum remains unchanged and the phase spectrum undergoes a
change in sign (phase reversal).
Department of Electronics 51
CAS,calicut
Digital Signal
Processing Lab
3. Convolution theorem
If F{x(n)} = X(ω )
and F{h(n)} = H(ω )
then F{x(n)*h(n)} = X(ω ) H(ω )
4. Frequency shifting
If F{x(n)} = X(ω )
ω
then F{ej 0n x(n)} = X(ω -ω 0)
ω 0n
According to this property the multiplication of a sequence x(n) by ej
is equivalent to a frequency translation of the spectrum X(ω ) by ω 0.
Department of Electronics 52
CAS,calicut
Digital Signal
Processing Lab
Aim
To write a program that plot real, imaginary, magnitude and phase spectrum of
DTFT of input signal with numerator coefficient [2, 1] & denominator coefficient
[1 -0.6].
Program
clf;
yco=[1 -0.6];
xco=[2 1];
w=[-pi*4:pi/512:pi*4];
y=freqz(xco,yco,w);
subplot(4,1,1);
plot(w,real(y));
subplot(4,1,2);
plot(w,imag(y));
subplot(4,1,3);
plot(w,abs(y));
subplot(4,1,4);
plot(w,angle(y));
Department of Electronics 53
CAS,calicut
Digital Signal
Processing Lab
Output
0
-15 -10 -5
Imaginary Part0 of X(ejw ) 5 10 frequency15
5
amplitude
-5
-15 -10 -5 0 5 10 frequency15
Magnitude Spectrum of X(e jw )
10
amplitude
0
-15 -10 -5 0 5 10 15
frequency
Phase Spectrum of X(ejw )
2
phase
-2
-15 -10 -5 0 5 10 frequency 15
Result
The DTFT of the given signal is found and its real part, imaginary part,
magnitude spectrum and phase spectrum are plotted.
Department of Electronics 54
CAS,calicut
Digital Signal
Processing Lab
Aim
To write a program that plot the magnitude and phase spectrum of DTFT of the
following sequence,
a= [1 3 5 7 9 11 13 15 17].
Program
clf;
yco=1;
xco=[1 3 5 7 9 11 13 15 17];
w=[-pi*4:pi/512:pi*4];
y=freqz(xco,yco,w);
subplot(2,1,1);
plot(w,abs(y));
subplot(2,1,2);
plot(w,angle(y));
Department of Electronics 55
CAS,calicut
Digital Signal
Processing Lab
Output
80
amplitude
60
40
20
0
-15 -10 -5 0 5 10 15
frequency
Phase Spectrum of X(ejw )
4
2
phase
-2
-4
-15 -10 -5 0 5 10 15
frequency
Result
The DTFT of the given signal is found and it’s magnitude spectrum and phase
spectrum are plotted.
Department of Electronics 56
CAS,calicut
Digital Signal
Processing Lab
Aim
To write a program that verifies the time shifting property of DTFT. Input be
x (n) = [1 2 3 4 5 6 7 8 9].
y(n) =[1] .Delay = 10 units
Program
clf;
yco=1;
xco=[1 2 3 4 5 6 7 8 9];
w=[-pi*4:pi/512:pi*4];
y=freqz(xco,yco,w);
subplot(4,1,1);
plot(w,abs(y));
subplot(4,1,2);
plot(w,angle(y));
d=10;
x1=[zeros(1,d) xco];
w=[-pi*4:pi/512:pi*4];
y1=freqz(x1,yco,w);
subplot(4,1,3);
plot(w,abs(y1));
subplot(4,1,4);
plot(w,angle(y1));
Department of Electronics 57
CAS,calicut
Digital Signal
Processing Lab
Output
50
0
-15 -10 -5 0 5 10 Frequency15
Phase Spectrum of x1(n)*x2(n)
5
Phase
-5
-15 -10 -5 0 5 10frequency 15
Magnitude Spectrum of e -jw noY(ejw )
100
amplitude
50
0
-15 -10 -5 0 5 10 frequency 15
Phase Spectrum of e-jw n0Y(ejw )
5
phase
-5
-15 -10 -5 0 5 10frequency 15
Result
Department of Electronics 58
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
a=[1 2 3];
b=[2 3 4];
c=conv(a,b);
yco=1;
w=[-pi*4:pi/512:pi*4];
y=freqz(c,yco,w);
subplot(4,1,1);
plot(w,abs(y));
subplot(4,1,2);
plot(w,angle(y));
y1=freqz(a,yco,w);
y2=freqz(b,yco,w);
y3=y1.*y2;
subplot(4,1,3);
plot(w,abs(y3));
subplot(4,1,4);
plot(w,angle(y3));
Department of Electronics 59
CAS,calicut
Digital Signal
Processing Lab
Output
50
0
-15 -10 -5 0 5 10 15
Phase Spectrum Of x1(n)x2(n) Frequency
5
Phase
-5
-15 -10 -5 0 5 10Frequency 15
Magnitude Spectrum Of X1(W)X2(W)
100
Amplitude
50
0
-15 -10 -5 0 5 10Frequency 15
Phase Spectrum Of X1(W)X2(W)
5
Phase
-5
15 10 5 0 -5 -10Frequency-15
Result
Department of Electronics 60
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
n=[0:2]
a=[1 2 3];
yco=1;
w=[-pi*4:pi/512:pi*4];
y=freqz(a,yco,w);
subplot(4,1,1);
plot(w,abs(y));
subplot(4,1,2);
plot(w,angle(y));
f=0.5;
w1=2*3.14*f;
y1=exp(j*w1*n);
y2=a.*y1;
w=[-pi*4:pi/512:pi*4];
y3=freqz(y2,yco,w);
subplot(4,1,3);
plot(w,abs(y3));
subplot(4,1,4);
plot(w,angle(y3));
Department of Electronics 61
CAS,calicut
Digital Signal
Processing Lab
Output
0
-15 -10 -5 0 5 10 Frequency15
5 Phase Spectrum Of X(ejw )
Phase
-5
-15 -10 -5 0 5 10 15
Magnitude Spectrum Of X(e j -w 0))
- (w Frequency
10
amplitude
0
-15 -10 -5 0 5 10 frequncy 15
Phase spectrum of X(e-j(w -w 0))
5
phase
-5
-15 -10 -5 0 5 10frequency 15
Result
Department of Electronics 62
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
a=[1 2 3];
b=[2 3 4];
yco=1;
w=[-pi*4:pi/512:pi*4];
y=freqz(a,yco,w);
y1=freqz(b,yco,w);
subplot(4,1,1);
plot(w,abs(y));
subplot(4,1,2);
plot(w,abs(y1));
y2=a.*b;
y3=freqz(y2,yco,w);
subplot(4,1,3);
plot(w,abs(y3));
Department of Electronics 63
CAS,calicut
Digital Signal
Processing Lab
Output
0
-15 -10 -5 0 5 10 15
Frequency
0
-15 -10 -5 0 5 10 15
Frequency
Magnitude Spectrum Of DTFT Of x1(n)x2(n)
20
Amplitude
10
0
-15 -10 -5 0 5 10 15
Frequency
Result
Department of Electronics 64
CAS,calicut
Digital Signal
Processing Lab
Program
clf;
yco=1;
xco=[1 2 3 4 5 6 7 8 9];
w=[-pi*4:pi/512:pi*4];
y=freqz(xco,yco,w);
subplot(4,1,1);
plot(w,abs(y));
subplot(4,1,2);
plot(w,angle(y));
y1=fliplr(xco)
y2=freqz(y1,yco,w);
subplot(4,1,3);
plot(w,abs(y2));
subplot(4,1,4);
plot(w,angle(y2));
Department of Electronics 65
CAS,calicut
Digital Signal
Processing Lab
Output
Result
Department of Electronics 66
CAS,calicut
Digital Signal
Processing Lab
Department of Electronics 67
CAS,calicut
Digital Signal
Processing Lab
The discrete Fourier Transform (DFT) of a discrete time signal x(n) is a finite
duration discrete frequency sequence. The DFT sequence is denoted by X(k). The DFT
is obtained by sampling one period of the Fourier Transform X(ω ) of the signal x(n) at
a finite number of frequency points. This sampling is conventionally performed at N
equally spaced points in the period 0 ≤ ω ≤ 2π or at ω k =2π k/N; 0 ≤ k ≤ N-1.
Let x(n) be a discrete time sequence with Fourier Transform X(ω ), then the
DFT of x(n) denoted by X(k) is defined as,
Department of Electronics 68
CAS,calicut
Digital Signal
Processing Lab
Generally the DFT is defined along with number of samples and is called N-
point DFT. The number of samples N for a finite duration sequence x(n) of length L
should be such that, N ≥ L .
Definition of DFT
The Inverse Discrete Fourier Transform (IDFT) of the sequence X(k) of length
N is defined as
N −1 π
IDFT{X(k)} = x(n) = 1/N ∑ X(k) e(j2 kn)/N ; for n= 0,1,. . . (N-1)
n=0
Properties:
1. Linearity
If DFT{x1(n)} = X1(k)
and DFT{x2(n)} = X2(k)
Let x(n) be the discrete sequence and x`(n) be a shifted sequence of x(n) by n0
units of time.
Let DFT{x(n)} = X(k)
π
Now, DFT{x`(n)} = DFT{x(n-n0)} = X(k) e-(j2 kn0)/N
If DFT{x(n)} = X(k)
then DFT{x(N-n)} = X(N-k)
Hence reversing the N-point sequence in time is equivalent to reversing the DFT
values.
If DFT{x(n)} = X(k)
Department of Electronics 69
CAS,calicut
Digital Signal
Processing Lab
π
then DFT{x(n-l , (mod N)) } = X(k) e-(j2 kl)/N
If DFT{x(n)} = X(k)
π
then DFT{x(n) e(j2 ln)/N } = X(k-l, (mod N)) .
6. Circular Convolution
If DFT{x1(n)} = X1(k)
and DFT{x2(n)} = X2(k)
then DFT{x1(n) x2(n) }= X1(k) X2(k)
Aim
To write a program that evaluate the DTFT & 4-point DFT of a causal 3
sampled sequence given by,
x(n) = ; 0<=n<=2
0 ; otherwise.
Program
clf;
xco=[1/3 1/3 1/3];
yco=1;
w=[-4*pi:pi/512:4*pi];
n=[0:15];
y1=freqz(xco,yco,w);
m1=abs(y1);
p1=angle(y1);
y2=fft(xco,16);
m2=abs(y2);
p2=angle(y2);
subplot(4,1,1);
plot(w,m1);
axis([0,6,0,2]);
Department of Electronics 70
CAS,calicut
Digital Signal
Processing Lab
grid
subplot(4,1,2);
plot(w,p1);
axis([0,6,-2.5,2.5]);
grid
subplot(4,1,3);
stem(n,m2);
grid
subplot(4,1,4);
stem(n,p2);
grid
Output
Department of Electronics 71
CAS,calicut
Digital Signal
Processing Lab
Result
Aim
Program
function y=circularshift(x,m)
ln=length(x);
if m<0
m=m*-1;
end
if m>ln
m=rem(m,ln);
end
y=[x((m+1):ln) x(1:m)];
clf;
x=[0:9];
m=4;
subplot(2,1,1);
stem(x,x);
subplot(2,1,2);
y=circularshift(x,m);
stem(x,y);
Output
Department of Electronics 73
CAS,calicut
Digital Signal
Processing Lab
Result
Department of Electronics 74
CAS,calicut
Digital Signal
Processing Lab
Prog. No: 21 CIRCULAR CONVOLUTION
23-09-09
Aim
Program
function y=circularsh(x,m)
ln=length(x);
if m<0
m=m*-1;
end
if m>ln
m=rem(m,ln);
end
y=[x((m+1):ln) x(1:m)];
function s=circularconvo(x1,x2)
l1=length(x1);
l2=length(x2);
if l1~=l2
display('Unequal signals');
end
s=zeros(1:l1);
x2f=[x2(1) x2(l2:-1:2)];
k1=x1.*x2f;
s(1)=sum(k1);
for m=1:l1-1
y=circularsh(x2f,m);
k=x1.*y;
s(m+1)=sum(k);
end
s=s(1:l1);
clf;
x1=input('Enter first signal:');
s=circularconvo(x1,x2);
display (s);
Department of Electronics 75
CAS,calicut
Digital Signal
Processing Lab
Input
Output
s=
14 16 14 16
Result
Department of Electronics 76
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
x=[1 0 1 0];
y=ifft(x);
display(y);
Department of Electronics 77
CAS,calicut
Digital Signal
Processing Lab
Output
y=
0.5000 0 0.5000 0
Result
Department of Electronics 78
CAS,calicut
Digital Signal
Processing Lab
SAMPLING
Department of Electronics 79
CAS,calicut
Digital Signal
Processing Lab
SAMPLING
If the sampling rate is higher than the Nyquist rate, it is called Over-sampling.
On the other hand, if the sampling rate is lower than the Nyquist rate, it is called under-
sampling. Finally, if the sampling rate is exactly equal to the Nyquist rate, it is called
critical sampling.
Sampling theorem
Department of Electronics 80
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
f=0.6;
t=[0:100];
w=pi/180;
ts=1/(2*f);
x1=cos(2*pi*f*t*w);
subplot(4,1,1);
plot(t,x1);
k=([length(t)-1])/ts;
n=[0:k];
x2=cos(2*pi*f*n*ts*w);
subplot(4,1,2);
stem(n,x2);
n=[0:k/10];
x3=cos(2*pi*f*(n*ts*10)*w);
subplot(4,1,3);
stem(n,x3);
n=[0:k*10];
x4=cos(2*pi*f*(n*ts/10)*w);
subplot(4,1,4);
stem(n,x4);
Department of Electronics 81
CAS,calicut
Digital Signal
Processing Lab
Output
Result
Department of Electronics 82
CAS,calicut
Digital Signal
Processing Lab
a) < Nyquist Rate
b) = Nyquist Rate
c) > Nyquist Rate.
ANALOG-TO-DIGITAL CONVERSION
Department of Electronics 83
CAS,calicut
Digital Signal
Processing Lab
ANALOG-TO-DIGITAL CONVERSION
Department of Electronics 84
CAS,calicut
Digital Signal
Processing Lab
Program
clf;
i=input('Enter the number:');
if i<0
f(1)=1;
else
f(1)=0;
ni=abs(i);
m=fix(ni);
x=ni-m;
for i=5:-1:2
f(i)=rem(m,2);
m=fix(m/2);
end
for i=6:1:10
x=x*2;
f(i)=fix(x);
x=x-fix(x);
end
display(f);
Department of Electronics 85
CAS,calicut
Digital Signal
Processing Lab
Input
Output
f=
0 1 1 1 0 1 0 0 1 1
Result
Department of Electronics 86
CAS,calicut
Digital Signal
Processing Lab
DIGITAL-TO-ANALOG CONVERSION
Department of Electronics 87
CAS,calicut
Digital Signal
Processing Lab
DIGITAL-TO-ANALOG CONVERSION
Department of Electronics 88
CAS,calicut
Digital Signal
Processing Lab
Aim
Program
clf;
x=input('Enter binary value');
if x(1)==1
s=-1;
else
s=1;
end
sum=0;
for i=6:10
sum=sum+(x(i))/(2^(i-5));
end
j=0;
for i=5:-1:2
sum=sum+(x(i))*(2^j);
j=j+1;
end
y=sum*s;
display(y);
Department of Electronics 89
CAS,calicut
Digital Signal
Processing Lab
Input
Output
y=
14.5938
Result
Department of Electronics 90
CAS,calicut
Digital Signal
Processing Lab
Department of Electronics 91
CAS,calicut
Digital Signal
Processing Lab
The filters designed by using finite number of samples of impulse response are
called FIR filters. These finite number of samples are obtained from the infinite
duration desired impulse response hd (n) .Here hd(n) is the inverse Fourier Transform of
Hd(ω), where Hd(ω) is the ideal(desired) frequency response. The various methods of
designing FIR filters differs only in the method of determining the samples of h(n) from
the samples of hd(n).
Department of Electronics 92
CAS,calicut
Digital Signal
Processing Lab
4. Round off noise, which is inherent in realizations with finite precision
arithmetic can easily be made small for non-recursive realization of FIR filters.
1. The duration of the impulse response should be large (i.e., N should be large) to
adequately approximate sharp cut-off filter. Hence a large amount of processing
is required to realize such filters when realized via slow convolution.
2. The delay of linear phase FIR filters need not always be an integer number of
samples. This non-integral delay can lead to problems in some signal processing
applications.
To design an FIR LPF for a cut off frequency of 1.5 KHz .Let the order of the
filter is 50.Use window method .Assume the sampling frequency be 8000 samples/sec.
Program
clf;
N=50;
Wn=[1500/4000];
B=fir1(N,Wn);
[p,w]=freqz(B,1,512,8000);
subplot(3,1,1);
plot(w,abs(p)) ;
subplot(3,1,2);
plot(w,angle(p));
subplot(3,1,3);
zplane(B,N);
Department of Electronics 93
CAS,calicut
Digital Signal
Processing Lab
Output
Department of Electronics 94
CAS,calicut
Digital Signal
Processing Lab
Result
To design an FIR HPF for a cut off frequency of 1.5 KHz .Let the order of the
filter be 50.Use window method .Assume the sampling frequency be 8000 samples/sec.
Program
clf;
N=50;
Wn=[1500/4000];
B=fir1(N,Wn,'high');
[p,w]=freqz(B,1,512,8000);
subplot(3,1,1);
plot(w,abs(p));
subplot(3,1,2);
plot(w,angle(p));
subplot(3,1,3);
zplane(B,N);
Department of Electronics 96
CAS,calicut
Digital Signal
Processing Lab
Output
Result
Department of Electronics 97
CAS,calicut
Digital Signal
Processing Lab
To design a FIR band pass filter. Let the order of the filter be 50.Use window
method .Assume the sampling frequency be 8000 samples/sec. Let the cut off
frequencies be 1500 and 3000 respectively.
Program
clf;
N=50;
Wn=[1500/4000,3000/4000];
B=fir1(N,Wn,'band');
[p,w]=freqz(B,1,512,8000);
subplot(3,1,1);
plot(w,abs(p));
subplot(3,1,2);
plot(w,angle(p));
subplot(3,1,3);
zplane(B,N);
Department of Electronics 98
CAS,calicut
Digital Signal
Processing Lab
Output
Result
Department of Electronics 99
CAS,calicut
Digital Signal
Processing Lab
Aim
To design a FIR stop band filter. Let the order of the filter be 50.Use window
method. Assume the sampling frequency be 8000 samples/sec. Let the cut off
frequencies be 1500 and 3000 respectively.
Program
clf;
N=50;
Wn=[1500/4000,3000/4000];
B=fir1(N,Wn,'stop'); % FIR stop band filter design using window
method
[p,w]=freqz(B,1,512,8000); % compute an N-point complex frequency
response
subplot(3,1,1);
plot(w,abs(p)); % magnitude spectrum
subplot(3,1,2);
plot(w,angle(p)); % phase spectrum
subplot(3,1,3);
zplane(B,N); % z-transform in the form of rational function
Output
Result
The filters designed by considering all the infinite samples of impulse response
are called IIR filters. The impulse response is obtained by taking inverse Fourier
Transform of ideal frequency response. The popular methods for such filter design use
the technique of transforming the analog filter to an equivalent digital filter. We know
that the analog filter with transfer function Ha(s) is stable if all its poles lie in the left
half of the s-plane. Consequently, if the conversion technique is to be effective, it
should posses the following desirable properties.
1. The imaginary axis in the s-plane should map into the unit circle in the z-
plane .Thus there will be a direct relationship between the two frequency
variables in the two domains.
2. The left half of the s-plane should map into the interior of the unit circle in the
z-plane. Thus a suitable analog filter will be converted to a stable digital filter.
The IIR filter is a discrete time system that is designed to pass the spectral
content of the input signal in a specified band of frequencies. Based on the frequency
response the filters are classified into four types. They are Low pass, High pass, Band
pass, and Band stop filters.
1. The physically realizable IIR filters does not have linear phase.
2. The IIR filter specifications include the desired characteristics for the magnitude
response only.
Program
clf;
Wp=1000/4000;
Ws=2000/4000;
rp=0.2;
rs=16;
[N,Wn]=buttord(Wp,Ws,rp,rs);
[B,A]=butter(N,Wn,'low');
[p,w]=freqz(B,A,512,8000);
subplot(2,1,1);
plot(w,abs(p));
grid
subplot(2,1,2);
zplane(B,N);
grid
Output
Result
Aim
Program
clf;
Wp=1000/4000;
Ws=2000/4000;
rp=0.002;
rs=20;
[N,Wn]=buttord(Wp,Ws,rp,rs);
[B,A]=butter(N,Wn,'high');
[p,w]=freqz(B,A,512,8000);
subplot(2,1,1);
plot(w,abs(p));
grid
subplot(2,1,2);
zplane(B,N);
grid
Output
Result
Program
clf;
Wp=[300/4000,1000/4000];
Ws=[200/4000,2000/4000];
rp=1;
rs=20;
[N,Wn]=buttord(Wp,Ws,rp,rs);
[B,A]=butter(N,Wn,'bandpass');
[p,w]=freqz(B,A,512,8000);
subplot(2,1,1);
plot(w,abs(p));
grid
subplot(2,1,2);
zplane(B,N);
grid
Output
Result
Program
clf;
Wp=[200/4000,1000/4000];
Ws=[100/4000,2000/4000];
rp=.1;
rs=10;
[N,Wn]=buttord(Wp,Ws,rp,rs);
[B,A]=butter(N,Wn,'stop');
[p,w]=freqz(B,A,256,8000);
subplot(2,1,1);
plot(w,abs(p));
grid
subplot(2,1,2);
zplane(B,A);
grid
Output
Result
In an LTI system the response y(n) of the system for an arbitrary input x(n) is
given by convolution of input x(n) with impulse response h(n) of the system. It is
expressed as
Properties of convolution:
Program
clf;
xco=[.9 -.45 .35 .002];
yco=[1 .71 -.46 -.62];
n=[0:50];
subplot(2,1,1);
ir=impz(xco,yco,n);
stem(n,ir);
subplot(2,1,2);
imp=[1 zeros(1,50)];
y=filter(xco,yco,imp);
stem(n,y);
Output
im
Program
clf;
subplot(3,1,1);
n=[0:50];
xco=[0.4 1 0.6 1 0.4];
yco=1;
y=impz(xco,yco,n);
stem(n,y);
subplot(3,1,2);
x=cos(pi.*n);
stem(n,x);
subplot(3,1,3);
yc=conv(x,y);
n1=[0:length(yc)-1];
stem(n1,yc);
Output
Result
Aim
To write a program to obtain the output y(n) for an LTI causal system whose
impulse response is h(n) = [3 2 1 -2 1 0 -4 0 3] for an input x(n) = [1 -2 3 -4 3 2
1].
Program
clf;
x=[1 -2 3 -4 3 2 1];
h=[3 2 1 -2 1 0 -4 0 3];
n=[0:2*length(x)];
y=conv(x,h);
subplot(2,1,1);
stem(n,y);
yco=1;
ip=[3 2 1 -2 1 0 -4 0 3 0 0 0 0 0 0];
y1=filter(x,yco,ip);
subplot(2,1,2);
stem(n,y1);
Result
CORRELATION
CORRELATION
Suppose that we have two real signal sequences x(n) and y(n) each of which has
finite energy. The crosscorrelation of x(n) and y(n) is a sequence rxy(l), which is defined
as
∞
rxy(l) = ∑ x(n) y(n-l) l=0, 1, 2, … ….. (1)
n =−∞
or, equivalently, as
∞
rxy(l)= ∑ x(n+l) y(n) l=0, 1, 2, … ….. (2)
n =−∞
The index l is the (time) shift(or lag) parameter and the subscripts xy on the
crosscorrelation sequence rxy(l) indicate the sequences being correlated. The order of
the subscripts, with x preceding y , indicates the direction in which one sequence is
shifted, relative to the other. In (1), the sequence x(n) is left unshifted and y(n) is
shifted by l units in time, to the right for l positive and to the left for l negative.
Equivalently, in (2), the sequence y(n) is left unshifted and x(n) is shifted by l units in
time , to the left for l positive and to the right for l negative. But shifting x(n) to the left
by l units relative to y(n) is equivalent to shifting y(n) to the right by l units relative to
x(n). hence the computations (1) and (2) yield identical crosscorrelation sequences.
If we reverse the roles of x(n) and y(n) in (1) and (2) and therefore reverse the
order of the indices xy, we obtain the crosscorrelation sequence
∞
ryx(l)= ∑ y(n) x(n-l) ….. (3)
n =−∞
or, equivalently,
∞
ryx(l)= ∑ y(n+l) x(n) ….. (4)
n =−∞
Department of Electronics 122
CAS,calicut
Digital Signal
Processing Lab
rxy(l)=ryx(-l)
Therefore, ryx(l) is simply rhe folded version of rxy(l), where the folding is done
with respect to l=0. Hence, ryx(l) provides exactly the same information as rxy(l), with
respect to the similarity of x(n) to y(n).
In the special case where y(n)= x(n), we have the autocorrelation of x(n), which
is defined as the sequence
∞
rxx(l)= ∑ x(n) x(n-l)
n =−∞
or, equivalently, as
∞
rxx(l)= ∑ x(n+l) x(n)
n =−∞
N − k −1
rxy(l)= ∑ x(n) y(n-l)
n =i
and
N − k −1
rxx(l)= ∑ x(n) x(n-l)
n =i
Properties:
1. Symmetry
The autocorrelation sequence of a WSS random process is a conjugate
symmetric function of k,
rxx(k)=rxx∗(-k)
For a real process, the autocorrelation sequence is symmetric
rxx(k)=rxx(-k)
Department of Electronics 123
CAS,calicut
Digital Signal
Processing Lab
2. Mean-square value
The autocorrelation sequence of a WSS process at lag k=0 is equal to the mean-
square value of the process
rxx(0)= E{| x(n) | 2} ≥ 0
3. Maximum value
The magnitude of the autocorrelation sequence of a WSS random process at lag
k is upper bounded by its value at lag k=0,
rxx(0) ≥ | rxx(k) |
4. Periodicity
If the autocorrelation sequence of a WSS random process is such that
rxx(k0) = rxx(0)
For some k0, then rx(k) is periodic with period k0. Further more,
E{| x(n) - x(n-k0) | 2}=0
And x(n) is said to be mean-square periodic.
Aim
Program
clf;
n=[0:100];
x=sin(2*pi*0.1*n);
subplot(2,1,1);
stem(n,x);
y=xcorr(x);
subplot(2,1,2);
n1=length(x);
n2=[0:(2*n1)-2];
stem(n2,y);
Output
Result
Aim
Program
clf;
n=[0:50];
x1=sin(2*pi*0.1*n);
subplot(3,1,1);
stem(n,x1);
x2=rand(1,51);
subplot(3,1,2);
stem(n,x2);
y=xcorr(x1,x2);
n1=length(x1);
m=[0:(2*n1)-2];
subplot(3,1,3);
stem(m,y);
Output
Result
Z-TRANSFORM
Z-TRANSFORM
Transform techniques are an important tool in the analysis of signals and linear
time invariant systems. z-transform plays an important role in analysis and
representation of linear discrete time systems. The z-transform provides a method for
the analysis of discrete time systems in the frequency domain which is generally more
efficient than its time domain analysis.
Definition:
The z-transform of a discrete time signal or sequence is defined as the power series
∞
X (z) = ∑xnz
n=
−
∞
() −n
..... (1)
where z is a complex variable.
The sequence of equation (1) is considered to be two sided and the transform is called
two sided z-transform, since the time index n is defined for both positive and negative
values. If the sequence x (n) is one sided sequence, then the z-transform is called one
sided z-transform.
∞
X (z) = ∑x (n)z
n =0
−n
Region of convergence
The z-transform is an infinite power series; it exists only for those values of z for
which the series converges. The region of convergence (ROC), of X (z) is the set of all
values of z, for which X (z) attains finite value. The ROC of a finite-duration signal is
the entire z-plane, except possibly the point z = 0 and/or z = ∞.
The roots of the numerator polynomial are those values of z for which X (z) = 0
and are referred to as the zeros of X (z). Values of z for which X (z) is infinite are
referred to as the poles of X (z).
Aim
Program
clf;
num=[2 5 9 5 3];
den=[5 45 2 1 1];
w=[-4*pi:pi/512:4*pi];
d=freqz(num,den,w);
subplot(4,1,1);
plot(w/pi,abs(d));
subplot(4,1,2);
plot(w/pi,angle(d));
subplot(4,1,3);
zplane(num,den);
subplot(4,1,4);
[z,po,k]=tf2zp(num,den);
zplane(z,po);
Output
Result
Aim
To write a program which generates the pole-zero plot for a given function that
has poles & zeros at the following points.
Zero’s 0, 1+2i, -3.2, 5.7
Poles 0.6, 7+3i, -2.7.
Program
clf;
z=[ 0
1+2i
-3.2
5.7];
p=[0.6
7+3i
-2.7];
zplane(z,p);
Output
Result
The pole-zero plot for the function given its poles & zeros is generated.
Program
clf;
z=[-1.0000+1.4142i
-1.0000-1.4142i
-0.2500+0.6614i
-0.2500-0.6614i];
p=[-8.9576
-0.2718
0.1147+0.2627i
0.1147-0.2627i];
k=0.4000;
[num,den]=zp2tf(z,p,k);
display(num);
display(den);
Output
num =
den =
Result
The numerator & denominator values of the rational function given its zeros (z),
poles (p) and k is generated.
INVERSE Z-TRANSFORM
INVERSE Z-TRANSFORM
The procedure for transforming from the z-domain to the time domain is called
the inverse z-transform.
Where ‘C’ is a counterclockwise closed path encircling the origin and entirely in
the region of convergence (ROC). The contour or path, C, must encircle all of the poles
of X(z).
Aim
Program
clf;
num=[0 1 -1.2 1];
den=[1 -1.3 1.04 -0.222];
[g,t]=impz(num,den);
subplot(2,1,1);
stem(t,g);
subplot(2,1,2);
[g1,t1]=impz(num,den,10,2);
stem(t1,g1);
Output
Result
Program
clf;
syms z;
f=z/(z-2)-z/(z-3);
y=iztrans(f);
display(y);
Output
2^n-3^n
Result
Aim
Program
a=[0 1 0 1 0 1 0 1
10101010
01010101
10101010
01010101
10101010
01010101
1 0 1 0 1 0 1 0];
imshow(a,'notruesize');
Output
Result
To write a program that displays a true-color image and its gray-scale image.
Program
clf;
b=imread('C:\Documents and Settings\King\My Documents\My Pictures\
St.Thomas college.jpg');
subplot(2,1,1);
imshow(b);
i=rgb2gray(b);
subplot(2,1,2);
imshow(i);
Output
Result
Aim
To write a program that displays a true-color image, its gray-scale image and
displaying pixel values of a true-color image.
Program
clf;
b=imread('D:\Department of Electronics\MSc\2006-2008 batch.jpg');
subplot(2,1,1);
imshow(b);
length(b);
b(1:400)
i=rgb2gray(b);
subplot(2,1,2);
imshow(i);
Output
ans =
Columns 1 through 16
213 213 213 213 213 213 213 213 213 213 213 213 214 214 214 214
Columns 17 through 32
Columns 33 through 48
214 214 214 212 213 214 212 214 213 213 213 213 213 213 213 213
Columns 49 through 64
213 213 214 213 213 213 213 212 214 214 213 213 213 213 213 213
Columns 65 through 80
213 213 213 213 213 213 213 213 212 213 214 214 213 213 212 214
Columns 81 through 96
215 215 214 215 216 215 214 215 215 216 215 216 217 217 218 217
217 217 216 217 217 217 215 215 216 216 216 215 215 215 214 214
215 215 214 213 213 213 213 212 213 213 213 213 213 213 213 213
212 212 213 213 213 213 212 212 213 213 213 214 214 214 215 215
213 214 214 214 214 214 213 213 213 213 213 213 213 213 213 213
213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213
213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213
213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213
213 213 213 213 213 213 213 212 212 212 212 212 212 212 212 212
212 211 211 211 211 211 210 210 210 210 208 207 208 209 208 207
207 207 207 207 207 207 207 207 207 207 208 207 208 207 206 206
206 206 206 206 206 206 206 206 206 206 207 208 209 210 211 212
212 213 212 213 213 212 211 210 210 210 209 209 209 209 208 208
208 207 207 206 206 206 206 206 206 206 206 206 206 206 206 206
206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206
206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206
206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206
206 206 206 206 205 205 205 205 204 204 204 205 205 205 205 206
206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206
206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206
Department of Electronics 152
CAS,calicut
Digital Signal
Processing Lab
Result
A true-color image, its gray-scale image and pixel values of the true-color
image are displayed.