0% found this document useful (0 votes)
216 views55 pages

DSP Lab 2014 15

Digital Signal Processing

Uploaded by

Eddie Nash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views55 pages

DSP Lab 2014 15

Digital Signal Processing

Uploaded by

Eddie Nash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.

Kale

ET317: Digital Signal Processing


Laboratory

DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGINEERING


Nagar Yuwak Shikshan Santhas
YESHWANTRAO CHAVAN COLLEGE OF ENGINEERING,
(An autonomous institution affiliated to Rashtrasant Tukadoji Maharaj Nagpur University,
Nagpur)
NAGPUR 441 110
2014-2015

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 1

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

General Instructions to the student


a. Strictly follow the following guideline to prepare Lab Report
1. First page of lab report-1
2. Lab manual of DSP-Lab-1
3. Lab report published by MATLAB
4. Last page of lab report-1
b. Strictly follow the following guideline to save the m-file of MATLAB
1. Use only characters, numbers and only special character _ (underscore) to save the mfile.
1&lab.m is incorrect
2. The file name should always start with character. e.g. 1.m is incorrect
3. Dont give space between file name e.g. lab one.m is incorrect
4. Dont use keywords of MATLAB and MATLAB built in functions to save the file. E.g. sin.m,
conv.m, min.m, for.m are incorrect.
Save your file with the name Lab0X_RollNo.m, Where X is Lab number.
Be sure to place semicolons wherever appropriate, to suppress unnecessary console output, such as
when loading images into memory, or operating on them.
Please include comments at the top of each m-file. The comments should contain at least the following:
your name, your roll number, your contact email, and Lab number.
You should submit your zipped m-files and lab report via the moodle account (www.et.ycce.in).
Prelab Instructions:
Prior to beginning this lab, you must have:
Carefully read over this lab in order to plan how to implement the tasks assigned. Make sure you
are aware of all the items you need to include in your lab report.
Keep the printed copy of this lab handout with you while coming to the Laboratory.
Please note that there are no prelab questions. The questions at the end of this lab must be answered
and submitted with the lab report.
Deliverables
You must be present and conducting the practical assigned to you during the scheduled lab session.
Please prepare a soft and hard copy of Lab report and submit your reports by the end of the lab.

Discussion of the Lab assignment is encouraged, but you may not share
code.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 2

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 01: Sampling and Reconstruction of Signal


Objectives:
This lab addresses concept of sampling, aliasing; and reconstruction of continuous time signal form
discrete time signal.

Introduction and Background:


The purpose of this lab is to illustrate the properties of continuous and discrete-time signals using
digital computers and the Matlab software environment. A continuous-time signal takes on a value
at every point in time, whereas a discrete-time signal is only defined at integer values of the time
variable. However, while discrete-time signals can be easily stored and processed on a computer, it
is impossible to store the values of a continuous-time signal for all points along a segment of the
real line. In later labs, we will see that digital computers are actually restricted to the storage of
quantized discrete-time signals. Such signals are appropriately known as digital signals. How then
do we process continuous-time signals?

In this lab, we will show that continuous- time signals may be processed by first approximating
them by discrete-time signals using a process known as sampling. We will see that proper selection
of the spacing between samples is crucial for an efficient and accurate approximation of a
continuous-time signal. Excessively close spacing will lead to too much data, whereas excessively
distant spacing will lead to a poor approximation of the continuous-time signal. Sampling will be an
important topic in future labs, but for now we will use sampling to approximately compute some
simple attributes of both real and synthetic signals.The lab handouts contain a short introduction to
the required theory. More details, as well as several concrete C and MATLAB implementations,
may be found in the book [1,2], which may be freely downloaded from the web page:
http://www.et.ycce.in

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 3

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Continuous-Time vs. Discrete-Time:


In the introduction part we have mentioned the important issue of representing continuous-time
signals on a computer. In the following sections, we will illustrate the process of sampling, and
demonstrate the importance of the sampling interval to the precision of numerical computations.

Displaying Continuous-Time and Discrete-Time Signals in Matlab:


See the help on the Following Functions of MATLAB
plot
stem
subplot
It is common to graph a discrete-time signal as dots in a Cartesian coordinate system. This can be
done in the Matlab environment by using the stem command. We will also use the subplot
command to put multiple plots on a single figure. Start Matlab on your workstation and type the
following sequence of commands.
=======================================================================
n = 0:2:60;
y = sin(n/6);
subplot(3,1,1);
stem(n,y)
=======================================================================
This plot shows the discrete-time signal formed by computing the values of the function sin(t/6) at
points which are uniformly spaced at intervals of size 2. Notice that while sin(t/6) is a continuoustime function, the sampled version of the signal, sin(n/6), is a discrete-time function.
A digital computer cannot store all points of a continuous-time signal since this would
require an infinite amount of memory. It is, however, possible to plot a signal which looks like a
continuous-time signal, by computing the value of the signal at closely spaced points in time, and
then connecting the plotted points with lines. The Matlab plot function may be used to generate
such plots.
Use the following sequence of commands to generate two continuous-time plots of the
signal sin(t/6).
=======================================================================
n1 = 0:2:60;
z = sin(n1/6);
subplot(3,1,2)

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 4

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale

plot(n1,z)
n2 = 0:10:60;
w = sin(n2/6);
subplot(3,1,3)
plot(n2,w)
=====================================================================
=======================================================================
As you can see, it is important to have many points to make the signal appear
appear smooth.But
smooth.
how
many points are enough for numerical calculations? In the following sections we will examine the
effect of the sampling interval on the accuracy of computations.

Sampling and Reconstruction of signal:


Most signals of Practical interest,
interest, such as speech, biological signals, seismic signals, radar
signals, sonar signals, an various communication signals such as audioaudio-video signals, are analog.
Hence, to process analog signals by digital means, it is first necessary to convert them onto digital
form, that is, to convert them to a sequence of numbers having finite precision. This process is
called analog to digital (A/D) conversion and the corresponding devices are called A/D converters
(ADCs).
In order to process analog signals using digital
digital hardware, we must first convert them into
discrete-time
time signals by sampling. The processed discrete-time
discrete time signal is usually converted back to
analog form by interpolation, resulting in a reconstructed analog signal xr(t).
The Discrete time sequence is developed by uniformly sampling a continuous time signal xa(t), as
illustrated in figure below.

Department of Electronics and Telecommuni


Telecommunication Engineering, YCCE, Nagpur

Page 5

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale

The relation between two signals is given by

x[n] = xa (t ) t = nT = xa (nT ),

n = ......., 2 , 1, 0, 1, 2,...... (a )

Where the time variable t of the continuous time signal is related to the time variable n of the
discrete time signal only at the discrete time instants tn given by

tn = nT =

With

FT =

1
T

n 2 n
=
FT T

denoting the sampling frequency and

(b)

T = 2 FT

denoting the sampling

angular frequency. For example,


xample, if continuous time signal is

xa (t ) = A cos(2 f 0t + ) = A cos(0t + )
Department of Electronics and Telecommuni
Telecommunication Engineering, YCCE, Nagpur

(c )
Page 6

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

The corresponding discrete time signal is given by

x[n] = A cos(0 nT + )
2 0

= A cos
n +
T

= A cos (0 n + )
Where

0 =

2 0
= 0T
T

Is the normalized digital angular frequency of the discrete time signal x[n]. The unit of the
normalized digital angular frequency w0 is radians per sample, while the unit of the normalized
analog frequency 0 is radians per second and the unit of the analog frequency f0 is hertz if the unit
of the sampling period is in seconds.
Example: The continuous-time signal x(t ) = cos(200 t ) is used as the input for a CD converter
with the sampling period 1/300 s. Determine the resultant discrete-time signal x[ n] .

According to equation (a)

2 n
x[n] = x(nT ) = cos(200n t ) = cos
,
3

n = ...., 1, 0,1, 2,...

The frequency in x (t ) is 200 radian/s while that of x[ n] is

2
radians/sample.
3

Q.1) Write a MATLAB code for the above example to convert the analog signal into a discrete
time signal.
Q.2) Repeat the Q.1 for Fs=100, Fs=200, Fs=300, and Fs=400 and write your conclusion.

Example: Ambiguity in the Discrete-Time Representation of Continuous-Time Signals.


Consider the three sequences generated by uniformly sampling the three cosine functions of
frequencies 3 Hz, 7 Hz, and 13 Hz, respectively:

