Sunteți pe pagina 1din 99

1 of 99

Digital Signal Processing Lab


Lab Manual and Workbook

Academic Year: _________ Semester: _______


Name: _________________ Reg. No.: ________
Department: ____________ Class: __________

DSP Lab

1 of 99

GRIET-ECE

2 of 99

DSP Lab

2 of 99

GRIET-ECE

3 of 99

Gokaraju Rangaraju
Institute of Engineering and Technology

Certificate

This is to certify that this is a bonafide record of practical work done by


Name ____________________,
Reg. No. ________________,
in the laboratory during the academic year ____________.

Signature of Faculty

DSP Lab

3 of 99

GRIET-ECE

4 of 99

DSP Lab

4 of 99

GRIET-ECE

5 of 99

Contents
C6713-based Experiments
1
2
3
4
5
6

Introduction to the TMS 320C6713 DSP Processor


Linear convolution
Circular convolution
FIR filters
IIR filters
Echo with variable delay and feedback

7
11
15
19
23
27

MATLAB-based Experiments
1
2
3
4
5
6
7
8

31-Linear and circular convolution


32-Filtering via DFT
33-Picket fence effect in DFT
41-IIR low-pass filter frequency response and simulation
43-Analog Butterworth low-pass filter frequency response
47-Design of analog Butterworth low-pass filter
48-Design of digital Butterworth low-pass filter
51-FIR low-pass filter

29
33
41
51
59
67
73
81

Extra-credit Experiments
1
2
3
4

DSP Lab

E1-Generation of DTMF
E2-Implementation of Decimation
E3-Implementation of Interpolation
E4-Implementation of I/D Rate Conversion

5 of 99

91
93
95
97

GRIET-ECE

6 of 99

DSP Lab

6 of 99

GRIET-ECE

7 of 99

1. Introduction to the TMS 320C6713 DSP Processor


DSK Board
The DSK package is powerful, yet relatively inexpensive, with the necessary hardware and
software support tools for real-time signal processing. It is a complete DSP system. The DSK board,
with an approximate size of 5 X 8 in., includes the C6713 floating-point digital signal processor and a
32-bit stereo codec TLV320AIC23 (AIC23) for input and output. The onboard codec AIC23 uses a
sigmadelta technology that provides ADC and DAC. It connects to a 12-MHz system clock. Variable
sampling rates from 8 to 96 kHz can be set readily. A daughter card expansion is also provided on the
DSK board. Two 80-pin connectors provide for external peripheral and external memory interfaces with
light-emitting diodes (LEDs) and liquid-crystal displays (LCDs) for spectrum display.
The DSK board includes 16MB (megabytes) of synchronous dynamic random access memory
(SDRAM) and 256kB (kilobytes) of flash memory. Four connectors on the board provide input and
output: MIC IN for microphone input, LINE IN for line input, LINE OUT for line output, and
HEADPHONE for a headphone output (multiplexed with line output). The status of the four user dip
switches on the DSK board can be read from a program and provides the user with a feedback control
interface. The DSK operates at 225MHz.Also onboard the DSK are voltage regulators that provide 1.26
V for the C6713 core and 3.3 V for its memory and peripherals.

(a)

DSP Lab

7 of 99

GRIET-ECE

8 of 99

(b)
FIGURE. TMS320C6713-based DSK board: (a) board; (b) diagram

TMS320C6713 Digital Signal Processor


The TMS320C6713 (C6713) is based on the VLIW architecture, which is very well suited for
numerically intensive algorithms. The internal program memory is structured so that a total of eight
instructions can be fetched every cycle. For example, with a clock rate of 225MHz, the C6713 is capable
of fetching eight 32-bit instructions every 1/(225 MHz) or 4.44 ns. Features of the C6713 include 264
kB of internal memory (8kB as L1P and L1D Cache and 256kB as L2 memory shared between program
and data space), eight functional or execution units composed of six arithmetic-logic units (ALUs) and
two multiplier units, a 32-bit address bus to address 4 GB (gigabytes), and two sets of 32-bit generalpurpose registers.
The C67xx (such as the C6701, C6711, and C6713) belong to the family of the C6x floatingpoint processors, whereas the C62xx and C64xx belong to the family of the C6x fixed-point processors.
The C6713 is capable of both fixed- and floating point processing.

Code Composer Studio


CCS provides an IDE to incorporate the software tools. 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
C6713 processor. A linear optimizer optimizes this source file to create an assembly file with extension
.asm (similar to the task of the C compiler).
To create an application project, one can add the appropriate files to the project.
Compiler/linker options can readily be specified. A number of debugging features are available,
DSP Lab

8 of 99

GRIET-ECE

9 of 99

including setting breakpoints and watching variables; viewing memory, registers, and mixed C and
assembly code; graphing results; and monitoring execution time. One can step through a program in
different ways (step into, over, or out).
Through the joint team action group (JTAG), communication with on-chip emulation support
occurs to control and monitor program execution. The C6713 DSK board includes a JTAG interface
through the USB port.

CCS Installation and Support


Use the USB cable to connect the DSK board to the USB port on the PC. Use the 5-V power supply
included with the DSK package to connect to the +5-V power connector on the DSK to turn it on. Install
CCS with the CD-ROM included with the DSK, preferably using the c:\C6713 structure.
The CCS icon should be on the desktop as C6713DSK CCS and is used to launch CCS. The code
generation tools (C compiler, assembler, linker) are used with CCS version 2.x.
CCS provides useful documentations included with the DSK package on the following (see the Help
icon):
1. Code generation tools (compiler, assembler, linker, etc.)
2. Tutorials on CCS, compiler, RTDX
3. DSP instructions and registers
4. Tools on RTDX, DSP/basic input/output system (DSP/BIOS), and so on.
An extensive amount of support material (pdf files) is included with CCS. There are also examples
included with CCS within the folder c:\C6713\examples.
They illustrate the board and chip support library files, DSP/BIOS, and so on. CCS Version 2.x was
used to build and test the examples included in this book. A number of files included in the following
subfolders/directories within c:\C6713 can be very useful:
1. myprojects: a folder supplied only for your projects. All the folders in the accompanying book
CD should be placed within this subdirectory.
2. bin: contains many utilities.
3. docs: contains documentation and manuals.
4. c6000\cgtools: contains code generation tools.
5. c6000\RTDX: contains support files for real-time data transfer.
6. c6000\bios: contains support files for DSP/BIOS.
7. examples: contains examples included with CCS.
8. tutorial: contains additional examples supplied with CCS.

DSP Lab

9 of 99

GRIET-ECE

10 of 99

Useful Types of Files


You will be working with a number of files with different extensions. They include:
1. file.pjt: to create and build a project named file
2. file.c: C source program
3. file.asm: assembly source program created by the user, by the C compiler,
or by the linear optimizer
4. file.sa: linear assembly source program. The linear optimizer uses file.sa as input to produce
an assembly program file.asm
5. file.h: header support file
6. file.lib: library file, such as the run-time support library file rts6700.lib
7. file.cmd: linker command file that maps sections to memory
8. file.obj: object file created by the assembler
9. file.out: executable file created by the linker to be loaded and run on the C6713 processor
10. file.cdb: configuration file when using DSP/BIOS

Support Files
The following support files located in the folder support (except the library files) are used for most of
the examples.
1. C6713dskinit.c: contains functions to initialize the DSK, the codec, the serial ports, and for
I/O. It is not included with CCS.
2. C6713dskinit.h: header file with function prototypes. Features such as those used to select
the mic input in lieu of line input (by default), input gain, and so on are obtained from this
header file.
3. C6713dsk.cmd: sample linker command file. This generic file can be changed when using
external memory in lieu of internal memory.
4. Vectors_intr.asm: a modified version of a vector file included with CCS to handle
interrupts. Twelve interrupts, INT4 through INT15, are available, and INT11 is selected
within this vector file. They are used for interrupt-driven programs.
5. Vectors_poll.asm: vector file for programs using polling.
6. rts6700.lib, dsk6713bsl.lib, csl6713.lib: run-time, board, and chip support library files,
respectively. These files are included with CCS and are located in C6000\cgtools\lib,
C6000\dsk6713\lib, and c6000\bios\lib, respectively.

DSP Lab

10 of 99

GRIET-ECE

11 of 99

2. Linear Convolution
Aim:
To verify linear convolution.
Equipments Required:
Operating System
Constructor
Software

Windows XP
Simulator
CStudio3 & MATLAB 7.5

Theory:
Convolution is a formal mathematical operation, just as multiplication, addition, and integration.
Addition takes two numbers and produces a third number, while convolution takes two signals and
produces a third signal. Convolution is used in the mathematics of many fields, such as probability and
statistics. In linear systems, convolution is used to describe the relationship between three signals of
interest: the input signal, the impulse response, and the output signal.
( )

( ) (

In this equation, x1(k), x2(n-k) and y (n) represent the input to and output from the system at
time n. Here we could see that one of the inputs is shifted in time by a value every time it is multiplied
with the other input signal. Linear Convolution is quite often used as a method of implementing filters of
various types.
Program for:
//Linear convolution program in c language using CCStudio
#include<stdio.h>
int x[15],h[15],y[15];
main()
{
int i,j,m,n;
printf("\n enter value for m");
scanf("%d",&m);
printf("\n enter value for n");
scanf("%d",&n);
printf("Enter values for i/p x(n):\n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf("Enter Values for i/p h(n) \n");
for(i=0;i<n; i++)
scanf("%d",&h[i]);
//padding of zeros
for(i=m;i<=m+n-1;i++)
x[i]=0;
for(i=n;i<=m+n-1;i++)
h[i]=0;
/*convolution operation */
DSP Lab

11 of 99

GRIET-ECE

12 of 99

for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
{
y[i]=0;
for(j=0;j<=i;j++)
{
y[i]=y[i]+(x[j]*h[i-j]);
}
}
for(i=0;i<m+n-1;i++)
printf("\n The Value of output y[%d]=%d",i,y[i]);
}

//displaying the o/p

Result:
enter value for m4
enter value for n4
Enter values for i/p
1234
Enter Values for n
1234
The Value of output y[0]=1
The Value of output y[1]=4
The Value of output y[2]=10
The Value of output y[3]=20
The Value of output y[4]=25
The Value of output y[5]=24
The Value of output y[6]=16

DSP Lab

12 of 99

GRIET-ECE

13 of 99

DSP Lab

13 of 99

GRIET-ECE

14 of 99

DSP Lab

14 of 99

GRIET-ECE

15 of 99

3. Circular Convolution
Aim:
To verify circular convolution.
Equipment Required:
Operating System
Constructor
Software

Windows XP
Simulator
CCStudio 3 & MATLAB 7.5

Theory:
Circular convolution is another way of finding the convolution sum of two input signals. It resembles
the linear convolution, except that the sample values of one of the input signals is folded and right
shifted before the convolution sum is found. Also note that circular convolution could also be found by
taking the DFT of the two input signals and finding the product of the two frequency domain signals.
The Inverse DFT of the product would give the output of the signal in the time domain which is the
circular convolution output. The two input signals could have been of varying sample lengths. But we
take the DFT of higher point, which ever signals levels to. For eg. If one of the signals is of length 256
and the other spans 51 samples, then we could only take 256 point DFT. So the output of IDFT would be
containing 256 samples instead of 306 samples, which follows N1+N2 1 where N1 & N2 are the
lengths 256 and 51 respectively of the two inputs. Thus the output which should have been 306 samples
long is fitted into 256 samples. The256 points end up being a distorted version of the correct signal. This
process is called circular convolution.
Program:
/* program to implement circular convolution */
#include<stdio.h>
int m,n,x[30],h[30],y[30],i,j, 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 length of both sequences are not equal*/
{
if(m>n)
/* Pad the smaller sequence with zero*/
{
for(i=n;i<m;i++)
h[i]=0;
n=m;
}
for(i=m;i<n;i++)
x[i]=0;
DSP Lab

15 of 99

GRIET-ECE

16 of 99

m=n;
}
y[0]=0;
a[0]=h[0];
for(j=1;j<n;j++)
a[j]=h[n-j];

/*folding h(n) to h(-n)*/

/*Circular convolution*/
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
/*circular shift*/
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
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]);
}

/*displaying the result*/;

OUTPUT:
Enter the length of the first sequence
4
Enter the length of the second sequence
3
Enter the first sequence
1234
Enter the second sequence
123
The circular convolution is
18 16 10 16

DSP Lab

16 of 99

GRIET-ECE

17 of 99

DSP Lab

17 of 99

GRIET-ECE

18 of 99

DSP Lab

18 of 99

GRIET-ECE

19 of 99

4. FIR Filters
Aim:
To verify FIR filters.
Equipment Required:
Operating System
Constructor
Software

Windows XP
Simulator
CCStudio 3 & MATLAB 7.5

Theory:
A Finite Impulse Response (FIR) filter is a discrete linear time-invariant system whose output is based
on the weighted summation of a finite number of past inputs. An FIR transversal filter structure can be
obtained directly from the equation for discrete-time convolution.
( )

