You are on page 1of 49

DIGITAL SIGNAL PROCESSING LABORATORY

CMR INSTITUTE OF TECHNOLOGY

DSP LABORATORY REPORT


NAME

VENKATARAGHAVAN.T

USN

1CR07TE057

SEM/BRANCH

5th SEM, TCE

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

LABORATORY CERTIFICATE
This is to certify that Mr.VENKATARAGHAVAN.T
bearing USN 1CR07TE057 has satisfactorily completed the course of
experiments in DIGITAL SIGNAL PROCESSING
Laboratory(06ECL57) prescribed by the VTU for 5th semester BE
TELECOMUNICATION course in the laboratory of this college in
the year 2008 2009.

DATE:

Signature of Teachers in charge

Signature of HOD
(Mrs.Pakia Rani)

Name of the Candidate


Register Number ..
Date of Practical Examination ..

Department of Telecommunication CMRIT

1C

DIGITAL SIGNAL PROCESSING LABORATORY

CONTENTS
MATLAB Programs

I. Verification of Sampling Theorem....1


II. Impulse Response of a given system.......3
III. Calculation of N Point DFT ..5
IV. Linear Convolution of two given finite length sequences.............9
V. Circular convolution of two given finite length sequences.......11
VI. Linear convolution of two given finite length sequences

using DFT-IDFT method............................................................13

VII. Circular convolution of two given finite length sequences

using DFT-IDFT method...........................................................16

VIII. Calculate Auto Correlation and verify its Properties ............19


IX. Calculate Cross Correlation...21
X. Solve a given Difference Equation23
XI. Realization of IIR filters ...25
XII. Realization of FIR filters...32

Code Composer Studio Programs


I.

Calculate the discrete time convolution of the given sequences37

II.

Calculate the circular convolution 38

III.

Impulse Response of system.....40

IV.

Calculate the N-Point DFT .............42

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

QUESTION BANK
MATLAB PROGRAMS
1. Write a MATLAB program to verify the sampling theorem for the following
three cases
Right sampling
Under sampling
Over sampling
2. Write a MATLAB program to determine the impulse response of the given
system whose difference equation is given by: y (n) y (n-1) + 1/8 y (n-2) = x
(n) 1/3 x (n-1).
3. Write a MATLAB program to compute the N-Point DFT of the sequence
x(n) = {1,1,1,1} taking N=4 and N=8.
4. Write a MATLAB program to perform the linear convolution of the sequences
given
x1(n) = {1, 2, 3, 4, 5}
x2(n) = {2, 3, 4}
5. Write a MATLAB program to perform circular convolution of the given
sequences
x1(n) = {1, 2, 3, 4} and x2(n) = {4, 3, 2, 2}
6. Write a MATLAB program to perform linear convolution of the given sequences
x1(n) = {2, 3, 4, 0}
x2(n) = {2, 1, 0, 0} by using DFT and IDFT method.
7. Write a MATLAB program to perform circular convolution of the given
sequences
x1(n) = {1, 2, 3, 4}
x2(n) = {4, 3, 2, 2} by using DFT and IDFT method.
8. Write a MATLAB program to compute the autocorrelation of the given
sequence and verify its properties.
X = {1, 2, 3, 6, 2, 4}
9. Write a MATLAB program to find the cross correlation of the given two
sequences
x = {1, 2, 4, 6, 5} and y = {1, 3, 2, 1, 4}

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

10. Write a MATLAB program to solve the given difference equation


y (n) 3/2 y (n-1) + y (n-2) = x (n). Given x(n) = (1/4)n y(n). Assume initial
conditions as y(-1) = 4,y(-2) = 10.

11. Using MATLAB design an IIR filter with pass band edge frequency at 1500Hz
and stop band edge at 2000Hz for a sampling frequency of 8000Hz.passband
attenuation is 1dB and stop band attenuation is 15dB. Use butter worth
prototype design and bilinear transformation.
12. Using MATLAB design an FIR filter with pass band edge frequency at 1500Hz
and stop band edge at 2000Hz for a sampling frequency of 8000Hz.passband
attenuation is 1dB and stop band attenuation is 15dB. Use chebyshev prototype
design and bilinear transformation.
PART B
Code Composer Studio Programs
1. Find out the Discrete Time Convolution of the sequences x(n)={1 2 3 4 5 6} and
h(n)={1 2 3 4}, using Code Composer Studio. Verify the results graphically.
2. Perform Circular Convolution of the given sequence using Code Composer
Studio.
3. Compute the response of the system whose co-efficients are a0=0.1311, a1=0.2622,
a2=0.1311 and b0=1, b1= -0.7478, b2=0.2722, when the input is a unit impulse
signal.
4.

Compute the DFT of an N Point sequence from a lookup table.

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 1:

Verification of Sampling Theorem


THEORY:
Sampling Theorem can be stated in two formsIf a finite energy signal g(t) contains no frequencies higher than w Hz, it is
completely determined by specifying its ordinates at a sequence of points spaced
1/2w sec apart.
If a finite energy signal g(t) contains no frequencies higher than w Hz, it may be
completely recovered from its ordinates at a sequence of points spaced 1/2w sec
apart.
The minimum sampling rate of 2w samples per second, for a signal bandwidth of wHz
is called NYQUIST RATE.
s = 2m
where,
s=Sampling Frequency
m=Message frequency
We encounter these conditions in Sampling Theorem, they are Right sampling, Under
sampling and Over sampling
.
Right Sampling s = 2m
Under Sampling s 2m
Right Sampling s 2m