x1 (t ) = cos(6 t ), x2 (t ) = cos(14 t ), & x3 (t ) = cos(26 t ) with a sampling rate of 10 Hz; that


is with T= 0.1 sec. The derived sequences are therefore:

x1[ n ] = cos(0.6 n),

x2 [ n] = cos(1.4 n),

x3 [ n] = cos(2.6 n)

Plots of these sequences and their parent time functions are given in Figure below.
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 7

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale

Figure a:: Ambiguity in the discrete time representation of Continuous


Continuous-Time signals.

Figure b: Ambiguity in the discrete time representation of Continuous


Continuous-Time signals.

Department of Electronics and Telecommuni


Telecommunication Engineering, YCCE, Nagpur

Page 8

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Figure (a+b): Ambiguity in the discrete time representation of Continuous-Time signals.


Note from these plots that each sequence has exactly the same sample value for any given n. This
also can be verified by observing that

x2 [n] = cos(1.4 n) = cos((2 0.6 )n) = cos(0.6 n)


x3[n] = cos(2.6 n) = cos((2 + 0.6 )n) = cos(0.6 n)
As a result, all three sequences above are identical, and it is difficult to associate a unique
continuous time function with any one of these sequences. In fact, all cosine waveform of
frequencies given by (10k 3) Hz with k being any nonnegative integer, lead to the sequence

x1[ n ] = cos(0.6 n) when sampled at a rate of 10 Hz.


In the general case, the family of continuous time sinusoids

xa ,k (t ) = A cos((0t + ) + k T t ), k = 0, 1, 2,....
Leads to identical sampled signals:

2 (0 + k T )n

xa , k (nT ) = A cos(( 0 + k T )nT + ) = A cos


+
T

2 0 n

= A cos
+ = A cos (0 n + ) = x[n]
T

The above phenomenon of a continuous time sinusoidal signal of higher frequency acquiring the
identity of a sinusoid sequence of lower frequency after sampling is called aliasing.
Since there are an infinite number of continuous time functions that can lead to given sequence
when sampled periodically, additional conditions need to be imposed so that the sequence

{ x[ n]} = { xa ( nT )} can uniquely represent the parent continuous time function xa (t ) . In which
case, xa (t ) can be fully recovered from knowledge of { x[ n ]} .

Viva Questions:1.
2.
3.
4.
5.
6.

What are the advantages of DSP?


Give applications of digital signal processing?
What is meant by discrete-time signal?
What do you mean by sampling?
How will you relate u(n) with (n)?
What is the effect of varying the value of a in exponential sequence?

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 9

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 02: Illustration of Aliasing


Consider the continuous time signal

va (t ) = 6 cos(60 t ) + 3sin(300 t ) + 2cos(340 t ) + 4 cos(500 t ) + 10sin(600 t )


Determine the discrete time signal v[ n] obtained by uniformly sampling a continuous time signal

va (t ) composed of a weighted sum of five sinusoidal signals of frequencies 30 Hz, 150 Hz,
170Hz, 250 Hz, and 330 Hz, at a sampling rate of 200 Hz.
Solution:
The sampling period T = 1 / 200 = 0.005sec . Hence, the generated discrete time signal v[ n] is
given by

v[n] = 6 cos(0.3 n) + 3sin(1.5 n) + 2cos(1.7 n) + 4cos(2.5 n) + 10sin(3.3 n)


= 6cos(0.3 n) + 3sin((2 0.5 )n) + 2cos((2 0.3 )n) + 4cos((2 + 0.5 )n)
+ 10sin((4 0.7 )n)
= 6cos(0.3 n) 3sin(0.5 n) + 2 cos(0.3 n) + 4 cos(0.5 n) 10sin(0.7 n)
As can be seen from above, the components

3sin(1.5 n), 2 cos(1.7 n), 4 cos(2.5 n) & 10sin(3.3 n)

have

been

aliased

into

three

components 3sin(0.5 n), 2 cos(0.3 n), 4 cos(0.5 n) & 10sin(0.7 n) , resulting in a discrete
time sequence

v[ n] = 8 cos(0.3 n) + 5 cos(0.5 n + 0.6435) 10 sin(0.7 n)


Composed

of

only

three

sinusoidal

sequences

of

normalized

angular

frequencies:

0.3 , 0.5 , 0.7 .

It should be noted that an identical discrete time signal is also generated by uniformly sampling at
a of 200 Hz sampling rate the following continuous time signal composed of a weighted sum of
three sinusoidal continuous time signals of frequencies 30 Hz , 50 Hz , 70 Hz.

wa (t ) = 8cos(60 t ) + 5cos(100 t + 0.6435) 10sin(140 t )


Another example of a continuous time signal generating the same discrete time sequence is

wa (t ) = 2 cos(60 t ) + 4cos(100 t ) + 10sin(260 t ) + 6cos(460 t ) + 3sin(700 t )


Which is composed of a weighted sum of five sinusoidal continuous time signals of frequencies
30 Hz , 50 Hz , 130 Hz , 30 Hz , 230 Hz & 350 Hz , respectively.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 10

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

From the above discussion it follows that if T > 20 , then the corresponding
normalized digital frequency

0 of the discrete time signal x[ n] obtained by sampling

the parent continuous time signal xa (t ) will be in the range , implying no


aliasing.

On the other hand if T < 20 , the normalized digital frequency will fold into a lower
digital frequency

0 in the range because of aliasing.

0 is given by 2 0 / T modulo 2 .

The value of

Hence, to prevent aliasing, the sampling frequency T should be greater than 2 times the
frequency 0 of the sinusoidal being sampled.

The condition to be satisfied by the sampling frequency to prevent aliasing is called the
sampling theorem.

Q.1) Verify the above example with help of MATLAB. And also
write a Matlab script for converting the discrete time signal
back to the continuous time signal.
Hint: For converting the D. T. Signal in to a C. T. Signal use the following interpolation formula.

x(t ) = x[ n]sin c ( f s (t nT ) ) , Where T =

1
fs

For example:
%=================================================================
clc; close all; clear all;
n=-10:30; %add 40 past and future samples
x=cos(pi.*n./4); %Discrete Time Signal
T=1/1000; %sampling interval is 1/1000
for l=1:2000 %observed interval is [0,0.02]
t=(l-1)*T/100;%successive sample separation is 0.01T
h=sinc((t-n.*T)./T);
xr(l)=x*h.'; %approximate interpolation of (4.11)
end
plot(xr)
%=================================================================
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 11

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 03: Generation of Discrete Time Signal


Types of Sequences

We use several elementary sequences in digital signal processing for analysis purposes. Their
definitions and MATLAB representations are given below.

1. Unit sample sequence:


1, n = 0
= {....0, 0,1, 0, 0, 0,.....}
0, n 0

( n) =

The logical relation n==0 is an elegant way of implementing (n). For example MATLAB code to
implement

1, n = n0
0, n n0

(n n0 ) =

Over the interval n1 n n2, we will use the following MATLAB function:
=================================================================
% Function to generate x(n)=delta(n-no), n1<=n<=n2
function [x,n]=impseq(n0,n1,n2)
n=n1:n2;
x=(n==n0);
end
=================================================================
MATLAB code to generate impulse sequence at a specified location n0 in the range between n1
and n2
Write down the above code in m-file and run the MATLAB code and execute it for
(i ) (n 4) 3 n 5
(ii ) (n + 2)

5 n 4

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 12

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

%================================================================
clc; clear all; close all;
n1=-3;
n2= 5;
n0=4;
[x,n]=impseq(n0,n1,n2);
stem(n,x);
xlabel('samples n');
ylabel('Amplitude');
title('Unit impulse Sequence');

%================================================================

Same can be done by

>> x,n]=impseq(4,-3,5);
>>stem(n,x)

2.

Unit step sequence:

1, n 0
u ( n) =
= {....0, 0,1,1,1,1,1,1.....}
0, n < 0
In MATLAB the function ones(1,N) generates a row vector of N ones. It can used to generates u(n)
over a finite interval.But an elegant way is to use logical relation n0.
Write a MATLAB Code to implement

1, n n0
u (n n0 ) =
0, n < n0
Over the interval n1 n n2, we will use the following MATLAB function:

% Function to generate x(n)=step(n-no), n1<=n<=n2


function [x,n]=stepseq(no,n1,n2)
n=n1:n2;
x= (n>=0);
end
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 13

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Write MATLAB code to generate following Unit step sequences at a specified location n0 in the
range between n1 and n2 using function given above
(i ) u (n 2) 0 n 10
(ii ) u (n + 3)

