Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CIIT,Wah
Lab: DSP
LAB# 1
Matlab Review
1.1 Overview
MATLAB will be used extensively in all the labs. The primary goal of this lab is
to familiarize you with basics of MATLAB.
>> v = [1 4 7 10 13]
Registration No: Section: Date: ______________
Column vectors are created in a similar way; however, semicolon (;) must
separate the components of a column vector.
Note: v (1) is the first element of vector v, v (2) its second element, and so
forth.
>> v(1:3)
Or, all elements from the third through the last elements,
>> v(:)
1.3 Matrices
To enter a matrix A, such as,
Type A = [1 2 3; 4 5 6; 7 8 9]
The element of row i and column j of the matrix A is denoted by A(i, j).
>> A(3,3) = 0
Registration No: Section: Date: ______________
Often we must deal with matrices or vectors that are too large to enter one
element at a time. For example, suppose we want to enter a vector x
consisting of points (0, 0.1,0.2, 0.3,,5). We can use the command
>> x = 0:0.1:5;
Task(2): Explain what does command linspace do?
The colon operator can also be used to pick out a certain row or column. For
example, the
>> A(2,:)
>> A(:,2:3)
>> A(:,2)=[ ]
>> A = [1 2 3; 4 5 6; 7 8 9]
>> size(A)
Registration No: Section: Date: ______________
On the other hand, array arithmetic operations or array operations for short,
are done element-by-element. The period character . distinguishes the
array operations from the matrix operations.
1.6 Plotting
The MATLAB command to plot a graph is plot(x,y).
plot(x1,y1,x2,y2,,xn,yn)
It is possible to specify line styles, colors, and markers (e.g., circles, plus
signs, . . . ) using the plot command:
plot(x,y,'style_color_marker')
Specify the components (in any order) as a quoted string after the data
arguments.
Task (6): Type the following commands and observe the plot
t = -5:.01:5;
y = sin (2*pi*t);
plot (t,y);
z = cos (2*pi*t);
plot(t,y,'r',t,z,'k')
>> figure(1)
Registration No: Section: Date: ______________
>> y = [0 1 2 4 2 1 0];
>> subplot(2,2,1);
>> stem(y)
>> subplot(2,2,2);
>> plot(y)
>> figure(2)
>> y = [0 1 2 4 2 1 0];
>> stem(y)
>> hold on
>> plot(y)
>> hold of
Registration No: Section: Date: ______________
DSP Lab#02
Introduction to Matlab (II)
>> edit
Note: Always save the script with .m extension. You can run script by typing
its name in command window e.g >>filename
The first line of M-File function starts with the keyword function. It gives the
function
Note: Name of the text file that you save will consist of the function name
with the extension .m
Example:
function y = amplitude_multiply(x)
y = 3 * x;
if ... end
if ... else ... end
if ... elseif ... else ... end
if expression
statements
end
Example:
x= i * i
end
while expression
statements
end
e.g x=1
while x <= 10
x = 3*x
end
Task (3): Write a noise function which generates noise signal of length n
with amplitude uniformly distributed in the interval (a, b).
Lower Limit - a
Upper Limit - b
Length of row vector - n
a + (b-a) * 0 = a
a + (b-a) * 1 = b
Registration No: Section: Date: ______________
DSP Lab#03
Discrete Time Signals and Signal Operations
To generate over the interval n1n0n2, we will use the following MATLAB
function
Task(1): Write code to generate and plot the following sequence over
interval -5n5
To generate over the interval n1n0n2, we will use the following MATLAB
function
Task(2): Write code to generate and plot the following sequence over
interval 0n20
Task(3): Run the following code and note down the output. Also use real,
imag and abs functions to plot real part, imaginary part and magnitude.
y1 (find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2 (find((n>=min(n2))&(n<=max(n2))==1))=x2;
y = y1+y2;
n = m+n0; y=x;
y = fliplr(x); n = - fliplr(n);
DSP Lab#04
Linear Convolution
4.1 Convolution
Convolution is used to calculate the response of a system to arbitrary inputs
by using the impulse response of a system.
Impulse response denoted by h[n] is the signal that exits a system when a
delta function (unit impulse) is the input.
Any arbitrary Input signal can be decomposed into a set of impulses, each of
which can be viewed as a scaled and shifted delta function. Second, the
output resulting from each impulse is a scaled and shifted version of the
impulse response. Third, the overall output signal can be found by adding
these scaled and shifted impulse responses. In other words, if we know a
system's impulse response, then we can calculate what the output will be for
any possible input signal.
A discrete system T[.] is a linear operator L [.] if and only if L[.] satisfies the
principle of superposition. So output of liner system becomes
Hence .
Note: The conv function neither provides nor accepts any timing
information. What is needed is a beginning point and an end point of y(n).
Registration No: Section: Date: ______________
Let then
A simple extension of the conv function, called conv_m, which performs the
convolution of arbitrary support sequences can now be designed
y=Hx
Registration No: Section: Date: ______________
where linear shifts in h(n-k) for n = 0,. . . , Nh - 1 are arranged as rows in the
matrix H.This matrix has an interesting structure and is called a Toeplitz
matrix.
Note that each diagonal of H contains the same number. Note carefully that
the first column of H contains the impulse response vector h(n) followed by
number of zeros equal to the number of x(n) values minus one. The first row
contains the first element of h(n) followed by the same number of zeros as in
the first column.
function [y,H]=conv_tp(h,x)
% Linear Convolution using Toeplitz Matrix
% -------------------------------------------------------------------------------------
% [y,H] = conv_tp(h,x)
% y = output sequence in column vector form
% H = Toeplitz matrix corresponding to sequence h so that y = Hx
% h = Impulse response sequence in column vector form
% x = input sequence in column vector form
Nx = length(x); Nh = length(h);
hc = [h; zeros(Nx-1, 1)];
hr = [h(1),zeros(1,Nx-1)];
H = toeplitz(hc,hr);
y = H*x;
Registration No: Section: Date: ______________
DSP LAB#05
Registration No: Section: Date: ______________
Note:
.
Registration No: Section: Date: ______________
>> k = [0:M];
>> X = x * (exp(-j*pi/M)) .^ (n * k);
Task 2: Compute the discrete time Fourier transform of the sequence
% M = no of equispaced frequencies
Task4: Using above DTFT function compute DTFT of the following sequence
x(n) = 2(0.8)n[u(n)-u(n-20)]
Take -30 n 30
Also plot Magnitude, Real part, Imaginary part and Angle of X(ejw).
Registration No: Section: Date: ______________
DSP LAB#06
Properties of DTFT & Frequency Response of LTI Systems
Linearity:
Time Shifting:
Frequency Shifting:
Conjugation:
Folding:
Convolution:
Multiplication:
Task 1: Using properties of DTFT, determine and plot DTFT of the following
sequence
x = T10(n) * T10(-n)
Registration No: Section: Date: ______________
where and
Where
b = [1]; a = [1 -1 0.9];
DSP Lab#07
z-Transform
The z-transform is a useful tool in the analysis of discrete-time signals and systems
and is the discrete-time counterpart of the Laplace transform for continuous-time
signals and systems. Discrete-time Fourier transform (DTFT) exist for absolutely
summable sequences. Unfortunately, many of the signals that we would like to
consider are not absolutely summable and, therefore, do not have a DTFT.
The z-transform is a generalization of the DTFT that allows one to deal with such
sequences and is defined as
where z is a complex variable. The set of z values for which X(z) exists is called the
region of convergence (ROC) and is given by
Then the roots of B(z) are called the zeros of X(z), while the roots of A(z) are called
the poles of X(z).
Registration No: Section: Date: ______________
Linearity:
Sample shifting:
Frequency shifting:
Folding:
Convolution:
Example (1): Let X1(z) = 2+3z-1 +4z-2 and X2(z)= 3+4z-1+5z-2+6z-3. Determine
X3(z)=X1(z)X2(z).
Then the convolution of the above two sequences will give the coefficients of the
required polynomial product.
So X3(z) will be
Matlab Implementation:
Similarly, [b ,a] =residuez(R,p ,C), with three input arguments and two output
arguments, converts the partial fraction expansion back to polynomials with
coefficients in row vectors b and a.
Example(2)Let
Matlab Function freqz returns the N-point frequency vector w and the N-point
complex frequency response vector H of the system, given its numerator and
denominator coefficients in vectors b and a.
[H,w] = freqz(b,a,N)
The function zplane (b, a) plots poles and zeros, given the numerator row vector
b and the denominator row vector a.
Task(3):Given
(a)Sketch pole-zero plot (b) Plot Magnitude response |H(e jw)| and Phase response
<H(ejw)
DSP Lab#0 8
The discrete time Fourier transform (DTFT) provided the frequency-domain (w)
representation for absolutely summable sequences. This transform is defined for
infinite-length sequences and is function of continuous variable (w). These two
features are troublesome because one has to evaluate infinite sums at uncountable
infinite frequencies. This problem is solved by DFT which is defined for discrete
signal whose non-zero values have a limited (finite) duration. It is obtained by
sampling the discrete time Fourier transform in the frequency domain. Such a
representation is very useful for digital computations and for digital hardware
implementation.
8.1 DFT
The Discrete Fourier Transform of an N-point sequence is given by
Here WN = e-j*2pi/N
and IDFT is given as
Matlab Implementation:
To get high density spectrum, zero padding operation is used in which zeros are
appended with sequence.
(a) Compute 8-point DFT by appending 4 zeros with the sequence and plot
magnitude
(b) Compute 16-point DFT by appending 12 zeros and plot magnitude.
Registration No: Section: Date: ______________
x=x(mod(-n,N)+1)
then
Note: Imagine that the DFT samples are arranged around a circle so that the
indices k = 0 and k = N overlap; then the samples will be symmetric with respect to
k = 0.
Registration No: Section: Date: ______________
Note: If we make both x1( n ) and x2( n ) N = N1+ N2 - 1 point sequences by padding
an appropriate number of zeros, then the circular convolution is identical to the
linear convolution.
DSP Lab#09
Sampling, quantization and digitization
The purpose of this lab is to familiarize students with the concepts of sampling,
quantization and digitization to bits. Firstly a simple sine wave with high sampling
frequency is generated assuming it to be an analog signal. Secondly this assumed
analog signal is sampled. This sampled signal is then quantized to certain levels.
After quantization bits are assigned to each level.
Lab tasks:
Step 1:
Generate the sine wave with
Frequency = 3Hz
Sampling Frequency = 1000 Hz
Amplitude = 1
Number of cycles = 3
Registration No: Section: Date: ______________
Step 2:
Sample the assumed analog signal. To do so, write the code in following steps
Generate a vector matrix having zeros; the length of this zero vector should
be same as that of assumed analog signal.
Assign the value one 1 to every 40 th sample in the zero vector. Now it is a
pulse train.
Perform element by element multiplication between the assumed analog
signal and pulse train, to obtain the sampled signal.
Note: If plot of assumed analog signal and stem of sampled signal is in the same
figure the sampled values can be seen quite evidently from the figure, as shown
below.
Sampling of an Analog signal
1.5
Analog signal
Sampled values
0.5
A m p lit ud e
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time
Step 3:
Quantize the sampled signals. To do so, write the code in following steps
Remove zeros from the sampled signals. (Hint: Apply ~= , == or see help
find).
Assign levels to the sampled signal as shown in the figure below.
Assign bits to the levels as according to the given table.
Registration No: Section: Date: ______________
The quantization done based on levels defined is explained through the figure
below.
2
Quantized Signal
Sampled Signal
1.5
0.5
-0.5
-1
-1.5
-2
0 5 10 15 20 25 30 35 40
Samples
Registration No: Section: Date: ______________
The primary disadvantage of FIR filters is that they often require a much
higher filter order than IIR filters to achieve a given level of performance.
Correspondingly, the delay of these filters is often much greater than for an
equal performance IIR filter.
where the filtered signal y(n) is just a linear combination of current and
previous values
of the input signal x(n). The coefficients b are the numerator coefficients of
the transfer
function. The denominator of the transfer function will always be a = 1. The
order of the
Registration No: Section: Date: ______________
filter is n = length(b) - 1.
Try:
wvtool(windowname(n))
opens WVTool with time and frequency domain plots of the n-length window
specified in
windowname, which can be any window in the Signal Processing Toolbox.
Several windows can be given as input arguments for comparative display.
Try:
wvtool(hamming(64),hann(64),gausswin(64),rectwin(64))
Note the diference between the spectrum of the rectangular and other
windows.
Try:
Registration No: Section: Date: ______________
wintool
The Filter Visualization Tool (FVTool) can be accessed from within the FDATool
by choosing the Full View Analysis option in the Analysis menu or by clicking
the Full View
Analysis button on the toolbar.
Command:
fdatool
DSP Lab# 11
IIR filters have infinite duration impulse responses; hence they can be
matched to analog filters, all of which generally have infinitely long impulse
responses. The primary advantage of IIR filters over FIR filters is that they
typically meet a given set of specifications with a much lower filter order
than a corresponding FIR filter. This has the obvious implementation
advantages.
IIR filters have nonlinear phase (they cannot have linear phase; they might
almost have
linear phase).
Butterworth filter
Provides the best Taylor series approximation to the ideal lowpass filter
response at analog frequencies = 0 and = 1; for any order n.
Response is monotonic overall, decreasing smoothly from = 0 to =
inf.This filter is characterized by the property that its magnitude
response is flat in both passband and stopband.
Registration No: Section: Date: ______________
Elliptic filter
Equiripple in both the passband and stopband. Generally meets filter
requirements with the lowest order of any supported filter type. Given
a filter order n, passband ripple, and stopband ripple, elliptic filters
minimize transition width.
1. Find an analog lowpass filter with cutof frequency of 1 and translate this
prototype filter to the desired band configuration.
Alternatively, the butter, cheby1, cheby2 and ellip functions perform all steps
of the filter design and the buttord, cheb1ord, cheb2ord, and ellipord
functions perform minimum order computations for IIR filters.
The design of these filters starts with the following specifications:
1. The passband frequency Wp of the lowpass or highpass filter. It is a
two element vector [Wp1 Wp2] for the design of a bandpass or
bandstop filter, where Wp2>Wp1.
2. The stopband frequency Ws of the lowpass or highpass filter, where
Ws>Wp for the lowpass filter and Ws<Wp for the highpass filter. Ws is
a two element vector [Ws1 Ws2] for the bandpass or bandstop filter.
We have Ws2>Wp2>Wp1>Ws1 for the bandpass filter and
Wp2>Ws2>Ws1>Wp1 for the bandstop filter. All of these frequencies
are specified within the interval [0 1], where 1 represents the Nyquist
frequency.
3. The maximum attenuation Rp (in decibels) in the passband.
4. The minimum attenuation Rs (in decibels) in the stopband.
For the design of a high pass filter and a bandstop filter, we have to include a
string high and stop as the last argument in the filter functions, for
example,[b,a] = butter(N,Wn,high) for designing a Butterworth highpass
filter and [b,a]=cheby2(N,Rs,Wn,stop) for designing a Chebyshev II
stopband filter.
Task: Design a Butterworth Bandstop filter with Wp1 = 0.18, Ws1 = 0.2, Ws2
= 0.205, Wp2 = 0.24,Rp = 0.5 and Rs = 50 using fdatool.
[b a] = yulewalk(n,F,D)
DSP Lab#12
The FFT algorithm takes advantage of the periodicity and symmetry of the twiddle
constants
For a radix-2 (base 2), the FFT decomposes an N-point DFT into two (N/2)-point or
smaller DFTs. Each (N/2)-point DFT is further decomposed into two (N/4)-point DFTs,
and so on. The last decomposition consists of (N/2) two-point DFTs.
So we get
Registration No: Section: Date: ______________
Using we get
So
This results in decomposition of an eight-point DFT into two four-point DFTs with the
decimation-in-time procedure. This decomposition or decimation process is
repeated so that each four-point DFT is further decomposed into two two-point DFTs.
Since the last decomposition is (N/2) two-point DFTs,this is as far as this process
goes.
Registration No: Section: Date: ______________
Note: The smallest transform is determined by the radix of the FFT. For a radix-2
FFT,N must be a power or base of 2, and the smallest transform or the last
decomposition is the two-point DFT.
reversal procedure can be applied for larger values of N. For example, for N = 64,
represented by six bits, the first and sixth bits, the second and fifth bits, and the
third and fourth bits are swapped.
Digital signal processors such as the TMS320C6x (C6x) family of processors are like
fast special-purpose microprocessors with a specialized type of architecture and
instruction set appropriate for signal processing. Based on a very-long-instruction-
word (VLIW) architecture, the C6x is considered to be TIs most powerful processor.
The DSP interfaces to analog audio signals through an on-board AIC23 codec and
four 3.5 mm audio jacks (microphone input, line input, line output, and headphone
output). The codec can select the microphone or the line input as the active input.
The analog output is sent to both the line out and headphone out connectors. The
line out has a fixed gain, while the headphone out allows for an adjustable gain
connectors.
Registration No: Section: Date: ______________
Steps:
Use blocks from Simulink and other blocksets to complete the model
Add the Embedded Target for TI C6000 DSP blocks that let your signal sources
and output devices communicate with your C6713 DSK
Add the C6713 DSK target preferences block from the C6000 Target
Preferences library to your model. Verify and set the block parameters for the
target hardware.
select Simulation Configuration Parameters. For Target Selection,
choose the file ti_c6000.tlc. For Simulation and Code generation, un-
select Block reduction optimization and Implement logic signals.
Choose the TI C6000 target sel.. Set Code generation target type to
C6713DSK. Choose the TI C6000 compiler. Set Symbolic debugging. In
Registration No: Section: Date: ______________
the Select tree, choose the Debug category. Select Verbose build here.
Ensure that Solver is set to Fixed type / discrete.
DSP Lab#13
In this lab you will build a simple audio system that can run on a Texas Instruments
digital signal processor IC. First, you will create a model of the system in
Matlab/Simulink, then you will use tools to compile the model, download it, and run
it on the DSK7613 board, which contains a TI TMS320C6713 DSP IC.
3. Bring up the Simulink library browser by typing simulink at the Matlab prompt.
4. Start a new model by clicking on the blank document icon in the Simulink library
browser.
5. Save the model using the File/Save As pulldown. You can name it audio delay or
another descriptive name. Save it in the M:/matlab/work/ directory on your network
drive.
6. In the Simulink library browser, open the Target Support Package TC6 library
group
and select C6000 Target Preferences
7. From the C6000 Target Preferences library, drag C6713DSK to your empty model.
8. A window will pop up. Click Yes in the pop-up, to initialize the model configuration
parameters to the default values.
9. In the Simulink library browser, Target Support Package TC6 library group, select
the
C6713 DSK Board Support library.
10. From the C6713 DSK Board Support library, drag an ADC block and a DAC block
into the model you're building.
11. Go to the Signal Processing Blockset library group and select Signal Operations.
12. From the Signal Operations library, drag the Delay block into the model.
13. Open the Simulink library group and select Commonly Used Blocks.
14. From the Commonly Used Blocks library, drag the Sum block into the model.
Now you should have ve blocks in your model. You need to connect them and set
some
parameters in some of the blocks. The C6713DSK block (Target Preferences) doesn't
have
any connections, it just denes the settings for the DSP board.
15. Put the ADC, Delay, Sum, and DAC blocks in a line from left to right. This order is
the signal
ow for the model.
16. Double-click on the ADC block.
17. Change the ADC source from Line In to Mic In.
18. Note the sampling rate (8 KHz) and the word length (16-bit), then click OK on
the
ADC Source Block Parameters. You'll see the block in your model now says Mic In.
19. Highlight the ADC block by clicking once on it.
20. Hold the Ctrl key down and click on the Delay block. You should see an arrow
connect
the two blocks.
21. Hold the Ctrl key down and click on the Sum block, then click on the DAC block.
There should be arrows connecting all the blocks.
22. Right-click on the second input to the Sum block, hold the mouse button down,
and
move the mouse to create a line about an inch long. The line is dashed red because
it
isn't connected to anything.
23. Release the mouse button, then click again at the unattached end of the arrow,
hold
the button down, then drag over to the arrow between the ADC and the delay and
release the mouse button. Now there is a path from the ADC to the summing block.
(If you have to delete a line, highlight it by clicking on it, then use the delete key.)
Registration No: Section: Date: ______________
24. Double-click on the delay block. Set the delay to 2048 samples. Leave all other
settings
alone and click OK. You'll see the notation on the block change to z2048.
25. You can double-click on the DAC block to see that the settings for sample rate
and
word length match the ADC block. Don't change the settings.
26. Save the model.
Compile, Link, and Run the Model for the DSP Hardware
In the Simulink window, click on the Simulation pulldown, and select Configuration
Parameters.
(a) On the left side of the Configuration Parameters window, there is a \Select"
browser; use it to go to the Solver section and set the Stop time to \inf" (infinite).
(b) Use the browser to go to the Real-Time Workshop section and select Embed-
ded IDE Link CC. Under Project Options, make sure that the System stack size
(MAUs) is set to 8192.
31. Use the browser pane to go to the Real-Time Workshop section and and the
Generate
code button on the right side of the right-hand pane. Click on it.
32. If a window pops up notifying you of unapplied changes, accept the changes.
33. Look in the main Matlab Command Window. You should see a message like
### Connecting to Code Composer Studio(tm) ...
34. It will be at least two full minutes before anything appears to happen in the
Matlab
command window; be patient. (Note that once CCS is running and authenticated,
future use will be much faster.) Check for the animated CCS icon in the system tray
while you wait. Eventually you should see these messages (among others) in
Matlab:
TLC code generation complete.
Building project...
Download done.
then CCS will start up.
35. Bring the CCS window to the front so you can see messages there.
36. If you get an error, you might have to power-cycle the DSK board and close CCS,
then
Registration No: Section: Date: ______________
click on the Generate code button to re-start the process. You should see a window
pop up briey saying that it is downloading the compiled program to the DSK6713.
37. Once you see \**starting the model**" in the CCS message pane, and a green
light
and the message \RUNNING" in the border at the lower left of the CCS window, you
should be able to try the model. In the command window you should see \Download
done". Snap your fingers or tap the microphone and listen for the output.