PROBLEM:
Using MATLAB, sample a band limited continuous time signal, band limited to fm=400Hz
under the following conditions:
1. Nyquist rate
2. Under sampling
3. Over sampling

MATLAB CODE:
s=800;
t=0:1/ s:256/ s;
X=sin(2*pi*400*t);
Xm=abs(fft(X));
K=0:length(X)-1;
Subplot(2,2,1);
Stem(k,Xm);
xlabel(Hz);
ylabel(Magnitude);
title(Nyquist Rate Sampling);

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

s=200;
t=0:1/ s:256/ s;
X=sin(2*pi*400*t);
Xm=abs(fft(X));
K=0:length(X)-1;
Subplot(2,2,2);
Stem(k,Xm);
xlabel(Hz);
ylabel(Magnitude);
title(Under Sampling);
s=1000;
t=0:1/ s:256/ s;
X=sin(2*pi*400*t);
Xm=abs(fft(X));
K=0:length(X)-1;
Subplot(2,2,3);
Stem(k,Xm);
xlabel(Hz);
ylabel(Magnitude);
title(Over Sampling);
OUTPUT:

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 2:

Impulse Response of a given system


THEORY:
A complete characterization of any LTI system can be represented in terms of its response
to an Unit Impulse, which is referred to as Impulse response of the system. Alternatively,
the impulse response is the output of a LTI system due to an impulse input applied at t=0,
or n=0.

PROBLEM:
Obtain the Impulse response of a system described by the difference equationy(n)-3/4 y(n-1)+1/8 y(n-2) = x(n)-1/3 x(n-1)
Solution:
Y(z) - 3/4 Y(z)z-1 + 1/8 Y(z)z-2 = X(z) - 1/3 X(z) z-1
H(z) = Y(z) / X (z) = (1-1/3 z-1) / (1-3/4 z-1+1/8 z-2)
Applying partial fractions ,
H (z) = (1-1/3 z-1) / (1-1/2 z-1)(1-1/4 z-1) = A / (1-1/2 z-1) + B / (1-1/4 z-1)
A=2/3;
B=1/3;
H(z) = (2/3) / (1-1/2 z-1) + (1/3) / (1-1/4 z-1)
h(n) = (2/3) (1/2)n u(n) + (1/3) (1/4)n u(n)
h(0)=1
h(1)=0.416
h(2)=0.187
h(3)=0.088

h(4)=0.0429
h(5)=0.02116
h(6)=0.0105
h(7)=0.0052

h(8)=0.0026
h(9)=0.0013

MATLAB CODE:
N=input('enter the length of impulse response');
b=input('enter the numerator coeff');
a=input('enter the denominator coeff');
[r,p,k]=residuez(b,a)
[h,t]=impz(b,a,N)
stem(t,h);
grid;
xlabel('time index');
ylabel('amplitude');
title('impulse response');

INPUT:
enter the length of impulse response 5

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

enter the numerator coeff [1 -1/3]


enter the denominator coeff [1 -3/4 1/8]

OUTPUT:
r=
0.6667
0.3333
p=
0.5000
0.2500
k=
[]
h=
1.0000
0.4167
0.1875
0.0885
0.0430
0.0212

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 3:

Computing the N-pt DFT of the given sequence.


THEORY:
The discrete Fourier transform (DFT) is one of the specific forms of Fourier analysis. It
transforms one function into another, which is called the frequency domain
representation, or simply the DFT, of the original function (which is often a function in
the time domain). But the DFT requires an input function that is discrete and whose nonzero values have a limited (finite) duration. Such inputs are often created by sampling a
continuous function
An N-point DFT is expressed as an N-by-N matrix multiplication as X = Wx, where x is
the original input signal, and X is the DFT of the signal.

PROBLEM:
Obtain the N point- dft of the sequence x (n) = {1 1 1 1}
Case1: With N=4
Solution:
1

-j

-1

4
0
=

-1

-1

-1

-j

MATLAB CODE:
x1=input('enter the sequence X1=');
N=input('enter the value of N=');
xk=fft(x1,N)
subplot(2,2,1);
n=0:1:length(xk)-1;
stem(n,abs(xk));
disp(abs(xk));
xlabel('time index');

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

ylabel('amplitude');
title('magnitude plot');
subplot(2,2,2);
stem(n,angle(xk));
disp(angle(xk));
xlabel('time index');
ylabel('amplitude');
title('angle plot');
xk1=ifft(xk,N)
subplot(2,2,3);
stem(n,(xk1));
xlabel('time index');
ylabel('amplitude');
title('inverse fourier transform');
Case2: With N=8
1
0

1
W 20
W2 0

W2 0

-1

-1

W40

-1

1 W 20
W2 0

-1

1
W41

W2 0

-1
-1

1
W20

-1

1
0

1-j

1j2.414

1+j

1-j0.414

W20
1

-1

W8
W8

W8

0
-1
1+j0.414

1-j
W4

W41

-1
-1

1 + j W8

-1
3

-1

1+j2.414

1
-1

INPUT:
Case1:
enter the sequence X1=[1 1 1 1]
enter the value of N=5
Case2:
enter the sequence X1=[1 1 1 1]
enter the value of N=8

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

OUTPUT:
Case1:
xk =
4 0

GRAPH:

Case2:
xk =
Columns 1 through 5
4.0000

1.0000 - 2.4142i

1.0000 - 0.4142i

Columns 6 through 8
1.0000 + 0.4142i

4.0000

0 1.0824

2.6131

0 -1.1781

0 -0.3927

1.0000 + 2.4142i
0
0

1.0824
0.3927

Department of Telecommunication CMRIT

0
0

2.6131
1.1781