3.

5 n 5.

Unit ramp sequence

n, n 0
ur ( n) =
= {....0, 0,1, 2,3, 4,5, 6.....}
0, n < 0
In MATLAB the function ones(1,N) generates a row vector of N ones. It can used to generates ur(n)
over a finite interval.But an elegant way is to use logical relation n0.
Write a MATLAB Code to implement

n, n n0
ur (n n0 ) =
0, n < n0

over the n1n0 n2.

% Function to generate unit ramp sequence


function [x,n]=rampseq(N)
n=0 :1 :N-1;
x=n ;

Lab Exercise:Generate and plot each of the following sequences over the interval.

(i ) x(n) = 2 (n + 2), 5 n 5.
(ii ) x(n) = (n 4), 5 n 5.
(iii) x(n) = 2 (n + 2) (n 4), 5 n 5.
(iv ) x ( n ) = u ( n ), 0 n 20.

(ii ) x (n) = u (n 10), 0 n 20.


(ii ) x ( n) = n [ u ( n) u ( n 10) ] , 0 n 20.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 14

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 04: Generation of Discrete Time Signal


1. Real-valued exponential sequence:

x(n) = a n , n; a
In MATLAB an array operator .^ is required to implement a real exponential sequence.
Example, x(n) = (0.9)n,

0 n 10,

%=================================================================
n=0:10;
x=(0.9).^n;
stem(n,x);
xlabel('samples n');
ylabel('Amplitude');
title('Unit ramp Sequence');
%=================================================================
Exercise 1. Take a=0.9, a=-0.9, a=1.2, a = -1.2 and plot all four sequences in single figure
window using subplot command.

2. Complex-valued exponential sequence:

x(n) = e( + j0 ) n , n
Where is called an attenuation and 0 is the frequency in radians. A MATLAB function exp is
used to generate exponential sequence.
(0.04+ j 0.9) n

Example: x(n) = e

0 n 10

n= [0:100];
x=exp ((0.04+0.9j)*n);
stem(n,x);
xlabel('samples n')
ylabel('x(n)')
title('Complex exponential signal')

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 15

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

3. Sinusoidal sequence :

x(n) = cos(0 n + ),

for all n

Where, is the phase in radians. A MATLAB function cos or sin is used to generate sinusoidal
sequences. Write a MATLAB script to generate sinusoidal sequence
(i)

x(n) = 3cos(0.1n+/3) , 0 n 10

(ii)

x(n) = 2sin(0.5n), 0 n 10

(iii)

4. Random sequences:
Many practical sequences can't be described by mathematical expressions. Those sequences are
called random (stochastic) sequences and are characterized by parameters of the associated
probability density function or their statistical moments. In MATLAB there are two types of
random sequences. The rand(1,N) generates a length N random sequence whose elements are
uniformly distributed between [0,1]. The randn(1,N) generates a length N Guassian random
sequence with mean 0 and variance 1.

Example: Generate two discrete-time signals called sig1 and sig2 of length 1, 000. The samples of
sig1 should be independent, Gaussian random variables with mean 0 and variance 1. The samples
of sig2 should be independent, Gaussian random variables with mean 0.2 and variance 1. Use the
Matlab command random or randn to generate these signals, and then plot them on a single figure
using the subplot command. (Recall that an alternative name for a Gaussian random variable is a
normal random variable.)
Next form a new signal avg1 of length 1,000 such that avg1 is the average of the
vector sig1(1:n) (the expression sig1(1:n) returns a vector containing the first n elements of
sig1). Similarly, compute ave2(n) as the average of sig2(1:n). Plot the signals avg1(n) and
avg2(n) versus n on a single plot. Refer to help on the Matlab plot command for information on
plotting multiple signals.

Exercise: Generate the following signal

( i ) x ( n ) = e ( 0.1+ j 0.3) n , 10 n 10
(i ) x ( ni ) = 10e 0.3( n 10) [ u ( n 10) u ( n 20) ] , 0 n 20

(iii ) x(n) = cos(0.04 n), 0 n 50.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 16

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 04: Operations on Discrete Time Signals


Objectives:
To perform various operations such as addition, multiplication, folding, shifting and convolution of
discrete time signals.

Introduction and Background:


The purpose of this lab is to perform various operations such as addition, multiplication, folding,
shifting and convolution of discrete time signals using digital computers and the Matlab software
environment. In theory classes you have studied various discrete time signals and also you have
generated those signals in the Lab-2. Now, its time to process those signals by using various
operations. In this lab we have provided some user defined functions for various operations; you
will have to understand these functions and use them to solve the lab exercise.
The lab handouts contain a short introduction to the required theory. More details, as well as several
concrete C and MATLAB implementations, may be found in the book [1,2], which may be freely
downloaded from the web page: http://www.et.ycce.in

1) Signal addition:
This is sample by sample addition given by
{x1(n)} + {x2(n)} ={x1(n) + x2(n)}
In MATLAB it is implemented by the array operator +. However length of both the signals must
be same. If the length is not same or if sample position are different for equal length sequences,
then + operator can not be used. First x1(n) and x2(n) are to be augmented so that they have same
position vector n.
In particular logical operation & and relational operator like <= and = = are required
to make x1(n) and x2(n) of equal length. The following function demonstrates these operations.
% Function to Add two discrete time signals
function [y,n]=sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1((n>=min(n1))&(n<=max(n1))==1)=x1;
y2((n>=min(n2))&(n<=max(n2))==1)=x2;
y=y1+y2;
%____________________________________________________________________
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 17

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Exercise 1: Let x1[ n ] = [1, 2 , 3, 4 , 5, 6 , 7, 6 , 5, 4]

for 2 n 7 and

x2 [n] = [1, 2, 0, 4, 5, 2, 7, 6, 8, 4, 3] for -3 n 7 Determine and plot


the following sequences.
y[n] = x1[n] + x2 [ n]

Solution:
%____________________________________________________________________
% The solution to the above exercise will be as given below
%____________________________________________________________________
clc; close all; clear all;
n1=-2:7; % range of n1
x1=[1 2 3 4 5 6 7 6 5 4];% sample values of x1
n2=-3:7; % range on n2
x2=[-1 2 0 4 5 -2 7 6 8 4 -3];% sample values of x1
% call the sigadd function
[y,n]=sigadd(x1,n1,x2,n2);
% plot the first sequence x1
subplot(2,2,1);stem(n1,x1)
title('First seq. x1(n)');
xlabel('sample');
ylabel('amplitude');
% plot the second sequence x1
subplot(2,2,2);stem(n2,x2)
title('Second seq. x2(n)');
xlabel('sample');
ylabel('Amplitude');
% plot the output sequence y
subplot(2,1,2);stem(n,y)
title('addition of signal')
xlabel('sample');
ylabel('Amplitude');
%____________________________________________________________________

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 18

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

First seq. x1(n)

Second seq. x2(n)

10

Amplitude

amplitude

6
4
2
0
-5

10

-5
-5

sample

10

sample
addition of signal

15

Amplitude

10
5
0
-5
-3

-2

-1

2
sample

In MATLAB it is implemented by the array operator

.* .

2) Signal Multiplication:
This is sample by sample multiplication given by
{x1(n)}.{x2(n)}={x1(n)x2(n)}

Write a function for the multiplication of two sequences x1(n) and x2(n).
%____________________________________________________________________
% function to multiply two discrete time signals
%____________________________________________________________________
function [y,n]= sigmul(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1((n>=min(n1))&(n<=max(n1))==1)=x1;
y2((n>=min(n2))&(n<=max(n2))==1)=x2;
y=y1.*y2;
%____________________________________________________________________

Exercise 2: Let x1[ n ] = [1, 2 , 3, 4 , 5, 6 , 7, 6 , 5, 4]

for 2 n 7 and

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 19

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

x2 [n] = [1, 2, 0, 4, 5, 2, 7, 6, 8, 4, 3] for -3 n 7 Determine and plot


the following sequences.

y[n] = { x1[n] } x { x2 [n]}

3) Scaling:
In this operation each sample is multiplied by a scalar .
{x(n)}={ x(n)}
An arithmetic operator * is used to implement the scaling operation in MATLAB.