( ) (

In this equation, x(k) and y(n) represent the input to and output from the filter at time n. h(n-k) is the
transversal filter coefficients at time n. These coefficients are generated by using FDS (Filter Design
Software or Digital filter design package).
FIR filter is a finite impulse response filter. Order of the filter should be specified. Infinite response is
truncated to get finite impulse response, placing a window of finite length does this Types of windows
available are Rectangular, Barlett, Hamming, Hanning, Blackmann window etc. This FIR filter is an all
zero filter.
Program:
#include<stdio.h>
#include<math.h>
#define pi 3.1415
int n,N,c;
float wr[64],wt[64];
void main()
{
printf("\n enter no. of samples,N= :");
scanf("%d",&N);
printf("\n enter choice of window function\n 1.rect \n 2. triang \n c= :");
scanf("%d",&c);
printf("\n elements of window function are:");
switch(c)
{
case 1:
for(n=0;n<=N-1;n++)
{
wr[n]=1;
printf(" \n wr[%d]=%f",n,wr[n]);
}
break;
DSP Lab

19 of 99

GRIET-ECE

20 of 99

case 2:
for(n=0;n<=N-1;n++)
{
wt[n]=1-(2*(float)n/(N-1));
printf("\n wt[%d]=%f",n,wt[n]);
}
break;
}
}
Result:

DSP Lab

20 of 99

GRIET-ECE

21 of 99

DSP Lab

21 of 99

GRIET-ECE

22 of 99

DSP Lab

22 of 99

GRIET-ECE

23 of 99

5. IIR Filters
Aim:
To design and implement IIR (LPF/HPF) filters.
Equipment Required:
Operating System
Construct
Software

Windows XP
Simulator
CCStudio 3 & MATLAB 7.5

Theory:
The IIR filter can realize both the poles and zeroes of a system because it has a rational transfer function,
described by polynomials in z in both the numerator and the denominator

( )

The difference equation for such a system is described by the following:


( )
(
)
(
)
M and N are order of the two polynomials bk and ak are the filter coefficients. These filter
coefficients are generated using FDS (Filter Design software or Digital Filter design package).IIR filters
can be expanded as infinite impulse response filters. In designing IIR filters, cutoff frequencies of the
filters should be mentioned. The order of the filter can be estimated using butter worth polynomial.
Thats why the filters are named as butter worth filters. Filter coefficients can be found and the response
can be plotted.
C-Program iirfilters:
#include<stdio.h>
#include<math.h>
int i,w,wc,c,N;
float H[100];
float mul(float, int);
void main()
{
printf("\n enter order of filter ");
scanf("%d",&N);
printf("\n enter the cutoff freq ");
scanf("%d",&wc);
printf("\n enter the choice for IIR filter 1. LPF 2.HPF ");
scanf("%d",&c);
switch(c)

DSP Lab

23 of 99

GRIET-ECE

24 of 99

{
case 1:
for(w=0;w<100;w++)
{
H[w]=1/sqrt(1+mul((w/(float)wc),2*N));
printf("H[%d]=%f\n",w,H[w]);
}
break;
case 2:
for(w=0;w<=100;w++)
{
H[w]=1/sqrt(1+mul((float)wc/w,2*N));
printf("H[%d]=%f\n",w,H[w]);
}
break;
}
}
float mul(float a,int x)
{
for(i=0;i<x-1;i++)
a*=a;
return(a);
}
RESULT:

DSP Lab

24 of 99

GRIET-ECE

25 of 99

DSP Lab

25 of 99

GRIET-ECE

26 of 99

DSP Lab

26 of 99

GRIET-ECE

27 of 99

6. Echo with variable delay and feedback


AIM:
To verify echo with variable delay and feedback
EQUIPMENTS:
Operating System
Constructor
Software

Windows XP
Simulator
CCStudio 3

PROGRAM:
#include "DSK6713_AIC23.h"
// codec support
Uint32 fs=DSK6713_AIC23_FREQ_8KHZ;
// set sampling rate
#define DSK6713_AIC23_INPUT_MIC 0x0015
#define DSK6713_AIC23_INPUT_LINE 0x0011
Uint16 inputsource=DSK6713_AIC23_INPUT_MIC;// select input
#define BUF_SIZE 8000
// this sets maximum length of delay
short input,output,delayed;
short buffer[BUF_SIZE];
// storage for previous samples
int i;
//index into buffer
interrupt void c_int11()
// interrupt service routine
{
input = input_left_sample();// read new input sample from ADC
delayed = buffer[i];
// read delayed value from buffer
output = input + delayed;
// output sum of input and delayed values
buffer[i] = input // store new input and a fraction //of the delayed value in buffer
if(++i >= BUF_SIZE)//test for end of buffer
output_left_sample(output)
return;
//return from ISR
}
void main()
{
for(i=0 ; i<MAX_BUF_SIZE ; i++)
//clear buffer
buffer[i] = 0;
comm_intr();
// init DSK, codec, McBSP
while(1);
//infinite loop}
RESULT: Audio input signal is given through MIC and echo of the same is observed with ear phones

DSP Lab

27 of 99

GRIET-ECE

28 of 99

DSP Lab

28 of 99

GRIET-ECE

29 of 99

Lab-31 Linear and circular convolution and DFT


Objective To perform linear and circular convolution of sequences and explore their relationship to
polynomial multiplication.
Background The formulae for linear and circular convolution are:
( )

( )

( )

( )

( )

( )

( )

( ) ((

To Do
A. Evaluate, by hand, the linear convolution of the sequences
{
}. Compare with the MATLAB solution.
B. Perform the circular convolution of the sequences
compare with the MATLAB solution.

( )

( )

{ ( )

( )

( ) and

C. Evaluate, by hand, the linear convolution of the sequences


convolution. Compare with the MATLAB solution.
D. Verify
( )}.

))

((

))

} and

( )

( )

( ) by hand (matrix method) and


( ) and

( )}, or equivalently,

( ) using circular
( )

( )} = { ( )

E. [Polynomial multiplication] Multiply the polynomials


( )
( )
Put the result in the form
( )
( ) ( )
} and show that the sequence ( ) is given
Give a general expression for the coefficients {
by the convolution of the coefficient sequences ( ) and ( ). Verify for
( )
( )

A1. Linear convolution by hand.


A2. Linear convolution, using the conv function. In the subplots we have zero-padded all sequences so
as, firstly, to extend the plots and, secondly, to line up all sequences on a matching time scale. Whether
we convolve the zero-padded or non-zero-padded sequences the result is the same.
x1n = [1, 2, 2, 1, 1]; x2n = [3, 2, 1]; nz=2; %Extra zeros
yn = conv(x1n, x2n), % Linear convolution
ynz=[yn, zeros(1, nz)];% Zeros appended to yn to extend the plot
lynz= length(yn) + nz; % Length of yn = length(x1n) + length(x2n) - 1;
e1=lynz - length(x1n); x1z=[x1n, zeros(1, e1)]; % Zero-pad for plotting purpose
e2=lynz - length(x2n); x2z=[x2n, zeros(1, e2)]; % Zero-pad for plotting purpose
m = 0: lynz-1;
subplot (2, 2, 1), stem(m, x1z); title ('Sequence x1(n)'), xlabel ('n'), ylabel('x1(n)');
DSP Lab

29 of 99

GRIET-ECE

30 of 99

subplot (2, 2, 2), stem(m, x2z); title ('Sequence x2(n)'), xlabel ('n'), ylabel('x2(n)');
subplot (2, 2, 3), stem(m, ynz); % Bottom left plot
title ('Sequence y(n)'), xlabel ('n'), ylabel('y(n)');
The result of convolution is given by yn = [3 8 11 9 7 3 1]
Q. Would the result be affected if an arbitrary number of zeros were appended to the original sequences
prior to convolution?

B1. Circular convolution by hand.


}
( ) {
B2. Circular convolution in MATLAB. The sequences are ( ) {
( )
We shall need to zero-pad the shorter sequence to the same length as the longer one. Thus
{
}.

}.

x1 = [1, 2, 2, 1, 1], %Row vector


% x2m is a matrix made up of circularly shifted rows of x2z
x2m = [3, 0, 0, 1, 2; 2, 3, 0, 0, 1; 1, 2, 3, 0, 0; 0, 1, 2, 3, 0; 0, 0, 1, 2, 3], %5 x 5 Matrix
ycn= (x2m*x1')', %As calculated (x2m*x1') is a column vector
x1 =[1 2 2 1 1]
x2m =[3 0 0 1
3]
ycn =[6 9 11 9 7]

2; 2

1; 1

0; 0

Q. What function(s) does MATLAB offer to perform circular convolution?


Q. Write and execute a MATLAB program which, given the input sequences
( ) using the matrix method.
perform the circular convolution ( )

0; 0

( ) and

( ), will

C1. Circular convolution of zero-padded sequences to get the result of linear convolution. The length of
} and ( ) {
} are, respectively,
the sequences ( ) {
and
. The
( )
( ), is
length of the result of linear convolution, ( )
( ) {
} and
. Therefore, we zero-pad ( ) and ( ) each to a length of 7 giving
( ) {
}. We now perform circular convolution of
( ) and
( ). The result
( )
( )
( )
( ) and
( ). In other words,
will be the same as the linear convolution of ( ) and ( ) or of
( ) { ( )
( )} { ( )
( )}
( ). Thus
( )

( )

( )

((

))

( )

( )

((

))

x1 = [1, 2, 2, 1, 1, 0, 0], %Row vector


% x2m is a matrix made up of circularly shifted rows of x2z
x2m = [3 0 0 0 0 1 2; 2 3 0 0 0 0 1; 1 2 3 0 0 0 0; 0 1 2 3 0 0 0; 0 0 1 2 3 0 0; 0 0 0 1 2 3 0; 0 0 0
0 1 2 3], %7 x 7 Matrix
ycn= (x2m*x1')', %As calculated (x2m*x1') is a column vector
DSP Lab

30 of 99

GRIET-ECE

31 of 99

x1 = [1 2 2 1 1 0 0]
x2m =[3 0 0 0 0 1 2; 2 3 0
3 0 0 0; 0 0 1 2 3 0 0; 0
ycn = [3 8 11 9 7 3 1]

0
0

0
0

0
1

C2. Linear convolution of the original (unpadded) sequences,


( ) {
} and
( ) {
sequences,
same result.

1; 1 2 3
2 3 0; 0

0
0

0
0

0
0

0; 0 1 2
1 2 3]

( ) and ( ), or of the zero-padded


}, should, of course, yield the

x1n = [1, 2, 2, 1, 1, 0, 0]; x2n = [3, 2, 1, 0, 0, 0, 0]; nz=2; %Extra zeros


yn = conv(x1n, x2n), % Linear convolution
ynz=[yn, zeros(1, nz)];% Zeros appended to yn to extend the plot
lynz= length(yn) + nz; % Length of yn = length(x1n) + length(x2n) - 1;
e1=lynz - length(x1n); x1z=[x1n, zeros(1, e1)]; % Zero-pad for plotting purpose
e2=lynz - length(x2n); x2z=[x2n, zeros(1, e2)]; % Zero-pad for plotting purpose
m = 0: lynz-1;
subplot (2, 2, 1), stem(m, x1z); % Top left plot
title ('Sequence x1(n)'), xlabel ('n'), ylabel('x1(n)');
subplot (2, 2, 2), stem(m, x2z); % Top right plot
title ('Sequence x2(n)'), xlabel ('n'), ylabel('x2(n)');
subplot (2, 2, 3), stem(m, ynz); % Bottom left plot
title ('Sequence y(n)'), xlabel ('n'), ylabel('y(n)');
yn = [3

11

0]

{ ( )
( )} or, equivalently,
{ ( ) ( )} = { ( )
D1. Verify ( ) ( ) =
( )}.
} and ( ) {
}. We shall need to zero-pad the
The sequences are ( ) {
( ) {
}. The following
shorter sequence to the same length as the longer one. Thus
MATLAB program
1. Computes the DFTs of ( ) and ( ) resulting in ( ) and
( ) ( )
2. Multiplies the DFTs resulting in ( )
( )
3. Computes the IDFT of the product, resulting in ( )

( )
{ ( )}

x1n = [1, 2, 2, 1, 1]; x2n = [3, 2, 1, 0, 0];


X1k=fft(x1n), X2k=fft(x2n), Yk=X1k.*X2k,
yn=ifft(Y)
X1k =[7
X2k =[6
Yk =[42
yn =[6

(-0.5000 - 1.5388i) (-0.5000 + 0.3633i) (-0.5000 - 0.3633i) (-0.5000 + 1.5388i)]


(2.8090 - 2.4899i) (1.6910 - 0.2245i) (1.6910 + 0.2245i) (2.8090 + 2.4899i)]
(-5.2361 - 3.0777i) (-0.7639 + 0.7265i) (-0.7639 - 0.7265i) (-5.2361 + 3.0777i)]
9 11 9 7]

E. Polynomial multiplication.

DSP Lab

31 of 99

GRIET-ECE

32 of 99

DSP Lab

32 of 99

GRIET-ECE

33 of 99

Lab-32 Filtering through DFT/FFT


Objective To perform digital filtering in the frequency domain using DFT.
Background The signal ( ) may be filtered in the time domain by implementing the filter difference
equation. Alternatively, where it is possible to process ( ) in blocks or off-line, we may work in the
frequency domain: we first find the DFT, ( ). Next we find ( ) (f for filtered) by setting to zero
selected components of ( ) while preserving the symmetry properties of ( ) about the mid point,
, of the sequence. We then find ( ), the IDFT of ( ), which should be a filtered version of
( ). Thus ( ) has been filtered entirely in the frequency domain.
Consider a signal consisting of two frequency components, a 2 Hz and a 4 Hz, from which we
would like to filter out, say, the 2 Hz component. Given the signals ( )
and ( )
( )
( ) is sampled at 16 Hz ( =1/16) resulting in the discrete-time
, the signal ( )
sequence ( ). Then we find its 16-point DFT, i.e., the ( ) values. We then filter the signal in the
frequency domain: remove the DFT values that correspond to 2 Hz. The resulting DFT, ( ), when
inverse-transformed should give a sequence, ( ), that contains only the 4 Hz component.
( )
( )

When sampled at 16 Hz, the 2 Hz and 4 Hz components have digital frequencies of 1/8 cycles/sample
and 1/4 cycles/sample.
To Do
A. Demonstrate filtering in the frequency domain.
A1. Sketch the continuous-time and discrete-time signals using ( )
16 Hz.
A2. Sketch the 16-point DFT of the 2-Hz signal.
A3. Sketch the 16-point DFTof the composite (2-Hz + 4-Hz) signal.
A4. Filter out the 4 Hz component.
B. Generate the continuous-time signal

( ) from the discrete-time sequence

sampled at

( ).

C. Repeat A and B if the signal ( )


is to be sampled at 16
Hz and filtered using the DFT method so as to eliminate all frequencies outside the range
.

DSP Lab

33 of 99

GRIET-ECE

34 of 99

A1. Sketch the signals ( )


period. Sketch the sequences

( )

, ( )
( )
( )

( )

, and ( )
( )
( )
( )

In MATLAB the following segment plots the three functions

( )

( ) over a 1-second
.
( ).

t = 0:1/160:1.0; x1t = cos(2*pi*2*t); x2t = cos(2*pi*4*t); xt = x1t + x2t;


%
subplot(3,1,1), plot(t,x1t); xlabel('t, sec.'), ylabel('x1(t)'); title('x1(t) = cos 2\pi2t');
%
subplot(3,1,2), plot(t,x2t); xlabel('t, sec.'), ylabel('x2(t)'); title('x2(t) = cos 2\pi4t');
%
subplot(3,1,3), plot(t,xt); xlabel('t, sec.'), ylabel('x(t)');
title('x(t) = cos 2\pi2t + cos 2\pi4t');
x1(t) = cos 22t

x1(t)

1
0
-1

0.1

0.2

0.3

0.4

0.5
0.6
t, sec.
x2(t) = cos 24t

0.7

0.8

0.9

0.1

0.2

0.3

0.4

0.5
0.6
0.7
t, sec.
x(t) = cos 22t + cos 24t

0.8

0.9

0.1

0.2

0.3

0.4

0.5
t, sec.

0.8

0.9

x2(t)

1
0
-1

x(t)

2
0
-2

0.6

0.7

The sequence values are:


( )

( )
( )

( )

( )

Note that there are 16 samples per second. The 16-sample record above occupies a period of one second.
In MATLAB the following segment plots the sequences ( ), ( ) and ( ).
DSP Lab

34 of 99

GRIET-ECE

35 of 99

%t = 0: 1/160: 1.0; x1t = cos(2*pi*2*t); x2t = cos(2*pi*4*t); xt = x1t + x2t;


%
n = 0:16; x1n = cos(pi*n/4); x2n = cos(pi*n/2); xn = x1n + x2n,
%
subplot(3,1,1), stem(n,x1n); xlabel('n'), ylabel('x1(n)'); title('x1(n) = cos(\pin/4)');
%
subplot(3,1,2), stem(n,x2n); xlabel('n'), ylabel('x2(n)'); title('x2(n) = cos(\pin/2)');
%
subplot(3,1,3), stem(n,xn); xlabel('n'), ylabel('x(n)');
title('x(n) = cos(\pin/4)+ cos(\pin/2)');
The sequence is ( ) = {2 0.7071 -1 -0.7071 0 -0.7071 -1 0.7071 2 0.7071 -1 -0.7071 0 -0.7071
-1 0.7071}
x1(n) = cos(n/4)

x1(n)

1
0
-1

8
10
n
x2(n) = cos(n/2)

12

14

16

12

14

16

12

14

16

x2(n)

1
0
-1

8
10
n
x(n) = cos(n/4)+ cos(n/2)

x(n)

2
0
-2

8
n

10

Q. Indicate the digital frequencies, record length of sequence and the frequency resolution of the
corresponding DFT.

DSP Lab

35 of 99

GRIET-ECE

36 of 99

A2. Sketch the 16-point DFT

( ). Note

( )

) for

In this case the DFT ( ) is real valued for all . Accordingly, all phase angle values should be zero,
( )
that is,
. However,
o If the input is listed explicitly by specifying all 16 values MATLAB computes all the phase
angles correctly (as zero).
o If the input sequence ( ) is implicitly specified by a statement like n = 0:15; x1n =
cos(pi*n/4) then the MATLAB output shows non-zero phase at several points. We shall
tentatively attribute this to finite precision effects within MATLAB.
Thus two sets of MATLAB plots for the magnitude and phase angle of ( ) are shown below.
First the explicit listing: Input specified as x1n = [1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2), 1,
1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2)]
Fs = 16; N = 16; % Sampling rate & Number of points
% Specify input sequence values explicitly
x1n = [1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2), 1, 1/sqrt(2), 0, -1/sqrt(2), -1, 1/sqrt(2), 0, 1/sqrt(2)],
% Magnitude and Phase of X1k
X1k = fft(x1n); MX1k = abs(X1k), PX1k = angle(X1k),
n = 0: (N-1); F = (Fs/N)*n;
subplot(2,1,1), stem(F, abs(X1k)); axis([0 16 0 10]),
xlabel('Frequency, Hz'), ylabel('|X1(k)|'); title ('Magnitude of X1(k)');
subplot(2,1,2), stem(n, angle(X1k)); axis([0 16 -4 4]),
xlabel('k'), ylabel('<X1(k)'); title ('Phase of X1(k)');
Magnitude of X1(k)

|X1(k)|

10

8
10
Frequency, Hz
Phase of X1(k)

12

14

16

12

14

16

<X1(k)

2
0
-2
-4

8
k

10

The 2 Hz component is indicated by the pair ( )


and ( )
. Recall properties of symmetry
about the midpoint of the sequence ( ). Recall also that for a sampling rate of 16 Hz the maximum
allowed frequency is 8 Hz.

DSP Lab

36 of 99

GRIET-ECE

37 of 99

A3. Sketch the 16-point DFT ( ) for ( )


The 16-point sequence is
( )
( )

) for