DIGITAL SIGNAL PROCESSING LABORATORY

GRAPH:

Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 4:

Linear convolution of two given finite length sequences


THEORY:
DEFINITION OF CONVOLUTION:
The convolution a * b of two functions a and b is defined as:

The output using the convolution operation, is: y(t) = x(t) * h(t)
The asterisk operator means convolution.
LINEAR CONVOLUTION:
Let x1(n) be the input sequence and h(n) be the impulse response of the system as shown
below.i.e
y(n)=x1(n)*h(n)
where y(n) is the output sequence.
h(n)

X1(n)

y(n)

Solution:

1
2

2
4

3
6

4
8

5
10

12

15

12

16

20

Y ( n ) = { 2, 7, 16, 25, 34, 31, 20}

MATLAB CODE:
X1=input('First Sequence');
X2=input('Second Sequence');
subplot(2,2,1);
n1=0:1:length(X1)-1;
stem(n1,X1);
xlabel('Time');
ylabel('Amplitude');
title('First sequence');
subplot(2,2,2);
n2=0:1:length(X2)-1;
stem(n2,X2);
xlabel('Time');
Department of Telecommunication CMRIT

DIGITAL SIGNAL PROCESSING LABORATORY

ylabel('Amplitude');
title('Second Sequence');
Y=conv(X1,X2)
subplot(2,2,3);
n3=0:1:length(Y)-1;
stem(n3,Y);
xlabel('Time');
ylabel('Amplitude');
title('Linear Convolution Output');

INPUT:
first sequence=[1 2 3 4 5]
second sequence=[2 3 4]

OUTPUT:
Y=
2

16

25

34 31

20

GRAPH:

Department of Telecommunication CMRIT

10

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 5:

Circular Convolution
THEORY:
A circular convolution of two functions is defined in terms of the periodic extension of
one or both functions. Periodic extension means a new function is formed by shifting the
original function by multiples of some period, T, and adding all the copies together.

PROBLEM:
For a given two sequences x(n) and h(n) compute the circular convolution.
Solution:
Finding Circular convolution using Matrix method:

Y=

26
25
=

28
31

MATLAB CODE:
x=input('Enter the sequence x(n) ');
h=input('Enter the sequence h(h) ');
N1=length(x);
N2=length(h);
N=max(N1,N2);
N3=N1N2;
if(N3>0)
h=[h zeros(1,N3)];
else
x=[x zeros(1,N3);];
end
disp('Sequence x(n) ');
disp(x);
disp('Sequence h(n) ');
disp(h);
n=1:N;
disp(n);
y=0;
for m=1:N
disp(m);
y=y+x(m)*h(mod(n-m,N)+1)

Department of Telecommunication CMRIT

11

DIGITAL SIGNAL PROCESSING LABORATORY

end
disp('circular covolution');
disp(y);
subplot(2,2,1);
stem(n,x);
title('Sequence x(n)');
xlabel('n');
ylabel('Amplitude');
subplot(2,2,2);
stem(n,h);
title('Sequence h(n)');
xlabel('n');
ylabel('Amplitude');
subplot(2,2,3);
stem(n,y);
title('Sequence y(n)');
xlabel('n');
ylabel('Amplitude');

INPUT:
Enter the sequence x(n) [1 2 3 4]
Enter the sequence h(h) [4 3 2 2]

OUTPUT:
y=

26

25

28

Department of Telecommunication CMRIT

12

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 6:

Linear convolution using DFT IDFT method.


THEORY:
DECIMATION IN TIME FFT:
The decimation in time algorithm uses the divide and conquer approach .Here
the number of points is assumed as a power of 2.That is N=2p
The decimation in time approach is one of breaking the N-point transform into
two N/2 point transform, then breaking each N/2 point transforms into two
N/4 point transforms, and continuing this process until two point DFTs are
obtained.
Example:
DECIMATION IN FREQUENCY FFT:
In this algorithm, the output sequence X [K] is divided into smaller and smaller subsequences in the same manner as in the decimation in time algorithm.

PROBLEM:

Linear convolution using DFT IDFT method.


Solution:
Finding the DFT of the two input sequences:

X1 =

X2 =

-j

-1

-2-3j

-1

-1

-1

-j

-j

-1

2-j

-1

-1

-1

-j

4
-2+3j

1
2+j

Y=X1*X2
Y={27, -7-4j, 3, -7+4j}

Finding IDFT of Y
Department of Telecommunication CMRIT

13

DIGITAL SIGNAL PROCESSING LABORATORY

Y=

27

16

-1

-j

-7-4j

-1

-1

44

-j

-1

-7+4j

16

32

y = 1/4{16, 32, 44, 16}


y = {4, 8, 11, 4}

MATLAB CODE:
x1=input('Enter the sequence x1(n) ');
x2=input('Enter the sequence x2(n) ');
N1=length(x1);
N2=length(x2);
N3=N1+N2-1;
if (N3>N2)
x2=[x2 zeros(1,N3-N2)];
end
if (N3>N1)
x1=[x1 zeros(1,N3-N1)];
end
X1=fft(x1,N3)
X2=fft(x2,N3)
Y=X1.*X2;
y=ifft(Y,N3)
subplot(3,1,1);
n1=0:1:length(x1)-1;
stem(n1,x1);
xlabel('Time');
ylabel('Amplitude');
title('First Sequence');
subplot(3,1,2);
n2=0:1:length(x2)-1;
stem(n2,x2);
xlabel('Time');
ylabel('Amplitude');
title('Second Sequence');
subplot(3,1,3);
n=0:1:length(y)-1;
stem(n,y);
xlabel('Time');