4) Shifting
In this operation each sample of x(n) is shifted by an amount k to obtain a shifted sequence y(n).
y(n}={ x(n-k)
If we let m = n k then n = m + k and above operation is given by
y(m+k)={x(m)}
Write a MATLAB function to shift the given sequence by k amount.
%____________________________________________________________________
% function to shift the discrete time signal
%____________________________________________________________________
function [y,n]=sigshift(x,m,k)
n = m+k;
y = x;
%____________________________________________________________________

Exercise 3: Let x[ n ] = [ 1, 2 , 3, 4 , 5, 6 , 7, 6 , 5, 4]

for 2 n 7

Determine and plot the following sequences.


1) y[n] = x[n 3]

2) y[n] = x[4 + n]

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 20

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

5) Folding
In this operation each sample of x(n) is flipped around its origin n=0 to obtained folded sequence y(n).
y(n)={x(-n)} Hint: Use fliplr( ) function.
%____________________________________________________________________
% function to fold the discrete time signal
%____________________________________________________________________
function [y,n] = sigfold(x,n)
y = fliplr(x);
n = -fliplr(n);
%____________________________________________________________________

Exercise 4: Let x[ n ] = [ 1, 2 , 3, 4 , 5, 6 , 7, 6 , 5, 4]

for 2 n 7

Determine and plot the following sequences.


1) y[n] = x[n]

2) y[n] = x[n + 4] 3) y[n] = x[4 n]

Lab Exercise
Let

x ( n ) = {1, 2,3, 4, 5, 6, 7, 6, 5, 4,3, 2,1}

Determine and plot the following sequences.

1. x1 ( n) = x (3 n)

2. x2 (n) = 2 x(n 5) 3 x(n + 4)


3. x3 (n) = x (n) x (n 2)

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 21

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 05: Convolution


The process of computing the convolution between x(k) and h(k) involves the following four steps,
1. Folding: fold h(k) about k=0 to obtain h(-k).
2. shifting: shift h(-k) by n0 to the right(left) if n0 is positive(negative),to obtain(n0-k)
3. Multiplication: multiply x(k) by h(n0-k) to obtain the product sequence

vn0 [ k ] = x[ k ]h[ n0 k ]
4. Summation: Sum all the values of the product sequence vn0(k) to obtain the value of the output at
time n=n0
This procedure results in the response of the system at a single time instant, say n=n0.in general we are
interested in evaluating the response of the system over all time instants
- <n< .consequently steps 2 through 4 must be repeated, for all possible time shifts - <n<
Note: In MATLAB the function conv is used to find the convolution of discrete time signals as given
below
>>

x = [ 3, 11, 7,

>>

h = [

>>

y = conv(x,h)

2,

y = 6 31

3,

47

0,
0,

-1,

-5 ,

-51

4,
2,

-5

2];
1];

41

18

-22

-3

However the conv function neither provides nor accepts any timing information. What is needed is a
beginning point and an end point of y(n). So a correction is needed.
Let

{x(n); nxb n nxe and h(n); nhb n nhe }


Be two finite duration sequence then beginning point and an end point of y(n) will be

n yb = nxb + nhb

and n ye = nxe + nhe

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 22

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

The convolution function is modified as


%____________________________________________________________________
% user defined function to find the convolution of discrete time
signals
%____________________________________________________________________
function [y, n]=conv_m(x,nx,h,nh)
n1=min(nx)+min(nh);
n2= max(nx)+max(nh);
n=nyb:nye;
y=conv(x,h);
%____________________________________________________________________

Exercise 1:

Let x ( n) = {3, 4, 6, 2, 4}; 2 n 2 and h( n) = {1, 2, , 3, 0, 5, 4,1, 7} 3 n 4

Determine the convolution of x(n) and h(n)

Exercise 2 Determine h(n) for the following interconnection system when


1 1 1
h1 (n) = , , , h2 ( n) = h3 ( n ) = ( n + 1)u ( n) , h4 ( n) = ( n 2) ,
2 4 2

0 n 10

h2(n)
y(n)

x(n)
h1(n)

h3(n)

h4(n)

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 23

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 06: Discrete Fourier Transform


Objectives:
This lab aims at achieving the following objectives:
1) To form a routine of discrete Fourier transform on Matlab and find discrete Fourier transform
of various signals on Matlab. Also analyze different properties of discrete Fourier transform.
2) To study various properties of discrete time Fourier transform and verify these properties on
various signals on Matlab.
3) To analyze fast Fourier algorithms and to learn how it can efficiently be used to calculate
discrete Fourier transform.

Introduction and Background:


The purpose of this lab is to study the Discrete Fourier Transform (DFT) and Fast Fourier
Transform (FFT) methods. Frequency analysis of discrete time signals is usually and most
conveniently performed on a digital signal processor, which may a general purpose digital computer
or specially designed digital hardware. Now, to perform frequency analysis on a discrete time signal,
we convert the time domain sequences to an equivalent frequency domain representation i.e. we take
its Fourier transform. But since X(w) is a continuous function of frequency it is not computationally
convenient. The representation of a sequence x(n) by samples of its spectrum X(w) in frequency
domain is called discrete Fourier transform (DFT).

Discrete-Time Fourier Transform:


The discrete-time Fourier transform, DTFT, provides the frequency-domain representation
for absolutely summable sequences. The transformation is a function of the continuous variable ()
and defined for infinite-length sequences, which is impractical. In other words, the discrete-time
Fourier transform is not numerically computable transform.
Therefore we turn our attention to a numerically computable transform. It is obtained by
sampling the discrete-time Fourier transform in the frequency domain. When applied to finiteduration sequences, this gives us a new transform, called the Discrete Fourier Transform (or DFT).
The DFT is the ultimate numerically computable Fourier transform for arbitrary finite-duration
sequences.
The Discrete Fourier Transform of an N-point sequence is given by
N 1

X ( k ) = x ( n )W Nnk ,

0 k N 1

(1)

n =0

where W N = e

2
N

. Note that the DFT X (k ) is also an N-point sequence, that is, it is not

defined outside 0 k N 1.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 24

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

The inverse discrete Fourier transform (IDFT) of an N-point DFT X (k ) is given by (once
again x (n) is not defined outside 0 n N 1 )

x( n) =

1
N

N 1

X (k )W
k =0

nk
N

0 n N 1

( 2)

In MATLAB, an efficient implementation of the both transforms, DFT and IDFT, would be
to use a matrix-vector multiplication for each of the relations in (1) and (2).If x(n) and X (k ) are
arranged as column vectors x and X, respectively, then from (1) and (2) we have

[ X]Nx1 = [ WN ]NxN [ x]Nx1


1
N

[ x]Nx1 = [ WN ]NxN [ X]Nx1


*

(3)
(4)

where WN and WN are a DFT(Twiddle factor) matrix and its conjugate, respectively.
%=================================================================
The following MATLAB functions implement the equations (3).
%=================================================================

function [Xk] = dft(xn, N)


% This function Computes Discrete Fourier Transform
% --------------------------------------------------% [Xk] = dft(xn, N)
% Xk = DFT coeff. Array over 0 <= k <= N-1
% xn = N-point finite-duration sequence
% N = Length of DFT
n = 0:1:N-1;
% row vector for n
k = 0:1:N-1;
% row vector for k
WN = exp(-1i*2*pi/N);
% Wn factor
nk = n'*k;
% creates a NxN matrix of nk values
WNnk = WN .^ nk;
% DFT matrix
Xk = xn * WNnk;
% row vector for DFT coefficients
%=================================================================

%=================================================================

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 25

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

The following MATLAB functions implement the equations (4).


%=================================================================

function [xn] = idft(Xk, N)


% Computes Inverse Discrete Fourier Transform
% ------------------------------------------% [xn] = idft(Xk, N)
% xn = N-point sequence over 0 <= n <= N-1
% Xk = DFT coeff. Array over 0 <= n <= N-1
% N = Length of DFT
n = 0:1:N-1;
% row vector for n
% row vector for k
k = 0:1:N-1;
WN = exp(-1i*2*pi/N);
% Wn factor
nk = n'*k;
% creates a NxN matrix of nk values
WNnk = WN .^ (-nk); % IDFT matrix
% row vector of IDFT values
xn = (Xk*WNnk)/N;
%==================================================================

Lab Exercise:a) For the sequence x (n) = [1, 1, 1, 1] , which has DTFT as in the appendix, use the following
MATLAB commands to compute its DFT and record the results.

>> x = [1, 1, 1, 1]; N = 4;


