DSP Lab 2014 15
DSP Lab 2014 15
Kale
Page 1
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Discussion of the Lab assignment is encouraged, but you may not share
code.
Page 2
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
In this lab, we will show that continuous- time signals may be processed by first approximating
them by discrete-time signals using a process known as sampling. We will see that proper selection
of the spacing between samples is crucial for an efficient and accurate approximation of a
continuous-time signal. Excessively close spacing will lead to too much data, whereas excessively
distant spacing will lead to a poor approximation of the continuous-time signal. Sampling will be an
important topic in future labs, but for now we will use sampling to approximately compute some
simple attributes of both real and synthetic signals.The lab handouts contain a short introduction to
the required theory. More details, as well as several concrete C and MATLAB implementations,
may be found in the book [1,2], which may be freely downloaded from the web page:
http://www.et.ycce.in
Page 3
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Page 4
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale
plot(n1,z)
n2 = 0:10:60;
w = sin(n2/6);
subplot(3,1,3)
plot(n2,w)
=====================================================================
=======================================================================
As you can see, it is important to have many points to make the signal appear
appear smooth.But
smooth.
how
many points are enough for numerical calculations? In the following sections we will examine the
effect of the sampling interval on the accuracy of computations.
Page 5
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale
x[n] = xa (t ) t = nT = xa (nT ),
n = ......., 2 , 1, 0, 1, 2,...... (a )
Where the time variable t of the continuous time signal is related to the time variable n of the
discrete time signal only at the discrete time instants tn given by
tn = nT =
With
FT =
1
T
n 2 n
=
FT T
(b)
T = 2 FT
xa (t ) = A cos(2 f 0t + ) = A cos(0t + )
Department of Electronics and Telecommuni
Telecommunication Engineering, YCCE, Nagpur
(c )
Page 6
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
x[n] = A cos(0 nT + )
2 0
= A cos
n +
T
= A cos (0 n + )
Where
0 =
2 0
= 0T
T
Is the normalized digital angular frequency of the discrete time signal x[n]. The unit of the
normalized digital angular frequency w0 is radians per sample, while the unit of the normalized
analog frequency 0 is radians per second and the unit of the analog frequency f0 is hertz if the unit
of the sampling period is in seconds.
Example: The continuous-time signal x(t ) = cos(200 t ) is used as the input for a CD converter
with the sampling period 1/300 s. Determine the resultant discrete-time signal x[ n] .
2 n
x[n] = x(nT ) = cos(200n t ) = cos
,
3
2
radians/sample.
3
Q.1) Write a MATLAB code for the above example to convert the analog signal into a discrete
time signal.
Q.2) Repeat the Q.1 for Fs=100, Fs=200, Fs=300, and Fs=400 and write your conclusion.
x2 [ n] = cos(1.4 n),
x3 [ n] = cos(2.6 n)
Plots of these sequences and their parent time functions are given in Figure below.
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur
Page 7
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale
Page 8
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
xa ,k (t ) = A cos((0t + ) + k T t ), k = 0, 1, 2,....
Leads to identical sampled signals:
2 (0 + k T )n
2 0 n
= A cos
+ = A cos (0 n + ) = x[n]
T
The above phenomenon of a continuous time sinusoidal signal of higher frequency acquiring the
identity of a sinusoid sequence of lower frequency after sampling is called aliasing.
Since there are an infinite number of continuous time functions that can lead to given sequence
when sampled periodically, additional conditions need to be imposed so that the sequence
{ x[ n]} = { xa ( nT )} can uniquely represent the parent continuous time function xa (t ) . In which
case, xa (t ) can be fully recovered from knowledge of { x[ n ]} .
Viva Questions:1.
2.
3.
4.
5.
6.
Page 9
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
va (t ) composed of a weighted sum of five sinusoidal signals of frequencies 30 Hz, 150 Hz,
170Hz, 250 Hz, and 330 Hz, at a sampling rate of 200 Hz.
Solution:
The sampling period T = 1 / 200 = 0.005sec . Hence, the generated discrete time signal v[ n] is
given by
have
been
aliased
into
three
components 3sin(0.5 n), 2 cos(0.3 n), 4 cos(0.5 n) & 10sin(0.7 n) , resulting in a discrete
time sequence
of
only
three
sinusoidal
sequences
of
normalized
angular
frequencies:
It should be noted that an identical discrete time signal is also generated by uniformly sampling at
a of 200 Hz sampling rate the following continuous time signal composed of a weighted sum of
three sinusoidal continuous time signals of frequencies 30 Hz , 50 Hz , 70 Hz.
Page 10
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
From the above discussion it follows that if T > 20 , then the corresponding
normalized digital frequency
On the other hand if T < 20 , the normalized digital frequency will fold into a lower
digital frequency
0 is given by 2 0 / T modulo 2 .
The value of
Hence, to prevent aliasing, the sampling frequency T should be greater than 2 times the
frequency 0 of the sinusoidal being sampled.
The condition to be satisfied by the sampling frequency to prevent aliasing is called the
sampling theorem.
Q.1) Verify the above example with help of MATLAB. And also
write a Matlab script for converting the discrete time signal
back to the continuous time signal.
Hint: For converting the D. T. Signal in to a C. T. Signal use the following interpolation formula.
1
fs
For example:
%=================================================================
clc; close all; clear all;
n=-10:30; %add 40 past and future samples
x=cos(pi.*n./4); %Discrete Time Signal
T=1/1000; %sampling interval is 1/1000
for l=1:2000 %observed interval is [0,0.02]
t=(l-1)*T/100;%successive sample separation is 0.01T
h=sinc((t-n.*T)./T);
xr(l)=x*h.'; %approximate interpolation of (4.11)
end
plot(xr)
%=================================================================
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur
Page 11
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
We use several elementary sequences in digital signal processing for analysis purposes. Their
definitions and MATLAB representations are given below.
( n) =
The logical relation n==0 is an elegant way of implementing (n). For example MATLAB code to
implement
1, n = n0
0, n n0
(n n0 ) =
Over the interval n1 n n2, we will use the following MATLAB function:
=================================================================
% Function to generate x(n)=delta(n-no), n1<=n<=n2
function [x,n]=impseq(n0,n1,n2)
n=n1:n2;
x=(n==n0);
end
=================================================================
MATLAB code to generate impulse sequence at a specified location n0 in the range between n1
and n2
Write down the above code in m-file and run the MATLAB code and execute it for
(i ) (n 4) 3 n 5
(ii ) (n + 2)
5 n 4
Page 12
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
%================================================================
clc; clear all; close all;
n1=-3;
n2= 5;
n0=4;
[x,n]=impseq(n0,n1,n2);
stem(n,x);
xlabel('samples n');
ylabel('Amplitude');
title('Unit impulse Sequence');
%================================================================
>> x,n]=impseq(4,-3,5);
>>stem(n,x)
2.
1, n 0
u ( n) =
= {....0, 0,1,1,1,1,1,1.....}
0, n < 0
In MATLAB the function ones(1,N) generates a row vector of N ones. It can used to generates u(n)
over a finite interval.But an elegant way is to use logical relation n0.
Write a MATLAB Code to implement
1, n n0
u (n n0 ) =
0, n < n0
Over the interval n1 n n2, we will use the following MATLAB function:
Page 13
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Write MATLAB code to generate following Unit step sequences at a specified location n0 in the
range between n1 and n2 using function given above
(i ) u (n 2) 0 n 10
(ii ) u (n + 3)
3.
5 n 5.
n, n 0
ur ( n) =
= {....0, 0,1, 2,3, 4,5, 6.....}
0, n < 0
In MATLAB the function ones(1,N) generates a row vector of N ones. It can used to generates ur(n)
over a finite interval.But an elegant way is to use logical relation n0.
Write a MATLAB Code to implement
n, n n0
ur (n n0 ) =
0, n < n0
Lab Exercise:Generate and plot each of the following sequences over the interval.
(i ) x(n) = 2 (n + 2), 5 n 5.
(ii ) x(n) = (n 4), 5 n 5.
(iii) x(n) = 2 (n + 2) (n 4), 5 n 5.
(iv ) x ( n ) = u ( n ), 0 n 20.
Page 14
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
x(n) = a n , n; a
In MATLAB an array operator .^ is required to implement a real exponential sequence.
Example, x(n) = (0.9)n,
0 n 10,
%=================================================================
n=0:10;
x=(0.9).^n;
stem(n,x);
xlabel('samples n');
ylabel('Amplitude');
title('Unit ramp Sequence');
%=================================================================
Exercise 1. Take a=0.9, a=-0.9, a=1.2, a = -1.2 and plot all four sequences in single figure
window using subplot command.
x(n) = e( + j0 ) n , n
Where is called an attenuation and 0 is the frequency in radians. A MATLAB function exp is
used to generate exponential sequence.
(0.04+ j 0.9) n
Example: x(n) = e
0 n 10
n= [0:100];
x=exp ((0.04+0.9j)*n);
stem(n,x);
xlabel('samples n')
ylabel('x(n)')
title('Complex exponential signal')
Page 15
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
3. Sinusoidal sequence :
x(n) = cos(0 n + ),
for all n
Where, is the phase in radians. A MATLAB function cos or sin is used to generate sinusoidal
sequences. Write a MATLAB script to generate sinusoidal sequence
(i)
x(n) = 3cos(0.1n+/3) , 0 n 10
(ii)
x(n) = 2sin(0.5n), 0 n 10
(iii)
4. Random sequences:
Many practical sequences can't be described by mathematical expressions. Those sequences are
called random (stochastic) sequences and are characterized by parameters of the associated
probability density function or their statistical moments. In MATLAB there are two types of
random sequences. The rand(1,N) generates a length N random sequence whose elements are
uniformly distributed between [0,1]. The randn(1,N) generates a length N Guassian random
sequence with mean 0 and variance 1.
Example: Generate two discrete-time signals called sig1 and sig2 of length 1, 000. The samples of
sig1 should be independent, Gaussian random variables with mean 0 and variance 1. The samples
of sig2 should be independent, Gaussian random variables with mean 0.2 and variance 1. Use the
Matlab command random or randn to generate these signals, and then plot them on a single figure
using the subplot command. (Recall that an alternative name for a Gaussian random variable is a
normal random variable.)
Next form a new signal avg1 of length 1,000 such that avg1 is the average of the
vector sig1(1:n) (the expression sig1(1:n) returns a vector containing the first n elements of
sig1). Similarly, compute ave2(n) as the average of sig2(1:n). Plot the signals avg1(n) and
avg2(n) versus n on a single plot. Refer to help on the Matlab plot command for information on
plotting multiple signals.
( i ) x ( n ) = e ( 0.1+ j 0.3) n , 10 n 10
(i ) x ( ni ) = 10e 0.3( n 10) [ u ( n 10) u ( n 20) ] , 0 n 20
Page 16
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
1) Signal addition:
This is sample by sample addition given by
{x1(n)} + {x2(n)} ={x1(n) + x2(n)}
In MATLAB it is implemented by the array operator +. However length of both the signals must
be same. If the length is not same or if sample position are different for equal length sequences,
then + operator can not be used. First x1(n) and x2(n) are to be augmented so that they have same
position vector n.
In particular logical operation & and relational operator like <= and = = are required
to make x1(n) and x2(n) of equal length. The following function demonstrates these operations.
% Function to Add two discrete time signals
function [y,n]=sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1((n>=min(n1))&(n<=max(n1))==1)=x1;
y2((n>=min(n2))&(n<=max(n2))==1)=x2;
y=y1+y2;
%____________________________________________________________________
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur
Page 17
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
for 2 n 7 and
Solution:
%____________________________________________________________________
% The solution to the above exercise will be as given below
%____________________________________________________________________
clc; close all; clear all;
n1=-2:7; % range of n1
x1=[1 2 3 4 5 6 7 6 5 4];% sample values of x1
n2=-3:7; % range on n2
x2=[-1 2 0 4 5 -2 7 6 8 4 -3];% sample values of x1
% call the sigadd function
[y,n]=sigadd(x1,n1,x2,n2);
% plot the first sequence x1
subplot(2,2,1);stem(n1,x1)
title('First seq. x1(n)');
xlabel('sample');
ylabel('amplitude');
% plot the second sequence x1
subplot(2,2,2);stem(n2,x2)
title('Second seq. x2(n)');
xlabel('sample');
ylabel('Amplitude');
% plot the output sequence y
subplot(2,1,2);stem(n,y)
title('addition of signal')
xlabel('sample');
ylabel('Amplitude');
%____________________________________________________________________
Page 18
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
10
Amplitude
amplitude
6
4
2
0
-5
10
-5
-5
sample
10
sample
addition of signal
15
Amplitude
10
5
0
-5
-3
-2
-1
2
sample
.* .
2) Signal Multiplication:
This is sample by sample multiplication given by
{x1(n)}.{x2(n)}={x1(n)x2(n)}
Write a function for the multiplication of two sequences x1(n) and x2(n).
%____________________________________________________________________
% function to multiply two discrete time signals
%____________________________________________________________________
function [y,n]= sigmul(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1((n>=min(n1))&(n<=max(n1))==1)=x1;
y2((n>=min(n2))&(n<=max(n2))==1)=x2;
y=y1.*y2;
%____________________________________________________________________
for 2 n 7 and
Page 19
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
3) Scaling:
In this operation each sample is multiplied by a scalar .
{x(n)}={ x(n)}
An arithmetic operator * is used to implement the scaling operation in MATLAB.
4) Shifting
In this operation each sample of x(n) is shifted by an amount k to obtain a shifted sequence y(n).
y(n}={ x(n-k)
If we let m = n k then n = m + k and above operation is given by
y(m+k)={x(m)}
Write a MATLAB function to shift the given sequence by k amount.
%____________________________________________________________________
% function to shift the discrete time signal
%____________________________________________________________________
function [y,n]=sigshift(x,m,k)
n = m+k;
y = x;
%____________________________________________________________________
Exercise 3: Let x[ n ] = [ 1, 2 , 3, 4 , 5, 6 , 7, 6 , 5, 4]
for 2 n 7
2) y[n] = x[4 + n]
Page 20
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
5) Folding
In this operation each sample of x(n) is flipped around its origin n=0 to obtained folded sequence y(n).
y(n)={x(-n)} Hint: Use fliplr( ) function.
%____________________________________________________________________
% function to fold the discrete time signal
%____________________________________________________________________
function [y,n] = sigfold(x,n)
y = fliplr(x);
n = -fliplr(n);
%____________________________________________________________________
Exercise 4: Let x[ n ] = [ 1, 2 , 3, 4 , 5, 6 , 7, 6 , 5, 4]
for 2 n 7
Lab Exercise
Let
1. x1 ( n) = x (3 n)
Page 21
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
vn0 [ k ] = x[ k ]h[ n0 k ]
4. Summation: Sum all the values of the product sequence vn0(k) to obtain the value of the output at
time n=n0
This procedure results in the response of the system at a single time instant, say n=n0.in general we are
interested in evaluating the response of the system over all time instants
- <n< .consequently steps 2 through 4 must be repeated, for all possible time shifts - <n<
Note: In MATLAB the function conv is used to find the convolution of discrete time signals as given
below
>>
x = [ 3, 11, 7,
>>
h = [
>>
y = conv(x,h)
2,
y = 6 31
3,
47
0,
0,
-1,
-5 ,
-51
4,
2,
-5
2];
1];
41
18
-22
-3
However the conv function neither provides nor accepts any timing information. What is needed is a
beginning point and an end point of y(n). So a correction is needed.
Let
n yb = nxb + nhb
Page 22
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Exercise 1:
0 n 10
h2(n)
y(n)
x(n)
h1(n)
h3(n)
h4(n)
Page 23
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
X ( k ) = x ( n )W Nnk ,
0 k N 1
(1)
n =0
where W N = e
2
N
. Note that the DFT X (k ) is also an N-point sequence, that is, it is not
defined outside 0 k N 1.
Page 24
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
The inverse discrete Fourier transform (IDFT) of an N-point DFT X (k ) is given by (once
again x (n) is not defined outside 0 n N 1 )
x( n) =
1
N
N 1
X (k )W
k =0
nk
N
0 n N 1
( 2)
In MATLAB, an efficient implementation of the both transforms, DFT and IDFT, would be
to use a matrix-vector multiplication for each of the relations in (1) and (2).If x(n) and X (k ) are
arranged as column vectors x and X, respectively, then from (1) and (2) we have
(3)
(4)
where WN and WN are a DFT(Twiddle factor) matrix and its conjugate, respectively.
%=================================================================
The following MATLAB functions implement the equations (3).
%=================================================================
%=================================================================
Page 25
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Lab Exercise:a) For the sequence x (n) = [1, 1, 1, 1] , which has DTFT as in the appendix, use the following
MATLAB commands to compute its DFT and record the results.
c) Apply the zero-padding operation, which is a technique that allows us to sample at dense (or
finer) frequencies, to get an 8-point sequence and then compute its DFT by using the
following MATLAB commands.
Page 26
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
e) Treat the x (n) in (a) as a 16-point sequence by padding 12 zeros instead of 4 zeros as in (c).
Plot the magnitude and phase of the 16-point DFT versus k. (Note: Save the plot for Lab
report.)
f) For the sequence x(n) = cos(0.48n) + cos(0.52n) ; Determine the 10-point DFT of x(n) to
obtain an estimate of its DTFT. (Note: Save the plot for Lab report.)
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
n = [0:1:99]; x = cos(0.48*pi*n)+cos(0.52*pi*n);
n1 = [0:1:9]; y1 = x(1:1:10);
subplot(2, 1, 1); stem(n1, y1);
title(signal x(n), 0 <= n <= 9);
xlabel(n);
Y1 = dft(y1, 10); magY1 = abs(Y1(1:1:6));
k1 = 0:1:5; w1 = 2*pi/10*k1;
subplot(2, 1, 2); plot(w1/pi, magY1);
title(Samples of DFT Magnitude);
xlabel(frequence in pi units);
g) Use the following commands to pad 90 zeros and obtain a dense spectrum of the x(n) .
(Note: Save the plot for Lab report.)
>>
>>
>>
>>
>>
>>
>>
>>
>>
h) Use the following commands to take the first 100 samples of x (n) and determine its DTFT.
(Note: Save the plot for Lab report.)
>>
>>
>>
>>
>>
>>
>>
>>
Page 27
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
i) Use the following commands to synthesize the sequence x (n) using the Inverse Discrete
Fourier Transform (IDFT).
>>
>>
>>
>>
x1 = idft(X, 100);
subplot(2, 1, 2); stem(n, x1);
title(Synthesized x(n));
xlabel(n);
Page 28
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
(5)
FFT algorithms can reduce the quadratic dependence on N of the DFT. Theoretically, the number of
computations used in the FFT algorithms could be as small as, depending on the radix used in the
algorithm,
C N = ( N log 2 N )
(6)
MATLAB provides a function called fft to compute the DFT of a vector x. It is invoked
by x = fft(x,N), which computes the N-point DFT. If the length of x is less than N, then x
is padded with zeros. If the argument N is omitted, then the length of the DFT is the length of x. If x
is a matrix, then fft(x,N) computes the N-point of each column of x.
This fft function is written in machine language and not using MATLAB commands (i.e.,
it is not available as a .m file). Therefore it executes very fast. It is written as a mixed-radix
algorithm. If N is a power of two, then a high-speed redix-2 FFT algorithm is employed. If N is not a
power of two, then N is decomposed into prime factors and a slower mixed-radix FFT algorithm is
used. Finally, if N is a prime number, then the fft function is reduced to the raw DFT algorithm.
The inverse DFT is computed using the ifft function, which has same characteristics as
fft.
Page 29
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Lab Exercise:1. To determine the execution time of the DFT as N varies from 1 to 512, invoke
the following commands and then save the plot.
%=================================================================
2. To determine the execution time of the FFT as N varies from 1 to 512, invoke
the following commands and then save the plot.
%=================================================================
Page 30
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
X ( k ) = x ( n )WNnk ,
0 k N 1
n =0
X ( k ) = x ( n )WNnk ,
0 k N 1
n =0
Page 31
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Theory:
A finite duration sequence x(n) with N samples over the range 0nN-1 is calleed as N-point sequence.
Let
be a periodic signal of period N, created using the N-point sequence x(n), i.e.
A simple way to interpret this operation is the following: if the argument n is betyween 0 and N-1, then
leaves it as it is; otherwise add or subtract multiples of N from n until the result is between 0 and N-1.
The rem(n,N) function in MATLAB determines the remainder after dividing n by N. This function can
be used to implement modulo-N operation when n0. When n<0,we need to modify the result to obtain
the correct values.This is shown in the following function
function m=mod(n,N)
m=rem(n,N);
m=m+N;
m=rem(m,N)
=========================================================================
If an N-point sequence is shifted in either direction, then the result is no longer between 0nN-1.
Therfore x(n) is first converted into its periodic extension
obain
into N-point sequence. The resulting sequence is called as circular shift of a sequence.
Page 32
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
function y= cirshftt(x,m,N)
if length(x) > N
error(N must be >= the length of x)
end
x=[x zeros(1,N-length(x))];
n=[0:1:N-1];
n=mod(n-m,N);
y=x(n+1);
Circular Convolution:
The N-point circular convolution of two length-N sequences g[n] and h[n], 0 n N 1, is defined by
x3 ( m ) =
N 1
x (n) x
n=0
(( m n )) N , m = 0,1, ...., N 1
x1 ( n ) x 2 ( n ) =
N 1
x (m ) x
m=0
(( n m )) N , n = 0,1, ...., N 1
x1 ( n ) x 2 ( n ) X 1 ( k ) X 2 ( k )
Page 33
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Linear Convolution:
The linear convolution of a length-N sequence x[n], 0 n N 1, with a Length-M sequence h[n], 0
n M 1, can be obtained by a (N + M 1)-point circular convolution of two length-(N +M 1)
sequences, xe[n] and he[n],
y L ( n ) = x ( n ) h ( n ) = x e ( n ) he ( n )
Where the extended sequences are obtained by padding zero- valued samples.
x ( n ), 0 n N 1
xe ( n ) =
0, N n N + M 1
Page 34
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Lab Exercise:
1) x( n) = 10 ( 0.8) , 0 n 10 Determine and sketch the following signals:
n
( a ) x (( n ))11 ,
( b ) x (( n + 4))11 ,
( c ) x (( n 3))15
2) x1 (n) = {1, 2, 2} and x2 (n) = {1, 2,3, 4} Compute the 4 point circular Convolution
Page 35
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Introduction:
The most common method of IIR filter design is based on the bilinear transformation of a prototype
analog transfer function. The analog transfer function is usually one of the following types:
Butterworth, Type 1 Chebyshev, Type 2 Chebyshev, and elliptic transfer functions. The difference
between these filter types can be explained by considering the analog lowpass filter.
1. The Butterworth lowpass transfer function has a maximally-flat magnitude response at dc, that
is, = 0, and a monotonically decreasing magnitude response with increasing frequency.
2. TheType 1 Chebyshev lowpass transfer function has an equiripple magnitude response in the
passband and a monotonically decreasing magnitude response with increasing frequency outside
the passband.
3. The Type 2 Chebyshev lowpass transfer function has a monotonically decreasing magnitude
response in the passband with increasing frequency and an equiripple magnitude response in the
stopband.
4. Finally, the elliptic lowpass transfer function has equiripple magnitude responses both in the
passband and in the stopband.
Background review:
The most widely used approach to IIR filter design is based on the bilinear transformation from s-plane
to z-plane given by
s =
2 1 z 1
T 1+ z 1
Using the above transformation an analog transfer function Ha(s) is converted into a digital transfer
function H(z) according to:
H ( z ) = H a ( s)
2 1 z 1
s =
T 1+ z 1
For the bilinear transformation, the relation between the imaginary axis (s = j) in the s-plane and the
unit circle (z = ej) in the z-plane is given by
=
tan
T
2
which maps the entire imaginary axis in the s-plane to the unit circle in the z-plane introducing a
distortion in the frequency axis called warping. To develop a digital filter meeting a specified
magnitude response, the analog equivalents (p and s) of the critical band-edge frequencies (p and
s) of the digital filter are first obtained using the relation of Eq. (a) the analog prototype Ha(s) is then
designed using the prewarped critical frequencies, and Ha(s) is transformed using the bilinear
transformation to obtain the desired digital filter transfer function H(z).
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur
Page 36
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Labwork
The first step in the filter design process is to choose the type of filter approximation to be employed
and then to estimate the order of the transfer function from the filter specifications. After the filter type
has been selected and its order estimated, the next step is to determine the transfer function of the filter.
To this end MATLAB provides functions for all four types of filters.
Butterworth and Chebyshev Filter
Parameter
Description
Wp
Passband corner frequency Wp, the cutoff frequency, is a scalar or a twoelement vector with values between 0 and 1, with 1 corresponding to the
normalized Nyquist frequency, radians per sample.
Ws
Rp
Rs
Stopband
Passband
Lowpass
(Ws,1)
(0,Wp)
Highpass
(0,Ws)
(Wp,1)
Bandpass
and (Wp(1),Wp(2))
(0,Wp(1)) and
(Wp(2),1)
Wp(2))
Choose the input arguments to specify the stopband and passband according to the following table.
Description of Stopband and Passband Filter Parameters
Page 37
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Example 2
Design a bandpass butterworth filter with passband of 60 Hz to 200 Hz, with less than 3 dB of ripple in
the passband, and 40 dB attenuation in the stopbands that are 50 Hz wide on both sides of the passband:
MATLAB Program
Wp = [60 200]; Ws = [50 250];
Wp = Wp/500; Ws = Ws/500;
Rp = 3; Rs = 40;
[N,Wc] = buttord(Wp,Ws,Rp,Rs)
% Returns N =16; Wc =[0.1198 0.4005];
[b,a] = butter(N,Wc);
freqz(b,a,128,1000)
title('N=16 Butterworth Bandpass Filter')
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur
Page 38
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
cheby1
Example 3
For data sampled at 1000 Hz, design a lowpass Chebyshev type I filter with less than 3 dB of ripple in
the passband defined from 0 to 40 Hz and at least 60 dB of ripple in the stopband defined from 150 Hz
to the Nyquist frequency (500 Hz):
Wp = 40/500; Ws = 150/500;
Rp = 3; Rs = 60;
[N,Wc] = cheb1ord(Wp,Ws,Rp,Rs)
% Returns N = 4 Wc =0.0800
[b,a] = cheby1(N,Rp,Wc);
freqz(b,a,512,1000);
title('N=4 Chebyshev Type I Lowpass Filter')
Example 4
Design a bandpass Chebyshev type I filter with a passband of 60 Hz to 200 Hz, with less than 3 dB of
ripple in the passband, and 40 dB attenuation in the stopbands that are 50 Hz wide on both sides of the
passband:
Exercise
1. For data sampled at 1000 Hz, design a lowpass Chebyshev type II filter with less than 3 dB of ripple
in the passband defined from 0 to 40 Hz, and at least 60 dB of attenuation in the stopband defined from
150 Hz to the Nyquist frequency (500 Hz):
2. Design a bandpass Chebyshev type II filter with a passband of 60 Hz to 200 Hz, with less than 3 dB
of ripple in the passband, and 40 dB attenuation in the stopbands that are 50 Hz wide on both sides of
the passband:
Use cheb2ord and cheby2
Page 39
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
fir1 always uses an even filter order for the highpass and bandstop configurations. This is because
for odd orders, the frequency response at the Nyquist frequency is 0, which is inappropriate for
highpass and bandstop filters. If you specify an odd-valued n, fir1 increments it by 1.
b = fir1(n,Wn,window) uses the window specified in column vector window for the design. The
vector window must be n+1 elements long. If no window is specified, fir1 uses a Hamming window
(see hamming) of length n+1.
Example 1
Design a 48th-order FIR bandpass filter with passband 0.35 0.65:
b = fir1(48,[0.35 0.65]);
freqz(b,1,512)
Example 2
The chirp.mat file contains a signal, y, that has most of its power above fs/4, or half the Nyquist
frequency. Design a 34th-order FIR highpass filter to attenuate the components of the signal below
fs/4. Use a cutoff frequency of 0.48 and a Chebyshev window with 30 dB of ripple:
load chirp
% Load y and fs.
b = fir1(34,0.48,'high',chebwin(35,30));
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur
Page 40
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
freqz(b,1,512)
Kaiser window FIR filter design estimation parameters
kaiserord
Example 1
Design a lowpass filter with passband defined from 0 to 1 kHz and stopband defined from 1500 Hz
to 4 kHz. Specify a passband ripple of 5% and a stopband attenuation of 40 dB:
fsamp = 8000;
fcuts = [1000 1500];
mags = [1 0];
devs = [0.05 0.01];
[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
freqz(hh)
Example 2
Design an odd-length bandpass filter (note that odd length means even order, so the input to fir1
must be an even integer):
fsamp = 8000;
fcuts = [1000 1300 2210 2410];
mags = [0 1 0];
devs = [0.01 0.05 0.01];
[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp);
n = n + rem(n,2);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
[H,f] = freqz(hh,1,1024,fsamp);
plot(f,abs(H)), grid on
Page 41
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Prelab Instructions: :
Prior to beginning this lab, you must have:
Carefully read over this lab in order to plan how to implement the tasks assigned. Make sure
you are aware of all the items you need to include in your lab report.
Please note that there are no prelab questions. The questions at the end of this lab must be
answered and submitted with the lab report.
Deliverables
You must be present and conducting the practical assigned to you during the scheduled lab session.
Please prepare a soft and hard copy of Lab report and submit your reports by the end of the lab.
Page 42
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Page 43
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey,, Prof. K.P.Kamble, Prof. Y.S.Kale
Figure 1.1a:
1a: Schematic of DSKC6711 Board
Figure 1.1b:: Simplified Schematic of DSKC6711 depicting various peripherals on the board
Page 44
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Design
Debug
Analyze
Conceptual
Planning
syntax checking,
probe points,
logging, etc.
real time
debugging,
statistics, tracing
The C compiler compiles a C source program with extension .c to produce an assembly source
file with extension .asm.
The assembler assembles an .asm source file to produce a machine language object file with
extension.obj.
The linker combines object files and object libraries as input to produce an executable file with
extension .out. This executable file represents a linked common object file format (COFF),
popular in Unix-based systems and adopted by several makers of digital signal processors.
This executable file can be loaded and run directly on the TMS320C6711 processor.
Page 45
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
A linear optimizer
System configuration:
Before starting actual working on CCS the selection of appropriate DSP processor is important. First
configure the code composer studio to use C6711 DSK board by using Setup CCS software as shown in
figure 1.3.
Page 46
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Connect.
New
Source File.
Page 47
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Page 48
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
New
Source File.
Page 49
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
bilinear.c.
2) Project
rts6700.lib.
3) Project
sinewave.cmd.
Build.
Rebuild.
Load Program
Run.
Page 50
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
The program can be readily modified to handle a larger set of data. No real-time implementation is used
in this example, and no real-time I/O support files are needed. The support functions for interrupts are
not needed here.
Create this project as dotp4 and add the following files to the project:
1. dotp4.c: C source file
2. vectors_poll.asm: vector file defining the entry address c_int00
3. hello.cmd: generic linker command file
Department of Electronics and Telecommunication Engineering, YCCE, Nagpur
Page 51
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Page 52
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
#include <stdio.h>
#include<math.h>
#define FREQ 500
float m[256];
main()
{
int i=0;
for(i=0;i<256;i++)
{
m[i]=sin(2*3.14*FREQ*i/24000);
printf("%f\n",m[i]);
}
}
Page 53
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Page 54
ET 318: Digital Signal Processing Laboratory Instructors: Prof. Ms.Y. K Dubey, Prof. K.P.Kamble, Prof. Y.S.Kale
Page 55