Department of Telecommunication CMRIT

14

DIGITAL SIGNAL PROCESSING LABORATORY

ylabel('Amplitude');
title('Linear Convolution using DFT and IDFT');

INPUT:
Enter the sequence x1(n) [2 3 4 0]
Enter the sequence x2(n) [2 1 0 0]

OUTPUT:
X1 =
Columns 1 through 5
9.0000
2.9804 - 6.2452i -2.2714 - 1.1892i 1.7911 + 1.8257i 1.7911 - 1.8257i
Columns 6 through 7
-2.2714 + 1.1892i 2.9804 + 6.2452i
X2 =
Columns 1 through 5
3.0000
2.6235 - 0.7818i 1.7775 - 0.9749i 1.0990 - 0.4339i 1.0990 + 0.4339i
Columns 6 through 7
1.7775 + 0.9749i 2.6235 + 0.7818i
y=
4.0000

8.0000 11.0000

4.0000

0.0000

Department of Telecommunication CMRIT

0.0000

0.0000

15

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 7:

Circular Convolution using DFT IDFT


THEORY:
DECIMATION IN TIME FFT:
The decimation in time algorithm uses the divide and conquer approach .Here
the number of points is assumed as a power of 2.That is N=2p
The decimation in time approach is one of breaking the N-point transform into
two N/2 point transform, then breaking each N/2 point transforms into two
N/4 point transforms, and continuing this process until two point DFTs are
obtained.
Example:
DECIMATION IN FREQUENCY FFT:
In this algorithm, the output sequence X [K] is divided into smaller and smaller subsequences in the same manner as in the decimation in time algorithm.

PROBLEM:
Write a MatLab program to compute the circular convolution using DFT IDFT method
for the given to sequences.
Solution:
Finding the DFT of the two input sequences:

X1 =

X2 =

10

-j

-1

-2+2j

-1

-1

-1

-j

-2
-2-2j

11

-j

-1

-1

-1

2-j
=
1

-1

-j

2+j

Y=X1*X2
Y={110, -2+6j, -2, -2-6j}
Department of Telecommunication CMRIT

16

DIGITAL SIGNAL PROCESSING LABORATORY

Finding IDFT of Y
1

110

104

-1

-j

-2+6j

-1

-1

-2

112

-j

-1

-2-6j

124

100

y=1/4{104, 110, 112, 124}


y={26, 25, 28, 31}

MATLAB CODE:
x1 = input('Enter the first sequence');
x2 = input('Enter the second sequence');
N1= length(x1);
N2= length(x2);
N=max(N1,N2);
N3=N1-N2;
if(N3>0)
x2=[x2 zeros(1,N3)];
else
x1=[x1 zeros(1,N3)];
end
X1=fft(x1,N)
X2=fft(x2,N)
y=X1.*X2
y=ifft(y,N)
subplot(3,1,1);
n1=0:1:length(X1)-1;
stem(n1,X1);
xlabel('Time');
ylabel('Amplitude');
title('First sequence');
subplot(3,1,2);
n2=0:1:length(X2)-1;
stem(n2,X1);
xlabel('Time');
ylabel('Amplitude');
title('Second sequence');
subplot(3,1,3);
n=0:1:length(y)-1;
stem(n,y);
xlabel('Time');
ylabel('Amplitude');
title('Circular Convolution using DFT and IDFT');

OUTPUT:
X1 = 10.0000

-2.0000 + 2.0000i -2.0000

Department of Telecommunication CMRIT

-2.0000 - 2.0000i

17

DIGITAL SIGNAL PROCESSING LABORATORY

X2 = 11.0000
y=

26

25

2.0000 - 1.0000i 1.0000


28

2.0000 + 1.0000i

31

Department of Telecommunication CMRIT

18

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 8:

Auto Correlation
THEORY:
Autocorrelation is a mathematical tool for finding repeating patterns, such as the presence
of a periodic signal which has been buried under noise, or identifying the missing
fundamental frequency in a signal implied by its harmonic frequencies. It is used
frequently in signal processing for analyzing functions or series of values, such as time
domain signals. Informally, it is the similarity between observations as a function of the
time separation between them. More precisely, it is the cross-correlation of a signal with
itself.

PROBLEM:
Write a MatLab program to compute the auto correlation of the given sequence and verify
its properties.
Solution:
1

8
2

12
4
6

24
6
12
3

8
12
18
6
2

16
4
36
9
4
1

8
12
18
6
2

24
6
12
3

12
4
6

8
2

70

46

45

22

10

10

22

45

46

MATLAB CODE:
x=[1 2 3 6 2 4];
subplot(2,2,1);
n=0:1:length(x)-1;
stem(n,x);
xlabel('n');
ylabel('amplitude');
title('input sequence');
Rxx=xcorr(x,x);
subplot(2,2,2);
nRxx=-length(x)+1:length(x)-1;
stem(nRxx,Rxx);
xlabel('auto correlation axis');
ylabel('auto correlation magnitude');
title('auto correlation output');
%verification of properties
energy=sum(x.^2)
center_index=ceil(length(Rxx)/2)
Rxx_0=Rxx(center_index)

Department of Telecommunication CMRIT

19

DIGITAL SIGNAL PROCESSING LABORATORY

if Rxx_0==energy
disp('Rxx(0) gives energy - proved');
else
disp('Rxx(0) gives energy - not proved');
end
Rxx_right=Rxx(center_index:1:length(Rxx))
Rxx_left=Rxx(center_index:-1:1)
if Rxx_right == Rxx_left
disp('Rxx is even');
else
disp('Rxx is odd');
end

OUTPUT:
energy =