>> X = dft(x, N);
>> magX = abs(X), phaX = angle(X)*180/pi
b) Plot the magnitude and phase of the DFT in (a) versus k (using the subplot command). In this
case the value k is identical to n. (Note: Save the plot for Lab report.)

c) Apply the zero-padding operation, which is a technique that allows us to sample at dense (or
finer) frequencies, to get an 8-point sequence and then compute its DFT by using the
following MATLAB commands.

>> x = [1, 1, 1, 1, zeros(1,4)]; N = 8;


>> X = dft(x, N);
>> magX = abs(X), phaX = angle(X)*180/pi
d) Record the results of (c) and plot the magnitude and phase of the 8-point DFT versus k.
(Note: Save the plot for Lab report.)

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 26

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

e) Treat the x (n) in (a) as a 16-point sequence by padding 12 zeros instead of 4 zeros as in (c).
Plot the magnitude and phase of the 16-point DFT versus k. (Note: Save the plot for Lab
report.)
f) For the sequence x(n) = cos(0.48n) + cos(0.52n) ; Determine the 10-point DFT of x(n) to
obtain an estimate of its DTFT. (Note: Save the plot for Lab report.)

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

n = [0:1:99]; x = cos(0.48*pi*n)+cos(0.52*pi*n);
n1 = [0:1:9]; y1 = x(1:1:10);
subplot(2, 1, 1); stem(n1, y1);
title(signal x(n), 0 <= n <= 9);
xlabel(n);
Y1 = dft(y1, 10); magY1 = abs(Y1(1:1:6));
k1 = 0:1:5; w1 = 2*pi/10*k1;
subplot(2, 1, 2); plot(w1/pi, magY1);
title(Samples of DFT Magnitude);
xlabel(frequence in pi units);

g) Use the following commands to pad 90 zeros and obtain a dense spectrum of the x(n) .
(Note: Save the plot for Lab report.)

>>
>>
>>
>>
>>
>>
>>
>>
>>

n2 = [0:1:99]; y2 = [x(1:1:10) zeros(1,90);


subplot(2, 1, 1); stem(n2, y2);
title(signal x(n), 0 <= n <= 9 + 90 zeros);
xlabel(n);
Y2 = dft(y2, 100); magY2 = abs(Y2(1:1:51));
k2 = 0:1:50; w2 = 2*pi/100*k2;
subplot(2, 1, 2); plot(w2/pi, magY2);
title(DFT Magnitude);
xlabel(frequence in pi units);

h) Use the following commands to take the first 100 samples of x (n) and determine its DTFT.
(Note: Save the plot for Lab report.)

>>
>>
>>
>>
>>
>>
>>
>>

subplot(2, 1, 1); stem(n, x);


title(signal x(n), 0 <= n <= 99);
xlabel(n);
X = dft(x, 100); magX = abs(X(1:1:51));
k = 0:1:50; w = 2*pi/100*k;
subplot(2, 1, 2); plot(w/pi, magX);
title(DFT Magnitude);
xlabel(frequence in pi units);

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 27

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

i) Use the following commands to synthesize the sequence x (n) using the Inverse Discrete
Fourier Transform (IDFT).

>>
>>
>>
>>

x1 = idft(X, 100);
subplot(2, 1, 2); stem(n, x1);
title(Synthesized x(n));
xlabel(n);

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 28

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 07: Fast Fourier Transform


The DFT introduced previously is the only transform that is discrete in both the time and the
frequency domains, and is defined for finite-duration sequences. Although it is a computable
transform, the straightforward implementation of it is very inefficient, especially when the sequence
length N is large. In 1965 Cooley and Tukey showed a procedure to substantially reduce the amount
of computations involved in the DFT. This led to the explosion of application of the DFT, including
in digital signal processing area. Furthermore, is also led to the development of other efficient
algorithms. All these efficient algorithms are collectively known as Fast Fourier Transform (FFT)
algorithms.
In an efficiently designed algorithm the number of computations should ne constant per data
sample, and therefore the total number of computations should be linear with respect to N. The
number of DFT computations for an N-point sequence depends quadratically on N, which will be
denoted by the notation
CN = (N 2 )

(5)

FFT algorithms can reduce the quadratic dependence on N of the DFT. Theoretically, the number of
computations used in the FFT algorithms could be as small as, depending on the radix used in the
algorithm,

C N = ( N log 2 N )

(6)

MATLAB provides a function called fft to compute the DFT of a vector x. It is invoked
by x = fft(x,N), which computes the N-point DFT. If the length of x is less than N, then x
is padded with zeros. If the argument N is omitted, then the length of the DFT is the length of x. If x
is a matrix, then fft(x,N) computes the N-point of each column of x.
This fft function is written in machine language and not using MATLAB commands (i.e.,
it is not available as a .m file). Therefore it executes very fast. It is written as a mixed-radix
algorithm. If N is a power of two, then a high-speed redix-2 FFT algorithm is employed. If N is not a
power of two, then N is decomposed into prime factors and a slower mixed-radix FFT algorithm is
used. Finally, if N is a prime number, then the fft function is reduced to the raw DFT algorithm.
The inverse DFT is computed using the ifft function, which has same characteristics as

fft.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 29

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab Exercise:1. To determine the execution time of the DFT as N varies from 1 to 512, invoke
the following commands and then save the plot.
%=================================================================

clc; close all; clear all;


Nmax = 512;
exe_time = zeros(1,Nmax);
for n = 1:1:Nmax
x = rand(1,n);
t = clock;
dft(x, n);
dftexe_time(n) = etime(clock, t);
end
n = [1:1:Nmax];
figure(1);
plot(n, dftexe_time, '.');
xlabel('N'); ylabel('Time in Sec.');
title('DFT execution times');
%=================================================================

2. To determine the execution time of the FFT as N varies from 1 to 512, invoke
the following commands and then save the plot.
%=================================================================

clc; close all; clear all;


Nmax = 512;
exe_time = zeros(1,Nmax);
for n = 1:1:Nmax
x = rand(1,n);
t = clock;
fft(x, n);
fftexe_time(n) = etime(clock, t);
end
n = [1:1:Nmax];
figure(2);
plot(n, fftexe_time, '.');
xlabel('N'); ylabel('Time in Sec.');
title('FFT execution times');
%=================================================================

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 30

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

3. Observe and compare the two plots.


4. To investigate the execution time characteristic of the built-in function fft,
invoke the following commands and save the plot. (Note: for a high
performance PC such as Pentium 4, Nmax should be large (~10,000-50,000)).
5. Using MATLAB, Prove the following general properties of the DFT :
a) Linearity property
b) Circular frequency shifting
c) Circular time shifting
d) Parsevals relation

Viva Questions:1. What is difference between DTFT and DFT.


2. What is the effect of zero padding on X(k), explain briefly ?
3. Explain the disadvantage point of computing Discrete Fourier Transform
directly from equation
N 1

X ( k ) = x ( n )WNnk ,

0 k N 1

n =0

4. Determine the numbers of complex additions and complex multiplications for


computing the DFT
N 1

X ( k ) = x ( n )WNnk ,

0 k N 1

n =0

5. Determine the numbers of complex additions and complex multiplications for


computing the FFT
6. For the sequence x(n) with the number of points not of the form of 2N, list the
methods that can be used to modify the sequence x(n) so that the FFT will
work.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 31

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 08: Circular Convolution


Objectives:
This lab aims at achieving the following objectives:
4) To find modulo-N and Circular Shift of a sequence.
5) To find circular convolution of discrete time signals.

Theory:
A finite duration sequence x(n) with N samples over the range 0nN-1 is calleed as N-point sequence.
Let

be a periodic signal of period N, created using the N-point sequence x(n), i.e.

Using Modulo N operation on the argument it is simplified as

A simple way to interpret this operation is the following: if the argument n is betyween 0 and N-1, then
leaves it as it is; otherwise add or subtract multiples of N from n until the result is between 0 and N-1.
The rem(n,N) function in MATLAB determines the remainder after dividing n by N. This function can
be used to implement modulo-N operation when n0. When n<0,we need to modify the result to obtain
the correct values.This is shown in the following function

Function for Modulo N:


=========================================================================

function m=mod(n,N)
m=rem(n,N);
m=m+N;
m=rem(m,N)
=========================================================================
If an N-point sequence is shifted in either direction, then the result is no longer between 0nN-1.
Therfore x(n) is first converted into its periodic extension
obain

.This is called a periodic shift of

