Professional Documents
Culture Documents
2021-2022
USN NUMBER :
BATCH :
Prepared By:
Dr. MAHALINGA V MANDI, Professor, Dept of ECE.
NAGARATHNA H S, Assistant Professor, Dept. of ECE.
MANJULA N, Assistant Professor, Dept. of ECE.
SHWETHA N, Assistant Professor, Dept. of ECE.
Syllabus
Subject Title: DSP Lab
1 To analyze the sampling process, impulse response, convolution, frequency domain response of
LTI systems
2 To analyze and design of digital IIR and FIR filters
3 To demonstrate the DSP algorithms using Matlab software
4 To demonstrate the DSP algorithms using Code Composer Studio
5 To study DSP algorithms on Virtual Laboratory
TEXT 1
Hardware programs using TMS320C6713 DSK
1. Linear convolution of two given sequences. 12 L1, L2
B 2. Circular convolution of two given sequences.
3. Computation of N- Point DFT of a given sequence
4. Impulse response of first order system.
TEXT 2
Virtual Laboratory
C 1. Study of Sampling Theorem 09 L1, L2, L3
2. Study of DFT and its inverse
3. Study of FIR low pass and high pass filters
4. Study of IIR low pass and high pass filters
TEXT 1 and Web link 1
Course Outcomes:
CO1 Define and verify the sampling theorem, impulse response, convolution and frequency response
of the system
Web Links:
1. http://vlabs.iitkgp.ernet.in/dsp/index.html#
Table of Contents
Introduction to MATLAB
4. Graphics: MATLAB has extensive facilities for displaying vectors and matrices as
graphs, as well as annotating and printing these graphs. It includes high-level functions for
two-dimensional and three-dimensional data visualization, image processing, and animation
and presentation graphics. It also includes low-level functions that allow you to fully customize
the appearance of graphics as well as to build complete graphical user interfaces on your
MATLAB applications.
5. MATLAB Application Program Interface (API): This is a library that allows you to
write C and FORTRAN programs that interact with MATLAB. It includes facilities for calling
routines from MATLAB (dynamic linking), calling MATLAB as a computational engine and for
reading and writing MAT-files.
C. MATLAB windows:
In MATLAB there are three basic windows -
1. Command window: This is the main window. It is characterized by the MATLAB command
prompt ‘>>’. When you launch the application program, MATLAB puts you in this window.
All commands, including those for running user-written programs, are typed in this
window at the MATLAB prompt.
Also there are several sub windows, which include -
Launch Pad: This sub window lists all MATLAB related applications and toolboxes
that are installed on your machine.
Workspace: This sub window lists all variables that you have generated so far and
shows their type and size.
Command History: All commands typed on the MATLAB prompt in the command
window get recorded, even across multiple sessions (you worked on Monday, then
on Thursday, and then on next Wednesday, and so on), in this window. You can
select a command from this window with the mouse and execute it in the command
window by double clicking on it.
Current Directory: This is where all your files from the current directory are listed.
You can do file navigation here.
2. Graphics window: The output of all graphics commands typed in the command window
are flushed to the graphics or Figure window, a separate gray window with (default) white
background color. The user can create as many figure windows as the system memory will
allow.
How to Program?
S1: Click the Icon on the Desktop
Ideal Window
S4: Enter the code in editor and save it with appropriate name.
1.M – Files: These are standard ASCII text files, with a .m extension to the filename. There are
two types of these files: script files and function files. All built-in functions in MATLAB are M-
files, most of which reside on your computer in precompiled format.
2.Mat – Files: These are binary data files, with a .mat extension to the filename. Mat – files are
created by MATLAB when you save data with the save command. The data is written in a
special format that only MATLAB can read. Mat – files can be loaded into MATLAB with the
load command.
3. Mex- Files: These are MATLAB – callable FORTRAN and C programs, with a .mex extension
to the filename. Use of these files requires some experience with MATLAB and a lot of patience.
Part A
MATLAB Programs
Theory:
A discrete time LTI system (also called digital filters) as shown in Fig.2.1 is
represented by
o A linear constant coefficient difference equation, for example,
y[n] a1 y[n 1] a 2 y[n 2] b0 x[n] b1 x[n 1] b2 x[n 2];
o A system function H(z) (obtained by applying Z transform to the difference
Y ( z ) b0 b1 z 1 b2 z 2
equation). H ( z )
X ( z ) 1 a1 z 1 a 2 z 2
o A frequency response function obtained by applying DTFT on the impulse
response h[n] (or by replacing z with ejΩ in H(z)) to get
Y (e j ) b0 b1e j b2 e 2 j
H ( e j )
X (e j ) 1 a1e j a 2 e 2 j
Given the difference equation or H(z), the impulse response of the LTI system is found
using filter or impz MATLAB functions. This is elaborated in experiment no.7 -
Solving a given difference equation. If the difference equation contains past samples of
output, i.e., y[n-1], y[n-2], etc , then its impulse response is of infinite duration (IIR). For
such systems the impulse response is computed for a large value of n, say n=100 (to
approximate n=∞).
Given only the input sequence x[n] and the output sequence y[n], we can find the
impulse function h[n] by using the inverse operation deconv. (The conv operation
convolves 2 sequences x[n] and h[n] to obtain the output y[n]. for convolution
operation refer experiment no 3: Linear convolution of two given sequences). If both
y[n] and x[n] are finite then the impulse response is finite (FIR).
The deconvolution operation is valid only if the LTI system is ‘invertible’.
Algorithm :
1. Input the two sequences as x and y.
2. Use deconv to get impulse response h.
3. Plot the sequences.
4. Verify using conv(x,h) to get y back.
Result:
Length of impulse response required = 10
h=
1.000000000000000
1.000000000000000
0.100000000000000
-0.800000000000000
-0.890000000000000
-0.170000000000000
0.631000000000000
0.784000000000000
0.216100000000000
-0.489500000000000
Theory:
The output y[n] of a LTI (linear time invariant) system can be obtained by convolving
the input x[n] with the system’s impulse response h[n].
The convolution sum is y[n] x[n] h[n] x[k ]h[n k ]
k
x[n k ]h[k ]
k
analytically evaluate the convolution formula to get a functional form (closed form
solution) of y[n].
If both the sequences are of finite duration, then we can use the MATLAB function
‘conv’ to evaluate the convolution sum to obtain the output y[n]. Convolution is
implemented as polynomial multiplication (refer MATLAB help).
The length of y[n] = xlength + hlength -1.
The conv function assumes that the two sequences begin at n=0 and is invoked by
y=conv(x,h).
Even if one of the sequences begin at other values of n, say n=-3,or n=2; then we need
to provide a beginning and end point to y[n] which are ybegin=xbegin+hbegin and
yend=xend+hend respectively.
Algorithm:
1. Input the two sequences as x1, x2
2. Convolve both to get output y.
3. Plot the sequences.
MATLAB Program:
Level 1 Program (both sequences are right sided)
%main part of computation
x =input('Enter the first sequence = ');
h =input('Enter the second sequence = ');
y=conv(x, h);
disp('Linear convolution of x(n) and h(n) is y = ');
disp(y);
subplot(2,2,3);
stem(x);
xlabel('Time Index n');
ylabel('Amplitude');
title('Plot of x(n)');
subplot(2,2,4);
stem(h);
xlabel('Time index n');
ylabel('Amplitude');
title('Plot of h(n)’);
Result
Result
x1 =1 2 3 2 1 3 4
n1 =-3 -2 -1 0 1 2 3
x2 =2 -3 4 -1 0 1
n2 =-1 0 1 2 3 4
linear con of x1 & x2 is y = 2 1 4 2 6 9 3 2 15 -3 3 4
Theory:
As seen in the last experiment, the output y[n] of a LTI (linear time invariant) system
can be obtained by convolving the input x[n] with the system’s impulse response h[n].
The above linear convolution is generally applied to aperiodic sequences. Whereas
the Circular Convolution is used to study the interaction of two signals that are
periodic.
The linear convolution sum is y[n] x[n] h[n] x[k ]h[n k ] x[n k ]h[k ] . To
k k
compute this equation, the linear convolution involves the following operations:
o Folding- fold h[k] to get h[-k] ( for y[0])
o Multiplication – vk[n] = x[k] × h[n-k] (both sequences are multiplied sample by
sample)
o Addition- Sum all the samples of the sequence vk[n] to obtain y[n]
o Shifting – the sequence h[-k] to get h[n-k] for the next n.
The circular convolution sum is y[n] x[n] N h[n] x[k ]h[ n k N
] where the
k
x[-4] and x[N-2] =x[-3] places. Now x[n] is entered as x[n] {1,1,0,2,3} .
Algorithm:
1. Input the two sequences as x and h.
2. Circularly convolve both to get output y.
3. Plot the sequences.
MATLAB Program:
%plot
n1=0:N-1;
stem(n1,y);
title('Circular convolution output y(n)');
Result:
circular convolution of x1 &x2 is
y= 26 28 26 20
The plot is as shown in Fig. 4.1
Verification:
Find circular convolution manually for x1(n) = [1 2 3 4] and x2(n) = [4 3 2 1]
Exercise:
1. Find the circular convolution of x(n) = [1 2 3 4] and h(n) = [1 -1 0 -1]
Ans: y(n) = - 5. - 2. - 3. 0.
2. Find the circular convolution of x(n) = [1 2 3 4] and h(n) = [4 3 2 1]
Aim: To find linear convolution of x(n) and h(n) using DFT and IDFT
Theory:
Discrete Fourier Transform (DFT) is used for performing frequency analysis of discrete
time signals. DFT gives a discrete frequency domain representation whereas the other
transforms are continuous in frequency domain.
The N point DFT of discrete time signal x[n] is given by the equation
N -1 j 2kn
X (k ) x[n]e N
; k 0,1,2,....N - 1
n 0
Where N is chosen such that N L , where L=length of x[n].
The inverse DFT allows us to recover the sequence x[n] from the frequency samples.
j 2kn
1 N 1
x[n] x[n]e N ; n 0,1,2,....N - 1
N k 0
X(k) is a complex number (remember ejw=cosw + jsinw). It has both magnitude and phase
which are plotted versus k. These plots are magnitude and phase spectrum of x[n]. The ‘k’
gives us the frequency information.
Algorithm:
1. Input the two sequences as x1, x2
2. Obtain N-point DFTs (X1, X2) of both the sequences.
3. Multiply both the DFTs (Y=X1×X2).
4. Perform IDFT on Y to get y[n] (the linearly convolved sequence) in time domain.
5. Verify using the ‘conv’ command.
6. Plot the sequences.
MATLAB Program:
x1=[1 2 3 4];
x2=[1 -1 0 -1];
N=length(x1)+length(x2)-1;
% Obtain DFTs
X1=fft(x1,N);
X2=fft(x2,N);
%Verify
disp('Output using conv command');
yv=conv(x1,x2);
disp(yv);
%Plot
stem(yn);
xlabel('Time Index');
ylabel('Amplitude');
title('Linear convolution output y(n)');
Result:
Exercise:
1. Find the linear convolution of x(n) = [1 2 3 4] and h(n) = [1 1 1 1] using DFT and IDFT.
Ans: y(n) = 1 3 6 10 9 7 4
2.Find the linear convolution of x(n) = [1 2 3 4] and h(n) = [1 2 3 4] using DFT and IDFT.
Ans: y(n) = 1 4 10 20 25 24 16
Aim: To perform circular convolution of two given sequences x1 & x2 using DFT &
IDFT
Theory:
By multiplying two N-point DFTs in the frequency domain, we get the circular
convolution in the time domain.
N DFT
y[n] x[n] h[n] Y (k ) X (k ) H (k )
Circular Convolution is made identical to linear convolution by choosing the number
of points in DFT as N ≥ xlength + hlength -1.
For Circular Convolution, N = max (xlength, length).
Algorithm:
1.Input the two sequences as x1, x2
2.Compute N = xlength + hlength -1
(for circular convolution, N=max(xlength + hlength))
3. Obtain N-point DFTs (X1, X2) of both the sequences.
4. Multiply both the DFTs (Y=X1×X2).
5. Perform IDFT on Y to get y[n] (the linearly convolved sequence) in time domain.
6. Verify using the ‘conv’ command.
7. Plot the sequences.
MATLAB Program:
%obtain DFTs
X1= fft(x1,N);
X2= fft(x2,N);
Result:
circular convolution of x1 &x2 is yn=
26 28 26 20
Exercise:
1. Find the circular convolution of x1(n) = [1 2 3 4] and x2(n) = [1 -1 0 -1] using DFT and
IDFT.
Ans: x3(n) = - 5. - 2. - 3. 0.
2.Find the circular convolution of x1(n) = [1 2 3 4] and x2(n) = [4 3 2 1] using DFT and
IDFT.
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the window.
Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with π
corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz
Wc = 2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians
For MATLAB the Normalized cut-off frequency is in the range 0 and 1, where 1
corresponds to fs/2 (i.e.,fmax)). Hence to use the MATLAB commands
wc = fc / (fs/2) = 400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then the normalized frequency is computed
as wc = Wc / π
Step 2: Compute the Impulse Response h(n) of the required FIR filter using the given
Window type and the response type (lowpass, bandpass, etc). For example given a
rectangular window, order N=20, and a high pass response, the coefficients (i.e., h[n]
samples) of the filter are computed using the MATLAB inbuilt command ‘fir1’ as
h =fir1(N, wc , 'high', boxcar(N+1));
Note: In theory we would have calculated h[n]=hd[n]×w[n], where hd[n] is the desired
impulse response (low pass/ high pass,etc given by the sinc function) and w[n] is the
window coefficients. We can also plot the window shape as stem(boxcar(N)).
Plot the frequency response of the designed filter h(n) using the freqz function and observe
the type of response (lowpass / highpass /bandpass).
Method 2:
Given the pass band (wp in radians) and Stop band edge (ws in radians) frequencies, Pass
band ripple Rp and stopband attenuation As.
Step 1: Select the window depending on the stopband attenuation required. Generally
if As>40 dB, choose Hamming window. (Refer table )
Step 2: Compute the order N based on the edge frequencies as
Transition bandwidth = tb=ws-wp;
N=ceil (6.6*pi/tb);
Matlab Program
%Matlab program to plot frequency response of FIR filter using window method
Columns 6 through 10
-0.014216197752006 -0.020888713510276 -0.018508552753882 0.000000000000000
0.037361228367975
Columns 11 through 15
0.088951656982208 0.142932290865991 0.184007885117237 0.199361767674871
0.184007885117237
Columns 16 through 20
0.142932290865991 0.088951656982208 0.037361228367975 0.000000000000000 -
0.018508552753882
Columns 21 through 25
-0.020888713510276 -0.014216197752006 -0.005775105285073 -0.000000000000000
0.002249712122493
Columns 26 through 27
0.002347898907031 0.001857013100866
Waveforms:
%Method 2:
% Method 2 -To Design a Low Pass FIR Filter
wp=input('Passband frequency = ');
ws=input('Stopband frequency = ');
wt=ws-wp;
N=(8/wt)+1;
N=round(N);
wh=(hamming(N));
b=fir1(N-1,wp,wh);
disp('Impulse response coefficients = ');
disp(b);
freqz(b);
title('Response of FIR filter using Hamming window');
Result:
Save and execute the program;
Passband frequency=0.3
Stopband frequency=0.45
Ans:
Aim: To find order of LP Butterworth IIR filter and to plot frequency response
Method I:
Given the order N, cutoff frequency fc, sampling frequency fs and the IIR filter type
(butterworth, cheby1, cheby2).
Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with π
corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz
Wc = 2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians
For MATLAB the Normalized cut-off frequency is in the range 0 and 1, where 1 corresponds
to fs/2 (i.e.,fmax)). Hence to use the MATLAB commands
wc = fc / (fs/2) = 400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then the normalized frequency is computed as
wc = Wc / π
Step 2: Compute the Impulse Response [b,a] coefficients of the required IIR filter and the
response type (lowpass, bandpass, etc) using the appropriate butter, cheby1, cheby2
command. For example given a butterworth filter, order N=2, and a high pass response, the
coefficients [b,a] of the filter are computed using the MATLAB inbuilt command ‘butter’ as
[b,a] =butter(N, wc , 'high');
Method 2:
Given the pass band (Wp in radians) and Stop band edge (Ws in radians) frequencies,
Pass band ripple Rp and stopband attenuation As.
Step 1: Since the frequencies are in radians divide by π to obtain normalized
frequencies to get wp=Wp/pi and ws=Ws/pi
If the frequencies are in Hz (note: in this case the sampling frequency should be
given), then obtain normalized frequencies as wp=fp/(fs/2), ws=fstop/(fs/2), where
fp, fstop and fs are the passband, stop band and sampling frequencies in Hz
Step 2: Compute the order and cut off frequency as
[N, wc] = BUTTORD(wp, ws, Rp, Rs)
Step 3: Compute the Impulse Response [b,a] coefficients of the required IIR filter and
the response type as [b,a] =butter(N, wc , 'high');
1. Once the coefficients of the IIR filter [b,a] are obtained, the next step is to simulate an
input sequence x[n], say input of 100, 200 & 400 Hz (with sampling frequency of fs),
each of 20/30 points. Choose the frequencies such that they are >, < and = to fc.
2. Filter the input sequence x[n] with Impulse Response, to obtain the output of the filter
y[n] using the ‘filter’ command.
3. Infer the working of the filter (low pass/ high pass, etc).
MATLAB IMPLEMENTATION
BUTTORD Butterworth filter order selection.
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
Butterworth filter that loses no more than Rp dB in the passband and has at least Rs dB of
attenuation in the stopband. Wp and Ws are the passband and stopband edge frequencies,
normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For example,
Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1
Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB frequency") to
use with BUTTER to achieve the specifications.
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which
case Wp and Ws are in radians/second.
When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp in BUTTORD.
%Calculation part
w1=2*f1/fs;
w2=2*f2/fs;
[n,wn]=buttord(w1,w2,k1,k2);
[b,a]=butter(n,wn);
freqz(b,a);
grid on;
title('Butterworth Filter Frequency Response');
Result:
wn=
0.410440972699444
Numerator coefficients b=
Columns 1 through 5
0.011653932946109 0.069923597676657 0.174808994191642 0.233078658922190
0.174808994191642
Columns 6 through 7
0.069923597676657 0.011653932946109
Denominator coefficients a=
Columns 1 through 5
1.000000000000000 -1.063463131405513 1.200505288359735 -0.583591594317949
0.231817249706670
Columns 6 through 7
-0.043710924415157 0.004294820623221wn=
0.4104
Exercise:
Design an IIR Butterworth LP filter to meet the following specifications:
Pass band edge (Hz) =1000 Hz
Stop band edge (Hz) = 3000 Hz
Pass band attenuation (dB) = - 1
Stop band attenuation (dB) = -15
Sampling rate (Hz) = 10000 Hz
Aim: To find order of LP Chebyshev IIR filter and to plot frequency response
Chebyshev filters are analog or digital filters having a steeper roll-off and more passband
ripple (type I) or stopband ripple (type II) than Butterworth filters. Chebyshev filters have the
property that they minimize the error between the idealized and the actual filter characteristic
over the range of the filter, but with ripples in the passband.
%Calculation part
w1=2*f1/fs;
w2=2*f2/fs;
[n,wn]=cheb1ord(w1,w2,k1,k2);
[b,a]=cheby1(n,k1,wn);
freqz(b,a);
grid on;
title('Chebychev Filter Frequency Response');
Result:
Save and execute the program;
RESULT:
Enter Passband frequency f1 = 1500
Enter Stopband frequency f2 = 2000
Enter Sampling frequency fs = 8000
Enter Passband attenuation k1 = 1
Enter Stopband attenuation k2 = 15
Denominator coefficients a=
1.000000000000000 -1.799381978282539 1.963665269278695 -1.151337941252095
0.33013186753684
Exercise:
Design an IIR Chebyshev LP filter to meet the following specifications:
Part B
CCS INTRODUCTION
TMS320C6713 DSK and Code Composer Studio
1. Introduction
The hardware experiments in the DSP lab are carried out on the Texas Instruments
TMS320C6713 DSP Starter Kit (DSK), based on the TMS320C6713 floating point DSP running
at 225 MHz. The basic clock cycle instruction time is 1/(225 MHz)= 4.44 nanoseconds. During
each clock cycle, up to eight instructions can be carried out in parallel, achieving up to 8×225
= 1800 million instructions per second (MIPS).
The C6713 processor has 256KB of internal memory, and can potentially address 4GB
of external memory. The DSK board includes a 16MB SDRAM memory and a 512KB Flash ROM.
It has an on-board 16-bit audio stereo codec (the Texas Instruments AIC23B) that serves both
as an A/D and a D/A converter. There are four 3.5 mm audio jacks for microphone and stereo
line input, and speaker and head-phone outputs. The AIC23 codec can be programmed to
sample audio inputs at the following sampling rates:
fs = 8, 16, 24, 32, 44.1, 48, 96 kHz
The ADC part of the codec is implemented as a multi-bit third-order noise-shaping
delta-sigma converter that allows a variety of oversampling ratios that can realize the above
choices of fs. The corresponding oversampling decimation filters act as anti-aliasing pre-filters
that limit the spectrum of the input analog signals effectively to the Nyquist interval [−fs/2,
fs/2]. The DAC part is similarly implemented as a multi-bit second-order noise-shaping delta-
sigma converter whose oversampling interpolation filters act as almost ideal reconstruction
filters with the Nyquist interval as their passband.
The DSK also has four user-programmable DIP switches and four LEDs that can be used
to control and monitor programs running on the DSP.
All features of the DSK are managed by the CCS, which is a complete integrated development
environment (IDE) that includes an optimizing C/C++ compiler, assembler, linker, debugger,
and program loader. The CCS communicates with the DSK via a USB connection to a PC. In
addition to facilitating all programming aspects of the C6713 DSP, the CCS can also read signals
stored on the DSP’s memory, or the SDRAM, and plot them in the time or frequency domains.
The following block diagram depicts the overall operations involved in all of the
hardware experiments in the DSP lab. Processing is interrupt-driven at the sampling rate fs,
as explained below.
The AIC23 codec is configured (through CCS) to operate at one of the above sampling
rates fs. Each collected sample is converted to a 16-bit two’s complement integer (a short data
type in C). The codec actually samples the audio input in stereo, that is, it collects two samples
for the left and right channels.
At each sampling instant, the codec combines the two 16-bit left/right samples into a
single 32-bit unsigned integer word (an unsigned int, or Uint32 data type in C), and ships it
over to a 32-bit receive register of the multichannel buffered serial port (McBSP) of the C6713
processor, and then issues an interrupt to the processor.
Upon receiving the interrupt, the processor executes an interrupt service routine (ISR)
that implements a desired sample processing algorithm programmed with the CCS (e.g.,
filtering, audio effects, etc.). During the ISR, the following actions take place: the 32-bit input
sample (denoted by x in the diagram) is read from the McBSP, and sent into the sample
processing algorithm that computes the corresponding 32-bit output word (denoted by y),
which is then written back into a 32-bit transmit-register of the McBSP, from where it is
transferred to the codec and reconstructed into analog format, and finally the ISR returns from
interrupt, and the processor begins waiting for the next interrupt, which will come at the next
sampling instant. Clearly, all processing operations during the execution of the ISR must be
completed in the time interval between samples, that is, T = 1/fs. For example, if fs = 44.1 kHz,
then, T = 1/fs = 22.68 μsec. With an instruction cycle time of Tc = 4.44 nsec, this allows T/Tc =
5108 cycles to be executed during each sampling instant, or, up to 8×5108 = 40864
instructions, or half of that per channel.
NOTE: You can do this alternatively by following the below mentioned steps
5. Click on Use default location check box, this project will be stored in the location default
location
NOTE: In the location field do not change the default path. This avoids misplacing of your projects.
Type in your program in this new source file and save as ‘main.c’ in the folder where your project will
be saved
Expression
Window
Project
Window
Editor
Window
Output Graph
Window
Window
C Program
#include<stdio.h>
int y[12];
main()
{
int m=6; /*Lenght of i/p samples sequence*/
int n=6; /*Lenght of impulse response Co-efficients */
int i=0,j;
int x[15]={1,2,3,4,5,6}; /*Input Signal Samples*/
int h[15]={1,2,3,4,5,6}; /*Impulse Response Co-efficients*/
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
for(i=0;i<m+n-1;i++)
printf("%d\n",y[i]);
h[k ]x n k
The circular convolution sum is y[n] x[n] N h[n] N
k
C Program
#include<stdio.h>
#include<math.h>
float x[5]={1,2,3,4,5};
float h[5]={2,1,3,4,5};
float y[10];
main()
{
int N=5;
int k,n,i;
for(n=0;n<N;n++)
{
y[n]=0;
for(k=0;k<N;k++)
{
i=(n-k)%N;
if(i<0)
i=i+N;
y[n]=y[n]+h[k]*x[i];
}
printf("%f\t",y[n]);
}
}
Result
C Program
#include<stdio.h>
float x[20],y[20],y1[20];
main()
{
float a1,a2,b0,b1,b2;
int i,j,N=20;
a1=-1.1430;a2=0.4128;
b0=0.0675;b1=0.1349;b2=0.0675;
x[0]=1;
for(i=1;i<N;i++)
{
x[i]=0;
}
for(j=0;j<N;j++)
{
y[j]=b0*x[j];
if(j>0)
y[j]=y[j]+b1*x[j-1]-a1*y[j-1];
if((j-1)>0)
y1[j]=y[j]+b2*x[j-2]-a2*y[j-2];
printf("%f\t",y[j]);
printf("%f\n",y1[j]);
}
}
Theory:
Discrete Fourier Transform (DFT) is used for performing frequency analysis of
discrete time signals. DFT gives a discrete frequency domain representation whereas
the other transforms are continuous in frequency domain.
The N point DFT of discrete time signal x[n] is given by the equation
N -1 j 2kn
X (k ) x[n]e N
; k 0,1,2,....N - 1
n 0
Where N is chosen such that N L , where L=length of x[n].
The inverse DFT allows us to recover the sequence x[n] from the frequency samples.
j 2kn
1 N 1
x[n] x[n]e N ; n 0,1,2,....N - 1
N k 0
X(k) is a complex number (remember ejw=cosw + jsinw). It has both magnitude and
phase which are plotted versus k. These plots are magnitude and phase spectrum of
x[n]. The ‘k’ gives us the frequency information.
Here k=N in the frequency domain corresponds to sampling frequency (fs). Increasing
N, increases the frequency resolution, i.e., it improves the spectral characteristics of the
sequence. For example if fs=8kHz and N=8 point DFT, then in the resulting spectrum,
k=1 corresponds to 1kHz frequency. For the same fs and x[n], if N=80 point DFT is
computed, then in the resulting spectrum, k=1 corresponds to 100Hz frequency. Hence,
the resolution in frequency is increased.
Algorithm
1. Input the sequence for which DFT is to be computed.
2. Input the length of the DFT required (say 4, 8, >length of the sequence).
3. Compute the DFT using the ‘fft’ command.
4. Plot the magnitude & phase spectra.
C Program
#include<stdio.h>
#include<math.h>
float y[16];
float x[4]={1,3,2,5};
float w;
main()
for(k=0;k<2*N;k=k+2)
y[k]=0;y[k+1]=0;
k1=k/2;
for(n=0;n<xlen;n++)
w=-2*3.14*k1*n/N;
y[k]=y[k]+x[n]*cos(w);
y[k+1]=y[k+1]+x[n]*sin(w);
printf("%f+j%f\n",y[k],y[k+1]);
Result
Part C
Virtual Laboratory
Virtual laboratory
Introduction:
Theory:
The real life signals that we encounter in our day to day basis are mostly analog signals. These
signals are defined continuously in time and have infinite range of amplitude values. In order
to process these signals to obtain meaningful information, they need to be converted to a
format which is easily handled by computing resources like microprocessors, computers etc...
The first step in this process is to convert the real-time signal into discrete-time signals.
Discrete-time signals are defined only at a particular set of time instances. They can thus be
represented as sequence of numbers with continuous range of values.
The process of converting an analog signal (denoted as x(t)) to a digital signal (denoted as
x(n)) is called the analog-to-digital conversion (referred to as digitization), usually performed
by an analog-to-digital converter (ADC). Here t is the continuous time variable and n is the
sequence order. In many applications after the processing of the digital signal is performed,
x(n) needs to be converted back to analog signal x(t) before it is applied to appropriate analog
device. This reverse process is called digital-to-analog conversion and is typically performed
using a digital-to-analog converter (DAC).
The typical block diagram of an ADC is shown in Fig. 1 below.
The process of digitization consists of first sampling (digitization in time) and quantization
(digitization in amplitude). In this experiment we will study and understand the principle of
sampling, while the principle of quantization will be studied in the next experiment. The
sampling process depicts an analog signal as a sequence of values. The basic sampling function
can be carried out with an ideal 'sample-and-hold' circuit which maintains the sampled signal
until next sample is taken. An ideal sampler can be considered as a switch that periodically
opens and closes every T seconds. The sampling frequency (fs in Hertz) is thus defined as
fs=1T....(1)
The sampled discrete time signal x(nT) , n=0,1,2,.... of the original continuous time signal x(t)
is shown in Fig. 2 below.
In order to represent an analog signal x(t) by a discrete-time signal x(nT) accurately, so that
the analog signal can be exactly reconstructed back from the discrete-time signal, the sampling
frequency fs must be at least twice the maximum frequency component (fM) of the original
analog signal. Thus we have,
Now let's consider, the analog signal of frequency 5Hz as shown in Fig. 4(a) below. The
sampling frequency is same as above, i.e. 4Hz. The sampled signal is shown in Fig. 4(b), Note
that the reconstruction of the original analog signal is not possible since the sampling
frequency does not satisfy Shannon's Sampling Theorem. In this case the reconstructed signal
has a frequency of 1Hz. The signal of 5Hz is folded back as 1Hz, into the range determined by
the sampling frequency leading to the problem of aliasing.
Procedure:
1. Click on the Experiment tab SIMULATOR, it will open the workspace.
2. See the movie in experiment page by pressing help button ? to understand how the following
steps are to be executed.
3. In the workspace click on Browse Blocks BROWSE BLOCKS.to understand how the
following steps are to be executed.
4. Drag Sinewave Generator in the left side of the workspace. Click it to parameterize the
sinusoidal signal output. Make amplitude = 3V, frequency = 19 Hz, phase=0 angle.
5. Drag Sampling Block in the workplace. Place it after Sinewave Generator to its right. Click it
to parameterize. Make sampling frequency =40 Hz. It will show no. of sample as 80 for display.
The display is conformed for 2 sec.
8. Connect sinewave generator O/P to I/O of sampler by clicking at both blocks and a link will
appear.
9. Similarly connect sampler O/P to one of the I/O of scope.
10. Click somewhere in the middle of the link connecting signal generator & sampler. Keep
clicking at bends till you connect it to the other input of the scope.
11. Click on the scope, a new window will appear. It shows that the sinusoidal signal as well as
sample in red dots.
12. Move the cursor from one sample to another by dragging the slider provide along the x-
axis and you will find sample values in the boxes at bottom left part of the window. Note the
first 15 values in a note book. This 15 values will go as table 1 in your report.
13. In this window, you can change parameters like frequency, amplitude and phase angle of
sinusoidal signal generator & sampling frequency.
14. Change the sinusoidal signal generator O/P amplitude to 1.5V and note 1st 15 values. This
will form Table 2 in your report. In your observation & discussion part of the reporting, you
have to compare Table 1 and Table 2.
15. Change sampling frequency to 43 Hz and amplitude of sinusoidal signal generator at 1V.
Note first 15 readings to from Table 3. Compare Table 1 and Table 3
16. Make sampling frequency 40Hz, amplitude of sinusoidal signal generator 10V, phase=30.
Table 1st reading to from Table 4. Compare Table 1 and Table 4.
Theory:
The discrete-time Fourier transform (DTFT) of a sequence is a continuous function of ω, and
repeats with period 2π In practice we usually want to obtain the Fourier components using
digital computation, and can only evaluate them for a discrete set of frequencies. The discrete
Fourier transform (DFT) provides a means for achieving this .
The DFT is itself a sequence, and it corresponds roughly to samples, equally spaced in
frequency, of the Fourier transform of the signal. The discrete Fourier transform of a length N
signal x[n], n = 0, 1... N - 1 is given by
Procedure:
1. Click on the simulator tab SIMULATOR, it will open the workspace.
2. See the movie in experiment page by pressing help button ? to understand how the following
steps are to be executed.
3. Workspace provides three user controls to visualize the DFT and inverse DFT: signal
selector, a slider to set the input signal no. of samples and another slider to set the DFT no. of
points.
Fig-1
4. Select rectangular signal first and set the no. of samples in input signal to 8 also DFT points
to 8. The input signal plot will be be shown in Graph 1.
5. Now observe the magnitude and spectrum of the signal after applying DFT in Graph no. 2
and 3. Simultaneously in Graph 4 will show the signal after inverse DFT.
Fig-2
6. Note down the values of No. of samples in input signal, DFT points and first 10 to 15 values
of Graph 1, 2, 3 and 4 which will give you table no. 1 of report generations
7. Now repeat step 4 to 6 for other two types of input signals and take the reading which will
give you table 2 and 3.
8. Then set value of no. of DFT points to 16 and choose ramp signal as input, see the changes
in the magnitude and phase spectrum of the signal
Fig-3
9. Take the reading which will give you table 4 and observe the graphs and make a discussion
on it in report generation.
10. Now set the value of DFT points to 4. See the changes in graph. And take the reading will
give you table no. 5. Again do some discussions on graph you obtained in report generation.
11. For every set of readings take a screenshot of the simulation stage, using the button
provided on the simulation stage at right upper most corner and upload each
image with the table data in report generation.
12. Complete the Observation and discuss the results in report generation.
13. Then click Yes I have finished my Experiment button to submit your report.
Theory:
Finite Impulse Response (FIR) filters are digital filters with finite impulse response.
They are also known as non-recursive digital filters as they do not have the feedback (a
recursive part of a filter), even though recursive algorithms can be used for FIR filter
realization. Hence it is an all zero filter. Therefore input and output difference equation for FIR
filter is given by
y(n)=b0x(n)+b1x(n−1)+b2x(n−2)+...+bM−1x(n−N+1)
Where b0, b1, b2 ... b(M-1) are filter coefficients. FIR filters are particularly useful for
applications where exact linear phase response is required. The FIR filter is generally
implemented in a non-recursive way which guarantees a stable filter.
FIR filters can be designed using different methods, but most of them are based on ideal filter
approximation. The objective is not to achieve ideal characteristics, as it is impossible anyway,
but to achieve sufficiently good characteristics of a filter. The transfer function of FIR filter
approaches the ideal as the filter order increases, thus increasing the complexity and amount
of time needed for processing input samples of a signal being filtered.
Fig-1
FIR filters can have linear phase characteristic, which is not like IIR filters. Obviously, in such
cases when it is necessary to have a linear phase characteristic, FIR filters are the only option
available. If the linear phase characteristic is not necessary, as is the case with processing
speech signals, FIR filters are not good solution at all.
One of the drawbacks of FIR filters is a high order of designed filter. The order of FIR filter is
remarkably higher compared to an IIR filter with the same frequency response. This is the
reason why it is so important to use FIR filters only when the linear phase characteristic is
very important.
A number of delay lines contained in a filter, i.e. a number of input samples that should be
saved for the purpose of computing the output sample, determines the order of a filter. For
example, if the filter is assumed to be of order 10, it means that it is necessary to save 10 input
samples preceding the current sample. All eleven samples will affect the output sample of FIR
filter
The transform function of a typical FIR filter can be expressed as a polynomial of a complex
variable z-1. All the poles of the transfer function are located at the origin. For this reason, FIR
filters are guaranteed to be stable, whereas IIR filters have potential to become unstable
Basic concepts and FIR filter specification
Most FIR filter design methods are based on ideal filter approximation. The resulting filter
approximates the ideal characteristic as the filter order increases, thus making the filter and
its implementation more complex
Figure 4.a and 4.b: Transfer functions of two standard ideal filters
The ideal filter frequency response can be computed via inverse Fourier transform. The two
standard ideal filters frequency responses are contained in the table 1 below.
If the transition region of designed filter is wider than needed, it is necessary to increase the
filter order, re-estimate the window function coefficients and ideal filter frequency samples,
multiply them in order to obtain the frequency response of designed filter and re-estimate the
transfer function as well. If the transition region is narrower than needed, the filter order can
be decreased for the purpose of optimizing hardware and/or software resources. It is also
necessary to re-estimate the filter frequency coefficients after that. For the sake of precise
estimates, the filter order should be decreased or increased by 1.
Window functions
The window method is most commonly used method for designing FIR filters. The simplicity
of design process makes this method very popular.
A window is a finite array consisting of coefficients selected to satisfy the desirable
requirements. This chapter provides a few methods for estimating coefficients and basic
characteristics of the window itself as well as the result filters designed using these
coefficients. The point is to find these coefficients denoted by w[n].
When designing digital FIR filters using window functions it is necessary to specify:
A window function to be used; and
The filter order according to the required specifications (selectivity and stop band
attenuation).
These two requirements are interrelated. Each function is a kind of compromise between the
two following requirements:
The higher the selectivity, i.e. the narrower the transition region; and
The higher suppression of undesirable spectrum, i.e. the higher the stop band attenuation.
Table 2 below contains all window functions mentioned in this chapter and briefly compares
their selectivity and stop band attenuation
These two requirements are interrelated. Each function is a kind of compromise between the
two following requirements:
Figure 5: Main lobe, main lobe width, side lobes, and transition region
As can be seen in the table 2 above, the stopband attenuation of these windows is not
adjustable. It is only possible to affect the transition region by increasing the filter order. For
this reason it is preferable to start design process by specifying the appropriate window
function on the basis of the stopband attenuation. It is most preferable to specify a window
with the least stopband attenuation that satisfies the given requirements. This enables the
designed filter to have the narrowest transition region
Procedure:
1. Click on the simulator tab SIMULATOR, it will open the workspace.
2. See the movie in experiment page by pressing help button to understand how the different
steps, as mentioned next, are to be executed..
3. User controls like filter selection, no. of samples in the ideal filter in time domain, cutoff
frequency, window length (filter order) and window type selection are given to compare the
effect of different windows for designing FIR filters.
4. In this experiment we have provided two types of filters Lowpass and Highpass filters. The
sampling frequency is set to 5000 Hz, you can vary cut-off frequency (fa) from 1000 Hz to 3000
Hz and window length (Filter order M) from 0 to N. You can choose following window
functions: Rectangular, Barlett, Hamming, Hanning, Blackman and Kaiser windows. If you
choose Kaiser window function there is a parameter called a through which you can change
shape of the Kaiser window.
Fig-1
1. Graph 1 plots the impulse response of the filter (Green) in time domain with a time shift
of M/2 and Window function (Red) . A slider is given along the x-axis by this you can
read sample by sample value for impulse response of the filter
Fig-2
Fig-3
3. Graph no. 3 showing the input signal before and after passing through the filter. In the
same graph you can observe the frequency response of the output signal just by
checking the checkbox provides
Fig-4
Fig-5
4. While changing the window function you can see the window function expressions on
the top right half of the experiment space. Similarly a text box is provide on the top right
hand side from this you can copy the final filter difference equation to design your filter
on hardware or software.
Fig-6
9. You can choose between sine or square signal as input signal and select input signal
frequency.
10. When experiment loads first time the default values are set in all user controls. Initially for
the Low pass filter fs = 5000 Hz, fa = 1000 Hz and M = 50. Rectangular window is being selected
by default. Square wave is the input by default and its frequency if 100 Hz.
11. Keeping filter cut-off frequency constant change the input signal frequency and note down
input signal and output signal peak to peak values. Which will give you table no. 1 for your
report generation.
12. Similarly, choose High pass filter and repeat the step 10 -12 to note down another set of
data which will give you Table no. 2.
13. Now change the window function and do the earlier steps which will give you number of
tables.
14. From each table of data you have input and output signal peak to peak values, compute the
gain in decibel and plot the gain Vs frequency plot which will give you the same plot what you
have with filter frequency response (Graph 2).
15. Use take snapshot button to take the screenshot of the experiment space.
16. Complete the Observation and discuss the results in report generation.
17. Then click Yes I have finished my Experiment button to submit your report.
Theory:
Infinite Impulse Response (IIR) Filter are digital filters with infinite impulse response. Unlike
FIR filters, they have the feedback (a recursive part of a filter) and are known as recursive
digital filters therefore. Block diagrams of FIR and IIR filters
For this reason IIR filters have much better frequency response than FIR filters of the same
order. Unlike FIR filters, their phase characteristic is not linear which can cause a problem to
the systems which need phase linearity. For this reason, it is not preferable to use IIR filters in
digital signal processing when the phase is of the essence.
Otherwise, when the linear phase characteristic is not important, the use of IIR filters is an
excellent solution.
There is one problem known as a potential instability that is typical of IIR filters only. FIR
filters do not have such a problem as they do not have the feedback. For this reason, it is always
necessary to check after the design process whether the resulting IIR filter is stable or not.
IIR filters can be designed using different methods. One of the most commonly used is via the
reference analog prototype filter. This method is the best for designing all standard types of
filters such as low-pass, high-pass, band-pass and band-stop filters.
Figure 10.1 illustrates the block diagram of this method.
Fig-10.1
FIR filters can have linear phase characteristic, which is not typical of IIR filters. When it is
necessary to have linear phase characteristic,
FIR filters are the only available solution. In other cases when linear phase characteristic is not
necessary, such as speech signal processing,
FIR filters are not good solution. IIR filters should be used instead. The resulting filter order is
considerably lower for the same frequency response.
The filter order determines the number of filter delay lines, i.e. number of input and output
samples that should be saved in order that the next output sample can be computed. For
instance, if the filter order is 10, it means that it is necessary to save 10 input samples plus 10
output samples preceeding the current sample. All these 21 samples will affect the next output
sample.
The IIR filter transfer function is a ratio of two polynomials of complex variable z-1. The
numerator defines location of zeros, whereas the denominator defines location of poles of the
resulting IIR filter transfer function.
Figure 10.2. illustrates input and output signals of the system with non-linear phase
characteristic.
Fig-10.2
The system introduces phase shift of 0 radians at frequency of ω and Π radians at three times
higher frequency. Input signal consists of nature frequency ω and harmonics with the same
amplitude at three times higher frequency. Figure on the left illustrates an input signal,
whereas Figure on the right illustrates an output signal. As seen, these two signals have
different waveforms. Neither the power of the signal nor amplitudes of particular harmonics
have been changed, but the phase of the second harmonic.
Assume that an input represents a speech signal where the phase is not important. In this case
such phase distortion would be negligible as the system satisfies the stated requirements.
Otherwise, if the phase is important, such a huge distortion mustn’t be allowed.
Infinite impulse response (IIR) filter design
The most commonly used IIR filter design method uses reference analog prototype filter. It is
the best method to use when designing standard filters such as low-pass, high-pass, bandpass
and band-stop filters
The filter design process starts with specifications and requirements of the desirable IIR filter.
A type of reference analog prototype filter to be used is specified according to the
specifications and after that everything is ready for analog prototype filter design.
The next step in the design process is scaling of the frequency range of analog prototype filter
into desirable frequency range. This is how an analog prototype filter is converted into an
analog filter.
After the analog filter is designed, it is time to go through the last step in the digital IIR filter
design process. It is conversion from analog to digital filter. The most popular and most
commonly used converting method is bilinear transformation method. The resulting filter,
obtained in this way, is always stable. However, instability of the resulting filter, when bilinear
transformation is used, may be caused only by the finite word-length side-effect.
Basic concepts and IIR filter specification
First of all, it is necessary to learn the basic concepts that will be used further in this book. You
should be aware that without being familiar with these concepts, it is not possible to
understand analyses and synthesis of digital filters.
Figure 10.3 illustrates a low-pass digital filter specification. The word specification refers to
the frequency response specification.
H(ω)
Procedure:
1. Click on the Simulator tab SIMULATOR, it will open the workspace.
2. See the movie in experiment page by pressing help button ? to understand how the
following steps are to be executed.
3. In this experiment we have provided two types of filters Lowpass and Highpass filter. The
sampling frequency is set to 700 Hz.
4. We have provided user controls on top left side of the simulation screen. Here you have an
option to change filter design technique like Butterworth, Chebyshiv 1 etc. You can change
filter passband and stopband frequency from the dropdown menu also the passband ripple
and stopband attenuation. You can choose between filter frequency response or pole zero plot.
fig-1
5. Graph 1 plots the filter frequency response and pole zero plot. Two radio buttons has been
provided to change the plot from frequency response to pole zero plot vice versa. For
frequency response plot you can choose between magnitude or phase response or both by
selecting the corresponding checkbox given below the plot. In pole zero plot mode you will get
the filter coefficients you can use these filter coefficients for filter designing in any other
software or programming.
fig-2
6. Here we have taken sum of two sinusoidal signals of different frequencies (60Hz & 190Hz)
as input so that user can easily understand the filter operation by choosing appropriate cutoff
frequencies.
7. Graph 2 and 3 plots input and output signal respectively and also the corresponding
frequency domain plots. Here we have given two radio buttons so that user can change from
time domain to frequency domain plot.