70

center_index =
Rxx_0 =

70

Rxx(0) gives energy - proved


Rxx_right =
Rxx_left =

70
70

46
46

45
45

22
22

10
10

4
4

Rxx is even

Department of Telecommunication CMRIT

20

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 9:

Cross correlation
THEORY:
In signal processing, cross-correlation is a measure of similarity of two waveforms as a
function of a time-lag applied to one of them. This is also known as a sliding dot product
or inner-product. It is commonly used to search a long duration signal for a shorter,
known feature. It also has applications in pattern recognition and cryptanalysis.For
continuous functions f and g the cross-correlation is defined as:

where f * denotes the complex conjugate of f.

PROBLEM:
Compute the cross correlation of the two sequences.
Solution:

16

24

20

12

10

12

18

15

41

31

32

21

3
2
1
4
4

20

35

MATLAB CODE:
X=[1 2 4 6 5]
Y=[1 3 2 1 4]
n = 0:1:length(X)-1;
m = 0:1:length(Y)-1;
subplot(2,2,1);
stem(n,X);
xlabel('n');
ylabel('amplitude');
title('input1 sequence');
subplot(2,2,2);
stem(m,Y);
xlabel('n');
ylabel('amplitude');
Department of Telecommunication CMRIT

21

DIGITAL SIGNAL PROCESSING LABORATORY

title('input2 sequence');
RXY = Xcorr(X,Y)
nRXY = -length(X) +1:length(X)-1
subplot(2,2,3);
stem(nRXY,RXY);
xlabel('crosscorr axis');
ylabel('crosscorr magnitude');
title('crosscorr output');

OUTPUT:
RXY =
4.0000
nRXY =
-4 -3

9.0000 20.0000 35.0000 41.0000 31.0000 32.0000 21.0000 5.0000


-2

-1

GRAPH:

Department of Telecommunication CMRIT

22

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 10:

Solution for difference equation.


Aim: To write the MATLAB code for solving given differential equation
PROBLEM:
where x(n)=

initial condition

y(-1)=4, y(-2)=10.
SOLUTION:
x(n)=
n=0:5
n=0,x(0)=1
n=1,x(1)=0.25
n=2,x(2)=0.0625
n=3,x(3)=0.0156
n=4,x(4)=0.0039
n=5,x(5)=0.0010

n=0
y(0)=1+6-5
=2
n=1
y(1) =.25+3-2
=1.2500
n=2
y(2)=0.625+1.875-1
=0.7969
n=3
y(3)=0.0156+1.40625-0.625
=0.7969
n=4
y(4)=0.0039+1.19535-0.46625
=0.7365

Department of Telecommunication CMRIT

23

DIGITAL SIGNAL PROCESSING LABORATORY

n=5
y(5) =0.0010+1.09575-.3982 = 0.6982

MATLAB CODE:
a=[1 -3/2 1/2]
b=[1]
n=0:5;
xn=(1/4).^n;
disp('values of x(n)');
disp(xn);
y=[4,10];
xic=filtic(b,a,y);
y=filter(b,a,xn,xic);
disp('the solution for difference equation');
disp(y)
OUTPUT:
a = 1.0000 -1.5000

0.5000

b= 1
values of x(n)
1.0000 0.2500 0.0625

0.0156

0.0039

0.0010

the solution for difference equation


2.0000 1.2500 0.9375 0.7969 0.7305

0.6982

Department of Telecommunication CMRIT

24

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 11:

Realization of IIR Filters


THEORY:
A desired frequency response is approximated by a transfer function expressed as a
ratio of polynomials. This type of transfer function yields an impulse response of infinite
duration. Therefore, the analog filters are commonly referred to as infinite impulse
response (IIR) filters The main classes of analog filters are 1.Butterworth Filter.
2.Chebyshev Filter.
These filters differ in the nature of their magnitude responses as well as in their design and
implementation.
BUTTERWORTH FILTERS:
Butterworth filters have very smooth passband, which we pay for with a relatively wide
transition region. A Butterworth filter is characterized by its magnitude frequency
response,
| H(j) | = 1 / [1+(/c)2N]1/2
where N is the order of the filter and c is defined as the cutoff frequency where the filter
magnitude is 1/2 times the dc gain (=0).
log10

Order

10
10

2 log10

K1
10
K2

10

1
1

1
2

H j

N1

N2

N3

N2

N3

Butterworth filter tables


N=1 (s + 1)
N=2 (s^2 +0.5 s^1 +1)
N=3 (s^2 +s+1)(s+1)
N=4 (s^2+0.76536s+1)(s^2+1.864776s+2)

Department of Telecommunication CMRIT

25

DIGITAL SIGNAL PROCESSING LABORATORY

N=5 (s+1)(s^2+0.6180s+1)(s^2+1.6180s+1)
CHEBYSHEV FILTERS:
Chebyshev filters are equiripple in either the passband or stopband. Hence the magnitude
response oscillates between the permitted minimum and maximum values in the band a
number of times depending upon the order of filters. There are two types of chebyshev
filters. The chebyshev I filter is equiripple in passband and monotonic in the stopband,
where as chebyshev II is just the opposite.
The Chebyshev low-pass filter has a magnitude response given by

H j

where

C N2

0.5
c

A is the filter gain


is a constant and
is the 3-dB cutoff frequency
c
The Chebyshev polynomial of the 1st kind of Nth order, CN(x) is given by
cos N cos 1 x ,

for x

1, passband

CN x
cosh N cosh 1 x ,

for x

1, stopband

H j

N odd

Magnitude response of low pass


Chebyshev Filter.

K1

N even
N1 N 2

