Documente Academic
Documente Profesional
Documente Cultură
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
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
7
11
15
19
23
27
MATLAB-based Experiments
1
2
3
4
5
6
7
8
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
(a)
DSP Lab
7 of 99
GRIET-ECE
8 of 99
(b)
FIGURE. TMS320C6713-based DSK board: (a) board; (b) diagram
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.
DSP Lab
9 of 99
GRIET-ECE
10 of 99
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]);
}
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];
/*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]);
}
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
( )
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
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
( )
( )
( )
( )
( )
( )
( ) ((
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
))
((
))
} and
( )
( )
( )}, or equivalently,
( ) using circular
( )
( )} = { ( )
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?
}.
2; 2
1; 1
0; 0
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
( )
( )
( )
((
))
( )
( )
((
))
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
1; 1 2 3
2 3 0; 0
0
0
0
0
0
0
0; 0 1 2
1 2 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 ( )
( )
{ ( )}
E. Polynomial multiplication.
DSP Lab
31 of 99
GRIET-ECE
32 of 99
DSP Lab
32 of 99
GRIET-ECE
33 of 99
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
sampled at
( ).
DSP Lab
33 of 99
GRIET-ECE
34 of 99
( )
, ( )
( )
( )
( )
, and ( )
( )
( )
( )
( )
( ) over a 1-second
.
( ).
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
( )
( )
( )
( )
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
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
( ). 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
DSP Lab
36 of 99
GRIET-ECE
37 of 99
) 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
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
( )
}:
xf(n)
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
10
15
DSP Lab
38 of 99
GRIET-ECE
39 of 99
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
( )
( )
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
DSP Lab
41 of 99
GRIET-ECE
42 of 99
( )
( )
( )
( )
[
]=[ ( ) ( )
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
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 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
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. 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 ( )
{
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 ( )
{
DSP Lab
49 of 99
GRIET-ECE
50 of 99
DSP Lab
50 of 99
GRIET-ECE
51 of 99
( )
transfer function 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
)
(
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 ( )
( )
( ) 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
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
( )
) 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
-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
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
1.5
0.5
10
20
30
n
40
50
60
DSP Lab
56 of 99
GRIET-ECE
57 of 99
1.8
1.6
1.4
1.2
0.8
10
20
30
n
40
50
60
DSP Lab
( )
( )
57 of 99
GRIET-ECE
58 of 99
DSP Lab
58 of 99
GRIET-ECE
59 of 99
( )
and
( )
( )
) is 1 and is the
( )
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
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
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
3141.6]; a = [1
3141.6].
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
} 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.
(
| (
)|)
(
)
( )
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
( )
( )
( )
(
(
)
(
(
(
)
)
} are
%Pole-zero plot
b = [0 0 0 0 0 4.1994e+17];
DSP Lab
69 of 99
GRIET-ECE
70 of 99
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
)
(
)
(
(
)
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
( )
)(
)(
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
DSP Lab
71 of 99
GRIET-ECE
72 of 99
DSP Lab
72 of 99
GRIET-ECE
73 of 99
} 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
|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
( )
( )
( )
( )
, so that
75 of 99
)
GRIET-ECE
76 of 99
} 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
).
.
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
(
)
)
( ).
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
( )
( )
( )
78 of 99
GRIET-ECE
79 of 99
DSP Lab
79 of 99
GRIET-ECE
80 of 99
DSP Lab
80 of 99
GRIET-ECE
81 of 99
( )
so that
( )
DSP Lab
81 of 99
GRIET-ECE
82 of 99
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
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
-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
( ).
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
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
( ( )
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
91 of 99
GRIET-ECE
92 of 99
'FitHeightToText','on');
Output:
DSP Lab
92 of 99
GRIET-ECE
93 of 99
Ouput:
DSP Lab
93 of 99
GRIET-ECE
94 of 99
DSP Lab
94 of 99
GRIET-ECE
95 of 99
DSP Lab
95 of 99
GRIET-ECE
96 of 99
DSP Lab
96 of 99
GRIET-ECE
97 of 99
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
DSP Lab
99 of 99
GRIET-ECE