( )

The magnitude and phase of ( ) are plotted below. Note that ( ) is specified by an explicit list.
(Implicit specification with n = 0:15; xn = cos(pi*n/4) + cos(pi*n/2) is problematic.)
Fs = 16; N = 16; % Sampling rate & Number of points
% Specify input sequence values explicitly
xn = [2, 1/sqrt(2), -1, -1/sqrt(2), 0, -1/sqrt(2), -1, 1/sqrt(2), 2, 1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2), -1, 1/sqrt(2)],
% MXk = magnitude of Xk, PXk = phase angle of Xk
Xk = fft(xn), MXk = abs(Xk), PXk = angle(Xk),
%
k = 0: (N-1); F = (Fs/N)*k;
subplot(2,1,1), stem(F, abs(Xk)); axis([0 16 0 10]),
xlabel('Frequency, Hz'), ylabel('|X(k)|'); title ('Magnitude of X(k)');
subplot(2,1,2), stem(k, angle(Xk)); axis([0 16 -4 4]),
xlabel('k'), ylabel('<X(k)'); title ('Phase of X(k)');
Magnitude of X(k)

|X(k)|

10

8
10
Frequency, Hz
Phase of X(k)

12

14

16

12

14

16

<X(k)

2
0
-2
-4

8
k

10

The 2 Hz component is indicated by the pair ( )


and ( )
(symmetry considerations) and
(
)
(
)
the 4 Hz component by the pair
and
. Recall that for a sampling rate of 16 Hz the
maximum allowed frequency is 8 Hz.

DSP Lab

37 of 99

GRIET-ECE

38 of 99

(
)
(
) so that the 4 Hz component (the
A4. [Filtering] Filter the sequence ( )
(
)) is removed. From the resulting { ( )} find and plot ( ).
Solution In terms of the DFT, ( ), this means setting both ( ) and ( ) to zero to preserve the
symmetry properties of the DFT. The resulting DFT sequence, ( ), is given by
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 8 0 8 0 0 0 0 0 0 0 8 0 8 0
0 0 8 0 0 0 0 0 0 0 0 0 0 0 8 0

( )
( )
We next find

( ) the inverse DFT of

( )

}:

Xfk = [0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0],


xfn = ifft(Xfk), N=length(Xfk), n = 0: (N-1);
stem(n, xfn); xlabel('n'), ylabel('xf(n)'); title ('Filtered signal xf(n)');
The output from MATLAB gives the filtered version of the discrete-time sequence as
( ) xfn = [1 0.7071 0 -0.7071 -1 -0.7071 0 0.7071 1 0.7071 0 -0.7071
-1 -0.7071 0 0.7071]
(
):
It is seen that ( ) is identical to the original 2 Hz component, ( )
( )
( )
{

Filtered signal xf(n)


1
0.8
0.6
0.4

xf(n)

0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

15

Q. Repeat with either ( ) or (

DSP Lab

) (but not both) set to zero and explain the outcome.

38 of 99

GRIET-ECE

39 of 99

B. Generate the continuous-time signal ( ) from the discrete-time sequence ( ) in B above.


Given the sequence, ( ), the operation of D/A conversion followed by low pass filtering should
( )). This may be viewed roughly as connecting the
produce the continuous-time signal ( ) (
dots, resulting in a continuous-like graph. In the following MATLAB segment the subplot (2, 1, 1) uses
the plot function to connect the dots resulting in the continuous-looking graph of ( ) while subplot (2,
1, 2) simply calibrates the horizontal axis in terms of time in seconds and re-labels the diagram
accordingly. In either plot the same 16 data points are connected.
Fs = 16; % Sampling frequency
Xfk = [0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0],
xfn = ifft(Xfk), N=length(Xfk), n = 0: (N-1);
%
subplot(2, 1, 1), plot(n, xfn); axis([0 16 -1 1]); xlabel('n'), ylabel('xf(n)');
title ('Filtered signal xf(n)');
%
subplot(2,1,2), plot(n/Fs, xfn); xlabel('Time, sec'), ylabel('xf(t)');
title ('Filtered signal xf(t)');
Filtered signal xf(n)
1

xf(n)

0.5
0
-0.5
-1

8
10
n
Filtered signal xf(t)

12

14

16

xf(t)

0.5
0
-0.5
-1

0.1

0.2

0.3

0.4

0.5
0.6
Time, sec

0.7

0.8

0.9

Q. Compare filtering in the time domain with filtering in the frequency domain.
C. Repeat A and B if the signal ( )
is to be sampled at 16
Hz and filtered using the DFT method so as to eliminate all frequencies outside the range
.

DSP Lab

39 of 99

GRIET-ECE

40 of 99

DSP Lab

40 of 99

GRIET-ECE

41 of 99

Lab-33 Picket fence effect in DFT


Objective This Lab deals with the Fourier spectra of finite-duration sequences using the signal
to demonstrate the picket fence effect in DFT.
Background The DTFT of the -point sequence ( ) is given by ( )
. The DFT of the sequence is obtained by taking samples of the DTFT:
( )

( )

( )

We may either use fft to compute the DFT or use freqz to compute the DTFT and take samples of it to
arrive at the DFT, the latter approach being more insightful. The resulting set of DFT values, ( ), thus
depends on the particular set of frequencies where ( ) is sampled. The spectrum ( ) is, in effect,
viewed through a picket fence. Therefore when a DFT is computed it is possible that spectral
components located at frequencies other than the selected set may be missed (obscured by the pickets).
This is known as the picket-fence effect.
To minimize the picket-fence effect we increase the number of DFT points while holding the
(time-domain) sampling rate fixed. This increases the record length and hence the spectral resolution of
the DFT.
To Do
A. For the -point sequence ( ) obtained by sampling the signal ( )
find the DTFT ( ( )) and the DFT ( ( )).
A1. DTFT of 8-point sequence and 8-point DFT
A2. The 8-point DFT using the function fft
A3. DTFT of 16-point zero-padded sequence
A4. 16-point DFT with zero-padding using the function fft
A5. 16-point DFT without zero-padding using the function fft

at 16 Hz starting

B. Repeat A using a sampling frequency of 12 Hz.

DSP Lab

41 of 99

GRIET-ECE

42 of 99

A1. The signal ( )


, when sampled at 16 Hz, gives ( )
(
). The samples in the
8-point sequence ( ) = {1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2)} are spaced (1/16)-second
apart over a record length of 0.5 sec. (one cycle of the cosine). The average value of the sequence (the
dc component) is zero.
The DTFT, ( ), of the 8-point sequence is a continuous function of :
( )

( )

( )

( )

( )

[
]=[ ( ) ( )
The numerator and denominator coefficients are
( )] and
[ ]
. The DTFT is computed using the function freqz and plotted below (Xw). Further, the
DTFT is sampled at 8 points (Xw2) resulting in the 8-point DFT (
, spaced 2 Hz apart).
In the two sub-plots the horizontal axes are calibrated both in Hz and in the frequency index, .
% DTFT of { ( )} ={1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2)}
Fs= 16; N= 8; F0= Fs/N; w0= 2*pi/N; %Sampling freq; No. of points; Resolution
b=[1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2)]; %Numerator coefficients
a=[1]; %Denominator coefficients
w=0: pi/256: 2*pi; k=w/w0; F=k*F0; Xw=freqz(b, a, w); %DTFT
w2=0: 2*pi/N: 2*pi; k2=w2/w0; F2=k2*F0; Xw2=freqz(b, a, w2); %DFT
subplot(2, 1, 1), stem(F2, abs(Xw2), 'k'); hold on
plot(F, abs(Xw)); legend ('Samples of DTFT', 'DTFT', 'Location', 'North' );
xlabel('F, Hz'), ylabel('|X(k)|'); grid; title('Magnitude'); hold off
subplot(2, 1, 2), plot(k, angle(Xw)); title('Phase');
xlabel('k'), ylabel('<X(k)'); grid
Magnitude
6
Samples of DTFT
DTFT

|X(k)|

X: 2
Y: 4

8
F, Hz
Phase

10

12

14

16

4
k

<X(k)

2
0
-2
-4

Q. From the plot read off all 8 DFT values. What frequency components are present?

DSP Lab

42 of 99

GRIET-ECE

43 of 99

A2. Compute the 8-point DFT using the function fft and compare with the result of A1.
Fs= 16; N= 8; F0= Fs/N; %Sampling freq; No. of points; Resolution
xn = [1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2)];
Xk = fft(xn), MXk = abs(Xk); AXk = angle(Xk); %DFT, Magnitude and Phase
%
k = 0:7; F = k*F0; %
subplot(2,1,1), stem(F, abs(Xk)); axis([0 16 0 5])
xlabel('Frequency, Hz (= k*F0)'), ylabel('|X(k)|'); title ('Magnitude of X(k)');
subplot(2,1,2), stem(k, angle(Xk)); xlabel('k'), ylabel('<X(k)'); axis([0 8 -1 1])
title ('Phase of X(k)');
The table below shows that the 8-point DFT contains a component at 2 Hz, (
1 and 7, based
on symmetry properties), that is ( ) and ( ). The DFT values are all real numbers symmetrically
disposed about
, the center of symmetry. The frequency resolution is
Hz. Note
that with a sampling frequency of 16 Hz the maximum frequency possible is 8 Hz corresponding to
. For easier comparison the plots go up to
8.
k=0

Hz 0
{0
( )
{0
| ( )|
{0
( )

1 2 3

4
(

2
4
4
0

4
0
0
0

6
0
0
0

)
8
0
0
0

10 12 14 16
0 0 4} 0
0 0 4} 0
0 0 0} 0

Magnitude of X(k)
5

|X(k)|

4
3
2
1
0

6
8
10
Frequency, Hz (= k F0)
Phase of X(k)

12

14

16

<X(k)

0.5
0
-0.5
-1

4
k

Q. What frequency components do you expect to see in the DFT?

DSP Lab

43 of 99

GRIET-ECE

44 of 99

A3. [DTFT of the 16-point zero-padded sequence] The 8-sample sequence of the 2-Hz cosine,
(sampled at 16 Hz) is concatenated with 8 zeros:
( ) = {1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2), 0, 0, 0, 0, 0, 0, 0, 0}
Zero-padding improves the spectral resolution by increasing the record length. Demonstrate that this 16point sequences DTFT is identical to that of the 8-point sequence in A1. Take 16 samples of this DTFT
and compare the resulting 16-pont DFT to the 8-point DFT.
% DTFT of xnz, the zero-padded sequence
% xnz ={1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2), zeros(1, 8)}
Fs= 16; N= 16; %Sampling frequency & No. of points
F0= Fs/N; w0= 2*pi/N; %Resolution
xn=[1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2)],
b=[xn, zeros(1, 8)], a=[1]; %Numerator & denominator coefficients
w=0: pi/256: 2*pi; k=w/w0; F=k*F0; Xw=freqz(b, a, w); %DTFT
w2=0: 2*pi/N: 2*pi; k2=w2/w0; F2=k2*F0; Xw2=freqz(b, a, w2); %DFT
subplot(2, 1, 1), stem(F2, abs(Xw2), 'k'), hold on, plot(F, abs(Xw));
legend ('Samples of DTFT', 'DTFT', 'Location', 'North' ), legend boxoff;
xlabel('F, Hz'), ylabel('|X(k)|'); grid; title('Magnitude'), hold off
subplot(2, 1, 2), plot(k, angle(Xw)); title('Phase');
xlabel('k'), ylabel('<X(k)'); grid
Q. From the plot read off all 16 DFT values. What frequency components are present?

A4. [16-point DFT with zero-padding] The MATLAB function fft(xn, 16) will zero-pad the sequence
( ) to a length of 16 and compute the DFT of the resulting 16-point sequence. Zero-padding improves
the spectral resolution by increasing the record length. The 8-sample sequence of the 2-Hz cosine,
sampled at 16 Hz, is appended with 8 zeros:
( ) = {1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2), 0, 0, 0, 0, 0, 0, 0, 0}

Find the 16-point DFT using the function fft. The frequency resolution is
.
Fs= 16; N= 16; F0= Fs/N; %Sampling freq; No. of points; Resolution
xn=[1, 1/sqrt(2), 0, -1/sqrt(2), -1, -1/sqrt(2), 0, 1/sqrt(2)],
xnz=[xn, zeros(1, 8)], %Zero-padded sequence
n = 0:15;
subplot(3,1,1), stem(n, xnz);
xlabel('n'), ylabel('x(n)'); title ('Zero-padded sequence xnz');
Xk = fft(xn, 16), %x(n) is zero-padded to a length of 16
MXk = abs(Xk), AXk = angle(Xk), % Magnitude & Phase angle of Xk
k = 0:15; F=k*F0;
subplot(3,1,2), stem(F, abs(Xk)); axis([0 16 0 5])
xlabel('Frequency, Hz (= k*F0)'), ylabel('|X(k)|'); title ('Magnitude of X(k)');
subplot(3,1,3), stem(k, angle(Xk)); axis([0 16 -2 2])
xlabel('k'), ylabel('<X(k)'); title ('Phase of X(k)');
Q. What kind of symmetry does the sequence ( ) possess?
Q. List the MATLAB-generated DFT sequence ( ) and identify the 2 Hz component.
DSP Lab

44 of 99

GRIET-ECE

45 of 99

A5. [16-point DFT without zero-padding] Given all 16 sample values of a 2-Hz cosine, ( )
, obtained at a sampling rate of 16 Hz find its 16-point DFT using fft. The frequency resolution