N 2 N3
K2

N3

The magnitude response has equiripple pass band and maximally flat stop band.
It can be seen that by increasing the filter order N, the Chebyshev response approximates the
ideal response.
The phase response of the Chebyshev filter is more non-linear than the Butter worth filter for a
given filter length N.

Department of Telecommunication CMRIT

26

DIGITAL SIGNAL PROCESSING LABORATORY

PROBLEM:
Using MATLAB design an IIR filter with passband edge frequency 1500Hz and stop band
edge at 2600Hz for a sampling frequency of 8000Hz, variation of gain within pass band
1db and stopband attenuation of 15 db. Use Butterworth prototype design and Bilinear
Transformation.
Design:
W1=(2*pi* F1 )/ Fs = 2*pi*250)/1000 =0.5 rad
W2=(2*pi* F2 )/ Fs = 2*pi*375)/1000 =0.75 rad
Prewarp:
1 = 2/T tan (w1/2)
= 2 rad/sec

T=1sec

2 = 2/T tan (w2/2)


= 4.8rad/sec
Order:
n = log[(10-Ap/10 - 1)/ log[(10-As/10 - 1)
2*log[1 / 2 ]
n = 1.956 = 2
Cut-off Frequency:
c1 = 1 / [(10-Ap/10 - 1)](1/2n)
c1 = 2
c2 = 2 / [(10-As/10 - 1)](1/2n)
c2 = 2.04
Choose c = 2 rad\sec
Normalized Transfer Function:
Hn(s) =

1/[ s2 + 1.4142s + 1]

Denormalized Transfer Function:


H(s)= Hn(s) |s-s/c

H(s)= Hn(s) |s-s/2

H(s) = 1/[(s/2)2 + (s/2) + 1 = 4\[s2 + 2.8484s + 4]


Apply BLT:
H(Z) = H(s)|s=(2/T)[(1-z-1)/(1+z-1)]
H(Z) = 0.29+0.586z-1 +0.29z-2
1+0.172 z-2

Department of Telecommunication CMRIT

27

DIGITAL SIGNAL PROCESSING LABORATORY

MATLAB CODE:
%Butterworth Prototype
Ap=input(enter passband attenuation =);
As=input(enter stopband attenuation =);
fp=input(enter passband frequency =);
fs= input(enter stopband frequency =);
Fs=input(enter sampling frequency=);
W1=2* fp / Fs ;
W2=2* fs / Fs ;
[N,Wn]=buttord(W1,W2,Ap,As)
[b,a]=butter(N,Wn)
freqz(b,a);
title(Butterworth frequency response)

INPUT:
enter passband attenuation =1
enter stopband attenuation =15
enter passband frequency =1500
enter stopband frequency =2000
enter sampling frequency=8000

OUTPUT:
N= 6
Wn = 0.4104
b = 0.0117 0.0699 0.1748 0.2331 0.1748 0.0699 0.0117
a = 1.0000 -1.0635 1.2005 -0.5836 0.2318 -0.0437 0.0043

Department of Telecommunication CMRIT

28

DIGITAL SIGNAL PROCESSING LABORATORY

Chebyshev Filter
PROBLEM:
Using MATLAB design an IIR filter with passband edge frequency 1500Hz and stop band
edge at 2600Hz for a sampling frequency of 8000Hz, variation of gain within pass band 1
db and stopband attenuation of 15 db. Use Chebyshev prototype design and Bilinear
Transformation.
DESIGN:
W1=(2*pi* F1 )/ Fs = 2*pi*100)/4000 =0.05 rad
W2=(2*pi* F2 )/ Fs = 2*pi*500)/4000 =0.25 rad
Prewarp:
1 = 2/T tan (w1/2)

T=1sec

= 0.157 rad/sec
2 = 2/T tan (w2/2)
= 0.828 rad/sec
Order:
=

10-Ap/10 - 1

= 0.765
A= 10-As/20

g=

, A = 1020/20

(A2 - 1) /

r= 2 / 1

A=10

, g = 13.01

r=0.828/0.157 = 5.27 rad\sec

Department of Telecommunication CMRIT

29

DIGITAL SIGNAL PROCESSING LABORATORY

n=log10

(g2-1)

g+

log10{r+

(r2 1) }

n= 1.388
Therefore n= 2.

Cut-off Frequency:
c = p = 1 = 0.157 rad\sec
Normalized Transfer Function:
H(s)=[bo /

1+ 2

] / [ s2+b1s+b0]

= 0.505/[ s2+0.8s+0.036]
Denormalized Transfer Function:
H(s)= Hn(s) |s-s/c

H(s)= Hn(s) |s-s/0.157

H(s) = 0.0125 / [s2+0.125s+0.057]


Apply BLT:
H(Z) = H(s)|s=(2/T)[(1-z-1)/(1+z-1)]
H(Z) = 0.0125+0.025Z-1 + 0.0125 Z-2
4.2769-7.96Z-1 + 3.76Z-2
H(Z) = 0.0029+0.0052Z-1 + 0.0029 Z-2
1-1.86Z-1 + 0.88Z-2

MATLAB CODE:
%Chebyshev Prototype
Ap=input(enter passband attenuation =);
As=input(enter stopband attenuation =);
fp=input(enter passband frequency =);
f s= input(enter stopband frequency =);
Fs=input(enter sampling frequency=);
W1=2* fp / Fs ;
W2=2* fs / Fs ;
[N,Wn]=cheb1ord(W1,W2,Ap,As)
[b,a]=cheby1(N,Ap,Wn)
freqz(b,a);
title(Chebyshev frequency response);

Department of Telecommunication CMRIT

30

DIGITAL SIGNAL PROCESSING LABORATORY

INPUT:
enter passband attenuation =1
enter stopband attenuation =15
enter passband frequency =1500
enter stopband frequency =2000
enter sampling frequency=8000

OUTPUT:
N= 4
Wn = 0.3750
b = 0.0191 0.0764 0.1147 0.0764 0.0191
a=
1.0000 -1.7994 1.9637 -1.1513 0.3301

Department of Telecommunication CMRIT

31

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 12:

Realization of FIR Filters


THEORY:
A linear-phase is required throughout the passband of the filter to preserve the shape of
the given signal in the passband. A causal IIR filter cannot give a linear-phase
characteristics and only special types of FIR filters that exhibit center symmetry in its
impulse response give the linear-space. An FIR filter with impulse response h(n) can be
obtained as follows:
h(n) = hd(n)
=0

0nN-1
otherwise

.(a)

The impulse response hd(n) is truncated at n = 0, since we are interested in causal FIR
Filter. It is possible to write above equation alternatively as
h(n) = hd(n)w(n)

.(b)

where w(n) is said to be a rectangular window defined by


w(n) = 1
0nN-1
=0
otherwise
Taking DTFT on both the sides of equation(b), we get
H() = Hd()*W()

HAMMING WINDOW:
The impulse response of an N-term Hamming window is defined as follows:
wHam(n) = 0.54 0.46cos(2n / (N-1)) 0nN-1
=0
otherwise

PROBLEM:
Using MATLAB design an IIR filter to meet the following specifications choosing
Hamming window:
Window length, N = 27
Stop band attenuation = 50dB
Cut-off frequency = 100 Hz

Department of Telecommunication CMRIT

32

DIGITAL SIGNAL PROCESSING LABORATORY

Sampling frequency = 1000 Hz

MATLAB CODE:
N=input(Enter the window length=);
fc=input(Enter cut-off frequency=);
Fs=input(Enter sampling frequency=);
Wc=2*fc/ Fs;
Wh = (hamming(N))
b = fir1(N-1, Wc ,Wh)
[h,omega] = freqz(b,1,256);
mag = 20*log10(abs(h));
plot(omega/pi,mag);
grid;
xlabel(Frequncy in radians in terms of pi);
ylabel(gain in dB);
title(Response of FIR filter using Hamming window);

INPUT:
Enter the window length=27
Enter cut-off frequency=100
Enter sampling frequency=1000

OUTPUT:
Wh =
0.0800
0.0934
0.1327
0.1957
0.2787
0.3769
0.4846
0.5954
0.7031
0.8013
0.8843
0.9473
0.9866
1.0000
0.9866
0.9473
0.8843
0.8013
0.7031
0.5954
0.4846
0.3769
Department of Telecommunication CMRIT

33

DIGITAL SIGNAL PROCESSING LABORATORY

0.2787
0.1957
0.1327
0.0934
0.0800
b=
Columns 1 through 6
0.0019 0.0023 0.0022 -0.0000 -0.0058 -0.0142
Columns 7 through 12
-0.0209 -0.0185 0.0000

0.0374

0.0890

0.1429

Columns 13 through 18
0.1840 0.1994 0.1840

0.1429

0.0890

0.0374

Columns 19 through 24
0.0000 -0.0185 -0.0209 -0.0142 -0.0058 -0.0000
Columns 25 through 27
0.0022 0.0023 0.0019

Department of Telecommunication CMRIT

34

DIGITAL SIGNAL PROCESSING LABORATORY

GENERAL LAYOUT OF
CODE COMPOSER STUDIO
STEPS TO EXECUTE A PROGRAM USING CCS
1. In the main screen go to project - - > new.. the following screen will appear .
Give a appropriate project name, project type as executable (.out) file.
Select target as TMS320C67xx (depends on the target processor we are using, in our
case its 67xx), and click finish in dialog box finally.
2. Now a new project will be created(e.g. linear.pjt) and a tree will be displayed on
left hand side.
3. Now we need to write C source code . Go to File - - > New - - > Source file.
4. Now a blank screen will appear in which we have to write our C source
code. Type the source code and click save.
5. We have to save the source code file as .c file .When we press save icon in main
screen, following dialog box will open. Save the file as C\C++ source file (e.g. lin.c).
6. Now we have to add 3 files to our project.
C source code (e.g. lin.c).
A library file (e.g. rts6700.lib).
A linker command file (e.g. hello.cmd).
Firstly add the source code file.
Go to Project - - > Add files to project...
Now dialog box will open ,here browse for the source code file in projects directory
and click open .(usually the common paths are ..\ti\MyProjects\projectname\...)
Similarly Add two more files following same procedure .
First, add a library file from path...\ti\c6000\cgtools\lib\rts6700.lib (here file
type is object and library file).
Secondly, add a linker command file from path
\ti\tutorial\dsk6713\hello1\hello.cmd (here the file type is linker
command file *.lcf,*.cmd).
Department of Telecommunication CMRIT

35

DIGITAL SIGNAL PROCESSING LABORATORY

7. After the addition of all three files ,the expanded project tree on left hand side will
resemble this format.(observe the LHS project tree which contains a library file, a
source code and linker command file.)
8. Now we need to compile and build our program.
To compile go to project -- > compile file. Now the project will be compiled and any
errors will be displayed below if there.
To build go to project - - >build. Now project will be built and any errors or warnings
will be displayed below.
9. Now we need to load program in CPU or simulator .
Go to file - - > load program.
A dialog box will open in which we have select executable .out file.
The usual path is \ti\MyProjects\linear\debug\linear.out
10. Now we can assembly equivalent of C source code which the simulator has
generated.
11. To see the output of the program we have to run the program.
Go to Debug - - > Run .
12. Now we can see output of the program below.
If the program needs any user input, a pop-up window will open.
13. Now we need to see the plot of output .
Go to View - - > Graph - - > Time\frequency..
Now a graph property dialog box will open up.
Select Display type = single time.
Graph title = Any name.
Start address = the variable which we want to display.
Index increment= 1(usually).
DSP data type = 32-bit signed integer.
DATA plot style = bar(for displaying discrete values).
Grid style = Full grid.
Now the graph will be displayed.
*We can select different elements by using left and right keys of keyboard.

Department of Telecommunication CMRIT

36

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 1:

Descrete Time Convolution Of Two Sequences


PROBLEM:
Find out the Discrete Time Convolution of the sequences x(n)={1 2 3 4 5}
and h(n)={1 2 3 4}, using Code Composer Studio. Verify the results
graphically.
#include<stdio.h>
int y[20];
main()
{
int m=6;
int n=6;
int i=0,j;
int x[15]={1,2,3,4,5,6,0,0,0,0,0,0};
int h[15]={1,2,3,4,5,6,0,0,0,0,0,0};
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
}
for(i=0;i<m+n-1;i++)
printf("%d\n",y[i]);
}
Input:
x(n)=[1,2,3,4,5,6]
h(n)=[1,2,3,4,5,6]
Output:
y(n)=[1,4,10,20,35,56,70,76,73,60,36]

