0% found this document useful (0 votes)
205 views68 pages

Lab Manual DSP (2020)

This document provides an overview of Code Composer Studio and the DSK6713 development kit. It describes Code Composer Studio as an integrated development environment for editing, building, debugging and managing DSP projects. It also outlines the key features of digital signal processors and compares programming in C versus assembly. Finally, it details the hardware and software installation process for the DSK6713 kit, which contains a 225MHz C6713 DSP, codec, memory and is programmed using Code Composer Studio via a USB connection.

Uploaded by

Ch Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views68 pages

Lab Manual DSP (2020)

This document provides an overview of Code Composer Studio and the DSK6713 development kit. It describes Code Composer Studio as an integrated development environment for editing, building, debugging and managing DSP projects. It also outlines the key features of digital signal processors and compares programming in C versus assembly. Finally, it details the hardware and software installation process for the DSK6713 kit, which contains a 225MHz C6713 DSP, codec, memory and is programmed using Code Composer Studio via a USB connection.

Uploaded by

Ch Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

LABORATORY MANUAL

EE-330
Digital Signal
Processing
DEE

College of Electrical and Mechanical Engineering


National University of Sciences and Technology
LAB 1: Installation and Introduction to Code
Composer Studio 7.4
Introduction
Aim of this lab is to get the basic knowledge and understanding of Code Composer Studio 7.4.
Introduction to Code Composer Studio:
Code Composer is the DSP industry's first fully integrated development environment (IDE) with DSP-
specific functionality. With a familiar environment liked MS-based C++TM, Code Composer lets you
edit, build, debug, profile and manage projects from a single unified environment. Other unique
features include graphical signal analysis, injection/extraction of data signals via file I/O, multi-processor
debugging, automated testing and customization via a C-interpretive scripting language and much more.

Code Composer Features Include:

• IDE
• Debug IDE
• Advanced watch windows
• Integrated editor
• File I/O, Probe Points, and graphical algorithm scope probes
• Advanced graphical signal analysis
• Interactive profiling
• Automated testing and customization via scripting
• Visual project management system
• Compile in the background while editing and debugging
• Multi-processor debugging
• Help on the target DSP

Useful Types of Files:


You will be working with a number of files with different extensions. They include:
• file.pjt: to create and build a project named file.
• file.c: C source program.
• file.asm: assembly source program created by the user, by the C compiler,or by the linear
optimizer.
• file.sa: linear assembly source program. The linear optimizer uses file.saas input to produce an
assembly program file.asm.
• file.h: header support file.
• file.lib: library file, such as the run-time support library file rts6701.lib.
• file.cmd: linker command file that maps sections to memory.
• file.obj: object file created by the assembler.
• file.out: executable file created by the linker to be loaded and run on the processor.

Installation Procedure and Creating First Project:

The complete installation video guide can be viewed by clicking this link. A step-by-step procedure for
creating the first project in CCS 7.4 is described below:
1. Run CCS 7.4 with administrator.
2. Create new CCS project by clicking ‘CCS Project’ from ‘File → New → CCS Project

3. Type ‘6713’ in the target field and select ‘DSK6713’ from the drop-down menu on the right.
4. Select ‘Spectrum digital DSK-EVM-eZdsp onboard USB emulator’ from the connections drop-down
menu.

5. Type project name in the ‘Project name’ field, e.g. Test Project.
6. Make sure that the compiler version ‘TI v7.4.4’ is selected.
7. Click on ‘Hello World’ project template, and click finish. This will create a new project as shown in
the following screenshots.
8. Connect DSK6713 kit via USB connection and right click on the project name and click ‘Rebuild All’.
At this stage, CCS will show errors and/warnings of your code, if any. You need to correct the errors
(at-least) at this stage to continue running your code.
9. Now click ‘Debug Test Project’ icon, as shown in the figure below. USB connection of the kit is made
verified at this stage. If you get some error about connection, give a retry or reconnect your device.
10. Once the debug process is finished, press F8 or click ‘resume’ button from the menu. You will see the
output in the console at the bottom of the CCS screen.
LAB 2: Introduction to Code Composer
Studio and DSKC6713 Development Kit
Introduction
Aim of this lab is to get the basic knowledge and understanding of DSP Processors and the bases
of working on Code Composer Studio

Theory:

A digital signal processor (DSP) is an integrated circuit designed for high-speed data
manipulations, and is used in audio, communications, image manipulation, and other data- acquisition
and data-control applications. The microprocessors used in personal computers are optimized for tasks
involving data movement and inequality testing. Typical applications requiring such capabilities are
word processing, database management, spread sheets, etc. When it comes to mathematical
computations the traditional microprocessor are deficient, particularly where real-time performance is
required. Digital signal processors are microprocessors optimized for basic mathematical calculations
such as additions and multiplications.

Fixed versus Floating Point:

Digital Signal Processing can be divided into two categories, fixed point and floating point which refer
to the format used to store and manipulate numbers within the devices. Fixed point DSPs usually
represent each number with a minimum of 16 bits, although a different length can be used. There are
four common ways that these 216 i.e., 65,536 possible bit patterns can represent a number. In
unsigned integer, the stored number can take on any integer value from 0 to 65,535, signed integer uses
two's complement to include negative numbers from -32,768 to 32,767. With unsigned fraction
notation, the 65,536 levels are spread uniformly between 0 and 1 and the signed fraction format allows
negative numbers, equally spaced between -1 and 1. The floating point DSPs typically use a minimum
of 32 bits to store each value. This results in many more bit patterns than for fixed point, 232 i.e.
4,294,967,296 to be exact. All floating point DSPs can also handle fixed point numbers, a necessity to
implement counters, loops, and signals coming from the ADC and going to the DAC. However, this
doesn't mean that fixed point math will be carried out as quickly as the floating point operations; it
depends on the internal architecture.

C versus Assembly:

DSPs are programmed in the same languages as other scientific and engineering
applications, usually assembly or C. Programs written in assembly can execute faster, while programs
written in C are easier to develop and maintain. In traditional applications, such as programs run on
PCs and mainframes, C is almost always the first choice. If assembly is used at all, it is restricted to
short subroutines that must run with the utmost speed.

How fast are DSPs?

The primary reason for using a DSP instead of a traditional microprocessor is speed: the ability
to move samples into the device and carry out the needed mathematical operations, and output the
processed data. The usual way of specifying the fastness of a DSP is: fixed point systems are often
quoted in MIPS (million integer operations per second). Likewise, floating point devices can be
specified in MFLOPS (million floating point operations per second).

The DSK is USB port interfaced platform that allows to efficiently develop and test applications
for the C6713. With extensive host PC and target DSP software support, the DSK provides
ease-of-use and capabilities that are attractive to DSP engineers. The 6713
DSP Starter Kit (DSK) is a low-cost platform which lets customers evaluate and develop
applications for the Texas Instruments C67X DSP family. The primary features of the DSK are:

• 225 MHz TMS320C6713 Floating Point DSP


• AIC23 Stereo Codec
• Four Position User DIP Switch and Four User LEDs
• On-board Flash and SDRAM

TI’s Code Composer Studio development tools are bundled with the 6713DSK providing the user with
an industrial-strength integrated development environment for C and assembly programming. Code
Composer Studio communicates with the DSP using an on-board JTAG emulator through a USB
interface. The TMS320C6713 DSP is the heart of the system. It is a core member of Texas
Instruments‟ C64X line of fixed point DSPs whose distinguishing features are an extremely
high performance 225MHz VLIW DSP core and 256Kbytes of internal memory. On-chip
peripherals include a 32-bit external memory interface (EMIF) with integrated SDRAM controller, 2
multi-channel buffered serial ports (McBSPs), two on- board timers and an enhanced DMA controller
(EDMA). The 6713 represents the high end of TI‟s C6700 floating point DSP line both in terms of
computational performance and on- chip resources.
The 6713 has a significant amount of internal memory so many applications will have all code and
data on-chip. External accesses are done through the EMIF which can connect to both synchronous
and asynchronous memories. The EMIF signals are also brought out to standard TI expansion bus
connectors so additional functionality can be added on daughter card modules. DSPs are frequently
used in audio processing applications so the DSK includes an on-board codec called the AIC23.
Codec stands for coder/decoder,the job of the AIC23 is to code analog input samples into a digital
format for the DSP to process, then decode data coming out of the DSP to generate the processed
analog output. Digitial data is sent to and from the codec on McBSP1.

DSK hardware installation:


• Shut down and power off the PC
• Connect the supplied USB port cable to the board
• Connect the other end of the cable to the USB port of PC
• Plug the other end of the power cable into a power outlet
• Plug the power cable into the board
• The user LEDs should flash several times to indicate board is operational
• When you connect your DSK through USB for the first time on a Windows loaded PC the new
hardware found wizard will come up. So, Install the drivers (The CCS CD contains the require
drivers for C6713 DSK).
• Install the CCS software for C6713 DSK

Troubleshooting DSK Connectivity:


If Code Composer Studio IDE fails to configure your port correctly, perform the following steps:
Test the USB port by running DSK Port test from the start menu

Use Start Programs Texas Instruments Code Composer Studio Code Composer Studio C6713 DSK
Tools C6713 DSK Diagnostic Utilities
Select 6713 DSK Diagnostic Utility Icon from Desktop

Select Start Option


Utility Program will test the board
After testing Diagnostic Status you will get PASS

Software Installation
You must install the hardware before you install the software on your system. The
requirements for the operating platform are;
Insert the installation CD into the CD-ROM drive

An install screen appears; if not, goes to the windows Explorer and run setup.exe
Choose the option to install Code Composer Studio

If you already have C6000 CC Studio IDE installed on your PC,do not install DSK software. CC
Studio IDE full tools supports the DSK platform Respond to the dialog boxes as the installation
program runs The Installation program automatically configures CC Studio IDE for operation with
your DSK and creates a CCStudio IDE DSK icon on your desktop. To install, follow these
instruction.
Introduction to Code Composer Studio:
Code Composer is the DSP industry's first fully integrated development environment (IDE) with DSP-
specific functionality. With a familiar environment liked MS-based C++TM, Code Composer lets you
edit, build, debug, profile and manage projects from a single unified environment. Other unique
features include graphical signal analysis, injection/extraction of data signals via file I/O, multi-processor
debugging, automated testing and customization via a C-interpretive scripting language and much more.

Code Composer Features Include:

• IDE
• Debug IDE
• Advanced watch windows
• Integrated editor
• File I/O, Probe Points, and graphical algorithm scope probes
• Advanced graphical signal analysis
• Interactive profiling
• Automated testing and customization via scripting
• Visual project management system
• Compile in the background while editing and debugging
• Multi-processor debugging
• Help on the target DSP