of the DFT is
.
In the MATLAB segment below the sequence values are specified implicitly (by a statement
such as t = 0: 1/16: 15/16; xn = cos (2*pi*2*t) or n = 0: 1: 15; xn = cos (2*pi*2*n/Fs). Although the
MATLAB-generated DFT, ( ), is correct, the phase values,
( ), are incorrect (seems to happen
when ( ) is implicitly specified).
N=16; Fs=16; F0= Fs/N; %N points sampled at Fs Hz & Resolution
t = 0: 1/16: 15/16; xn = cos (2*pi*2*t), %Sequence specified implicitly
n = 0:15;
subplot(3,1,1), stem(n, xn); xlabel('n'), ylabel('x(n)'); title ('Sequence x(n)');
%
Xk = fft(xn), MXk = abs(Xk), AXk = angle(Xk), % Magnitude & Phase of Xk
k = 0:15; F=k*F0;
subplot(3,1,2), stem(F, abs(Xk)); axis([0 16 0 10])
xlabel('Frequency, Hz (= k*F0)'), ylabel('|X(k)|'); title ('Magnitude of X(k)');
subplot(3,1,3), stem(k, angle(Xk)); axis([0 16 -5 5])
xlabel('k'), ylabel('<X(k)'); title ('Phase of X(k)');
The DFT (reproduced from the MATLAB output) is
X(k) = {0, (-0+0i), (8-0i), (0-0i), (0-0i), (0-0i), (0-0i), (0+0i),
-0, (0-0i), (0+0i), (0+0i), (0+0i), (0+0i), (8+0i), (-0-0i)}
Q. What kind of symmetry does the sequence ( ) possess?
Q. List the MATLAB-generated DFT sequence ( ) and identify the 2 Hz component.
Q. Examine why the MATLAB-generated phase values are incorrect.
B1. An -point sequence is obtained by sampling the signal ( )
at 12 Hz starting at
(satisfies the sampling theorem). Demonstrate the picket fence effect by calculating the DTFT and from
it the -point DFT for ranging from 6 to 16. Compare the results.
(1) 6-point DFT. The statement n = 0: 1: 5; xn = cos (pi*n/3), produces the sequence ( )
{

}. The average value of the sequence (the dc component) is zero. The


samples are spaced (1/12)-second apart and the record length is 6(1/12) = 0.5 sec.
The DTFT, ( ), of the 6-point sequence is a continuous function of :
( )

( )

( )

( )

( )

[
]=[ ( ) ( )
The numerator and denominator coefficients are
( )] and
[ ]
. The DTFT is computed using the function freqz and plotted below (Xw). Xw2 represents
the 6 samples (of the DTFT) which make up the 6-point DFT (
, spaced 12/6 = 2 Hz
apart). In the two sub-plots the horizontal axes are calibrated both in Hz and in the frequency index, .
% DTFT of { ( )} ={1, 1/2, -1/2, -1, -1/2, 1/2}
Fs= 12; N= 6; F0= Fs/N; w0= 2*pi/N; %Sampling freq; No. of points; Resolution
b=[1, 1/2, -1/2, -1, -1/2, 1/2]; %Numerator coefficients
a=[1]; %Denominator coefficients
DSP Lab

45 of 99

GRIET-ECE

46 of 99

w=0: pi/256: 2*pi; k=w/w0; F=k*F0; Xw=freqz(b, a, w); %DTFT


w2=0: 2*pi/N: 2*pi; k2=w2/w0; F2=k2*F0; Xw2=freqz(b, a, w2); %6 points
subplot(2, 1, 1), stem(F2, abs(Xw2), 'k'); hold on
plot(F, abs(Xw)); legend ('Samples of DTFT', 'DTFT', 'Location', 'North' );
xlabel('F, Hz'), ylabel('|X(k)|'); grid; title('Magnitude'); hold off
subplot(2, 1, 2), plot(k, angle(Xw)); title('Phase');
xlabel('k'), ylabel('<X(k)'); grid
Q. What kind of symmetry does the sequence ( ) possess?
Q. List the DFT sequence ( ) and identify the 2 Hz component.
(2) 8-point DFT. The statement n = 0: 1: 7; xn = cos (pi*n/3), produces the sequence ( )
{

}. Unlike in the 6-point DFT the average value of the sequence


(the dc component) in this case is not zero. The samples are spaced (1/12)-second apart and the record
length is 8(1/12) = 2/3 sec.
The DTFT, ( ), of the 8-point sequence is a continuous function of :
( )
( )
( )
( ) ( )
[
]=[ ( ) ( )
The numerator and denominator coefficients are
( )] and
[ ]
. The DTFT is computed using the function freqz and plotted below (Xw). Xw2 represents
the 8 samples (of the DTFT) which make up the 8-point DFT (
, spaced 12/8 = 1.5 Hz
apart). In the two sub-plots the horizontal axes are calibrated both in Hz and in the frequency index, .
% DTFT of { ( )} ={1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2}
Fs= 12; N= 8; F0= Fs/N; w0= 2*pi/N; %Sampling freq; No. of points; Resolution
b=[1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2]; %Numerator coefficients
a=[1]; %Denominator coefficients
w=0: pi/256: 2*pi; k=w/w0; F=k*F0; Xw=freqz(b, a, w); %DTFT
w2=0: 2*pi/N: 2*pi; k2=w2/w0; F2=k2*F0; Xw2=freqz(b, a, w2); %6 points
subplot(2, 1, 1), stem(F2, abs(Xw2), 'k'); hold on
plot(F, abs(Xw)); legend ('Samples of DTFT', 'DTFT', 'Location', 'North' );
xlabel('F, Hz'), ylabel('|X(k)|'); grid; title('Magnitude'); hold off
subplot(2, 1, 2), plot(k, angle(Xw)); title('Phase');
xlabel('k'), ylabel('<X(k)'); grid
Q. What kind of symmetry does the sequence ( ) possess?
Q. List the DFT sequence ( ) and identify the 2 Hz component.
(3) 12-point DFT. The statement n = 0: 1: 11; x = cos (pi*n/3), produces the sequence ( )
{
}. The average value of the sequence (the dc
component) in this case is zero. The samples are spaced (1/12)-second apart and the record length is
12(1/12) = 1 sec.
The DTFT, ( ), of the 12-point sequence is a continuous function of :
( )
( )
( )
( ) ( )
DSP Lab

46 of 99

GRIET-ECE

47 of 99

[
]=[ ( ) ( )
The numerator and denominator coefficients are
( )] and
[ ]
. The DTFT is computed using the function freqz and plotted below (Xw). Xw2 represents
the 12 samples (of the DTFT) which make up the 12-point DFT (
, spaced 12/12 = 1 Hz
apart). In the two sub-plots the horizontal axes are calibrated both in Hz and in the frequency index, .
% DTFT of { ( )} ={1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2, -1/2, -1, -1/2, 1/2}
Fs= 12; N= 12; F0= Fs/N; w0= 2*pi/N; %Sampling freq; No. of points; Resolution
b=[1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2, -1/2, -1, -1/2, 1/2]; %Numerator coefficients
a=[1]; %Denominator coefficients
w=0: pi/256: 2*pi; k=w/w0; F=k*F0; Xw=freqz(b, a, w); %DTFT
w2=0: 2*pi/N: 2*pi; k2=w2/w0; F2=k2*F0; Xw2=freqz(b, a, w2); %6 points
subplot(2, 1, 1), stem(F2, abs(Xw2), 'k'); hold on
plot(F, abs(Xw)); legend ('Samples of DTFT', 'DTFT', 'Location', 'North' );
xlabel('F, Hz'), ylabel('|X(k)|'); grid; title('Magnitude'); hold off
subplot(2, 1, 2), plot(k, angle(Xw)); title('Phase');
xlabel('k'), ylabel('<X(k)'); grid
Q. What kind of symmetry does the sequence ( ) possess?
Q. List the DFT sequence ( ) and identify the 2 Hz component.
(4) 16-point DFT. The statement n = 0: 1: 15; x = cos (pi*n/3), produces the sequence ( )
{
}. The average value of the
sequence (the dc component) in this case is zero. The samples are spaced (1/12)-second apart and the
record length is 16(1/12) = 1.333 sec.
The DTFT, ( ), of the 16-point sequence is a continuous function of :
( )

( )

( )

( )

[
]=[ ( ) ( )
The numerator and denominator coefficients are
( )] and
[ ]
. The DTFT is computed using the function freqz and plotted below (Xw). Xw2 represents
the 16 samples (of the DTFT) which make up the 16-point DFT (
, spaced 12/16 = 3/4
Hz apart). In the two sub-plots the horizontal axes are calibrated both in Hz and in the frequency index,
.
% DTFT of { ( )} ={1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2, -1/2, -1}
Fs= 12; N= 16; F0= Fs/N; w0= 2*pi/N; %Sampling freq; No. of points; Resolution
b=[1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2, -1/2, -1]; %Numerator
coefficients
a=[1]; %Denominator coefficients
w=0: pi/256: 2*pi; k=w/w0; F=k*F0; Xw=freqz(b, a, w); %DTFT
w2=0: 2*pi/N: 2*pi; k2=w2/w0; F2=k2*F0; Xw2=freqz(b, a, w2); %6 points
subplot(2, 1, 1), stem(F2, abs(Xw2), 'k'); hold on
plot(F, abs(Xw)); legend ('Samples of DTFT', 'DTFT', 'Location', 'North' );
xlabel('F, Hz'), ylabel('|X(k)|'); grid; title('Magnitude'); hold off
subplot(2, 1, 2), plot(k, angle(Xw)); title('Phase');
xlabel('k'), ylabel('<X(k)'); grid
Q. What kind of symmetry does the sequence ( ) possess?
DSP Lab

47 of 99

GRIET-ECE

48 of 99

Q. List the DFT sequence ( ) and identify the 2 Hz component.


B2. The signal ( )
is sampled at 12 Hz (satisfies the sampling theorem). Using the
MATLAB function fft calculate the -point DFT for = 6, 8 and 12. Compare the results and illustrate
the picket fence effect.
(1) 6-point DFT. The statement n = 0: 1: 5; xn = cos (pi*n/3), produces the sequence ( )
{

}. The average value of the sequence (the dc component) is zero. The


} which does show a
frequency resolution is = 12/6 = 2 Hz. The DFT is ( ) {
component at 2 Hz.
N=6; Fs=12; F0=Fs/N; %N points sampled at Fs Hz & Resolution
xn = [1, 1/2, -1/2, -1, -1/2, 1/2], %List the sequence values explicitly
n = 0: 5; %Plot of sequence
subplot(3,1,1), stem(n, xn); xlabel('n'), ylabel('x(n)');
title ('6-point sequence x(n)');
%
Xk = fft(xn), MXk = abs(Xk), AXk = angle(Xk), % Magnitude & Phase of X(k)
k = 0:5; F=k*F0;
subplot(3,1,2), stem(F, abs(Xk)); axis([0 12 0 4])
xlabel('Frequency, Hz (= k*F0)'), ylabel('|X(k)|'); title ('Magnitude of X(k)');
subplot(3,1,3), stem(k, angle(Xk)); axis([0 6 -1 1])
xlabel('k'), ylabel('<X(k)'); title ('Phase of X(k)');

Q. What kind of symmetry does the sequence possess about its midpoint (
)?
Q. List the MATLAB-generated DFT sequence ( ) and identify the 2 Hz component.
(2) 8-point DFT. The statement n = 0: 1: 7; xn = cos (pi*n/3), produces the sequence ( )
{

}. Unlike in the 6-point case the average value of the sequence

(the dc component) in this case is not zero. The frequency resolution is


Hz. The
DFT is ( ) {1.5, (2.5607 + j2.5607), -j1.5, (0.4393 - j0.4393), 0.5, (0.4393 + j0.4393), j1.5, (2.5607
- j2.5607)}.
The frequency resolution being 1.5 Hz and since the DFT spectral components only occur at
integer multiples of 1.5 Hz it is not possible to know that there is a spectral component at 2 Hz. This is
akin to looking at a scene through a picket fence. You only see what is visible through the gaps between
the pickets, the rest being blocked from view by the pickets. It is as though the 2 Hz component is
blocked from view by a picket.
N=8; Fs=12; F0=Fs/N; %N points sampled at Fs Hz & Resolution
xn = [1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2], %List sequence values explicitly
n = 0:7; %Plot of sequence
subplot(3,1,1), stem(n, xn); xlabel('n'), ylabel('x(n)');
title ('8-point sequence x(n)');
%
Xk = fft(xn), MXk = abs(Xk), AXk = angle(Xk), % Magnitude & Phase of X(k)
DSP Lab

48 of 99

GRIET-ECE

49 of 99

k = 0:7; F=k*F0;
subplot(3,1,2), stem(F, abs(Xk)); axis([0 12 0 4])
xlabel('Frequency, Hz (= k*F0)'), ylabel('|X(k)|'); title ('Magnitude of X(k)');
subplot(3,1,3), stem(k, angle(Xk)); axis([0 8 -2 2])
xlabel('k'), ylabel('<X(k)'); title ('Phase of X(k)');
Q. What kind of symmetry does the sequence possess about its midpoint (
)?
Q. Identify the 2 Hz component. What is your best estimate of the frequency content of ( )?
Q. How do you explain the non-zero ( )?
(3) 12-point DFT. The statement n = 0: 1: 11; xn = cos (pi*n/3), produces the sequence ( )
{

}. Like in the 6-point case the average


value of the sequence (the dc component) in this case is zero. The frequency resolution is

Hz. The DFT is ( ) {0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 6, 0} which does show a component at 2


Hz.
N=12; Fs=12; F0=Fs/N; %N points sampled at Fs Hz & Resolution
xn = [1, 1/2, -1/2, -1, -1/2, 1/2, 1, 1/2, -1/2, -1, -1/2, 1/2], %List explicitly
n = 0:11; %Plot of sequence
subplot(3,1,1), stem(n, xn); xlabel('n'), ylabel('x(n)');
title ('12-point sequence x(n)');
%
Xk = fft(xn), MXk = abs(Xk), AXk = angle(Xk), % Magnitude & Phase of X(k)
k = 0:11; F=k*F0;
subplot(3,1,2), stem(F, abs(Xk)); axis([0 12 0 14])
xlabel('Frequency, Hz (= k*F0)'), ylabel('|X(k)|'); title ('Magnitude of X(k)');
subplot(3,1,3), stem(k, angle(Xk)); axis([0 12 -12 12])
xlabel('k'), ylabel('<X(k)'); title ('Phase of X(k)');
Q. What kind of symmetry does the sequence possess about its midpoint (
)?
Q. List the MATLAB-generated DFT sequence ( ) and identify the 2 Hz component.
Q. How do you explain that ( )
compared to case of
?

DSP Lab

49 of 99

GRIET-ECE

50 of 99

DSP Lab

50 of 99

GRIET-ECE

51 of 99

Lab-41 Frequency response and time-domain simulation of IIR-LP filter


Objective To examine and simulate the IIR filter ( )
sampling rate of 150 Hz.

( )

Background The impulse response of the system ( )

transfer function is

( )

. The frequency response is

) which operates at a