Department of Telecommunication CMRIT

37

DIGITAL SIGNAL PROCESSING LABORATORY

Department of Telecommunication CMRIT

38

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 2:

Circular Convolution
PROBLEM:
Perform the Circular Convolution of the given sequence using Code Composer
Studio.
#include<stdio.h>
int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30];
void main()
{
printf(" enter the length of the first sequence\n");
scanf("%d",&m);
printf(" enter the length of the second sequence\n");
scanf("%d",&n);
printf(" enter the first sequence\n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf(" enter the second sequence\n");
for(j=0;j<n;j++)
scanf("%d",&h[j]);
if(m-n!=0)
{
if(m>n)
{
for(i=n;i<m;i++)
h[i]=0;
n=m;
}
for(i=m;i<n;i++)
x[i]=0;
m=n;
}
y[0]=0;
a[0]=h[0];
for(j=1;j<n;j++)
a[j]=h[n-j];

for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];

Department of Telecommunication CMRIT

39

DIGITAL SIGNAL PROCESSING LABORATORY

for(i=0;i<n;i++)
{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}
}
printf(" the circular convolution is\n");
for(i=0;i<n;i++)
printf("%d \t",y[i]);
}
INPUT:
Length of first sequence = 4
Length of second sequence = 4
First sequence =[1 2 2 1 ]
Second sequence =[1 2 2 1]
OUTPUT:
The circular convolution is [9 8 9 10]