Useful Types of Files:


You will be working with a number of files with different extensions. They include:
• file.pjt: to create and build a project named file.
• file.c: C source program.
• file.asm: assembly source program created by the user, by the C compiler,or by the linear
optimizer.
• file.sa: linear assembly source program. The linear optimizer uses file.saas input to produce an
assembly program file.asm.
• file.h: header support file.
• file.lib: library file, such as the run-time support library file rts6701.lib.
• file.cmd: linker command file that maps sections to memory.
• file.obj: object file created by the assembler.
• file.out: executable file created by the linker to be loaded and run on the processor.

Introduction to the TMS320C6713 DSP Device

5
The Texas Instruments TMS320C6000 DSP platform of high-performance digital signal processors
(DSPs) now includes the TMS320C6713. The C6713 brings the highest level of performance in the
C6000 DSP platform of floating-point DSPs. At the initial clock rate of 225 MHz, the C6713 can process
information at a rate of 1.35 giga-floating-point operations per second (GFLOPS). Introduced in
February 1997, the C6000 DSP platform is based on TI’s VelociTI architecture, an advanced very-
long-instruction-word (VLIW) architecture for DSPs. Advanced features of the VelociTI architecture
include instruction packing, conditional branching, and pre- fetched branching, all of which overcome
problems that were associated with previous VLIW implementations. The architecture is highly
deterministic, with few restrictions on how or when instructions are fetched, executed, or stored. This
architectural flexibility is key to the breakthrough efficiency levels of the C6000 compiler.

TMS320C6000 Compatibility:

All C6000 DSP platform devices are code-compatible with one another, with the exception that there are
some floating-point instructions that are only valid on the floating-point (TMS320C67x) members. The
C67x DSP core employs the VelociTI architecture that is designed to achieve high performance through
increased instruction-level parallelism. VelociTI provides eight execution units, including two multipliers
and six arithmetic logic units (ALUs). Out of eight functional units, six (L1, L2, S1, S2, M1, and M2) can
perform six floating-point operations every cycle. These functional units operate in parallel and can
perform up to eight instructions, including six floating-point operations during a single clock cycle—up
to 1800 million instructions per second (MIPS), or 1.35 GFLOPS at 225 MHz initial device clock speed.
The common architecture allows designers to begin development with existing C6000 software tools for
those devices currently in development. This also allows for migration from one C6000 processor to
another, as design specifications require.
In addition to the DSP core, many of the on-chip peripherals are common between C6000 devices. Figure
2 shows a block diagram of the C6713 device. The blocks in white are common among members of C6700
generation of floating-point DSPs. The blocks in gray are new peripheral/improved features available on
the C6713 device.

6
C6713 DSP Processor
• 32-bit Processor
• Floating Point Processor
• clock rate is 225MHz
• 1800 MIPS
• 1350 MFLOPS

AIC23 CODEC:

The DSK uses a Texas Instruments AIC23 (part #TLV320AIC23) stereo codec for input and output of
audio signals. The codec samples analog signals on the microphone or line inputs and converts them into
digital data so it can be processed by the DSP. When the DSP is finished with the data it uses the codec to
convert the samples back into analog signals on the line and headphone outputs so the user can hear the
output. The codec communicates using two serial channels, one to control the codec’s internal
configuration registers and one to send and receive digital audio samples. McBSP0 is used as the
unidirectional control channel. It should be programmed to send a 16-bit control word to the AIC23 in SPI
format. The top 7 bits of the control word should specify the register to be modified and the lower 9 should
contain the register value. The control channel is only used when configuring the codec, it is generally idle
when audio data is being transmitted, McBSP1 is used as the bi-directional data channel. All audio data
flows through the data channel. Many data formats are supported based on the three variables of sample
width, clock signal source and serial data format. The DSK examples generally use a 16-bit sample width
with the codec in master mode so it generates the frame sync and bit clocks at the correct sample rate

7
without effort on the DSP side. The preferred serial format is DSP mode which is designed specifically to
operate with the McBSP ports on TI DSPs.

The codec has a 12MHz system clock. The 12MHz system clock corresponds to USB sample rate mode,
named because many USB systems use a 12MHz clock and can use the same clock for both the codec and
USB controller. The internal sample rate generate subdivides the 12MHz clock to generate common
frequencies such as 48KHz, 44.1KHz and 8KHz. The sample rate is set by the codec’s SAMPLERATE
register. The figure below shows the codec interface on the C6713 DSK.

DipSwitch / LEDs:
Four user dip switches on the DSK board can read from a program and provides the user with a feedback
control interface. Four LEDs can be activated/deactivated from the program.

Daughter Card Interface:

The DSK provides three expansion connectors that can be used to accept plug-in daughter cards. The
daughter card allows users to build on their DSK platform to extend its capabilities and provide customer
and application specific I/O. The expansion connectors are for memory, peripherals, and the Host Port
Interface (HPI).
The memory connector provides access to the DSP’s asynchronous EMIF signals to interface with
memories and memory mapped devices. It supports byte addressing on 32 bit boundries. The peripheral
connector brings out the DSP’s peripheral signals like McBSPs, timers, and clocks. Both connectors
provide power and ground to the daughter card The HPI is a high speed interface that can be used to allow
multiple DSPs to communicate and cooperate on a given task. The HPI connector brings out the HPI

8
specific control signals. Most of the expansion connector signals are buffered so that the daughter card
cannot directly influence the operation of the DSK board. The use of TI low voltage, 5V tolerant buffers,
and CBT interface devices allows the use of either +5V or
+3.3V devices to be used on the daughter card.
Other than the buffering, most daughter card signals are not modified on the board. However, a few
daughter card specific control signals like DC_RESET and DC_DET exist and are accessible through the
CPLD DC_REG register. The DSK also multiplexes the McBSP0 and McBSP1 of on-board or external
use. This function is controlled through the CPLD MISC register.

9
LAB 3: Switching LEDs and Working of
Codec on DSK6713
Introduction
Aim of this lab is to perform LED switching and understand working of Codec.
Setting up CCS 7.4 for DSK6713:
Before the start of lab task, it is mandatory to follow the instructions below in order to setup CCS 7.4 for
DSK6713 development kit.

1. Download DSK6713 source files from this link, extract and navigate to the ‘include’ folder inside
‘c6000’ folder. Copy all the header files from this ‘include’ folder and paste in the following
directory: C:\ti\ccsv7\tools\compiler\ti-cgt-c6000_7.4.4\include
2. From the same source files (downloaded in step 1), now navigate to ‘lib’ folder inside ‘c6000’
folder. Copy the library file named ‘dsk6713bsl.lib’ and paste in the following directory:
C:\ti\ccsv7\tools\compiler\ti-cgt-c6000_7.4.4\lib
3. Download Chip Support Library (CSL) installation setup from here, and install on the same drive
(i.e. C drive) where CCS has already been installed. It is recommended to install it at: C:\C6xCSL
directory.
4. Make a new CCS project as you did in Lab 1. Right click on the project name in left-pane and
click on ‘properties’. Go to ‘general’ from left-pane and set ‘Linker command file’ to
‘C6713.cmd’, and ‘Runtime support library’ to ‘dsk6713bsl.lib’. Click OK.
5. Add CSL headers to the project Project Properties → CCS Build → C6000 compiler → Include
options Click Add and select path "C:\C6xCSL\include".
6. Add "CHIP_6713" to Predefined names. Project Properties → CCS Build → C6000 compiler →
Predefined Symbols.
7. Add CSL to project: Project Properties → CCS Build → C6000 linker → File search path →
Include library file or ... Click Add and select path "C:\C6xCSL\lib_3x\csl6713.lib"
Procedure:

In order to control LED switching and Audio playback, run the following script in your created project.
The initial values are used for the configuration of CODEC. After running this code, the LED #1 will be
switched off. Attached earphones to both the audio IN and audio OUT ports. The CODEC will playback
the audio on the output port.

Lab tasks:

• Observe the audio playback and comment on the result.


• Using the built-in function ‘DSK6713_waitusec(delay in micro seconds in max 32 bit decimal
value)’, modify the LED part of the code so that the LED which was initially ON, is turned off after
exactly 2 seconds.
LAB 4: Convolution Sum Implementation on
DSK6713
Introduction
Aim of this lab is to perform convolution sum using C++ and implement on DSK6713.
Setting up CCS 7.4 for DSK6713:
Before the start of lab task, it is mandatory to follow the instructions below in order to setup CCS 7.4 for
DSK6713 development kit.

1. Add CSL headers to the project Project Properties → CCS Build → C6000 compiler → Include
options Click Add and select path "C:\C6xCSL\include".
2. Add "CHIP_6713" to Predefined names. Project Properties → CCS Build → C6000 compiler →
Predefined Symbols.
3. Add CSL to project: Project Properties → CCS Build → C6000 linker → File search path →
Include library file or ... Click Add and select path "C:\C6xCSL\lib_3x\csl6713.lib"
Background:

In signal processing, convolution sum is used to compute the output of an FIR filter by performing flip-
drag-sum operation. For an input 𝑥[𝑛] and impulse response ℎ[𝑛], the output 𝑦[𝑛] is given by the following
discrete-time equation:

The flip and drag operation can be performed on any of the input sequences. If the length of input is 𝑁,
and the length of impulse response is 𝑀, then the length of output will be 𝑁 + 𝑀 − 1.

Procedure:

Using the template code structure provided below, you have to write the code for the computation of
convolution sum of two sequences and observe the output in the workspace of CCS. Both the input
sequences are given in the template. You have to only write the code for computation of convolution sum.
Don’t change anything in the template.

Lab tasks:

• Perform convolution of the same sequences using built-in MATLAB function ‘conv()’, and verify
the output of your code.
LAB 5: Speech Processing using Lowpass
Filter on DSK6713
Introduction
Aim of this lab is to perform speech processing using lowpass filtering on DSK6713.
Setting up CCS 7.4 for DSK6713:
Before the start of lab task, it is mandatory to follow the instructions below in order to setup CCS 7.4 for
DSK6713 development kit.

1. Download DSK6713 source files from this link, extract and navigate to the ‘include’ folder inside
‘c6000’ folder. Copy all the header files from this ‘include’ folder and paste in the following
directory: C:\ti\ccsv7\tools\compiler\ti-cgt-c6000_7.4.4\include
2. From the same source files (downloaded in step 1), now navigate to ‘lib’ folder inside ‘c6000’
folder. Copy the library file named ‘dsk6713bsl.lib’ and paste in the following directory:
C:\ti\ccsv7\tools\compiler\ti-cgt-c6000_7.4.4\lib
3. Download Chip Support Library (CSL) installation setup from here, and install on the same drive
(i.e. C drive) where CCS has already been installed. It is recommended to install it at: C:\C6xCSL
directory.
4. Make a new CCS project as you did in Lab 1. Right click on the project name in left-pane and
click on ‘properties’. Go to ‘general’ from left-pane and set ‘Linker command file’ to
‘C6713.cmd’, and ‘Runtime support library’ to ‘dsk6713bsl.lib’. Click OK.
5. Add CSL headers to the project Project Properties → CCS Build → C6000 compiler → Include
options Click Add and select path "C:\C6xCSL\include".
6. Add "CHIP_6713" to Predefined names. Project Properties → CCS Build → C6000 compiler →
Predefined Symbols.
7. Add CSL to project: Project Properties → CCS Build → C6000 linker → File search path →
Include library file or ... Click Add and select path "C:\C6xCSL\lib_3x\csl6713.lib"
Procedure:

In this lab, you will filter the unwanted noise from the speech by using a lowpass filter. The filter will be
designed by using MATLAB’s filter design toolbox. Follow the instructions below:

1. To open this toolbox, type ‘filterDesigner’ in the command window of MATLAB. This will open
the filter design toolbox. Configure all the options as shown in the figure below.
2. To export the filter coefficients, go to file and then export. A dialogue box will be opened. With all
the other default options, change the name of Numerator to ‘h’ and click ‘Export’.
3. A new variable named ‘h’ will be created in the MATLAB workspace. Copy the contents of this
variable and paste in notepad. Insert commas between all adjacent values so that they can be copied
in the CCS code.
4. Use the template given below to copy the coefficients and perform filtering by using your own
convolution sum code written and tested in the previous lab. Here ‘FILTER_LEN’ is the filter
length to be defined in code using #define.
In the last while statement, write ‘msg’ if you want to hear the unfiltered voice, and replace it with ‘y’ to
hear the filtered voice. Follow all the previous instructions to build and run your program.

Lab tasks:

• Playback the unfiltered and filtered voices to note any difference and comment on the result.
LAB 6: FREQUENCY ANALYSIS
Introduction

If x(n) is absolutely summable, that is then its discrete-time Fourier


transform is given by

The inverse discrete-time Fourier transform (IDTFT) of X(ejw) is given by

1. Fourier anaylsis of discrete systems described by difference equation


Provide the plots for the following along with the title of each by matching its response to
Low pass, high pass, band pass or band stop filter. Also include in the title whether the
system is FIR or IIR. The frequency response can be obtained using freqz(num,den).
Poles and zero plot is obtained using zplane(num,den). Comment on the poles and zeros
location of each filter.

a. Y[n] = 0.08x[n] + 0.34x[n-1] + 0.34x[n-2] + .34x[n-3] + 0.08x[n]

b = [0.08 0.34 0.34 0.34 0.08];


subplot(2,1,1), freqz(b,1)
subplot(2,1,2), zplane(b,1)

b. Y[n] – 1.11y[n-1] + 0.57 y[n-2] = x[n] + 2x[n-1] + x[n-2]

b = [ 1 2 1];
a = [1 -1.11 0.57 ];
figure
subplot(2,1,1), freqz(b,a)
subplot(2,1,2), zplane(b,a)
c. Y[n] = -0.21x[n] -0.17x[n-1] + 0.81x[n-2] -0.17x[n-3] -0.21x[n-4]

b = [-0.21 -0.17 0.81 -0.17 -0.21];


figure
subplot(2,1,1), freqz(b,1)
subplot(2,1,2), zplane(b,1)

d. Y[n] – 1.11y[n-1] + 0.57y[n-2] = x[n] – 2x[n-1] + x[n-2]

b = [1 -2 1];
a = [ 1 -1.11 0.57];
figure
subplot(2,1,1), freqz(b,a)
subplot(2,1,2), zplane(b,a)

e. Y[n] = -0.35x[n] + 0.2x[n-1] -0.07x[n-2] + 0.20x[n-3] – 0.35x[n-4]

b = [-0.35 0.20 -0.07 0.20 -0.35];


figure
subplot(2,1,1), freqz(b,1)
subplot(2,1,2), zplane(b,1)

f. 2y[n] + 1.63y[n-1] + 0.65y[n-2] = x[n] – x[n-2]

b = [ 1 0 -1];
a = [2 1.63 0.65];
figure
subplot(2,1,1), freqz(b,a)
subplot(2,1,2), zplane(b,a)

2. Properties of DTFT
In this part fft(x,n) function will be used to prove some of the Fourier transform
properties.

a. Linearity
The discrete-time Fourier transform is a linear transformation; that is,
For every α, β, x1(n), and x2(n)

close all, clear all

n = -2*pi:.01:2*pi;
size(n);
x1=sin(10*2*pi*n);
x2=sin(20*2*pi*n);

y1 = 10*x1;
y2 = 5*x2;

Y1 = abs(fft(y1));
Y2 = abs(fft(y2));
subplot(3,1,1) , plot(Y1+Y2)
title('F[aX1 + bX2]');
X1 = abs(fft(x1));
X2 = abs(fft(x2));

X1 = 10*X1;
X2 = 5*X2;
subplot(3,1,2) , plot(X1+X2)
title('aF[X1] + bF[X2]');

diff = X1 + X2 - (Y1 + Y2);


subplot(3,1,3) , plot(diff)
title('aF[X1] + bF[X2] - F[aX1 + bX2]');

b. Time shifting
A shift in the time domain corresponds to the phase shifting

close all, clear all


x = rand(1,11);
n = 0:10;

k = 0:500;
w = (pi/500)*k;
X = x*(exp(-j*pi/500)).^(n'*k);
% X signal shifted by two samples
y =x;
m = n+20;
Y = y*(exp(-j*pi/500)).^(m'*k);
% X verification
Y_check = (exp(-j*20).^w).*X;
subplot(2,1,1), plot(abs(X))
subplot(2,1,2), plot(abs(Y))

error = max(abs(Y-Y_check))

c. Frequency shifting
Multiplication by a complex exponential corresponds to a shift in the frequency
domain

close all, clear all

n=0:100;
x = cos(pi*n/2);
k = -100:100;
w = (pi/100)*k;
X = x*(exp(-j*pi/100)).^(n'*k);

y = exp(j*pi*n/4).*x;
Y = y*(exp(-j*pi/100)).^(n'*k);
subplot(2,2,1) ; plot(w/pi, abs(X)); grid; axis( [-1, 1,0,60])
xlabel('frequencg in pi units'); ylabel('|X1|')
title('Hagnitude of X')
subplot (2,2,2) ; plot (w/pi ,angle(X)/pi); grid; axis([-1, 1, -1, 1])
xlabel('frequency in pi units'); ylabel('radiauds/pi')
title('hgle of X')
subplot (2,2,3) ; plot (w/pi, abs (Y)) ; grid; axis( [-1,1,0,60])
xlabel('frequency in pi units'); ylabel('|Y|')
title('Magnitude of Y')
subplot (2,2,4) ; plot (w/pi,angle(Y)/pi) ; grid; axis( [-1 1 -1 1])
xlabel('frequency in pi units'); ylabel('radians/pi')
title('Angle of Y')

Exercise 1: Write a program which proves the convolution property described by

Excercise2: Write a program which proves the multiplication property


LAB 7: Z-TRANSFORM
Introduction

Just as the Fourier transform forms the basis of signal analysis, the z-transform forms the
basis of system analysis. If x[n] is a discrete signal, its z-transform X(z) is given by:

The z-transform maps a signal in the time domain to a power series in the complex
(frequency) domain: x[n] → X(z).
There are many advantages to working with z-transformed signals:
• linearity and superposition are preserved
• x[n − k] → z−kX(z)
• x[−n] → X(1/z)
• anx[n] → X(z/a)
• x[n] ∗ y[n] → X(z)Y (z)
The overall result is that the algebra of system analysis becomes greatly simplified in the z
domain. The only tradeoff is the necessity of taking an inverse transform to obtain time
domain responses.
Since the response y[n] of an LTI system to input x[n] is given by the convolution x[n] ∗h[n],
where h[n] is the impulse reponse, we have

The ratio H(z) = Y (z)/X(z) defines the impulse response (and so the system response), and is
called the transfer function of the system.

1. Convolution property
a. If X1(Z) = 2 + 3z-1 + 4z-2 and X2(z) = 3 + 4z-1 + 5z-2 + 6z-3. Determine X3(z) = X1(z)X2(z).

clear all, close all


x1 = [2,3,4]; x2 = [3 4 5 6];
x3 = conv(x1,x2)
x3 =
6 17 34 43 38 24
Hence,
X3(z) = 6 + 17z-1 + 34z-2 + 43z-3 + 38z-4 + 24z-5

b. If we are given X3(z) and want to compute X2(z) then we need to divide X3(z) by X1(Z).
In MATLAB this can be done using deconv function.

x3 = [ 6 17 34 43 38 24];
x1 = [2,3,4];

x2 = deconv(x3,x1)

x2 =

3 4 5 6
Hence,

X2(z) = 3 + 4z-1 + 5z-2 + 6z-3

Exercise 3: If X1(z) = 2 + 3z-1 + z-2 and X2(z) = 3 + 5z-1 + 4z-2 + 2z-2. Determine X3(z) =
X1(z)X2(z).

2. Zero and pole analysis in MATLAB


If the numerator and denominator of the proper form of a transfer function are
factored, the zeros and poles become apparent:

The location of the zeros and poles of the transfer function determines the response of
an LTI system.

Example: H(z) = (z2 + 2z) / ( z2 + 0.2)


Zeros : z=0, -2
Poles: z=±√0.2 j
Gain: k =1
b = [1 2];
a = [1 0 0.2];
[z,p,k] = tf2zpk(b,a)
zplane(z,p)

3. Zero-Pole placement in Filter Design


LTI systems, particularly digital filters, are often designed by positioning zeros and
poles in the z-plane. The strategy is to identify passband and stopband frequencies
on the unit circle and then position zeros and poles by cosidering the following.

i. Conjugate symmetry
All poles and zeros must be paired with their complex conjugates

ii. Causality
To ensure that the system does not depened on future values, the number of
zeros must be less than or equal to the number of poles

iii. Origin
Poles and zeros at the origin do not affect the magnitude response

iv. Stability
For a stable system, poles must be inside the unit circle. Pole radius is
proportional to the gain and inversely proportional to the bandwidth.
Passbands should contian poles near the unit circle for larger gain.

v. Minimum phase
Zeros can be placed anywhere in the z-plane. Zeros inside the uint circle
ensure minimum phase. Zeros on the unit circle give a null response.
Stopbands should contain zeros on or near the unit circle.

vi. Tranistin band


A steep transition from passband to stopband can be achieved when stopband
zeros are paired with poles along (or near) the same radial line and close to
the unit circle.

vii. Zero-pole interaction


Zeros and poles interact to produce a composite response that might not
match design goals. Poles closer to the unit circle or farther from one another
produce smaller interactions. Zeros and poles might have to be repositioned
or added, leading t a higher filter order.

Zero-Pole-Gain Editing in SPTool

Run:
Sptool

To acces the Pole/Zero Editor in SPTool, do the following:


1. Click the New button under the Filters list in SPTool.
2. Select the Pole/Zero Editor in the Algorithm list.
3. View system characteristics as you position poles and zeros.

Exercise 4: Provide examples of all seven properties mentioned above by placing


zero/poles as outlined.

4. Rational Z-transform to partial fraction form


When taking inverse z-transform it is most convenient that the transfer function be
in partial fraction form. To convert from rational z-transform to partial fraction form
MATLAB residuez function can be used.

Example:

clear all, close all

b = [ 0 1 0];
a = [3 -4 1];

[z,p,k] = residuez(b,a)

z=
0.5000
-0.5000

p=

1.0000
0.3333

k=

Z Æ Zeros
PÆ Poles
K Æ Gain

Hence,

5. Partial fraction form to z-transform


MATLAB residuez function can also be used to convert from partial fraction
expression into rational z-transform.

Example

z = [ .5 -.5];
p = [1 0.3333];
k = [];

[b,a] = residuez(z,p,k)
b=

0 0.3334

a=

1.0000 -1.3333 0.3333

Exercise 5: Determine the inverse z-transform of


LAB 8: The Sampling Theorem

Objectives
• To understand the issues involved in sampling of a continuous-time signal
• To study the effects of different sampling rates

Sampling Issues
A signal can be classified as either continuous or discrete based on its independent variable (time).
Continuous-time signals are defined along a continuum of time whereas discrete-time signals are
defined at discrete times and represented as sequences of numbers. Continuous-time signals are
often referred to as analog signals. Speech signals or the scan of the electrical activity of the brain
(electroencephalogram or EEG) are examples of such signals.

In addition, the signal amplitude may also be either continuous or discrete. If both time and
amplitude of the signal are discrete, it is called a digital signal. Examples of which are computer
data and telegraph signals.

For processing, transmitting, and storing of continuous-time signals, a huge size of data should be
manipulated which is not efficient and applicable in most of signal processing applications.
Furthermore, numerous digital signal processors, software, tools, and etc. exist todays which can
only be employed for manipulating digital signals. For these reasons it is essential to sample
continuous-time signals. Sampling process may result in information loss. Sampling theorem is a
theorem that expresses the process of sampling operation through which information loss will not
happen.
Periodic Sampling
One of the most popular sampling methods is periodic sampling. In the periodic sampling
operation, exact values of the continuous-time signal at uniformly spaced discrete intervals (𝑛𝑛𝑇𝑇𝑠𝑠 ),
where 𝑇𝑇𝑠𝑠 denotes the sampling period) are retained. A sampled signal can be generated by using a
method referred to as sampling by modulation as shown in Figure 3-1, where 𝑥𝑥𝑐𝑐 (𝑡𝑡) is the continues-
time signal and 𝑠𝑠(𝑡𝑡) is an impulse train (the carrier) defined as

Equation 3-1:

𝑠𝑠(𝑡𝑡) = � 𝛿𝛿(𝑡𝑡 − 𝑛𝑛𝑇𝑇𝑠𝑠 )


𝑛𝑛=−∞

Figure 3-1: Sampling by modulation

The sampled signal 𝑥𝑥𝑠𝑠 (𝑡𝑡) (where the subscript “s” refers to sampled) can now be represented as

Equation 3-2: 𝑥𝑥𝑠𝑠 (𝑡𝑡) = 𝑥𝑥𝑐𝑐 (𝑡𝑡)𝑠𝑠(𝑡𝑡)


Note that 𝑥𝑥𝑠𝑠 (𝑡𝑡) is still a continuous-time signal. In order to understand the effect of sampling of a
continuous-time signal, it will be more straightforward to analyse it in frequency domain. The
frequency domain representation of Equation 3-2 can be obtained as follow [2]:

Equation 3-3:
+∞
1
𝑋𝑋𝑠𝑠 (𝑗𝑗Ω) = � 𝑋𝑋𝑐𝑐 �𝑗𝑗(Ω − 𝑘𝑘Ω𝑠𝑠 )�
𝑇𝑇𝑠𝑠
𝑘𝑘=−∞

2𝜋𝜋
where 𝛺𝛺𝑠𝑠 = is the sampling frequency. It means that sampling of a continuous-time signal with
𝑇𝑇𝑠𝑠

sampling period of 𝑇𝑇𝑠𝑠 will cause the frequency spectrum to be periodic with 𝛺𝛺𝑠𝑠 (sampling
frequency). It can also be shown [1] that

Equation 3-4:
𝜔𝜔
𝑋𝑋�𝑒𝑒 𝑗𝑗𝑗𝑗 � = 𝑋𝑋𝑠𝑠 (𝑗𝑗Ω), Ω=
𝑇𝑇𝑠𝑠

Now with Equation 3-3 and Equation 3-4 we obtain

Equation 3-5:
+∞
𝑗𝑗𝑗𝑗
1 𝜔𝜔 2𝜋𝜋𝜋𝜋
𝑋𝑋�𝑒𝑒 �= � 𝑋𝑋𝑐𝑐 �𝑗𝑗 � − ��
𝑇𝑇𝑠𝑠 𝑇𝑇𝑠𝑠 𝑇𝑇𝑠𝑠
𝑘𝑘=−∞

Note that in the above equations we used 𝑡𝑡, and Ω for continues-time signals and also 𝑛𝑛, and 𝜔𝜔 for
the discrete ones. If 𝑥𝑥𝑐𝑐 (𝑡𝑡) is an example of a band-limited signal, then according to the above
equations 𝑋𝑋𝑐𝑐 (𝑗𝑗Ω), 𝑋𝑋𝑠𝑠 (𝑗𝑗Ω), and X�𝑒𝑒 𝑗𝑗𝑗𝑗 � can be depicted as shown in Figure 3-2. According to
1
Equation 3-3, and Equation 3-5, the magnitude of the frequency spectrum has been scaled by .
𝑇𝑇𝑠𝑠
Figure 3-2: Frequency representation of periodic sampling

Nyquist Theorem
The example signal shown in Figure 3-2 (the first) is a band-limited signal. It means that

Equation 3-6:

𝑋𝑋𝑐𝑐 (𝑗𝑗Ω) = 0, |Ω| ≥ Ω𝑁𝑁

As mentioned above, sampling of a continuous-time signal with sampling period of 𝑇𝑇𝑠𝑠 will cause
the frequency spectrum to be periodic with 𝛺𝛺𝑠𝑠 (sampling frequency). This can be seen in Figure 3-2
(the middle). As the sampling period 𝑇𝑇𝑠𝑠 is decreased (𝛺𝛺𝑠𝑠 increases), all replicas of 𝑋𝑋𝑐𝑐 (𝑗𝑗𝑗𝑗) move
farther apart. On the other hand, if 𝑇𝑇𝑠𝑠 increases (𝛺𝛺𝑠𝑠 decreases), the replicas of 𝑋𝑋𝑐𝑐 (𝑗𝑗𝑗𝑗) move closer
together. As 𝑇𝑇𝑠𝑠 is continually increased, a point will be reached where the replicas will begin to
overlap as shown in Figure 3-3. This overlap of the frequency spectrum is known as aliasing or
folding. According to Figure 3-2 (the middle) the maximum sampling period (equivalent to
minimum sampling frequency) at which there is no aliasing (overlapping) is attained when
Equation 3-7:

𝛺𝛺𝑠𝑠 − 𝛺𝛺𝑁𝑁 > 𝛺𝛺𝑁𝑁 → 𝛺𝛺𝑠𝑠 > 2𝛺𝛺𝑁𝑁


It should be noted that for above equation to be valid, the continuous-time signal should be band-
limited. This theorem is very popular and is called Nyquist theorem.

Nyquist Theorem: If 𝑥𝑥𝑐𝑐 (𝑡𝑡) is a band-limited continuous-time signal and sampling


frequency (𝛺𝛺𝑠𝑠 ) is greater than two times of maximum frequency component of the signal
(𝛺𝛺𝑁𝑁 ) (Equation 3-7), then 𝑥𝑥𝑐𝑐 (𝑡𝑡) can be reconstructed completely from its discrete time
sequence (𝑥𝑥[𝑛𝑛]).

In order to avoid information loss due to aliasing the Nyquist theorem should be satisfied. In the
Nyquist theorem, 𝛺𝛺𝑁𝑁 is called Nyquist frequency and 𝛺𝛺𝑠𝑠 = 2𝛺𝛺𝑁𝑁 is called Nyquist rate.

Figure 3-3: As 𝜴𝜴𝒔𝒔 is continually decreased the replicas will begin to overlap. This overlap of the frequency
spectrum is known as aliasing

Reconstruction
If Nyquist theorem is satisfied while sampling a band-limited continuous-time signal, the aliasing
will not be happened and therefore, that signal can be reconstructed completely using its samples.
As mentioned before, sampling a continuous-time signal will cause its frequency spectrum to be
periodic. For reconstruction purpose, the frequency components related to the band-limited
continuous-time signal should be retained while frequencies related to its replicas should be
removed. Therefore, as shown in Figure 3-4, the reconstruction process can be done by applying
an ideal low pass filter with gain 𝑇𝑇𝑠𝑠 and cut-off frequency Ω𝑐𝑐 .

1
Due to sampling operation as the magnitude of frequency spectrum is scaled by , the gain value
𝑇𝑇𝑠𝑠

of the applied ideal low pass filter is considered to be 𝑇𝑇𝑠𝑠 to rescale the spectrum to its original
value.
Figure 3-4: Reconstruction of band-limited signal by applying an ideal low pass filter

The cut-off frequency Ω𝑐𝑐 should be determined in the range

Equation 3-8:

𝛺𝛺𝑁𝑁 < 𝛺𝛺𝑐𝑐 < 𝛺𝛺𝑠𝑠 − 𝛺𝛺𝑁𝑁

𝛺𝛺𝑠𝑠 𝛺𝛺𝑠𝑠
As mentioned above, 𝛺𝛺𝑠𝑠 > 2𝛺𝛺𝑁𝑁 ⇒ > 𝛺𝛺𝑁𝑁 or − 2
< −𝛺𝛺𝑁𝑁 . With minor modification we get 𝛺𝛺𝑠𝑠 −
2
𝛺𝛺𝑠𝑠 𝛺𝛺𝑠𝑠 𝛺𝛺𝑠𝑠 𝛺𝛺𝑠𝑠
< 𝛺𝛺𝑠𝑠 − 𝛺𝛺𝑁𝑁 , and thus < 𝛺𝛺𝑠𝑠 − 𝛺𝛺𝑁𝑁 . Now since > 𝛺𝛺𝑁𝑁 ⇒ 𝛺𝛺𝑁𝑁 < 2
< 𝛺𝛺𝑠𝑠 − 𝛺𝛺𝑁𝑁 . Therefore, if
2 2 2

we consider Ω𝑐𝑐 = Ω2𝑠𝑠, Equation 3-8 will be satisfied and we get

Equation 3-9:
Ω𝑠𝑠 2𝜋𝜋 1 𝜋𝜋
Ω𝑐𝑐 = = × =
2 𝑇𝑇𝑠𝑠 2 𝑇𝑇𝑠𝑠

The reconstruction process can be seen in Figure 3-5, where

Equation 3-10:
+∞

𝑥𝑥𝑠𝑠 (𝑡𝑡) = � 𝑥𝑥[𝑛𝑛]𝛿𝛿(𝑡𝑡 − 𝑛𝑛𝑇𝑇𝑠𝑠 )


𝑛𝑛=−∞

and 𝐻𝐻𝑟𝑟 (𝑗𝑗Ω) is the ideal low pass filter where its time representation is

Equation 3-11:
𝜋𝜋𝜋𝜋
𝜋𝜋𝜋𝜋 𝑠𝑠𝑠𝑠𝑠𝑠 � � 𝑠𝑠𝑠𝑠𝑠𝑠(Ω 𝑡𝑡)
𝑇𝑇𝑠𝑠 𝑐𝑐
ℎ𝑟𝑟 (𝑡𝑡) = 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠 � � = 𝜋𝜋𝜋𝜋 =
𝑇𝑇𝑠𝑠 Ω𝑐𝑐 𝑡𝑡
𝑇𝑇𝑠𝑠
Figure 3-5: The reconstruction process

Then using convolution operation, 𝑥𝑥𝑐𝑐 (𝑡𝑡) can be expressed (reconstructed) exactly in terms of its
samples 𝑥𝑥𝑐𝑐 (𝑛𝑛𝑇𝑇𝑠𝑠 )

Equation 3-12:
+∞ +∞

𝑥𝑥𝑐𝑐 (𝑡𝑡) ≅ 𝑥𝑥𝑟𝑟 (𝑡𝑡) = 𝑥𝑥𝑠𝑠 (𝑡𝑡) ∗ ℎ𝑟𝑟 (𝑡𝑡) = � 𝑥𝑥[𝑛𝑛]𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠�Ω𝑐𝑐 (𝑡𝑡 − 𝑛𝑛𝑇𝑇𝑠𝑠 )� = � 𝑥𝑥𝑐𝑐 (𝑛𝑛𝑇𝑇𝑠𝑠 )𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠�Ω𝑐𝑐 (𝑡𝑡 − 𝑛𝑛𝑇𝑇𝑠𝑠 )�
𝑛𝑛=−∞ 𝑛𝑛=−∞

In practice, the conversion of a sampled signal to a continuous-time form, referred to as the digital
to analog (D/A) operation, involves two parts, namely a hold operation followed by low pass
filtering.

The most common form of hold operation is the zero-order hold or sample and hold. The impulse
response for the sample and hold device is given by

Equation 3-13:
0 ≤ 𝑡𝑡 < 𝑇𝑇𝑠𝑠 1,
ℎℎ𝑜𝑜𝑜𝑜𝑜𝑜 (𝑡𝑡) = �
0,
𝑜𝑜𝑜𝑜ℎ𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒
The operation of the sample and hold is illustrated in Figure 3-6.
Figure 3-6: Reconstruction of a continuous time signal using its samples. The input to the “Sample and Hold”
is 𝒙𝒙[𝒏𝒏] and the output is 𝒙𝒙(𝒕𝒕). The output of the “Low Pass filter” is 𝒙𝒙𝒄𝒄 (𝒕𝒕).

Lab Problems
 Problem 1. Consider the sinusoidal signal
𝑥𝑥(𝑡𝑡) = sin 𝛺𝛺0 𝑡𝑡, where 𝛺𝛺0 = 10𝜋𝜋 𝑟𝑟𝑟𝑟𝑟𝑟/𝑠𝑠𝑠𝑠𝑠𝑠.
(a) Using a sampling rate 20 times the Nyquist rate, simulate the continuous-time signal 𝑥𝑥(𝑡𝑡).
Using the MATLAB plot function, generate the graph over the time interval 0 ≤ 𝑡𝑡 ≤ 1
seconds.
𝛺𝛺
Hint: For this purpose, the maximum frequency of the signal should be found ( 0 ) and then
2𝜋𝜋

the Nyquist rate will be computed easily. Here the sampling rate is considered to be 20
times of Nyquist rate so compute the wanted sampling rate (𝐹𝐹𝑠𝑠 ) and sampling period (𝑇𝑇𝑠𝑠 ).
See the following code segment for more help

t = 0 : Ts : 1; %time interval
x_s = sin(2 * pi * f_max * t);

(b) Following Equation 3-11 and Equation 3-12, one can reconstruct the signal using its
samples according to the following code segment:
h_r = sinc(pi * t / Ts);
x_c = conv(h_r, x_s, 'full');
l = length(x_c);
t_c = linspace(0, 1, l / 2);
figure, plot(t_c, x_c(1 : l / 2));

Run the code, see the results, and explain the reason of writing each of the statement of the
above code completely.

(c) Now sample 𝑥𝑥(𝑡𝑡) at 1, 2, 5, and 10 times of the Nyquist rate and reconstruct the signal
using the corresponding samples. Explain your observations of the reconstructed signals.
(d) Sample 𝑥𝑥(𝑡𝑡) at 5 times of the Nyquist rate and then apply a low-pass filter, with
Ω𝑐𝑐 = 0.2𝜋𝜋, on this signal and see the result. Reconstruct the signal using the sample and
hold operation by holding the sample for 6 next samples. Now apply a low-pass filter on
this signal and plot the reconstructed signal.
Hint 1: A low-pass filter removes high-frequency content from a signal and preserves low-
frequency content. For this lab, you can use the following code segment for low-pass filtering:

b = fir1(48, 0.2); %Design a 48th-order FIR lowpass filter with w <= .2


* pi
x_c = filter(b, 1, x); %Appling filter on x using b coefficients

Hint 2: For simulating the sample and hold operation in MATLAB, if the length of the
signal 𝑥𝑥(𝑡𝑡) is 𝐿𝐿, and we want to hold each sample for n next samples (here 𝑛𝑛 = 6), we can
define a new signal with length 𝐿𝐿 × (𝑛𝑛 + 1) and then fill its elements using for loop.

 Problem 2. The signal of interest is

Equation 3-14:
1 1 1 1
𝑆𝑆(𝑡𝑡) = sin(14𝜋𝜋𝜋𝜋) + sin(18𝜋𝜋𝜋𝜋) + sin(24𝜋𝜋𝜋𝜋) + sin(30𝜋𝜋𝜋𝜋)
2 3 5 7

(a) Sample 𝑆𝑆(𝑡𝑡) on the interval 0 ≤ 𝑡𝑡 ≤ 20 at 20 times the Nyquist rate. Plot its two-sided
spectrum.
Hint: In order to plot the two-sided spectrum of signal 𝑆𝑆 with sampling frequency 𝐹𝐹𝑠𝑠 you can
use fftshift MATLAB built-in function as follow:
X = fft(S);
Y = fftshift(X); %Shift zero-frequency component to center of spectrum
L = length(Y);
P = abs(Y / L);

%Define the frequency domain f and plot the two-sided amplitude spectrum P.
f = Fs * (-L / 2 : L / 2 - 1) / L;
plot(f, P)

(b) Plot the two-sided spectrum of 𝑆𝑆(𝑡𝑡) with the following sampling frequencies:
i. 3/2 time the Nyquist rate
ii. 5/6 times the Nyquist rate
iii. 13/30 times the Nyquist rate
(c) Explain the differences in signals spectra in all of the cases of part b. In case of Nyquist
criterion (no aliasing) and violating it (having aliasing). In case of aliasing as shown in
Figure 3-3 completely explain why the resulting frequency components exist in the observed
locations? (What are these frequency locations?)
(d) Given that the sampling rate for 𝑦𝑦(𝑡𝑡) is fixed to be 20Hz, how would it be possible to ensure
that the spectrum of the sampled signal 𝑦𝑦[𝑛𝑛] for the frequencies 0 ≤ 𝑓𝑓 ≤ 10 is a true
representation. Draw a block diagram of the procedure to meet such an objective.
LAB 9: Decimation and Interpolation

Objectives
• To learn how to use MATLAB to implement decimation and interpolation

Downsampling and Decimation


Downsampling can be considered as the discrete-time counterpart of sampling. Whereas in
sampling we start with a continuous-time signal 𝑥𝑥𝑐𝑐 (𝑡𝑡) and convert it to a sequence of
samples 𝑥𝑥[𝑛𝑛], in downsampling we start with a discrete-time signal 𝑥𝑥[𝑛𝑛] and convert it to another
discrete-time signal 𝑥𝑥𝑜𝑜 [𝑛𝑛], which consists of subsamples of 𝑥𝑥[𝑛𝑛]. Thus, the formal definition of
M-fold downsampling is

Equation 4-1:

𝑥𝑥𝑜𝑜 [𝑛𝑛] = 𝑥𝑥[𝑀𝑀𝑛𝑛]

where 𝑀𝑀 is a positive integer. The block diagram representation of the downsampling operation is
shown in Figure 4-1.

Figure 4-1: Downsampling by integer factor M.


1
Where the output sequence 𝑥𝑥𝑜𝑜 [𝑛𝑛] is sampled at sampling rate equal to ( )𝑡𝑡ℎ of the sampling rate
𝑀𝑀

of input sequence 𝑥𝑥[𝑛𝑛]. Downsampling operation can be implemented by keeping all the input
samples that have indices equal to an integer multiple of 𝑀𝑀.

For example Figure 4-2 shows downsampling by integer factor 𝑀𝑀 = 2.


(2) (2) (2)
x[n] (1.5) (1.5) (1.5)
(1) (1) (1)
(0.5)

0 1 2 3 4 5 6 7 8 9 n

xD[n]=x[2n] (1.5) (1.5)

(1) (1) (1)

0 1 2 3 4 n

Figure 4-2: Down-sampling by integer factor 𝑴𝑴 = 𝟐𝟐

Note that the samples corresponding to 𝑛𝑛 = 1, 3, 5, 7, 9 are lost by downsampling.

It is more convenient to analyse downsampling operation in frequency domain. The frequency


representation of downsampling operation is given [1] by

Equation 4-2:
𝑀𝑀−1
𝑗𝑗𝑗𝑗
1 𝑗𝑗�
𝜔𝜔−2𝜋𝜋𝜋𝜋

𝑋𝑋𝑑𝑑 �𝑒𝑒 � = � 𝑋𝑋 �𝑒𝑒 𝑀𝑀 �
𝑀𝑀
𝑖𝑖=0

According to Equation 4-2, downsampling by integer factor 𝑀𝑀 has three effects on the frequency
spectrum:

1. It expands the frequency spectrum by 𝑀𝑀.


2. It makes the frequency spectrum be periodic with 2𝜋𝜋 and sums up all the
resulted replicas.
1
3. It scales the magnitude by .
𝑀𝑀
Figure 4-3: The effect of downsampling operation by factors 𝑴𝑴 = 𝟐𝟐 (the second plot), and 𝑴𝑴 = 𝟑𝟑 (the third
plot)

In Figure 4-3 these three effects can be seen on an example signal by considering two
downsampling factors 𝑀𝑀 = 2, and 𝑀𝑀 = 3. In Figure 4-3 (the second plot) aliasing does not happen
(there isn’t any overlap between replicas), but in this example when 𝑀𝑀 = 3, downsampling leads
to aliasing. As can be seen in Figure 4-3 (the third plot) red line shows the resulted frequency
spectrum after downsampling by 𝑀𝑀 = 3, and the correct frequency spectrum band which is not
𝜋𝜋 𝜋𝜋
affected by aliasing is in the range �− , �. In Figure 4-4 a lowpass filter with cut-off frequency
2 2
𝜋𝜋
Ω𝑐𝑐 = , and gain 1 is applied on the signal before downsampling operation. Figure 4-4 second plot
3

shows the result of applying this filter. Due to the downsampling by factor 𝑀𝑀 = 3 the frequency
spectrum will be expanded by 3, which will now cover the range [−𝜋𝜋, 𝜋𝜋]. Although in this case
we have still information loss but compared to Figure 4-3 (the third plot), aliasing does not happen
and the correct frequency spectrum band covers whole of the range [−𝜋𝜋, 𝜋𝜋]. The downsampling
filter is called anti-aliasing filter or decimation filter.
Figure 4-4: The effect of applying lowpass filtering before downsampling operation

According to the above mentioned, it is desirable to precede the downsampler with an anti-aliasing
filter. Unlike sampling, here the input signal is already in discrete time, so we use a digital anti-
aliasing filter. The anti-aliasing filter (also called the decimation filter), should approximate an
𝜋𝜋
ideal lowpass filter with cut-off frequency , as shown in Figure 4-5. The combination of the
𝑀𝑀

lowpass filter followed by a downsampler is called decimation.

Figure 4-5: A general decimation system: lowpass filter has gain equal to 1 and cut-off frequency 𝝅𝝅/𝑴𝑴
combined with down-sampler by integer factor M.
Upsampling and Interpolation
Upsampling is another operation on a discrete-time signal that yields a discrete-time signal.
Upsampling by factor 𝐿𝐿, adds 𝐿𝐿 − 1 new samples between each two samples of the input discrete-
time signal. The magnitude of these new added samples will be estimated by interpolation
technique. Similar to the continuous-time signals, in order to perform interpolation in discrete-
time signals, an ideal digital lowpass filter can be applied on the signal. This filtering process will
not generate additional samples, therefore, before interpolation operation an upsampler (expander)
will be employed which adds 𝐿𝐿 − 1 new samples with zero magnitude between each two samples
of the original signal. If the input signal is 𝑥𝑥[𝑛𝑛], the output signal of upsampler (expander) is 𝑥𝑥𝑒𝑒 [𝑛𝑛]
which is defined as follow:

Equation 4-3:
𝑛𝑛
𝑥𝑥 � � 𝑛𝑛 = 0, ±𝐿𝐿, ±2𝐿𝐿, …
𝑥𝑥𝑒𝑒 [𝑛𝑛] = � 𝐿𝐿
0 𝑂𝑂𝑂𝑂ℎ𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒

The block diagram representation of an expander with integer factor 𝐿𝐿 is shown in Figure 4-6.

Figure 4-6: Expander by integer factor L

For an example Figure 4-7 shows expanding by integer factor 𝐿𝐿 = 2.

(2)
x[n]
(1.5) (1.5)
(1) (1)

0 1 2 3 4 n

(2)
xe[n] (1.5) (1.5)
(1) (1)

0 1 2 3 4 5 6 7 8 9 n

Figure 4-7: Expanding by integer factor 𝑳𝑳 = 𝟐𝟐


The frequency representation of an expander is [1] as follow:

Equation 4-4:

𝑋𝑋𝑒𝑒 �𝑒𝑒 𝑗𝑗𝑗𝑗 � = 𝑋𝑋�𝑒𝑒 𝑗𝑗𝑗𝑗𝑗𝑗 �

From Equation 4-4 it is obvious that the upsampler (expander) will compress the frequency
spectrum of the signal by 𝐿𝐿. In the other words aliasing (and information loss) will not be
happened. As can be seen in Figure 4-8 an example signal is expanded by 𝐿𝐿 = 2, and because of
this its frequency spectrum has been compressed by 2.

Figure 4-8: The effect of expanding a signal with 𝑳𝑳 = 𝟐𝟐

Upsampler (expander) will place additional samples with zero magnitude between each two
samples of the original signal. Now it is the time to estimate the value of those samples by applying
an ideal interpolation filter which is an ideal digital lowpass filter. It is worth noting that upsampler
(expander) also yields undesired replicas of the signal's spectrum in the range 𝜔𝜔 ∈ [−𝜋𝜋, 𝜋𝜋]. These
replicas can be eliminated by applying the mentioned interpolation filter. The interpolation filter
is also called anti-imaging filter. This filter has cut-off frequency 𝜔𝜔𝑐𝑐 = 𝜋𝜋�𝐿𝐿 and gain equal to
𝐿𝐿 [1]. The combination of upsampler (expander) followed by an interpolation filter is called
interpolation system as in Figure 4-9.
Figure 4-9: A general interpolation system: upsampler (expander) with integer factor L combined with
interpolation filter.

It should be noted that the upsampler and downsampler operations are linear but time-varying (not
time-invariant) discrete-time systems. The upsampler and the downsampler building blocks are
often involved in multirate signal processing.

Lab Problems
 Problem 1.
(a) Write a MATLAB script to generate the signals 𝑥𝑥1 [𝑛𝑛], 𝑥𝑥2 [𝑛𝑛], and 𝑥𝑥3 [𝑛𝑛] that have the
DTFT shown in Figure 4-10 (a), (b), and (c), respectively. Make sure that each signal has
length of 1025 samples. Plot the resulting DFT over the range −𝜋𝜋 ≤ 𝜔𝜔 ≤ 𝜋𝜋.
Hint: For this purpose you can use fir2 MATLAB function. b=fir2(n,f,m)
produces an 𝑛𝑛𝑡𝑡ℎ order FIR filter with frequencies 𝑓𝑓 and magnitudes 𝑚𝑚, returning the 𝑛𝑛 + 1
filter coefficients 𝑏𝑏 [3]. For example the following code segment will produce a function
which has frequency spectrum similar to Figure 4-10 (a).

N = 1024;
T=1/N;
f = 0 : T : 1; %Frequency range
L = length(f); %Length of f
m = zeros(1, L); %Frequency magnitude
m(1 : 1/4 * L)= -4 * f(1: 1/4 * L) + 1; %Frequency magnitude
x = fir2(n, f, m); %nth-order FIR filter with frequency-magnitude
characteristics specified in the vectors f and m.
X = fft(x); %Fourier Transform of x
L = length(X); %Length of X
fshift = linspace(-pi, pi, L); %Generate linearly spaced vector
X = fftshift(X); %Shift zero-frequency component to center of spectrum
plot(fshift, abs(X));
title('FFT of input signal X');
ax = gca; %Current axes or chart
ax.XTick = -3:.2:3;
ax.YTick = 0:.5:1;

�𝑋𝑋1 (𝑒𝑒 𝑗𝑗 ω )�
1

−2𝜋𝜋 −𝜋𝜋 − 𝜋𝜋 0 𝜋𝜋 𝜋𝜋 2𝜋𝜋 ω


2 2
(a)

�𝑋𝑋2 (𝑒𝑒 𝑗𝑗 ω )�

−2𝜋𝜋 −𝜋𝜋 𝜋𝜋 0 𝜋𝜋 2𝜋𝜋


𝜋𝜋 ω

4 4
(b)

�𝑋𝑋3 (𝑒𝑒 𝑗𝑗ω )�


1

−2𝜋𝜋 𝜋𝜋 𝜋𝜋 2𝜋𝜋
−𝜋𝜋 − 𝜋𝜋 ω
8 8
(c)

Figure 4-10: Discrete Fourier transform of signals 𝒙𝒙𝟏𝟏 [𝒏𝒏], 𝒙𝒙𝟐𝟐 [𝒏𝒏] and 𝒙𝒙𝟑𝟑 [𝒏𝒏].
(b) Use 𝑥𝑥1 [𝑛𝑛] of part (a) as the input to the systems (a), (b), (c), and (d) shown in
Figure 4-11 and write a MATLAB script to generate the output 𝑦𝑦[𝑛𝑛] for each system. Plot
the discrete time Fourier transform (over the range −𝜋𝜋 ≤ 𝜔𝜔 ≤ 𝜋𝜋) of the input signal 𝑥𝑥1 [𝑛𝑛]
and the output signal 𝑦𝑦[𝑛𝑛] on same figure using “subplot” command for each system.
Adjust the title, xlabel and ylabel for each figure accordingly. Repeat this scenario for
𝑥𝑥2 [𝑛𝑛], and 𝑥𝑥3 [𝑛𝑛].
𝜋𝜋
Hint: In order to design a low-pass filter with order 128, cutoff frequency , and gain 𝑔𝑔,
3

you can use fir1 MATLAB function as follow:

b = g * fir1(128, 1/3); %Window-based low pass FIR filter cutoff pi/3


y = filter(b, 1, x); %y is the output of applying the designed filetr
on signal x

In order to do downsampling and upsampling you can use downsample and


upsample MATLAB functions [3].

(c) Explain the reason of each resulted plots in part (b) completely.

x[n] 3 y[n]
(a)
Lowpass filter v[n]
x[n] Gain=1
Cutoff=π/3
3 y[n]
(b)

x[n] 3 y[n]
(c)
v[n] Lowpass filter
x[n] 3 Gain=3
Cutoff=π/3
y[n]
(d)
Figure 4-11: Down-sampler, decimation system, up-sampler, interpolation system
LAB 10: Sample Rate Conversion

Objectives
• To learn how to use MATLAB to implement sampling rate conversion and multirate signal
processing systems.

Key Issues
A multirate Digital Signal Processing (DSP) system is characterized by the use of multiple
sampling rates in its realization. That is, signal samples at various points in the systems may not
correspond to the same physical sampling frequency. Some applications of multi-rate processing
include

• Sampling rate conversion between different digital audio standards


• Digital anti-aliasing filtering (used in commercial CD players)
• Sub-band coding of speech and video signals
In some applications, the need for multi-rate processing comes naturally from the problem
definition (e.g. sampling rate conversion). In other applications, multi-rate processing is used to
achieve improved performance of a DSP function (e.g. lower binary rate in speech compression).

In our previous experiments, we have assumed that all signals in a given system have the same
sampling rate. We have interpreted the time index 𝑛𝑛 as an indicator of the physical time 𝑛𝑛𝑇𝑇𝑠𝑠 , where
𝑇𝑇𝑠𝑠 is the sampling interval. In many practical applications of DSP, one is faced with the problem
of changing the sampling rate of a signal, either increasing it or decreasing it by some ratio. For
example, in telecommunication systems that transmit and receive different types of signals (e.g.
speech, video, etc.), there is a requirement to process the various signals at different rates
commensurate with the corresponding bandwidths of the signals. The process of converting a
signal from a given rate to a different rate is called sampling rate conversion. In turn, systems that
employ multiple sampling rates in the processing of digital signals are called multirate digital
signal processing systems.
The two basic operations in a multirate system are decreasing and increasing the sampling rate of
a signal. The former is called decimation or down-sampling. The latter is called interpolation or
up-sampling.

Sampling Rate Conversion


A common use of multirate signal processing is for sampling rate conversion. Sampling-rate
conversion can be accomplished by first upsampling by factor 𝐿𝐿 followed by lowpass filtering and
then down-sampling by factor 𝑀𝑀 as it is observed in Figure -1 . In this figure the sampling time
interval for all input and output signals have been shown under the name of each signal.
Interpolation and decimation filters can also be combined with each other (Figure -2 ). In this
case the lowpass filter performs both interpolation of the upsampled signal and anti-aliasing before
downsampling. If the sampling rate is to be increased, then we have 𝐿𝐿 > 𝑀𝑀. The lowpass filter
𝜋𝜋
should then have a cutoff frequency . If the sampling rate is to be decreased, then we have 𝐿𝐿 <
𝐿𝐿
𝜋𝜋
𝑀𝑀. The lowpass filter should then have a cutoff frequency . Thus, the sampling-rate conversion
𝑀𝑀
𝜋𝜋 𝜋𝜋
filter should always have a cut-off frequency Ω𝑐𝑐 = 𝑚𝑚𝑚𝑚𝑚𝑚 � , �.
𝐿𝐿 𝑀𝑀

Figure 1- : System for changing the sampling rate by a non-integer factor with interpolation and decimation
filters.
Figure 2 : System for changing the sampling rate by a non-integer factor with combined interpolation and
decimation filters.

Suppose that we are given a digital signal 𝑥𝑥[𝑛𝑛] sampled at interval 𝑇𝑇1 and we wish to obtain from
it a signal 𝑦𝑦[𝑛𝑛] sampled at interval 𝑇𝑇2 . The techniques of decimation and interpolation enable this
𝑇𝑇1 𝐿𝐿
operation, provided the ratio is a rational number say . We distinguish now between two
𝑇𝑇2 𝑀𝑀

possibilities:

1) 𝑇𝑇1 > 𝑇𝑇2 , meaning that the sampling rate should be increased. This is always possible
without aliasing.
2) 𝑇𝑇1 < 𝑇𝑇2 , meaning that the sampling rate should be decreased. This is possible without
𝑇𝑇
aliasing only if 𝑥𝑥[𝑛𝑛] is band limited to a frequency range not higher than ±𝜋𝜋( 1 ). If 𝑥𝑥[𝑛𝑛]
𝑇𝑇2

does not fulfill this condition, a part of its frequency contents must be eliminated before
decimation to avoid aliasing.
Lab Problems
 Problem 1.

Use 𝑥𝑥1 [𝑛𝑛] of prev lab as the input to the system shown in
Figure -3 and write a MATLAB script to generate the output 𝑦𝑦[𝑛𝑛] for each system. Plot
the discrete time Fourier transform (over the range −𝜋𝜋 ≤ 𝜔𝜔 ≤ 𝜋𝜋) of the input signal 𝑥𝑥1 [𝑛𝑛]
and the output signal 𝑦𝑦[𝑛𝑛] on same figure using “subplot” command .
Adjust the title, xlabel and ylabel for each figure accordingly. Repeat this scenario for
𝑥𝑥2 [𝑛𝑛], and 𝑥𝑥3 [𝑛𝑛].
𝜋𝜋
Hint: In order to design a low-pass filter with order 128, cutoff frequency , and gain 𝑔𝑔,
3

you can use fir1 MATLAB function as follow:

b = g * fir1(128, 1/3); %Window-based low pass FIR filter cutoff pi/3


y = filter(b, 1, x); %y is the output of applying the designed filetr
on signal x

In order to do downsampling and upsampling you can use downsample and


upsample MATLAB functions.

Explain the reason of resulted plot completely.

Lowpass filter v[n]


x[n] 3 Gain=3
Cutoff=π/3
2 y[n]

Figure 3: Sample rate conversion


 Problem 2.
Consider the analysis-synthesis system shown in Figure -4 .

wL[n] gL[n] vL[n] yL[n]


hL[n] 2 2 hL[n]
x[n] y[n]=yL[n]+yH[n]
+
wH[n] gH[n] vH[n]
hH[n] 2 2 hH[n]
yH[n]
Analyzer synthesizer
Figure 4 : An analysis-synthesis system

𝜋𝜋
(a) Design a lowpass FIR filter ℎ𝑗𝑗 [𝑛𝑛], with order 128, cutoff frequency , and gain 1. You can
2

use fir1 MATLAB function.


𝜋𝜋
(b) Design a highpass FIR filter ℎ𝐻𝐻 [𝑛𝑛], with order 128, cutoff frequency , and gain 1. You
2

can use fir1 MATLAB function.


For the following parts, plot on the same figure using “subplot” command for each system.
Adjust the title, xlabel and ylabel for each figure accordingly.
(c) Plot the magnitude of both 𝐻𝐻𝑗𝑗 (𝑒𝑒 𝑗𝑗𝑗𝑗 ) and 𝐻𝐻𝐻𝐻 (𝑒𝑒 𝑗𝑗𝑗𝑗 ) over the range −𝜋𝜋 ≤ 𝜔𝜔 ≤ 𝜋𝜋.
2 2
(d) Plot the magnitude of �𝐻𝐻𝑗𝑗 �𝑒𝑒 𝑗𝑗𝑗𝑗 �� + �𝐻𝐻𝐻𝐻 �𝑒𝑒 𝑗𝑗𝑗𝑗 �� over the range −𝜋𝜋 ≤ 𝜔𝜔 ≤ 𝜋𝜋.

(e) Plot the magnitude of 𝐻𝐻𝑗𝑗 �𝑒𝑒 𝑗𝑗𝑗𝑗 �𝐻𝐻𝐻𝐻 �𝑒𝑒 𝑗𝑗𝑗𝑗 � over the range −𝜋𝜋 ≤ 𝜔𝜔 ≤ 𝜋𝜋.
(f) According to the results of parts (d) and (e) what is your idea about the value of 𝑌𝑌(𝑍𝑍).
LAB 11: Analysis of Noisy Signals
Objectives
• In this lab experiment, you will learn how to analyze different signals in the presence of noise.

Signal Restoration
“Signal Restoration” usually refers to the enhancement or improvement of signals that have been
degraded in some way, for example, through noise or echo. Signal restoration recovers a signal
from a degraded version. It is a fundamental problem in signal processing, and it also provides a
testbed for more general inverse (or recovery) problems, that is, problems for which information
that is lost (due, for example, to transmission or compression), is attempted to be recovered. Key
issues that need to be addressed are the quality of the restored signal, the computational complexity
of the solution (algorithm), and the estimation of necessary parameters such as those of the impulse
response (or the point-spread function PSF). To restore a signal, we need to design a filter suitable
for the problem to solve. To measure restored signal quality, we need to design objective quality
measure.

Objective Quality Measurement


A corrupted signal is degraded version of the original. For each corrupted (noisy and echo) signal
we would like to quantify how much corruption has taken place. A common measure of
degradation is Mean Squared Error (MSE). If we have a signal 𝑥𝑥[n] and the same signal corrupted
by some way called 𝑥𝑥_𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 [𝑛𝑛], each of length 𝑁𝑁, then the following is the 𝑀𝑀𝑀𝑀𝑀𝑀

Equation 5-1:
𝑁𝑁
1 2
𝑀𝑀𝑀𝑀𝑀𝑀 = ��𝑥𝑥 [𝑛𝑛] − 𝑥𝑥𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 [𝑛𝑛]�
𝑁𝑁
𝑛𝑛=1

Where the lower the MSE value the better the signal quality. We can also measure signal quality using the
Signal to Noise Ratio (SNR) in decibels (dB), where 0.5 to 1 dB is said to be a perceptible difference
between two signals. The SNR indicates the strength of the signal with respect to the distortion (such as the
noise or echo) power and is defined as

Equation 5-2:
∑𝑁𝑁
𝑛𝑛=1 |𝑥𝑥[𝑛𝑛]|
2
𝑆𝑆𝑆𝑆𝑆𝑆 = 10𝑙𝑙𝑙𝑙𝑙𝑙 .
𝑀𝑀𝑀𝑀𝑀𝑀

The higher the SNR value the better the signal quality.

Lab Problems
(a) In the supplementary matarial of Lab 8 , you find a file with the name
“HappyBirthday.wav”, which is a noise-free audio signal. Write a MATLAB code to read
this signal. Add Gaussian noise to it with mean equal to zero and variance equal 𝜎𝜎 2 , that
is, if your original noise-free signal is 𝑥𝑥[𝑛𝑛] then the noisy signal 𝑥𝑥𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛 [𝑛𝑛] = 𝑥𝑥[𝑛𝑛] + 𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛.

Create 3 noisy signals with different variances 0.1, 0.3, and 0.5. Listen to all generated
noisy signals and finally save all of the noisy signals with meaningful names.
Hint 1: In order to generate a Gaussian noise with length “𝐿𝐿”, mean “𝑚𝑚” and variance
“𝑛𝑛𝑟𝑟𝑠𝑠𝑠𝑠𝑒𝑒_𝑣𝑣𝑎𝑎𝑟𝑟”, you can use the following code segment:

noise = sqrt(noise_var) * randn(L, 1) + m; %Generate a Gaussian noise


with variance "noise_var" and mean "m"

Hint 2: In order to process audio signals you may need to use these MATLAB functions:
audioread, audiowrite, soundsc, and pause [3].
(b) Determine the MSE between the original signal and the noisy signals you generated in part
(a). What is the relationship between the computed MSEs and the variance of each
Gaussian noise signal?
(c) Write a MATLAB code to read the noise-free signal in part (a) and add three different
Gaussian noise signals with the SNRs (Signal-to-Noise-Ratio) [10 5.22 3.01].
Hint 1: In order to add a Gaussian noise to a signal with requested SNR you can use awgn
MATLAB function as follow:

s_noisy = awgn(s, SNR); %Add Gaussian noise with requested SNR to s


Hint 2: For this purpose you can also use the following code segment:
sigPower = 1; %Signal power supposed to be 1
noisePower = sigPower / (10 ^ (reqSNR / 10)); %Power of the noise
signal considering the requested SNR
noise = sqrt(noisePower) * randn(1, L); % Noise signal of size L
x = x + noise; %Noisy signal with requested SNR

(d) Determine the MSE between the original signal and the noisy signals you generated in part
(c). Derive the relationship between requested SNR and noise variance (use Hint 2 of
part(c)). What is the relationship between the computed MSEs and the variance of each
Gaussian noise?
Hint: Using Hint 2 of part (c), if we consider “𝑛𝑛𝑟𝑟𝑠𝑠𝑠𝑠𝑒𝑒𝑛𝑛𝑟𝑟𝑒𝑒𝑒𝑒𝑟𝑟” as the “𝑉𝑉𝑎𝑎𝑟𝑟𝑠𝑠𝑎𝑎𝑛𝑛𝑠𝑠𝑒𝑒” of the
noise we can derive a logarithmic formula which relates the requested 𝑀𝑀𝑁𝑁𝑅𝑅 to the noise
𝑉𝑉𝑎𝑎𝑟𝑟𝑠𝑠𝑎𝑎𝑛𝑛𝑠𝑠𝑒𝑒.
(e) In the supplementary matarial of Lab 8 , there is a file with the name
“HappyBirthday_Echo.wav” which is the echo corrupted signal of the original music
signal. Determine the MSE between the original signal and this echo corrupted signal.
LAB 12: Signal Restoration and Filter Design
Objectives
• In this lab experiment, you will learn how to use LTI filters as signal restoration tools and
how to use MATLAB for designing filters.

Signal Restoration
“Signal Restoration” usually refers to the enhancement or improvement of signals that have been
degraded in some way, for example, through noise or echo. Signal restoration recovers a signal
from a degraded version. It is a fundamental problem in signal processing, and it also provides a
testbed for more general inverse (or recovery) problems, that is, problems for which information
that is lost (due, for example, to transmission or compression), is attempted to be recovered. Key
issues that need to be addressed are the quality of the restored signal, the computational complexity
of the solution (algorithm), and the estimation of necessary parameters such as those of the impulse
response (or the point-spread function PSF). To restore a signal, we need to design a filter suitable
for the problem to solve. To measure restored signal quality, we need to design objective quality
measure.

Objective Quality Measurement


A corrupted signal is degraded version of the original. For each corrupted (noisy and echo) signal
we would like to quantify how much corruption has taken place. A common measure of
degradation is Mean Squared Error (MSE). If we have a signal 𝑥𝑥[n] and the same signal corrupted
by some way called 𝑥𝑥_𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 [𝑛𝑛], each of length 𝑁𝑁, then the following is the 𝑀𝑀𝑀𝑀𝑀𝑀

Equation 5-1:
𝑁𝑁
1 2
𝑀𝑀𝑀𝑀𝑀𝑀 = ��𝑥𝑥 [𝑛𝑛] − 𝑥𝑥𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 [𝑛𝑛]�
𝑁𝑁
𝑛𝑛=1

Where the lower the MSE value the better the signal quality. We can also measure signal quality using the
Signal to Noise Ratio (SNR) in decibels (dB), where 0.5 to 1 dB is said to be a perceptible difference
between two signals. The SNR indicates the strength of the signal with respect to the distortion (such as the
noise or echo) power and is defined as

Equation 5-2:
∑𝑁𝑁
𝑛𝑛=1 |𝑥𝑥[𝑛𝑛]|
2
𝑆𝑆𝑆𝑆𝑆𝑆 = 10𝑙𝑙𝑙𝑙𝑙𝑙 .
𝑀𝑀𝑀𝑀𝑀𝑀

The higher the SNR value the better the signal quality.

MATLAB’s Filter Designer tool


In order to restore a signal corrupted in some way, say noise or echo, we need to develop an
appropriate filtering operation. The Filter Designer tool in MATLAB is a powerful user interface
for designing and analyzing filters quickly. It enables you to design digital FIR or IIR filters by
setting filter specifications, by importing filters from your MATLAB workspace, or by adding,
moving or deleting poles and zeros. Filter Designer also provides tools for analyzing filters, such
as magnitude and phase response and pole-zero plots. Filter Designer seamlessly integrates
additional functionality from other Mathworks products. To use the Filter Designer in MATLAB,
type in command window

>>filterDesigner

Filter Designer will be opened. You can select FIR or IIR filter, order of filter, and cutoff frequency
of a filter (either HPF, LPF or BPF). That code will automatically generate .m file for you.

An introduction to the Filter Designer tool is reproduced in FilterDesignerMATLAB.pdf


and is available on LMS. The tutorial is also available online here
https://www.mathworks.com/help/signal/examples/introduction-to-filter-designer.html
Lab Problems
 Problem 1.
One method to reduce noise is applying a low-pass filter on the noisy signal. For each of the
noisy signals created in Problem 1 parts (a) and (c), try to recover the original signal by
designing an LTI lowpass filter using the MATLAB Filter Designer tool. Your LTI filter will
take the corrupted signal as input. The output should be as close to the original signal as
possible. Use MSE as a quality measure for this task and state the MSE that you obtain with
your filter. Do the following
(a) Design your system using an FIR filter of length 40 with normalized 𝑒𝑒𝑠𝑠𝑎𝑎𝑠𝑠𝑠𝑠 = 0.3,
and 𝑒𝑒𝑠𝑠𝑡𝑡𝑟𝑟𝑠𝑠 = 0.35.
(b) Design your system using an IIR Butterworth filter of length 40 with normalized 𝑒𝑒𝑠𝑠 =
0.3.
(c) Design your system using any filter you want. Try to reach less MSE than in parts (a)
and (b).
Hint: In order to employ your designed filter via Filter Designer you can select export from
file menu of “Filter Designer” window, and then set the parameters Export To: “MAT-File”,
Export As: “Objects”, and finally determine the name of Discrete Filter (which is Hd by
default) and then save it in your workspace as MAT-file (for example, myFilter.mat). Now for
applying the filter on your input signal you can use the following code:

load myFilter.mat; %Your designed discrete filter (Hd) using


filterDesigner will be loaded
s_filtered = filter(Hd, s_noisy); %Filter the noisy signal (s_noisy)

 Problem 2:
A method to recover the echo corrupted audio signal in Problem 1 part (e) will be explained
next. If the original audio signal is 𝑥𝑥[𝑛𝑛], in order to model the echo corrupted audio signal
𝑥𝑥𝑐𝑐𝑜𝑜𝑟𝑟𝑟𝑟𝑐𝑐𝑐𝑐𝑡𝑡 [𝑛𝑛], we can add a shifted version of the input signal 𝑥𝑥[𝑛𝑛 − ∆] attenuated by 𝛼𝛼 as follow

Equation 5-3: 𝒙𝒙[𝒏𝒏] + 𝜶𝜶𝒙𝒙[𝒏𝒏 − ∆] = 𝒙𝒙𝒄𝒄𝒇𝒇𝒇𝒇𝒇𝒇𝒖𝒖𝒄𝒄𝒕𝒕 [𝒏𝒏]

The Z-transform of the above equation will be:

Equation 5-4:

𝑋𝑋𝑐𝑐𝑜𝑜𝑟𝑟𝑟𝑟𝑐𝑐𝑐𝑐𝑡𝑡 (𝑍𝑍)
𝑋𝑋(𝑍𝑍)�1 + 𝛼𝛼𝑍𝑍 −∆ � = 𝑋𝑋𝑐𝑐𝑜𝑜𝑟𝑟𝑟𝑟𝑐𝑐𝑐𝑐𝑡𝑡 (𝑍𝑍) → 𝑋𝑋(𝑍𝑍) =
(1 + 𝛼𝛼𝑍𝑍 −∆ )
1
According to Equation 5-4, in order to recover 𝑋𝑋(𝑍𝑍), we can filter 𝑋𝑋𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 (𝑍𝑍) by
�1+𝛼𝛼𝑍𝑍 −∆ �

which is an IIR filter (see Example 2 of Lab 2). But we need to find the value of 𝛼𝛼 and ∆. 𝛼𝛼 is
an attenuation parameter and should be less than 1 (for example, 0.5). For finding the value of
∆ we can use autocorrelation. To compute an unbiased estimate of the signal autocorrelation
that corresponds to lags greater than zero we can use the following code segment, where Fs is
the sampling frequency:

[R, lags] = xcorr(x_corrupt, 'unbiased');


R = R(lags > 0);
lags = lags(lags > 0);
plot(lags / Fs, R)

The autocorrelation has a sharp peak at the lag (∆) at which the echo arrives; the lag can be
found according to the following code segment:

[~, delta] = findpeaks(R, lags, 'MinPeakHeight', 0.08);

In the above code, findpeaks returns only those peaks higher than ‘MinimumPeakHeight’
(0.08), which can be seen checking the autocorrelation plot for this specific audio signal.

After calculating the value of ∆ (‘delta’), according to Equation 5-4 the nominator and
denominator coefficients of this IIR filter are now determined. Apply this filter on 𝑥𝑥𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 [𝑛𝑛]
using the MATLAB function filter (similar to Example 2 of Lab 2). Calculate the MSE as
a quality measure for this task and state the MSE that you obtain with this filter. Listen to the
recovered signal and finally save it as an audio file with name “recovered_from_echo.wav”.

You might also like