and then shifted it by m samples to


. The periodic shift is converted

into N-point sequence. The resulting sequence is called as circular shift of a sequence.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 32

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Function for Circular shift:


=========================================================================

function y= cirshftt(x,m,N)
if length(x) > N
error(N must be >= the length of x)
end
x=[x zeros(1,N-length(x))];
n=[0:1:N-1];
n=mod(n-m,N);
y=x(n+1);

Circular Convolution:
The N-point circular convolution of two length-N sequences g[n] and h[n], 0 n N 1, is defined by

x3 ( m ) =

N 1

x (n) x
n=0

(( m n )) N , m = 0,1, ...., N 1

Where ((m-n))N is modulo N.


Circular Convolution is also given by

x1 ( n ) x 2 ( n ) =

N 1

x (m ) x

m=0

(( n m )) N , n = 0,1, ...., N 1

The DFT property of circular convolution is

x1 ( n ) x 2 ( n ) X 1 ( k ) X 2 ( k )

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 33

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Function for circular convolution (Time domain approach)


=========================================================================
function y = circonvt(x1,x2,N)
if length(x1) > N
error(N must be >= the length of x1)
end
if length(x2) > N
error(N must be >= the length of x2)
end
x1=[x1 zeros(1,N-length(x1))];
x2=[x2 zeros(1,N-length(x2))];
m=[0:1:N-1];
x2=x2(mod(-m,N)+1);
H=zeros(N,N);
for n=1:1:N
H(n,:)=cirshft(x2,n-1,N);
end
y=x1*H;
=========================================================================

Linear Convolution:
The linear convolution of a length-N sequence x[n], 0 n N 1, with a Length-M sequence h[n], 0
n M 1, can be obtained by a (N + M 1)-point circular convolution of two length-(N +M 1)
sequences, xe[n] and he[n],

y L ( n ) = x ( n ) h ( n ) = x e ( n ) he ( n )
Where the extended sequences are obtained by padding zero- valued samples.

x ( n ), 0 n N 1
xe ( n ) =
0, N n N + M 1

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 34

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab Exercise:
1) x( n) = 10 ( 0.8) , 0 n 10 Determine and sketch the following signals:
n

( a ) x (( n ))11 ,

( b ) x (( n + 4))11 ,

( c ) x (( n 3))15

2) x1 (n) = {1, 2, 2} and x2 (n) = {1, 2,3, 4} Compute the 4 point circular Convolution

and prove circular convolution property.


3) x1 (n) = {1, 2, 2,1} and x2 (n) = {1, 1, 1,1} ,determine linear convolution by means
of the circular convolution property.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 35

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 09: IIR Filter Design


Objectives:
To Design IIR filters
Butterworth filter

Chebyshev Type I and II

Introduction:
The most common method of IIR filter design is based on the bilinear transformation of a prototype
analog transfer function. The analog transfer function is usually one of the following types:
Butterworth, Type 1 Chebyshev, Type 2 Chebyshev, and elliptic transfer functions. The difference
between these filter types can be explained by considering the analog lowpass filter.
1. The Butterworth lowpass transfer function has a maximally-flat magnitude response at dc, that
is, = 0, and a monotonically decreasing magnitude response with increasing frequency.
2. TheType 1 Chebyshev lowpass transfer function has an equiripple magnitude response in the
passband and a monotonically decreasing magnitude response with increasing frequency outside
the passband.
3. The Type 2 Chebyshev lowpass transfer function has a monotonically decreasing magnitude
response in the passband with increasing frequency and an equiripple magnitude response in the
stopband.
4. Finally, the elliptic lowpass transfer function has equiripple magnitude responses both in the
passband and in the stopband.
Background review:
The most widely used approach to IIR filter design is based on the bilinear transformation from s-plane
to z-plane given by
s =

2 1 z 1

T 1+ z 1

Using the above transformation an analog transfer function Ha(s) is converted into a digital transfer
function H(z) according to:

H ( z ) = H a ( s)

2 1 z 1
s =

T 1+ z 1

For the bilinear transformation, the relation between the imaginary axis (s = j) in the s-plane and the
unit circle (z = ej) in the z-plane is given by
=

tan
T
2

which maps the entire imaginary axis in the s-plane to the unit circle in the z-plane introducing a
distortion in the frequency axis called warping. To develop a digital filter meeting a specified
magnitude response, the analog equivalents (p and s) of the critical band-edge frequencies (p and
s) of the digital filter are first obtained using the relation of Eq. (a) the analog prototype Ha(s) is then
designed using the prewarped critical frequencies, and Ha(s) is transformed using the bilinear
transformation to obtain the desired digital filter transfer function H(z).
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 36

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Labwork
The first step in the filter design process is to choose the type of filter approximation to be employed
and then to estimate the order of the transfer function from the filter specifications. After the filter type
has been selected and its order estimated, the next step is to determine the transfer function of the filter.
To this end MATLAB provides functions for all four types of filters.
Butterworth and Chebyshev Filter
Parameter

Description

Wp

Passband corner frequency Wp, the cutoff frequency, is a scalar or a twoelement vector with values between 0 and 1, with 1 corresponding to the
normalized Nyquist frequency, radians per sample.

Ws

Stopband corner frequency Ws, is a scalar or a two-element vector with values


between 0 and 1, with 1 corresponding to the normalized Nyquist frequency.

Rp

Passband ripple, in decibels. This value is the maximum permissible passband


loss in decibels.

Rs

Stopband attenuation, in decibels. This value is the number of decibels the


stopband is down from the passband.

Use the following guide to specify filters of different types.


Filter Type Stopband and Passband Specifications
Filter Type

Stopband and Passband Conditions

Stopband

Passband

Lowpass

Wp < Ws, both scalars

(Ws,1)

(0,Wp)

Highpass

Wp > Ws, both scalars

(0,Ws)

(Wp,1)

Bandpass

The interval specified by Ws contains the one (0,Ws(1))

and (Wp(1),Wp(2))

specified by Wp (Ws(1) < Wp(1) < Wp(2) < (Ws(2),1)


Ws(2)).
Bandstop

The interval specified by Wp contains the one (Ws(1),Ws(2))

(0,Wp(1)) and

specified by Ws (Wp(1) < Ws(1) < Ws(2) <

(Wp(2),1)

Wp(2))

Choose the input arguments to specify the stopband and passband according to the following table.
Description of Stopband and Passband Filter Parameters

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 37

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

MATLAB functions used


Step 1. Get order and cutoff frequency
buttord
[N,Wc]=buttord(Wp,Ws,Rp,Rs)
[N,Wc]=buttord(Wp,Ws,Rp,Rs,'s')
Step 2 Butterworth filter design (Get filter coefficient)
butter
[z,p,k]=butter(N,Wc)
Step 3 Get Frequency response of digital filter
[h,w]=freqz(b,a,n)

Butterworth Filters Design


Example 1
For data sampled at 1000 Hz, design a lowpass butterworth filter with less than 3 dB of ripple in the
passband, defined from 0 to 40 Hz, and at least 60 dB of attenuation in the stopband, defined from 150
Hz to the Nyquist frequency (500 Hz). Plot the filter's frequency response:
MATLAB Program
Wp = 40/500; Ws = 150/500;
[N,Wc] = buttord(Wp,Ws,3,60)
% Returns N = 5; Wc=0.0810;
[b,a] = butter(N,Wc);
freqz(b,a,512,1000);
title('N=5 Butterworth Lowpass Filter')

Example 2
Design a bandpass butterworth filter with passband of 60 Hz to 200 Hz, with less than 3 dB of ripple in
the passband, and 40 dB attenuation in the stopbands that are 50 Hz wide on both sides of the passband:
MATLAB Program
Wp = [60 200]; Ws = [50 250];
Wp = Wp/500; Ws = Ws/500;
Rp = 3; Rs = 40;
[N,Wc] = buttord(Wp,Ws,Rp,Rs)
% Returns N =16; Wc =[0.1198 0.4005];
[b,a] = butter(N,Wc);
freqz(b,a,128,1000)
title('N=16 Butterworth Bandpass Filter')
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 38

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Chebyshev Filter Design


Functions used are
cheb1ord

cheby1