Department of Telecommunication CMRIT

40

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM 3:

Impulse response of the system


PROBLEM:
Compute the response of the system whose co-efficients are a0=0.1311,a1=0.2622,
a2=0.1311 and b0=1, b1= -0.7478, b2=0.2722, when the input is a unit impulse signal.
#include <stdio.h>
float y[3]={0,0,0};
float x[3]={0,0,0};
float z[10];
float impulse[10]={1,0,0,0,0,0,0,0,0,0};
main()
{
int j;
float a[3]={0.1311, 0.2622, 0.1311};
float b[3]={1, -0.7478, 0.2722};

for(j=0;j<10;j++)
{
x[0]=impulse[j];
y[0] = (a[0] *x[0]) +(a[1]* x[1] ) +(x[2]*a[2]) - (y[1]*b[1])-(y[2]*b[2]);
printf("%f\n",y[0]);
z[j]=y[0];
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1] = x[0];
}
}

INPUT:
Coefficient of x(n)={0.1311,0.2622,0.1311}
Coefficient of y(n)={1,-0.7478,0.2722}

OUTPUT:
0.131100
0.360237
0.364799
0.174741
0.031373
-0.024104
-0.026565
-0.013304

Department of Telecommunication CMRIT

41

DIGITAL SIGNAL PROCESSING LABORATORY

-0.002718
0.001589
GRAPH:

Department of Telecommunication CMRIT

42

DIGITAL SIGNAL PROCESSING LABORATORY

PROGRAM:4

N-point DFT Computation


Problem:
compute the DFT of the given sequence x(n)={1,3,2,4,1,6,4,7} using code composer.
//DFT_logic: DFT of N-point from look up table ,output is viewed from watch window
#include<stdio.h>
#include<math.h>
void main(void) // DFT main function
{
short N=8;
short x[8]={1,3,2,4,1,6,4,7}; //Test data
float pi=3.1416;
float sumre=0,sumim=0;
float cosine=0,sine=0;
int i=0,k=0,n=0;
for(k=0;k<N,k++)
{
sumre=0;
sumim=0;
for(n=0;n<N;n++)
{
cosine=cos(2*pi*k*n/N); //Real
sine=sin(2*pi*k*n/N);
//imaginary
sumre=sumre+x[n]*cosine;
sumim=sumim-x[n]*sine;
}
out_real[k]=sumre;
out_img[k]=sumim;
Printf([%d] %7.3f %7.3f\n,k,out_real[k],out_img[k]);
}
}

Input:

Output:

Department of Telecommunication CMRIT

43

DIGITAL SIGNAL PROCESSING LABORATORY

Department of Telecommunication CMRIT

44

You might also like