( ) is ( )
(

It is useful to find the upper and lower bounds for the gain at dc and at
parameter . The magnitude and phase are:
| ( )|
The dc gain,

For
have

( ) at

(
, is

| ( )|

| ( )|

( )

( )|

we have 1

| ( )|

| ( )|
( )
For

| ( )|
( )|

( )

( ). The

as a function of the

(
(

. Similarly, at

, the high end of the frequency scale, we

)
(

we have 1 | ( )|
.
In calibrating the horizontal axis in terms of the cyclic frequency, , we use the relation
from which the point
corresponds to
.

To Do
A. Compute the frequency response by hand at a few critical points and plot it. Compare with
MATLAB. Calibrate the frequency axis in both digital and analog units ( rad/sample and Hz).
B. Construct a signal ( ) as the sum of two cosinusoids of frequencies 2 Hz and 75 Hz and appropriate
relative amplitudes. Generate time domain plots of ( ) and ( ) and comment on the gain (or
attenuation) of each frequency component.
C. In B, at the low frequency end we use either dc or something very close to it. At the high end, from
the viewpoint of a successful demo, we choose 75 Hz (the highest possible) which, since it has just 2
samples per cycle, keeps the diagram least cluttered. Further, we use cosines rather than sines. Repeat
with the 75 Hz cosine replaced with a 75 Hz sine. Comment on the results.
D. Repeat A if the sampling frequency were 3 kHz (instead of 150 Hz). In what way does this affect the
plots you obtained above?
( )
(
)
E. Indicate what kind of filter is represented by ( )
comparison with that in A. [Cascade, gain is squared, band width is reduced.]

DSP Lab

51 of 99

) in

GRIET-ECE

52 of 99

we have ( )

A1. Hand calculation For


transfer function is ( )

. The frequency response is

( )

( ) with ( )

( )

( ). The
. The

( )

( )
magnitude and phase are | ( )|
and
(
).

( )|
The value of ( ) at
is | ( )|
and
. Similarly, at
and
( )|
have | ( )|
.
In calibrating the horizontal axis in terms of the cyclic frequency, , we use the relation
from which the point
corresponds to
.

we

Q. What is the gain at 2 Hz? At 75 Hz?


A2. Plots of | | and
may be generated by the MATLAB function freqz. The frequency range of
interest is 0 to
rad., corresponding to 0 to 150 Hz. The MATLAB format is
( )
( )
( )
(
)
( )
( )
( )
Here the vectors and specify, respectively, the numerator and denominator coefficients. In our
example ( )
, ( )
, and ( )
. In the MATLAB segment below we have used w for
( ), are in radians.
and
. Both and the phase,
%Frequency response
Fs=150; b = [1]; a = [1, -0.5]; %Numerator & denominator coefficients
w = -pi: pi/256: pi; F = Fs*w/(2*pi); % Digital & analog frequency
[Hw] = freqz(b, a, w);
subplot(2, 1, 1), plot(F, abs(Hw));
xlabel('Frequency, Hz'), ylabel('Magnitude of H(\omega)'); grid
subplot(2, 1, 2), plot(F, angle(Hw));
xlabel('Frequency, Hz'), ylabel('Phase of H(\omega)'); grid

Magnitude of H()

2
X: 17.29
Y: 1.413

1.5

0.5
-80

-60

-40

-20

0
20
Frequency, Hz

40

60

80

-60

-40

-20

0
20
Frequency, Hz

40

60

80

Phase of H()

1
0.5
0
-0.5
-1
-80

Q. Indicate what frequencies you would consider as low (or high or medium) frequencies. Does this
system approximate a low pass or band pass etc. filter?
Q. What is the (3 dB) bandwidth?
Q. What is the gain at 2 Hz? At 75 Hz?
DSP Lab

52 of 99

GRIET-ECE

53 of 99

Q. Indicate if the phase is linear or nonlinear.


Q. Test and indicate in what way the system ( )

( )

) is different.

(
) and
B1. Let the input consist of a low frequency and a high frequency component: ( )
( )
(
). Plot ( ), ( ) and ( )
( )
( ) over a quarter cycle of the lower
frequency.
% Continuous-time plots of x(t)
A=5; F1=2; B=1; F2=75; t = 0: 0.001: 0.2;
x1 = A*cos (2*pi*F1*t); x2 = B*cos (2*pi*F2*t); %2 Hz and 75 Hz
xt = x1 +x2; % 2 Hz + 75 Hz
% Choose a multiplot so that all waveforms are on the same scale
plot(t, x1, 'b', t, x2, 'r', t, xt, 'k'); title('Continuous-time plots');
legend ('2 Hz', '75 Hz', '2 Hz + 75 Hz');
xlabel ('Time, t, seconds'), ylabel('x1, x2 and x(t)');
Continuous-time plots
6
2 Hz
75 Hz
2 Hz + 75 Hz

x1, x2 and x(t)

-2

-4

-6

0.02

0.04

0.06

0.08
0.1
0.12
Time, t, seconds

0.14

0.16

0.18

0.2

B2. In the following MATLAB segment we show the input, both continuous and discrete time,
( )
( ).
% Plots of x(t) and x(n)
A=5; F1=2; B=1; F2=75; Fs=150;
t= 0: 0.001: 0.4; % 0.4 sec interval so continuous & discrete time scales align
xt = A*cos (2*pi*F1*t) + B*cos (2*pi*F2*t); % 2 Hz + 75 Hz
%
n = 0: 1: 60; % Choose n = 60 so continuous & discrete time scales align
x = A*cos(2*pi*F1*n/Fs) + B*cos(2*pi*F2*n/Fs); % 2 Hz + 75 Hz
subplot(2, 1, 1), plot(t, xt, 'b'); legend ('2 Hz + 75 Hz');
title ('Continuous-time signal x(t)');
DSP Lab

53 of 99

GRIET-ECE

54 of 99

xlabel ('Time, t, seconds'), ylabel('x(t)');


subplot(2, 1, 2), stem(n, x, 'b*'); legend ('(1/75) rad + (1/2) rad');
title ('Discrete-time signal x(n)');
xlabel ('n'), ylabel('x(n)');
Continuous-time signal x(t)
10
2 Hz + 75 Hz

x(t)

5
0
-5
-10

0.05

0.1

0.15
0.2
0.25
Time, t, seconds
Discrete-time signal x(n)

0.3

0.35

0.4

10
(1/75) rad + (1/2) rad

x(n)

5
0
-5
-10

DSP Lab

10

20

30
n

54 of 99

40

50

60

GRIET-ECE

55 of 99

B3. In the following MATLAB segment we show the operation of the filter. Both sequences
( )
( ) are shown. The low frequency component (2 Hz) is amplified and the high frequency
component (75 Hz) is attenuated.
% Plots of input x(n) and low-pass filter output y(n)
A=5; B=1; F2=75; Fs=150;
b = [1]; a = [1, -0.5]; %Numerator and denominator coefficients
n = 0: 1: 60; x = A*cos(2*pi*F1*n/Fs) + B*cos(2*pi*F2*n/Fs); % dc + 75 Hz
y = filter(b, a, x);
subplot(2, 1, 1), stem(n, x, 'b*'); axis([0, 60, -20, 10]);
legend ('2 Hz + 75 Hz'); title ('Discrete-time signal x(n)');
xlabel ('n'), ylabel('x(n)');
subplot(2, 1, 2), stem(n, y, 'ko'); axis([0, 60, -20, 10]);
legend ('Low pass filtered'); title ('Low pass filter output y(n)');
xlabel ('n'), ylabel('y(n)');
Discrete-time signal x(n)
10
2 Hz + 75 Hz

x(n)

-10

-20

10

20

30
40
n
Low pass filter output y(n)

50

60

10
Low pass filtered

y(n)

-10

-20

10

20

30
n

40

50

60

Q. On an approximate basis verify if the output amplitudes of the 2 and 75 Hz components agree with
the gain figures obtained in A.

DSP Lab

55 of 99

GRIET-ECE

56 of 99

B4. In B2 replace the 2 Hz cosine with dc (the lowest frequency). Plot both sequences ( )
( ).
Use the data cursor and verify that the dc is amplified and the high frequency component (75 Hz) is
attenuated.
% Plots of input x(n) and low-pass filter output y(n)
A=1; B=1; F2=75; Fs=150;
b = [1]; a = [1, -0.5]; %Numerator and denominator coefficients
n = 0: 1: 60; x = A + B*cos(2*pi*F2*n/Fs); % dc + 75 Hz
y = filter(b, a, x);
plot(n, x, 'b*', n, y, 'ko'); legend ('x(n)', 'y(n)');
xlabel ('n'), ylabel('x(n) and y(n)');

3
x(n)
y(n)
2.5

x(n) and y(n)

1.5

0.5

10

20

30
n

40

50

60

Q. Identify the input and output by connecting the dots.


On an approximate basis verify if the output amplitudes of the dc and 75 Hz components agree with the
gain figures obtained in A.
Ans. Eyeballing the above diagram, the dc level of the input (blue) is 1 while that of the output (black) is
about 2 which agrees with a dc gain of 2 calculated earlier. In a similar fashion the amplitude of the 75
Hz at the input (blue) is 1 while that of the output (black) is about 0.7 which agrees with | ( )|
calculated earlier.
Q. Try the demo with sines instead of cosines. Why does the demo fail if we use sines instead of
cosines?

DSP Lab

56 of 99

GRIET-ECE

57 of 99

C1. Repeat B4 with the cosine replaced with a sine.


% Plots of input x(n) and low-pass filter output y(n)
A=1; B=1; F2=75; Fs=150;
b = [1]; a = [1, -0.5]; %Numerator and denominator coefficients
n = 0: 1: 60; x = A + B*sin(2*pi*F2*n/Fs); % dc + 75 Hz
y = filter(b, a, x);
plot(n, x, 'b*', n, y, 'ko'); legend ('x(n)', 'y(n)');
xlabel ('n'), ylabel('x(n) and y(n)');
2.2
x(n)
y(n)

x(n) and y(n)

1.8

1.6

1.4

1.2

0.8

10

20

30
n

40

50

60

Q. Identify the input and output by connecting the dots.


Q. Why does the demo fail if we use a sine instead of a cosine (of 75 Hz)? Reconcile with the sampling
theorem.
Q. On an approximate basis verify if the output amplitude of the dc component agrees with the gain
figures obtained in A.
D1. Repeat A2 with sampling frequency equal to 3000 Hz.
Q. In what way does changing the sampling frequency from 150 Hz to 3000 Hz affect the magnitude
and phase versus frequency plots? Would the vertical axis have to be relabeled?
Q. What is the effect of changing the sampling frequency from 150 Hz to 3000 Hz on the time-domain
demonstration?
Q. Repeat for a sampling frequency of 8000 Hz.
E1. The frequency response is
( )
At dc: ( )
,

DSP Lab

( )
( )

. The magnitude and phase are| ( )|

57 of 99

GRIET-ECE

58 of 99

DSP Lab

58 of 99

GRIET-ECE

59 of 99

Lab-43 Frequency response of analog Butterworth low-pass filter


Objective Analog Butterworth filter transfer functions are readily available and are the starting point for
some IIR digital filter design. In this Lab you will learn low-pass (LP) filter design through the use of
MATLAB functions such as butter(N,Wn,'s'), where N is the filter order, Wn is the cut-off frequency in
rad/second, and 's' signifies analog design.
Background The transfer function and frequency response of the RC low pass filter below are given by
( )

( )

and

( )

( )

, respectively. The dc gain (at

) is 1 and is the

( )

maximum gain. The 3dB cut-off frequency or half-power point is


| (

rad/sec.
)|

Input = ( )

Output = ( )

0.707

, rad/sec

Nomenclature Further terminology of filter transfer functions is illustrated in figure below using a lowpass filter characteristic. Here
and
are the pass-band edge frequency and the corresponding gain
and
and
are the stop-band edge frequency and the corresponding gain.
| (

Transition band

)|
Pass
band

Stop
band

rad/sec
| (

)|
rad/sec

DSP Lab

59 of 99

GRIET-ECE

60 of 99

Normalized filter Adjusting the values of and so that


, we would have ( )
(
). This is a first order normalized low-pass Butterworth filter with a cut-off frequency
rad/sec and a dc gain of 1 at
. It serves as a prototype in further design.
Frequency transformation and magnitude scaling The normalized LP filter could be transformed to a
different LP filter with an arbitrary cut-off frequency of
rad/sec by the low pass to low pass
transformation
(
). Further, in addition to scaling on the frequency axis, the gain of this filter
could be scaled by an arbitrary multiplier, . For example, if we desire a cut-off frequency of, say,
rad/sec, and a dc gain of, say,
, the required filter is given by
( )

The cut-off frequency,


rad/sec, is where the magnitude drops to of the maximum, that is,
to . The magnitude at other frequencies will be scaled proportionately.
To Do
A. [First order, normalized] Frequency response of the first order normalized low-pass Butterworth
filter.
A1. Frequency response
A2. Frequency response: frequency axis calibrated in Hz
A3. Frequency response: the plot function instead of fplot
B. [Low-pass to low-pass transformation, band-width 500 Hz] The first order low-pass Butterworth
filter with a cut-off frequency of 500 Hz.
B1. Frequency response: writing ( ) explicitly
B2. Frequency response: the freqs function plus the filter coefficient list
B3. Frequency response: the freqs function plus the coefficients via the butter function
B4. Frequency response: the freqs function + the butter function; dB magnitude
C. [Fifth order, normalized] Design of normalized fifth order LP analog Butterworth filter.
C1. Fifth order: the butter function
D. [Fifth order, band-width 500 Hz] Design of fifth order LP analog Butterworth filter with a cut-off
frequency of 500 Hz.
D1. Fifth order: the butter function

DSP Lab

60 of 99

GRIET-ECE

61 of 99

A. [First order, normalized] Plot the frequency response of the first order normalized low-pass
(
).
Butterworth filter ( )
A3. [Frequency response: the plot function instead of fplot] Use the function plot to obtain the
(
) with the frequency axis
frequency response of the first order normalized filter ( )
calibrated in Hz.
(
). The corresponding MATLAB plots are
Solution The frequency response is ( )
shown below using the function plot. The step size along the frequency axis can be controlled more
directly than in fplot (by changing N in F = -L: L/N: L). Omega (or F) is a vector, consequently we use
./ instead of / etc.
%L=6*pi; N=512; Omega = -L: pi/N: L; H = 1./(1.+ j .*Omega); F=Omega/(2*pi);
L= 3; N=512; F = -L: L/N: L; H = 1./(1.+ (j*2*pi) .*F); %Omega=2*pi*F
subplot(2, 1, 1), plot(F, abs(H), 'k');
xlabel ('Frequency, Hz'), ylabel('|H(F)|'); grid; title ('Magnitude')
subplot(2, 1, 2), plot(F, angle(H), 'k');
xlabel ('Frequency, Hz'), ylabel('Phase of H(F)'); grid; title ('Phase')
Magnitude

|H(F)|

X: 0.1582
Y: 0.7092

0.5

0
-3

-2

-1

0
Frequency, Hz
Phase

-2

-1

0
Frequency, Hz

Phase of H(F)

2
1
0
-1
-2
-3

Q. Use help plot and explain how its arguments are used.
Q. At how many points along the frequency axis does MATLAB compute the frequency response
function? Use the data cursor to verify. Are these points uniformly spaced?
Q. Does plot give any better control (than fplot) over the resolution along the frequency axis?
Q. With the plot function is there some way by which you can place the data cursor exactly at
i.e.,
? (Can you control the horizontal spacing of the data points?

DSP Lab

61 of 99

GRIET-ECE

62 of 99

B. [Low-pass to low-pass transformation] Transform the first order normalized low-pass Butterworth
(
), to obtain a first order low-pass Butterworth filter with a cut-off frequency of
filter ( )
500 Hz.
B2. [Frequency response: the freqs function plus the filter coefficient list]
Solution In prior work the frequency response was generated by spelling out the function ( ) in full
in the MATLAB program. This can be tedious as the order of the filter increases and the response
function gets quite involved. In this example we avoid writing out the frequency response function by
using the freqs function along with a list of the filter coefficients. From prior work the first order filter
with a cut-off frequency of 500 Hz is given by
( )

(
)
{
} {
The numerator and denominator coefficients are
{
} and are used as such by the MATLAB program below.
(

} and

Fc= 500; L= 2*pi*3*Fc; % Cut-off freq. in Hz & plotting limit


b=[0, 2*pi*500]; a=[1, 2*pi*500]; %Coefficients
%
Omega = -L: L/512: L; F= Omega/(2*pi); % Analog frequency range
H = freqs(b, a, Omega);
subplot(2, 1, 1), plot(F, abs(H), 'k'); xlabel ('Frequency, Hz'), ylabel('|H(F)|');
grid; legend ('Magnitude', 'Location', 'NorthWest'); title ('1st order Butterworth LP');
subplot(2, 1, 2), plot(F, angle(H), 'k'); xlabel ('Frequency, Hz'), ylabel('Phase of H(F)');
grid; legend ('Phase'); title ('1st order Butterworth LP');
Q. Use help freqs and explain how its arguments are used.
Q. Determine the dc gain and half-power bandwidth (in rad/sec and Hz) from the magnitude plot.
Q. Write an expression for ( ) so that it has a dc gain of 4 and redo the plots.
Q. What is the effect of magnitude scaling (changing dc gain from 1 to 4) on the phase plot?

B3. [Frequency response: the freqs function plus the coefficients via the butter function]
Solution In prior work we generated the frequency response by using the freqs function along with a list
of the filter coefficients. On the one hand the syntax for specifying the list of coefficients is simpler than
that of spelling out the frequency response function itself, but, on the other hand, the coefficients still
have to be derived by hand. In this example the function butter generates the coefficients which are then
used by freqs.
Fc= 500; L= 2*pi*3*Fc; % Cut-off freq. in Hz & plotting limit
%b=[0, 2*pi*500]; a=[1, 2*pi*500]; %Coefficients
[b, a] = butter(1, 2*pi*Fc, 's') % Design filter of order 1 and cut-off = 2Fc rad/sec.
%
Omega = -L: L/256: L; F= Omega/(2*pi); % Analog frequency range
H = freqs(b, a, Omega);
subplot(2, 1, 1), plot(F, abs(H), 'k'); xlabel ('Frequency, Hz'), ylabel('|H(F)|');
grid; legend ('Magnitude', 'Location', 'NorthWest'); title ('1st order Butterworth LP');
subplot(2, 1, 2), plot(F, angle(H), 'k'); xlabel ('Frequency, Hz'), ylabel('Phase of H(F)');
grid; legend ('Phase'); title ('1st order Butterworth LP');
DSP Lab

62 of 99

GRIET-ECE

63 of 99

The program generates coefficients (same as in prior work): b = [0

3141.6]; a = [1

3141.6].

1st order Butterworth LP


1
Magnitude

X: 498
Y: 0.7085

|H(F)|

0.8
0.6
0.4
0.2
-1500

-1000

-500

0
500
Frequency, Hz
1st order Butterworth LP

1000

1500

Phase of H(F)

Phase
1
0
-1
-2
-1500

-1000

-500

0
Frequency, Hz

500

1000

1500

Q. Use help butter and explain how its arguments are used.
Q. Determine the dc gain and half-power bandwidth (in rad/sec and Hz) from the magnitude plot.
Q. Generate the coefficients and write down the transfer function for a filter of order 1, dc gain 4 and
half-power point of 500 Hz.
Q. Generate the coefficients and write down the transfer function of a normalized 2nd order filter.
Q. Generate the coefficients and write down the transfer function of a filter of order 3 and cut-off
frequency 5 rad/sec.

B4. [Frequency response: the freqs function + the butter function; dB magnitude] Plot the absolute
magnitude, decibel magnitude and phase versus frequency.
Solution This is a repeat of prior work with the dB magnitude as an important alternative to the absolute
magnitude. Here again the function butter supplies the coefficients to freqs which generates the
frequency response.
Fc= 500; L= 2*pi*3*Fc; % Cut-off freq. in Hz & plotting limit
[b, a] = butter(1, 2*pi*Fc, 's') % Design filter of order 1 and cut-off = 2Fc rad/sec.
%b=[0, 2*pi*500]; a=[1, 2*pi*500]; %Coefficients
Omega = -L: L/512: L; F= Omega/(2*pi); % Analog frequency range
H = freqs(b, a, Omega); M=abs(H); dBM=20*log10(M); Phase= angle(H);
subplot(3, 1, 1), plot(F, M, 'k'); xlabel ('Frequency, Hz'), ylabel('|H(F)|');
grid; legend ('Abs. Magnitude'); legend boxoff; title ('1st order Butterworth LP');
subplot(3, 1, 2), plot(F, dBM, 'k'); xlabel ('Frequency, Hz'), ylabel('20 log |H(F)|');
grid; legend ('dB Magnitude'); legend boxoff; title ('1st order Butterworth LP');
subplot(3, 1, 3), plot(F, Phase, 'k'); xlabel ('Frequency, Hz'), ylabel('Phase of H(F)');
DSP Lab

63 of 99

GRIET-ECE

64 of 99
st

grid; legend ('Phase'); legend boxoff; title ('1 order Butterworth LP');
1st order Butterworth LP
1

|H(F)|

Abs. Magnitude
0.5
0
-1500

X: 501
Y: 0.7064

-1000

-500

0
500
Frequency, Hz
1st order Butterworth LP

1000

1500

20 log |H(F)|

0
dB Magnitude
-5

Phase of H(F)

-10
-1500

-1000

-500

0
500
Frequency, Hz
1st order Butterworth LP

1000

1500

2
Phase
0
-2
-1500

-1000

-500

0
Frequency, Hz

500

1000

1500

Q. On the dB magnitude plot determine the dc gain and half-power bandwidth (in rad/sec and Hz).
Q. Write an expression for ( ) so that it has a dc gain of 4 and redo the plots.
Q. What is the effect of magnitude scaling (changing dc gain from 1 to 4) on the phase plot?
Q. Generate the coefficients and write down the transfer function for a filter of order 2, dc gain 0 dB and
cut-off frequency 500 Hz.
Q. Generate the coefficients and write down the transfer function of a filter of order 3 and dc gain 10 dB.

C. [Fifth order, normalized] Design a normalized fifth order LP analog Butterworth filter. Give the
transfer function and plot the frequency response of the filter.
D. [Fifth order] Design a fifth order LP analog Butterworth filter with a cut-off frequency of 500 Hz.
Plot the absolute magnitude, decibel magnitude and phase versus frequency. Give the transfer function.
D1. [Fifth order: the freqs function + the butter function; dB magnitude]
Solution Here again the function butter supplies the coefficients to freqs which generates the frequency
response.
format short e % To display coefficients in full!
Fc= 500; L= 2*pi*3*Fc; % Cut-off freq. in Hz & plotting limit
[b, a] = butter(5, 2*pi*Fc, 's') % Design filter of order 5 and cut-off = 2Fc rad/sec.
Omega = -L: L/512: L; F= Omega/(2*pi); % Analog frequency range
H = freqs(b, a, Omega); M=abs(H); dBM=20*log10(M); Phase= angle(H);
subplot(3, 1, 1), plot(F, M, 'k'); xlabel ('Frequency, Hz'), ylabel('|H(F)|'); grid;
DSP Lab

64 of 99

GRIET-ECE

65 of 99
th

legend ('Abs. Magnitude', 'Location', 'NorthWest'); title ('5 order Butterworth LP');
subplot(3, 1, 2), plot(F, dBM, 'k'); xlabel ('Frequency, Hz'), ylabel('20 log |H(F)|'); grid;
legend ('dB Magnitude', 'Location', 'NorthWest'); title ('5th order Butterworth LP');
subplot(3, 1, 3), plot(F, Phase, 'k'); xlabel ('Frequency, Hz'), ylabel('Phase of H(F)'); grid;
legend ('Phase'); title ('5th order Butterworth LP');
b = [0 0 0 0 0 3.0602e+17]; a = [1 1.0166e+4 5.1678e+7 1.6235e+11 3.1522e+14 3.0602e+17]
5th order Butterworth LP
1

|H(F)|

Abs. Magnitude
0.5

20 log |H(F)|

0
-1500

-1000

-500

0
500
Frequency, Hz
5th order Butterworth LP

1000

1500

1000

1500

0
-20

dB Magnitude

-40
-1500

Phase of H(F)

X: 501
Y: 0.7036

-1000

-500

0
500
Frequency, Hz
5th order Butterworth LP

5
Phase
0
-5
-1500

-1000

-500

0
Frequency, Hz

500

1000

1500

Q. On the dB magnitude plot determine the dc gain and half-power bandwidth (in rad/sec and Hz).
Q. Write an expression for ( ) so that it has a dc gain of 4 and redo the plots.
Q. What is the purpose of format short e? What happens if it is removed?

DSP Lab

65 of 99

GRIET-ECE

66 of 99

DSP Lab

66 of 99

GRIET-ECE

67 of 99

Lab-47 Design of Analog Butterworth LP filter


Objective To design an analog Butterworth filter to given specifications, {

} and {

}.

Background The standard design procedure for an analog Butterworth filter starts with specifying the
} and {
}. The s are
coordinates of two points on the Butterworth characteristic, say, {
analog frequencies (rad/sec) and the s are the corresponding gains in dB. The parameters are shown in
the dB magnitude characteristic below.
(

| (

)|)

We then determine the order, , and natural (3-dB) frequency,


specifications from the following design equations:

, of the filter that satisfies the

(
)

( )

The relevant MATLAB functions are shown in the left side column in Table below. The connection to
MATLAB symbols (the s and the s) is as follows:
&
MATLAB Functions
Analog, in rad/sec.
Digital, in rad/sample
[N, Wn] = buttord(Wp, Ws, Rp, Rs, 's') [N, Wn] = buttord(Wp, Ws, Rp, Rs)
Wp, Ws are in rad/sec.
Wp, Ws are normalized (
)
[b, a] = butter(N, Wn, 's')
[b, a] = butter(N, Wn)
Wn is in rad/sec.
Wn is normalized (
)
H = freqs(b, a, W)
H = freqz(b, a, W)
W is in rad/sec.
W is in rad/sample
The pass-band and stop-band edge frequencies are denoted
and
associated attenuations. The parameter 's' denotes analog design.

with

and

being the

To Do Design a low-pass analog Butterworth filter ( ) with (a) 3.01 dB cut-off frequency of 500Hz,
(b) monotonic stop and pass bands, (c) magnitude of frequency response down at least 15 dB at 750 Hz.

DSP Lab

67 of 99

GRIET-ECE

68 of 99

A1. Filter order and natural frequency. Determine the filter order (N) and natural frequency (Wn) of the
LP filter such that
Pass band edge frequency = Wp =
rad/sec, attenuation = Rp = 3.01dB
Stop band edge frequency = Ws =
rad/sec, attenuation = Rs = 15 dB
(

| (

)|)

In the following MATLAB segment [N, Wn] = buttord (.) computes N = 5, Wn = 3346.9, which is 5th
order with a natural frequency of 3346.9 rad/sec. And [b, a] = butter (.) gives the filter coefficients
(requires format short e in order to display the coefficients in full)
b = [0 0 0 0 0 4.1994e+17]
a = [1 1.0831e+4 5.8651e+7 1.9630e+11 4.0604e+14 4.1994e+17]
The rest of the program produces the frequency response plots. (Note that the phase curve has wraparound and looks as though it is periodic, but is not. This may be demonstrated by plotting the frequency
response over a wider range, say, of 3 kHz.)
format short e % To display coefficients in full!
F1=500 ; Wp=2*pi*F1, Rp = 3.01, %Pass band edge spec
F2=750; Ws = 2*pi*F2, Rs = 15, % Stop band edge spec
[N, Wn] = buttord(Wp, Ws, Rp, Rs, 's') % Determine N and Wn
[b, a] = butter(N, Wn, 's') % Determine filter coefficients
%
Fmax=1000; L=2*pi*Fmax; Omega = -L: L/1024: L; % Analog frequency range
H = freqs(b, a, Omega);
subplot(3, 1, 1), plot(Omega/(2*pi), abs(H), 'k'); %Absolute value
xlabel ('Frequency, Hz'), ylabel('|H(Omega)|'); grid;
title ('Absolute Magnitude, Low Pass')
subplot(3, 1, 2), plot(Omega/(2*pi), 20*log10(abs(H)), 'k'); %db = 20*log10(abs(H))
xlabel ('Frequency, Hz'), ylabel('20 log10 |H(Omega)|'); grid;
title ('dB Magnitude, Low Pass')
subplot(3, 1, 3), plot(Omega/(2*pi), angle(H), 'k');
xlabel ('Frequency, Hz'), ylabel('Phase of H(Omega)'); grid; title ('Phase, Low Pass')

DSP Lab

68 of 99

GRIET-ECE

69 of 99
Absolute Magnitude, Low Pass

|H(Omega)|

1
0.5

20 log10 |H(Omega)|

0
-1000

-800

-600

-400

-200
0
200
400
Frequency, Hz
dB Magnitude, Low Pass

600

800

1000

0
X: 500
Y: -1.85

-20

Phase of H(Omega)

-40
-1000

-800

-600

-400

-200
0
200
Frequency, Hz
Phase, Low Pass

400

600

800

1000

-800

-600

-400

-200
0
200
Frequency, Hz

400

600

800

1000

5
0
-5
-1000

From the dB magnitude plot, using the data cursor it can be seen that the attenuation at 500 Hz is
1.85 dB (which is better than 3.01 dB). Thus the design exceeds the specification at the pass-band edge
frequency (and meets the spec exactly at the stop-band edge frequency, that is, the attenuation at 750 Hz
is exactly 15 dB).
Q. Using help freqs write down the transfer function ( ).
The filter coefficients are
] = [0 0 0 0 0 4.1994e+17]
b=[
] = [1 1.0831e+4 5.8651e+7 1.9630e+11 4.0604e+14 4.1994e+17]
a=[
The MATLAB notation gives
( )
( )
( )
(
(

)
(

Q. Comment on the pole-zero plot.


In MATLAB the transfer function is specified as a ratio of polynomials in
.
( )
( )
( )
(
)
( )
(
)
(
)
(
)
(
)
(
)
(
)
(
)
(
)
(
)
The numerator coefficients, {
} and the denominator coefficients {
specified as two vectors below.

(
(

)
)
} are

%Pole-zero plot
b = [0 0 0 0 0 4.1994e+17];
DSP Lab

69 of 99

GRIET-ECE

70 of 99

a = [1 1.0831e+4 5.8651e+7 1.9630e+11 4.0604e+14 4.1994e+17];


zplane (b, a);

The 5 poles are on the Butterworth circle in the left-half s-plane (the real pole is located at -3348
where the data cursor is). The radius of the circle is the natural frequency (=3348).

A2. Design by hand calculation. The order, cutoff frequency and transfer function of the analog
Butterworth filter that satisfies the specification

are obtained as follows:


(

)
(

)
(


(
)
This agrees with the MATLAB solution.
Next, determine the cutoff frequency
that corresponds to the given specifications and the
order N = 5 determined above. We choose to match the spec exactly at
and exceed the spec at , (as
does the MATLAB solution) in which case:

( )

or 532.7 Hz.
Thus we have the order (= 5) and cut-off frequency (= 3346.9 rad/sec) of the desired filter. To
arrive at its transfer function we start with the normalized 5th order filter whose cut-off frequency we
know is 1 rad/sec and whose transfer function we can get from a standard table or construct from the
Butterworth circle. The next step is to apply to the normalized 5th order Butterworth filter the low-pass
(
) so that it will have a cut-off frequency of 3346.9 rad/sec.
to low-pass transformation
th
The normalized 5 order filter is
( )

)(

)(

The required filter is thus given by


( )

After a modest amount algebra to simplify the expression for ( ), it should be possible to show that
the ( ) obtained here is the same as the MATLAB-generated ( ) in A1. We can skip the algebra but
DSP Lab

70 of 99

GRIET-ECE

71 of 99

it is fairly straight forward to see that the numerator of the


(
)
, which is the same as in A1.

DSP Lab

71 of 99

( ) obtained here should be (

GRIET-ECE

72 of 99

DSP Lab

72 of 99

GRIET-ECE

73 of 99

Lab-48 Design of Digital Butterworth LP filter


Objective To design a digital Butterworth filter to given analog specifications, {

} and {

}.

Background In the standard design procedure for an IIR filter, based on the bilinear transformation, the
} and {
}), or, where an
( )
filters specifications may be digital: ({
structure is
}
{
}).
involved, the specs may be analog: ({
and
In the latter case the specs are first
converted to digital using
, where is the sampling interval, the s are analog frequencies
(rad/sec), the s are digital frequencies and the s are the corresponding gains in dB. The next steps
are:
1. The digital specs are converted to analog using pre-warping. The pre-warped specs may be
} and {
}).
denoted ({
2. Using the pre-warped specs the required analog filter of the proper order and natural
frequency is designed.
3. Finally, the analog filter is transformed to digital using bilinear transformation.
In Step 2 we follow the standard design procedure for an analog Butterworth filter using the pre-warped
} and {
}). We determine the order, , the natural (3-dB) frequency, , and the
specs ({
transfer function of the prototype filter using the MATLAB functions shown in the left side column in
Table below. This is followed by the bilinear transformation of Step 3.
Alternatively, in MATLAB we may use the functions shown in the right side column in Table
and thus dispense with Steps 1 through 3. Note that Wp, Ws and Wn are normalized digital frequencies.
MATLAB Functions
Analog, in rad/sec.
Digital, in rad/sample
[N, Wn] = buttord(Wp, Ws, Rp, Rs, 's') [N, Wn] = buttord(Wp, Ws, Rp, Rs)
Wp, Ws are in rad/sec.
Wp, Ws are normalized (
)
[b, a] = butter(N, Wn, 's')
[b, a] = butter(N, Wn)
Wn is in rad/sec.
Wn is normalized (
)
H = freqs(b, a, W)
H = freqz(b, a, W)
W is in rad/sec.
W is in rad/sample

To Do
( )
A. Design a digital Butterworth filter ( ) that when used in an
structure gives an
equivalent low-pass analog filter with (a) 3.01 dB cut-off frequency of 500 Hz, (b) monotonic stop and
pass bands, (c) magnitude of frequency response down at least 15 dB at 750 Hz. The sampling rate is
2000 samples/sec.
B. How would the design be affected if the digital filter were specified in terms of the digital
} and {
})?
specification ({

DSP Lab

73 of 99

GRIET-ECE

74 of 99

A1. Design the digital Butterworth filter. Convert the analog frequencies
and
to digital frequencies
and
using the relation
. Here the sampling rate is
.

, attenuation = Rp = 3.01 dB
, attenuation = Rp = 15 dB
(

| (

)|)

Furthermore, in the MATLAB functions buttord (Wp, Ws, Rp, Rs) and butter(N, Wn) the Wp, Ws
and Wn are normalized digital frequencies. The normalized frequency is defined as
. Thus the
specifications become
Pass-band edge frequency = Wp =
, attenuation = Rp = 3.01dB
Stop-band edge frequency = Ws =
, attenuation = Rs = 15 dB
We next determine the order and natural (3-dB) frequency of the digital Butterworth filter that meets the
above specifications.
In the following MATLAB segment the statement [N, Wn] = buttord (Wp, Ws, Rp, Rs)
computes the filter order and natural frequency, respectively, as N = 2 and Wn = 0.5083. The
corresponding filter coefficients are generated by [b, a] = butter (N, Wn) as
b =[0.3005 0.6011 0.3005]
a =[1 0.0304 0.1717]
The rest of the program produces the frequency response plots which are seen to be periodic. In the
MATLAB function freqz(b, a, w) the w is just digital (not normalized) frequency.
Fs = 2000; F1=500; F2=750; %Sampling rate & analog edge frequencies
Wp=2*F1/Fs, Rp = 3.01, %Digital normalized pass-band edge frequency & attenuation
Ws =2*F2/Fs, Rs = 15, %Digital normalized stop-band edge frequency & attenuation
[N, Wn] = buttord(Wp, Ws, Rp, Rs) %Determine order (N) and natural frequency (Wn)
[b, a] = butter(N, Wn) %Determine filter coefficients
%
% Plot frequency response
DSP Lab

74 of 99

GRIET-ECE

75 of 99

w = -3*pi: pi/256: 3*pi; H = freqz(b, a, w);


subplot(3, 1, 1), plot(w, abs(H), 'k'); %Absolute value
xlabel ('Digital Frequency, \omega, rad/sample'), ylabel('|H(\omega)|');
grid; title ('Absolute Magnitude, Low Pass')
subplot(3, 1, 2), plot(w, 20*log10(abs(H)), 'k'); %db = 20*log10(abs(H))
xlabel ('Digital Frequency, \omega, rad/sample'), ylabel('20 log10 |H(\omega)|');
grid; title ('dB Magnitude, Low Pass')
subplot(3, 1, 3), plot(w, angle(H), 'k');
xlabel ('Digital Frequency, \omega, rad/sample'), ylabel('Phase of H(\omega)');
grid; title ('Phase, Low Pass')

Absolute Magnitude, Low Pass

|H()|

1
0.5
-8

-6

-4
-2
0
2
4
Digital Frequency, , rad/sample
dB Magnitude, Low Pass

10

0
X: 1.571
Y: -2.791

-50
-100
-10

Phase of H()

20 log10 |H()|

0
-10

-8

-6

-4
-2
0
2
4
Digital Frequency, , rad/sample
Phase, Low Pass

10

-8

-6

-4
-2
0
2
4
Digital Frequency, , rad/sample

10

5
0
-5
-10

From the dB magnitude plot, using the data cursor it can be seen that the attenuation at
rad/sec is 2.791 dB which exceeds the specified 3.01 dB at the pass-band edge frequency.
Further, the design meets the spec exactly at the stop-band edge frequency, that is, the attenuation at
rad/sec is exactly 15 dB).
Q. Using help freqz write down the transfer function
Answer The filter coefficients are
] = [0.3005 0.6011 0.3005]
b=[
] = [1 0.0304 0.1717]
a=[
According to the MATLAB notation
( )
( )
DSP Lab

( )
( )

( )

( )

( ) and the difference equation.

, so that

75 of 99

)
GRIET-ECE

76 of 99

Q. Comment on the pole-zero plot.


Answer In MATLAB the transfer function is specified as a ratio of polynomials in
( )
( )
( )
The numerator coefficients, {
} and the denominator coefficients {
specified as two vectors below.

} are

%Pole-zero plot
b = [0.3005 0.6011 0.3005];
a = [1 0.0304 0.1717];
zplane (b, a);
The two poles are inside the unit circle, so the filter is stable.
A2. Repeat the plots of A1 with the frequency axis relabeled in Hz over the range (
The sampling frequency is
Hz. The cyclic frequency is given by

).
.

Fs = 2000; F1=500; F2=750; %Sampling rate & analog edge frequencies


Wp=2*F1/Fs, Rp = 3.01, %Digital normalized pass-band edge frequency & attenuation
Ws =2*F2/Fs, Rs = 15, %Digital normalized stop-band edge frequency & attenuation
[N, Wn] = buttord(Wp, Ws, Rp, Rs) %Determine order (N) and natural frequency (Wn)
[b, a] = butter(N, Wn) %Determine filter coefficients
%
% Plot frequency response
w = -1*pi: pi/256: 1*pi; H = freqz(b, a, w); F= w*Fs/(2*pi);
subplot(3, 1, 1), plot(F, abs(H), 'k'); %Absolute value
xlabel ('Frequency, Hz'), ylabel('|H(\omega)|'); grid;
title ('Absolute Magnitude, Low Pass')
%
subplot(3, 1, 2), plot(F, 20*log10(abs(H)), 'k'); %db = 20*log10(abs(H))
xlabel ('Frequency, Hz'), ylabel('20 log10 |H(\omega)|'); grid;
title ('dB Magnitude, Low Pass')
%
subplot(3, 1, 3), plot(F, angle(H), 'k');
xlabel ('Frequency, Hz'), ylabel('Phase of H(\omega)'); grid;
title ('Phase, Low Pass')

DSP Lab

76 of 99

GRIET-ECE

77 of 99
Absolute Magnitude, Low Pass

|H()|

1
0.5
-800

-600

-400

-200
0
200
400
Frequency, Hz
dB Magnitude, Low Pass

600

800

1000

0
X: 750
Y: -15

-50
-100
-1000

Phase of H()

20 log10 |H()|

0
-1000

-800

-600

-400

-200
0
200
Frequency, Hz
Phase, Low Pass

400

600

800

1000

-800

-600

-400

-200
0
200
Frequency, Hz

400

600

800

1000

5
0
-5
-1000

A3. [Bilinear transformation] Design the digital Butterworth filter by hand calculation.
The analog frequencies
and
have previously been converted to digital
frequencies
and
using the relation
, resulting in the digital specification:
{
} {
}
{
} {
}
( ) that applies to

These digital specs are pre-warped to analog specs using the relation
bilinear transformation. The pre-warping step gives (use
)
(

)
)

so that the analog specification to which we design the Butterworth filter is:
{
} {
}
{
} {
}
Plugging in numerical values, the filter order is
(

)
(

(
(

)
)


(
)
Choosing to match the specs exactly at the stop-band edge frequency (as does MATLAB)

DSP Lab

77 of 99

GRIET-ECE

78 of 99

Note that in this case


. Another way of saying this is that this design exceeds the specification at
the pass-band edge frequency and meets it exactly at the stop-band edge.
Therefore, the required pre-warped, normalized (unit bandwidth), analog Butterworth filter of
order 2 (using the Butterworth Table or the Butterworth circle) is
( )

(
)

Since we need a cut-off frequency of


rad/sec, we next use the low-pass to low-pass
(
)
transformation
in order to move the cut-off frequency from 1 to 2.0527 rad/sec.
( )

)
( ).

See Aside-1 in box below for MATLABs way of arriving at the above
Finally, the digital Butterworth filter is obtained by substituting
above

( ):
( )

( )|

(
(

)
)

)(

(
(

(
(

, with

, in the

)
)

)
(

( )
)

( )

( )

( )

( )

( )

Q. Where have we seen this before? What is MATLABs way of arriving at this

( )?

****************
Aside-1 The ( )
, with
, arrived at above is done in
MATLAB by the following segment:
format short e % To display coefficients in full!
Wp= 2, Rp = 3.01, %Pass-band edge specification
Ws = 4.828, Rs = 15, % Stop-band edge spec
[N, Wn] = buttord(Wp, Ws, Rp, Rs, 's') % Determine N and Wn
[b, a] = butter(N, Wn, 's') % Determine filter coefficients
which generates the result:
(Wp = 2, Rp = 3.01; Ws = 4.828, Rs = 15)
N = 2, Wn = 2.0524 Order & natural frequency
b = [0 0 4.2122], a = [1 2.9025 4.2122] Filter coefficients
This MATLAB output is in agreement with the hand-calculated ( ) to within 2 or 3 decimal places.
****************
Aside-2 The

( )

( )

( )

arrived at above is obtained in a more merciful

fashion by the following MATLAB segment:


DSP Lab

78 of 99

GRIET-ECE

79 of 99

Fs = 2000; F1=500; F2=750; %Sampling rate & analog edge frequencies


Wp=2*F1/Fs, Rp = 3.01, %Digital normalized pass-band edge frequency & attenuation
Ws =2*F2/Fs, Rs = 15, %Digital normalized stop-band edge frequency & attenuation
[N, Wn] = buttord(Wp, Ws, Rp, Rs) %Determine order (N) and natural frequency (Wn)
[b, a] = butter(N, Wn) %Determine filter coefficients
which generates the result:
(Wp = 0.5, Rp = 3.01; Ws = 0.75, Rs = 15) Normalized digital frequencies
N = 2, Wn = 0.50826 Order & (normalized) natural frequency
b = [0.30053 0.60106 0.30053], a = [1 0.030385 0.17174] Filter coefficients
This MATLAB output is in agreement with the partial hand-calculated ( ) above.
****************

DSP Lab

79 of 99

GRIET-ECE

80 of 99

DSP Lab

80 of 99

GRIET-ECE

81 of 99

Lab-51 Frequency response and time-domain simulation of FIR-LP filter


( )

Objective Examine and simulate the FIR filter ( )


average (a smoothing operation) at a sampling frequency of 150 Hz.
Background The frequency response is
| ( )|

( )

which computes a moving

so that

( )

The zero crossings of the magnitude function occur where | ( )| (


)
, or
(
)
. In other words, a frequency of
rad./sample (
cycle/sample) is totally stopped (filtered out) by the filter. The corresponding digital signal is ( )
( ( ) ). The underlying continuous-time signal, ( ), depends on the sampling frequency. We
use the relation that the point
(or
) corresponds to
. If, for example, the sampling
frequency is 16 Hz, then ( )
( ( ) ), and a frequency of 16/3 Hz will be totally filtered out.
If the sampling frequency is 150 Hz, then ( )
( ( ) ), and a frequency of 50 Hz will be
eliminated.
In calibrating the horizontal axis in terms of the cyclic frequency, , we use the relation
. As goes from 0 to , goes from 0 to 1 and goes from 0 to .
To Do
A. Plot the frequency response by hand and compare with MATLAB. Calibrate the frequency axis in
digital units ( rad/sample) as well as analog units (Hz).
B. Find the one frequency that is totally filtered out. Demonstrate with time domain plots.
(
).
C. Evaluate by hand the output signal ( ) for
where the input is ( )
( )
Take ( )
. Explain the result in terms of the frequency response computed in A.
D. Construct a signal ( ) as the sum of two cosinusoids of frequencies 2 Hz and 75 Hz and appropriate
relative amplitudes. Generate time domain plots of ( ) and ( ) and comment on the gain (or
attenuation) of each frequency component.

DSP Lab

81 of 99

GRIET-ECE

82 of 99

A1. Hand calculation.


A2. Plots of | | and
may be generated by the MATLAB function freqz. The frequency range of
interest is 0 to
rad., corresponding to 0 to 150 Hz. The MATLAB format is
( )
( )
( )
( )
(
)
( )
( )
( )
The vectors and are the numerator and denominator coefficients, respectively. In our example
( )
( )
( )
, and ( )
. In the MATLAB segment below we have used
( ), are in radians.
w for and
. Both and the phase,
%Frequency response
b = [1/3, 1/3, 1/3]; a = [1]; % Numerator & denominator coefficients
Fs=150; w = -pi: pi/256: pi; F = Fs*w/(2*pi); % Digital & analog frequency
[Hw] = freqz(b, a, w);
subplot(2, 1, 1), plot(F, abs(Hw));
xlabel('Frequency, Hz'), ylabel('Magnitude of H(\omega)'); grid
subplot(2, 1, 2), plot(F, angle(Hw));
xlabel('Frequency, Hz'), ylabel('Phase of H(\omega)'); grid

Magnitude of H()

0.5

0
-80

-60

-40

-20

0
20
Frequency, Hz

40

60

80

-60

-40

-20

0
20
Frequency, Hz

40

60

80

Phase of H()

4
2
0
-2
-4
-80

Q. Indicate what frequencies you would consider as low (or high or medium) frequencies. Does this
system approximate a low pass or band pass etc. filter? What is the (3 dB) bandwidth?
Q. Indicate if the phase is linear or nonlinear. Explain the discontinuity in the plot.
Q. How will it affect the plots you obtained above if the sampling frequency were 3 kHz (instead of 150
Hz)? Would the vertical axis have to be relabeled? On the digital frequency scale, the zero crossing of
the magnitude plot stays the same at
. If the sampling frequency is 3000 Hz, then
corresponds to
Hz.

DSP Lab

82 of 99

GRIET-ECE

83 of 99

B1. If the sampling frequency is 150 Hz, then a frequency of 50 Hz will be eliminated.
(
) sampled at 150 Hz. The corresponding ( )
B2. Consider a signal ( )
is the input to the moving average filter. The following MATLAB segment plots ( )
( )
.

(
and

% Continuous-time plot of x(t)


% Choose 0.2 sec interval so that continuous and discrete time scales align
Fs=150; F2=50; %Sampling frequency & 50 Hz
t = 0: 1/500: 0.2; xt = cos (2*pi*F2*t); %50 Hz
subplot(3, 1, 1), plot(t, xt, 'b'); xlabel ('Time, t, seconds'), ylabel('x(t)');
% Discrete-time plots.
% Choose 30 samples so that continuous and discrete time scales align
n=0:1:30; x=cos(2*pi*F2*n/Fs);
b=[1/3, 1/3, 1/3]; a=[1]; %Filter coefficients
y=filter(b, a, x);
subplot(3, 1, 2), stem(n, x, 'b*'); title('Input x(n)=Cosine(2\pin/3)');
xlabel('n'), ylabel('x(n)')
subplot(3, 1, 3), stem(n, y, 'ko'); title('Output y(n)=[x(n)+x(n-1)+x(n-2)]/3');
xlabel('n'), ylabel('y(n)');

x(t)

1
0
-1

0.02

0.04

0.06

0.08
0.1
0.12 0.14
Time, t, seconds
Input x(n)=Cosine(2n/3)

0.16

0.18

0.2

x(n)

1
0
-1

10

15
20
n
Output y(n)=[x(n)+x(n-1)+x(n-2)]/3

25

30

25

30

y(n)

0.5
0
-0.5

10

15
n

20

Q. On an approximate basis verify if the output amplitudes of the 50 Hz component agrees with the gain
figures obtained in A.

DSP Lab

83 of 99

GRIET-ECE

84 of 99

B3. As an extension of above example, consider a signal consisting of a 2 Hz desirable component plus
(
)
a noise component of 50 Hz (with a smaller amplitude), sampled at 150 Hz: ( )
(
). The corresponding ( )
(
)
(
) is the input to the moving
( )
(
)
(
)
average filter ( )
. It is expected that the 2 Hz will be passed through while
the 50 Hz will be eliminated. Using MATLAB plot ( ) ( )
( ). We show the continuous-time
plots below.
% Continuous-time plots of x(t)
t = 0: 1/500: 0.4;
x2Hz = 5*cos (2*pi*2*t); %2 Hz
x50Hz = 1*cos (2*pi*50*t); %50 Hz
xt = x2Hz +x50Hz; %2 Hz + 50 Hz
% Choose a multiplot so that all waveforms are on the same scales
plot(t, x2Hz, 'b', t, x50Hz, 'r', t, xt, 'k'); title('Continuous-time plots');
legend ('2 Hz', '50 Hz', '2 Hz + 50 Hz');
xlabel ('Time, t, seconds'), ylabel('x2Hz, x50Hz and x(t)');
Continuous-time plots
6
2 Hz
50 Hz
2 Hz + 50 Hz

x2Hz, x50Hz and x(t)

-2

-4

-6

DSP Lab

0.05

0.1

0.15
0.2
0.25
Time, t, seconds

84 of 99

0.3

0.35

0.4

GRIET-ECE

85 of 99

B4. In the following MATLAB segment we show both ( )

( ).

% Plots of x(t) and x(n)


% Choose 0.4 sec interval so that continuous and discrete time scales align
A=5; F1=2; B=1; F2=50; Fs=150; t = 0: 1/500: 0.4;
xt = A*cos (2*pi*F1*t) + B*cos (2*pi*F2*t); % 2 Hz + 50 Hz
% Choose n=60 so that continuous and discrete time scales align
n = 0: 1: 60;
xn = A*cos(2*pi*F1*n/Fs) + B*cos(2*pi*F2*n/Fs);
subplot(2, 1, 1), plot(t, xt, 'b'); legend ('2 Hz + 50 Hz');
title ('Continuous-time signal x(t)');
xlabel ('Time, t, seconds'), ylabel('x(t)');
subplot(2, 1, 2), stem(n, xn, 'b*'); legend ('(1/75) rad + (1/3) rad');
title ('Discrete-time signal x(n)');
xlabel ('n'), ylabel('x(n)');
Continuous-time signal x(t)
10
2 Hz + 50 Hz

x(t)

5
0
-5
-10

0.05

0.1

0.15
0.2
0.25
Time, t, seconds
Discrete-time signal x(n)

0.3

0.35

0.4

10
(1/75) rad + (1/3) rad

x(n)

5
0
-5
-10

DSP Lab

10

20

30
n

85 of 99

40

50

60

GRIET-ECE

86 of 99

B5. We show the operation of the filter in the following MATLAB segment. Both sequences
( )
( ) are plotted on the same scale so the smoothing action of the filter is easily discernible:
The 50 Hz component is totally removed.
[Note: The signal magnitudes are such that MATLAB, on its own, would use two different scales
( ) In order to force MATLAB to use the same scale for both we have used the axis()
for ( )
command.]
% Plots of input x(n) and moving average filter output y(n)
A=5; F1=2; B=1; F2=50; Fs=150;
b = [1/3, 1/3, 1/3]; a = [1]; %Filter coefficients
n = 0: 1: 60; x = A*cos(2*pi*F1*n/Fs) + B*cos(2*pi*F2*n/Fs);
y = filter(b, a, x);
subplot(2, 1, 1), stem(n, x, 'b*'); axis([0, 60, -7, 7]);
legend ('(1/75) rad + (1/3) rad');
title ('Discrete-time signal x(n)'); xlabel ('n'), ylabel('x(n)');
subplot(2, 1, 2), stem(n, y, 'ko'); axis([0, 60, -7, 7]);
legend ('(1/3) rad is absent');
title ('Moving average filter output y(n)'); xlabel ('n'), ylabel('y(n)');
Discrete-time signal x(n)
(1/75) rad + (1/3) rad

x(n)

-5
0

10

20

30
40
n
Moving average filter output y(n)

60

(1/3) rad is absent

y(n)

50

-5
0

10

20

30
n

40

50

60

Q. On an approximate basis verify if the output amplitudes of the 2 and 50 Hz components agree with
the gain figures obtained in A.
Q. What is the effect, on the time-domain demonstration, of changing the sampling frequency from 150
Hz to, say, 3000 Hz or 8000 Hz?

DSP Lab

86 of 99

GRIET-ECE

87 of 99

C1. Evaluate by hand.


( )

( ( )

0
1
2
3
4
5
6

1
-0.5
-0.5
1
-0.5
-0.5
1

( )
(

( ( )
(
(
(
(
(

))

(
)

( )
)
=
)
)
)
)

))

0
0

Q. Write your own MATLAB program that will implement the above evaluation and is, in effect, the
equivalent of the MATLAB function filter().
Q. In the context of the 3-term moving average comment on the kind of filter represented by
( )
(
)
( )
(
)

}. Indicate
C2. The three-term moving average filter has an impulse response of ( ) {
what kind of filter (low-pass or high-pass etc.) the following are. In each case give the difference
equation and indicate the type of symmetry (of the impulse response).
}
1. ( ) {
}
2. ( ) {
3. h(n) = {0.25, 0.5, 0.25}
4. h(n) = {0.25, 0, 0.25}
Q. Comment on shape of magnitude plot for average of odd vs. average of even number of terms. For
instance consider a 4-term average or 5-term average.

D1. Let the input consist of a 2 Hz and a 75 Hz, the latter being the maximum permissible with a
(
)
(
). The corresponding continuoussampling frequency of 150 Hz. ( )
time plots are given below:
% Continuous-time plots of x(t)
A=5; F1=2; B=1; F2=75; Fs=150;
t = 0: 0.001: 0.2;
x2Hz = A*cos (2*pi*F1*t); % 2 Hz
x75Hz = B*cos (2*pi*F2*t); % 75 Hz
xt = x2Hz +x75Hz; % 2 Hz + 75 Hz
% Choose a multiplot so that all waveforms are on the same scale
plot(t, x2Hz, 'b', t, x75Hz, 'r', t, xt, 'k'); title('Continuous-time plots');
legend ('2 Hz', '75 Hz', '2 Hz + 75 Hz');
xlabel ('Time, t, seconds'), ylabel('x2Hz, x75Hz and xt');
DSP Lab

87 of 99

GRIET-ECE

88 of 99

D2. In the following MATLAB segment we show the input, both continuous and discrete time,
( )
( ).
% Plots of x(t) and x(n)
% Choose 0.4 sec interval so that continuous and discrete time scales align
A=5; F1=2; B=1; F2=75; Fs=150;
t = 0: 0.001: 0.4;
xt = A*cos (2*pi*F1*t) + B*cos (2*pi*F2*t); % 2 Hz + 75 Hz
% Choose n = 60
n = 0: 1: 60;
xn = A*cos(2*pi*F1*n/Fs) + B*cos(2*pi*F2*n/Fs); % 2 Hz + 75 Hz
subplot(2, 1, 1), plot(t, xt, 'b'); legend ('2 Hz + 75 Hz');
title ('Continuous-time signal x(t)');
xlabel ('Time, t, seconds'), ylabel('x(t)');
subplot(2, 1, 2), stem(n, xn, 'b*'); legend ('(1/75) rad + (1/2) rad');
title ('Discrete-time signal x(n)');
xlabel ('n'), ylabel('x(n)');

D3. In the following MATLAB segment we show the operation of the filter. Both sequences
( )
( ) are shown. The low frequency component (2 Hz) suffers much less attenuation than the
high frequency component (75 Hz).
% Plots of input x(n) and high pass filter output y(n)
A=5; F1=2; B=1; F2=75; Fs=150;
b = [1/3, 1/3, 1/3]; a = [1]; %Filter coefficients
n = 0: 1: 60;
xn = A*cos(2*pi*F1*n/Fs) + B*cos(2*pi*F2*n/Fs); % 2 Hz + 75 Hz
yn = filter(b, a, x);
subplot(2, 1, 1), stem(n, xn, 'b*'); axis([0, 60, -10, 10]);
legend ('2 Hz + 75 Hz'); title ('Discrete-time signal x(n)');
xlabel ('n'), ylabel('x(n)');
subplot(2, 1, 2), stem(n, yn, 'ko'); axis([0, 60, -10, 10]);
legend ('Low pass filtered'); title ('Low pass filter output y(n)');
xlabel ('n'), ylabel('y(n)');
Q. On an approximate basis verify if the output amplitudes of the 2 and 75 Hz components agree with
the gain figures obtained in A.
Q. What is the effect, on the time-domain demonstration, of changing the sampling frequency from 150
Hz to, say, 3000 Hz or 8000 Hz?

DSP Lab

88 of 99

GRIET-ECE

89 of 99

Extra-credit Experiments

DSP Lab

89 of 99

GRIET-ECE

90 of 99

DSP Lab

90 of 99

GRIET-ECE

91 of 99

Lab-E1 Generation of DTMF signals


Objective In this Lab you will generate dual tone multi frequency signals used in the modern telephone.
Background A DTMF signal consists of the sum of two sinusoids - or tones - with frequencies taken
from two mutually exclusive groups. These frequencies were chosen to prevent any harmonics from
being incorrectly detected by the receiver as some other DTMF frequency. Each pair of tones contains
one frequency of the low group (697 Hz, 770 Hz, 852 Hz, 941 Hz) and one frequency of the high group
(1209 Hz, 1336 Hz, 1477Hz) and represents a unique symbol. The frequencies allocated to the pushbuttons of the telephone pad are shown below:
Frequency 1209 Hz 1336 Hz 1477 Hz
697 Hz
ABC
DEF
1
2
3
770 Hz
GHI
JKL
MNO
4
5
6
852 Hz
PRS
TUV
WXY
7
8
9
941 Hz
*
0
#
symbol = {'1','2','3','4','5','6','7','8','9','*','0','#'};
lfg = [697 770 852 941]; % Low frequency group
hfg = [1209 1336 1477]; % High frequency group
f = [];
for c=1:4,
for r=1:3,
f = [ f [lfg(c);hfg(r)] ];
end
end
>> Fs = 8000; % Sampling frequency 8 kHz
N = 800; % Tones of 100 ms
t = (0:N-1)/Fs; % 800 samples at Fs
pit = 2*pi*t;
tones = zeros(N, size(f,2));
for toneChoice=1:12,
% Generate tone
tones(:,toneChoice) = sum(sin(f(:,toneChoice)*pit))';
% Plot tone
subplot(4,3,toneChoice),plot(t*1e3,tones(:,toneChoice));
title(['Symbol "', symbol{toneChoice},'":
[',num2str(f(1,toneChoice)),',',num2str(f(2,toneChoice)),']'])
set(gca, 'Xlim', [0 25]);
ylabel('Amplitude');
if toneChoice>9, xlabel('Time (ms)'); end
end
set(gcf, 'Color', [1 1 1], 'Position', [1 1 1280 1024])
annotation(gcf,'textbox', 'Position',[0.38 0.96 0.45 0.026],...
'EdgeColor',[1 1 1],...
'String', '\bf Time response of each tone of the telephone pad',
DSP Lab

91 of 99

GRIET-ECE

92 of 99

'FitHeightToText','on');
Output:

DSP Lab

92 of 99

GRIET-ECE

93 of 99

Lab-E2 Implementation of decimation


Objective In this Lab you will convert the sampling rate of a discrete time signal from its original higher
sampling rate to a new lower rate.
Background
clf;
M = input('Down-sampling factor = ');
n = 0:99;
x = sin (2*pi*0.043*n) + sin (2*pi*0.031*n);
y = decimate(x, M);
subplot(2,1,1);
stem(n, x(1:100));
title('Input Sequence');
xlabel('Time index n');
ylabel('Amplitude');
subplot(2,1,2);
m = 0 : (100/M)-1;
stem(m, y(1:100/M));
title('Output Sequence');
xlabel('Time index n');
ylabel('Amplitude');

Ouput:

DSP Lab

93 of 99

GRIET-ECE

94 of 99

DSP Lab

94 of 99

GRIET-ECE

95 of 99

Lab-E3 Implementation of interpolation


Objective In this Lab you will convert the sampling rate of a discrete time signal from its original lower
sampling rate to a new higher rate.
Background
% Illustration of Interpolation Process
clf;
L = input('Up-sampling factor =');
% Generate the input sequence
n = 0:49;
x = sin(2*pi*0.043*n) + sin(2*pi*0.031*n);
% Generate the interpolated output sequence
y = interp(x,L);
% Plot the input and the output sequences
subplot(2,1,1);
stem(n,x(1:50));
title('Input Sequence');
xlabel('Time index n');
ylabel('Amplitude');
subplot(2,1,2);
m = 0:(50*L)-1;
stem(m,y(1:50*L));
title('Output Sequence');
xlabel('Time index n');
ylabel('Amplitude');
Output:

DSP Lab

95 of 99

GRIET-ECE

96 of 99

DSP Lab

96 of 99

GRIET-ECE

97 of 99

Lab-E4 Implementation of I/D sampling rate converters


Objective In this Lab you will convert the sampling rate of a discrete time signal by an integer factor.
Background
% Illustration of Down-Sampling by an Integer Factor
%
clf;
n = 0: 49;
m = 0: 50*3 - 1;
x = sin(2*pi*0.042*m);
y = x([1: 3: length(x)]);
subplot(2,1,1)
stem(n, x(1:50)); axis([0 50 -1.2 1.2]);
title('Input Sequence');
xlabel('Time index n');
ylabel('Amplitude');
subplot(2,1,2)
stem(n, y); axis([0 50 -1.2 1.2]);
title('Output Sequence');
xlabel('Time index n');
ylabel('Amplitude');
Output:

% Illustration of Up-Sampling by an Integer Factor


%
clf;
n = 0:50;
x = sin(2*pi*0.12*n);
DSP Lab

97 of 99

GRIET-ECE

98 of 99

y = zeros(1, 3*length(x));
y([1: 3: length(y)]) = x;
subplot(2,1,1)
stem(n,x);
title('Input Sequence');
xlabel('Time index n');
ylabel('Amplitude');
subplot(2,1,2)
stem(n,y(1:length(x)));
title('Output Sequence');
xlabel('Time index n');
ylabel('Amplitude');
Output:

DSP Lab

98 of 99

GRIET-ECE

99 of 99

% Illustration of Sampling Rate Alteration by a Ratio of Two Integers


clf;
L = input('Up-sampling factor = ');
M = input('Down-sampling factor = ');
n = 0:29;
x = sin(2*pi*0.43*n) + sin(2*pi*0.31*n);
y = resample(x,L,M);
subplot(2,1,1);
stem(n,x(1:30));axis([0 29 -2.2 2.2]);
title('Input Sequence');
xlabel('Time index n'); ylabel('Amplitude');
subplot(2,1,2);
m = 0:(30*L/M)-1;
stem(m,y(1:30*L/M));axis([0 (30*L/M)-1 -2.2 2.2]);
title('Output Sequence');
xlabel('Time index n'); ylabel('Amplitude');
Up-sampling factor = 10
Down-sampling factor = 2
Output:

DSP Lab

99 of 99

GRIET-ECE

S-ar putea să vă placă și