Example 3
For data sampled at 1000 Hz, design a lowpass Chebyshev type I filter with less than 3 dB of ripple in
the passband defined from 0 to 40 Hz and at least 60 dB of ripple in the stopband defined from 150 Hz
to the Nyquist frequency (500 Hz):
Wp = 40/500; Ws = 150/500;
Rp = 3; Rs = 60;
[N,Wc] = cheb1ord(Wp,Ws,Rp,Rs)
% Returns N = 4 Wc =0.0800
[b,a] = cheby1(N,Rp,Wc);
freqz(b,a,512,1000);
title('N=4 Chebyshev Type I Lowpass Filter')

Example 4
Design a bandpass Chebyshev type I filter with a passband of 60 Hz to 200 Hz, with less than 3 dB of
ripple in the passband, and 40 dB attenuation in the stopbands that are 50 Hz wide on both sides of the
passband:

Wp = [60 200]; Ws = [50 250];


Wp=Wp/500; Ws=Ws/500;
Rp = 3; Rs = 40;
[N,Wc] = cheb1ord(Wp,Ws,Rp,Rs)
% Returns N =7 Wc =[0.1200 0.4000]
[b,a] = cheby1(N,Rp,Wc);
freqz(b,a,512,1000);
title('N=7 Chebyshev Type I Bandpass Filter')

Exercise
1. For data sampled at 1000 Hz, design a lowpass Chebyshev type II filter with less than 3 dB of ripple
in the passband defined from 0 to 40 Hz, and at least 60 dB of attenuation in the stopband defined from
150 Hz to the Nyquist frequency (500 Hz):

2. Design a bandpass Chebyshev type II filter with a passband of 60 Hz to 200 Hz, with less than 3 dB
of ripple in the passband, and 40 dB attenuation in the stopbands that are 50 Hz wide on both sides of
the passband:
Use cheb2ord and cheby2

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 39

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 10: FIR Filter Design


Objectives:
Design of FIR filter Using windowing technique.
Labwork
b = fir1(n,Wn,'ftype') specifies a filter type, where 'ftype' is:
The function fir1 can be used to design conventional lowpass, highpass, bandpass, and bandstop linearphase FIR filters.

'high' for a highpass filter with cutoff frequency Wn.


'stop' for a bandstop filter, if Wn = [w1 w2]. The stopband frequency range is specified by
this interval.
'DC-1' to make the first band of a multiband filter a passband.
'DC-0' to make the first band of a multiband filter a stopband.

fir1 always uses an even filter order for the highpass and bandstop configurations. This is because
for odd orders, the frequency response at the Nyquist frequency is 0, which is inappropriate for
highpass and bandstop filters. If you specify an odd-valued n, fir1 increments it by 1.
b = fir1(n,Wn,window) uses the window specified in column vector window for the design. The
vector window must be n+1 elements long. If no window is specified, fir1 uses a Hamming window
(see hamming) of length n+1.

Example 1
Design a 48th-order FIR bandpass filter with passband 0.35 0.65:
b = fir1(48,[0.35 0.65]);
freqz(b,1,512)
Example 2
The chirp.mat file contains a signal, y, that has most of its power above fs/4, or half the Nyquist
frequency. Design a 34th-order FIR highpass filter to attenuate the components of the signal below
fs/4. Use a cutoff frequency of 0.48 and a Chebyshev window with 30 dB of ripple:
load chirp
% Load y and fs.
b = fir1(34,0.48,'high',chebwin(35,30));
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 40

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

freqz(b,1,512)
Kaiser window FIR filter design estimation parameters
kaiserord

Example 1

Design a lowpass filter with passband defined from 0 to 1 kHz and stopband defined from 1500 Hz
to 4 kHz. Specify a passband ripple of 5% and a stopband attenuation of 40 dB:
fsamp = 8000;
fcuts = [1000 1500];
mags = [1 0];
devs = [0.05 0.01];
[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
freqz(hh)

Example 2

Design an odd-length bandpass filter (note that odd length means even order, so the input to fir1
must be an even integer):
fsamp = 8000;
fcuts = [1000 1300 2210 2410];
mags = [0 1 0];
devs = [0.01 0.05 0.01];
[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp);
n = n + rem(n,2);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
[H,f] = freqz(hh,1,1024,fsamp);
plot(f,abs(H)), grid on

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 41

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Lab 11: Introduction to DSP Tools


Objectives:
This lab aims to gain familiarity with TI-DSP Starter Kit (DSKC6711) and Code Composer
Studio CCS3.1 which is an integral software tool for developing DSP algorithms and for
programming the DSP hardware for this course. These goals can be accomplished by: Writing

simple program for Sine wave generation.

Introduction and Background:


This lab introduces you to the TI-DSP starter kit of TMS320C6711.
The DSP lab consists of a number of hardware experiments illustrating the programming of realtime processing algorithms on the Texas Instruments TMS320C6711 floating-point DSP.
Programming of the DSP chip is done in C (and some assembly) using the Code Composer Studio
(CCS) integrated development environment. Familiarity with C programming is necessary in order
to successfully complete this lab course. MATLAB is also necessary and will be used to generate
input signals to the DSP and to design the filters used in the various examples.
The hardware experiments include implementation of FIR and IIR filters and several digital audio
effects, such as audio reverberators, flangers etc. All of the above are real-time sample-by-sample
processing examples. In addition, real-time block processing applications using triple buffering are
also studied in the lab, such as real-time FFT/IFFT algorithms, and time- and frequency-domain
implementations of the overlap-add fast convolution method.
The lab handouts contain a short introduction to the required theory. More details, as well as several
concrete C and MATLAB implementations, may be found in the book [1,2], which may be freely
downloaded from the web page: http://www.et.ycce.in

Prelab Instructions: :
Prior to beginning this lab, you must have:
Carefully read over this lab in order to plan how to implement the tasks assigned. Make sure
you are aware of all the items you need to include in your lab report.
Please note that there are no prelab questions. The questions at the end of this lab must be
answered and submitted with the lab report.

Deliverables
You must be present and conducting the practical assigned to you during the scheduled lab session.
Please prepare a soft and hard copy of Lab report and submit your reports by the end of the lab.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 42

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Introduction to TMS320C6711 DSP Starter Kit:


The Texas Instrument 6711DSK is a DSP starter kit for the TMS320C6711 DSP chip.
The kit contains an emulator board which contains:
1) DSP chip, memory and control circuitry.
2) Input/output capabilities in the form of
a) An audio codec (ADC and DAC) which provides 1 input and 1 output channel
sampled at 8 kHz.
b) Digital inputs and outputs.
c) A connector for adding external evaluation modules, like the PCM3003Audio
daughter card which has 2 analog in and 2 analog out sampled at48 kHz.
3) A parallel port for interface to a host computer used for program development, program
download and debugging.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 43

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale

Figure 1.1a:
1a: Schematic of DSKC6711 Board

Figure 1.1b:: Simplified Schematic of DSKC6711 depicting various peripherals on the board

Department of Electronics and Telecommuni


Telecommunication Engineering, YCCE, Nagpur

Page 44

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Getting Familiar with Code composer studio:


Code composer studio TM (CCS studio or CCS) is a useful integrated development environment
(IDE) that provides an easy to use software tool to build and debug programs. In addition it allows
real time analysis of application programs. Figure 1.2 shows the phases associated with the CCS
code development process. During its set-up, CCS can be configured for different target DSO boards
(for example, DSKC6711, DSKC6416, EVMV6701, C6xxx simulators). The version used
throughout the course is based on CCS3.1.

Design

Code & Build

Debug

Analyze

Conceptual
Planning

Create project, write


source code,
configuration file

syntax checking,
probe points,
logging, etc.

real time
debugging,
statistics, tracing

Figure 1.2: Simplified Code Composer Studio IDE Development Flow


CCS provides a file management environment for building application programs. It includes an
integrated editor for editing C and assembly files. Foe debugging purposes, it provides breakpoints,
data monitoring and graphing capabilities, profiler for benchmarking and probe points to stream data
to and from the target DSP. This lab handout introduces the basic features of CCS that are needed in
order to build and debug an application program. To become familiar with all of its features, one
should go through the TI CCS Tutorial and TI CCS Users Guide manuals. The real time analysis and
scheduling features of CCS will be covered later.
CCS includes tools for code generation, such as a C compiler, an assembler, and a linker. It has
graphical capabilities and supports real time debugging. It provides an easy-to-use software tool to
build and debug programs.

The C compiler compiles a C source program with extension .c to produce an assembly source
file with extension .asm.

The assembler assembles an .asm source file to produce a machine language object file with
extension.obj.

The linker combines object files and object libraries as input to produce an executable file with
extension .out. This executable file represents a linked common object file format (COFF),
popular in Unix-based systems and adopted by several makers of digital signal processors.
This executable file can be loaded and run directly on the TMS320C6711 processor.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 45

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

A linear optimizer

optimizes this source file to create an assembly file with extension

.asm (similar to the task of the C compiler).


The CCS software is used for three phases in the overall DSP system design process:
1. Coding and building: - writing code using the editor, creating a project, compiling and
linking.
2. Debugging: - syntax checking, probe points, break points
3. Analysis: - statistics, benchmarking, real-time debugging

1.1 Useful Types of Files:


You will be working with a number of files with different extensions. They include:

file.pjt: to create and build a project named file.


file.c: C source program.
file.asm: assembly source program created by the user, by the C compiler, or by
the linear optimizer.
file.sa: linear assembly source program. The linear optimizer uses file.sa as
input to produce an assembly program file.asm.
file.h: header support file.
file.lib: library file, such as the run-time support library file rts6700.lib.
file.cmd: linker command file that maps sections to memory.
file.obj: object file created by the assembler.
file.out: executable file created by the linker to be loaded and run on theC6713
processor.
file.cdb: configuration file when using DSP/BIOS.

1.2 Steps involved in programming


1.2.1

System configuration:

Before starting actual working on CCS the selection of appropriate DSP processor is important. First
configure the code composer studio to use C6711 DSK board by using Setup CCS software as shown in
figure 1.3.

Steps for system configuration:


Code Composer Studio Setup

Select processor C6711 DSK (little)


Add

Save & Quit

This will start CCS with TMS320C7611 DSP Processor

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 46

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Figure 1.3: System Configuration

4.2.2 Connecting the C6711 DSK


Connect the C6711 DSK board with the computer by using parallel port cable. Now connect the
processor to the CCS through following steps shown in figure 1.4.
1) Debug

Connect.

4.2.3 Creating project in CCS


For creating new project follow the steps given below:
1) Project New.
2) Write the Name of your project.
3) The project type should be .out and select the target 67xx.
4) Finish.
These steps are summarized in Figure 1.5. This will create a project in the CCS directory provided by
you as shown in figure 1.6. Currently the project is empty and now we will write the program or source
file in this project. For writing the C program or source file in CCS follow the steps given below:

Steps for creating a source file in CCS:


1) File

New

Source File.

2) Write your C program in this source file.


3) Save the source file with .c extension.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 47

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Figure 1.4: Connecting C6711 DSK board with CCS

Figure 1.5: Creating Project in CCS

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 48

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Figure 1.6: Interpolation Project in CCS

Steps for creating a source file in CCS:


1) File

New

Source File.

2) Write your C program in this source file.


3) Save the source file with .c extension.

Figure 1.7: Creating source file in CCS

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 49

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Steps for adding library file to the project:


After writing the source program add the following files to the project as shown in figure 4.7.
1) Project

Add files to Project

bilinear.c.

2) Project

Add files to Project

rts6700.lib.

3) Project

Add files to Project

sinewave.cmd.

4) Now the project is ready to build.

Figure 1.8: Adding library files to project

Steps for build and executing the project:


After adding the library files to the project, now follow the following steps to build and execute the
program.
1) Project

Build.

2) Remove the errors if any from the project.


3) Project
4) File
5) Debug

Rebuild.
Load Program

Debug .out file.

Run.

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 50

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Some sample programs:


Example 1.1: Dot Product of Two Arrays (dotp4)
Operations such as addition/subtraction and multiplication are the key operations in a DSP. A very
important operation is multiply/accumulate, which is useful in a number of applications requiring
digital filtering, correlation, and spectrum analysis. Since the multiplication operation is executed
commonly and is essential for most DSP algorithms, it is important that it executes in a single cycle.
With the C6711 we can actually perform two multiply/accumulate operations within a single cycle.
This example illustrates additional features of CCS, such as single-stepping, setting breakpoints. Again,
the purpose here is to become more familiar with the tools.
Program: Sum-of-products program using C code (dotp4.c).
//Dotp4.c Multiplies two arrays, each array with 4 numbers
int dotp(short *a,short *b,int ncount);//function prototype
#include <stdio.h> //for printf
#define x_array 1,2,3,4
#define y_array 0,2,4,6
#define count 4 //# data in each array
short x[count] = {x_array}; //declaration of 1st array
short y[count] = {y_array}; //declaration of 2nd array
main()
{
int result = 0; //result sum of products
result = dotp(x, y, count); //call dotp function
printf("result = %d (decimal) \n", result); //print result
}
int dotp(short *a,short *b,int ncount) //dot product function
{
int sum = 0; //init sum
int i;
for (i = 0; i < ncount; i++)
sum += a[i] * b[i]; //sum of products
return(sum); //return sum as result
}

The program can be readily modified to handle a larger set of data. No real-time implementation is used
in this example, and no real-time I/O support files are needed. The support functions for interrupts are
not needed here.

Create this project as dotp4 and add the following files to the project:
1. dotp4.c: C source file
2. vectors_poll.asm: vector file defining the entry address c_int00
3. hello.cmd: generic linker command file
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 51

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

4. rts6700.lib: library file


Do not add any include files using Add Files to Project since they are added by selecting
Project
Scan All File Dependencies. The header file stdio.h is needed due to the printf statement
in the program dotp4.c to print the result.

Implementing a Variable Watch


1. Select Project
Options with -gs as the compiler option and the default linker option with no
optimization.
2. Rebuild all by selecting the toolbar with the three arrows (or select Project
Rebuild All). Load the
executable file dotp4.out within the folder dotp4\Debug.
3. Select View
Quick Watch. Type sum to watch the variable sum and click on Add to Watch. The
message identifier not found associated with sum is displayed (as Value) because this local variable
does not exist yet.
4. Set a breakpoint at the line of code sum += a[i] * b[i];
b[i]; by placing the mouse cursor (clicking) on that
line, then right-click and select the Toggle breakpoint. Or, preferably, with the cursor on that line of
code (at the extreme left), double-click. A red circle to the left of that line of code should appear. (Note:
placing the cursor on a line of code with a set breakpoint and double clicking will remove the
breakpoint.)
5. Select Debug Run (or use the running man toolbar). The program executes up to (excluding) the
line of code with the set breakpoint. A yellow arrow will also point to that line of code.
6. Single-step using F8. Repeat or continue to single-step and observe/watch the variable sum in the
Watch window change in value to 0, 4, 16, 40. Select Debug
Run and verify that the resulting value
of sum is printed as sum = 40 (decimal)
7. Note the printf statement in the C program dotp4.c for printing the result. This statement (while
excellent for debugging) should be avoided after the debugging stage, since it takes over 6000 cycles to
execute.
Animating
1. Select File
Reload Program to reload the executable file dotp4.out. Or, preferably, select
Debug
Restart. Note that after the executable file is loaded, the entry address for execution is
c_int00, as can be verified by the disassembled file.
2. The same breakpoint should be set already at the same line of code as before. Select
Debug Animate or use the equivalent toolbar in the left window (below the Halt running man).
Observe the variable sum change in values through the Watch window. The speed of animation can
be controlled by selecting Option
Customize
Animate Speed (the maximum speed is set to
default at 0 second).

Example 1.2: Generation of Sine waveform


Program: Generation of sine waveform program using C code (mysin.c)

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 52

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
#include <stdio.h>
#include<math.h>
#define FREQ 500
float m[256];
main()
{
int i=0;
for(i=0;i<256;i++)
{
m[i]=sin(2*3.14*FREQ*i/24000);
printf("%f\n",m[i]);
}
}

Plotting with CCS


To see an array in a graph, go to "View Graph
Time/Frequency.". On the pop up window (as in
Figure 2.18), fill:
i. Start Address: Name of the array (to be plotted).
ii. Acquisition Buffer Size: Size of the array.
iii. Display Data Size: Size of the data to be displayed on the graph.
iv. DSP Data Type: Type of the array (integer or float).
v. Sampling Rate: Sampling rate of the signal in the array.
For example, to observe the array 'm' in the above demo code, adjust the parameters as in Figure 1.9.
The resulting graph will be as in Figure 1.10.

Figure 1.9: Graph property Dialog

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 53

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Figure 1.10: Output waveform

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 54

ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale

Department of Electronics and Telecommunication Engineering, YCCE, Nagpur

Page 55

You might also like