ece 491-153 fall 05 undergraduate research

57
ECE 491 6/5/2007 DSP & Quantization 1 Tanim Taher ECE 491-153 Fall 05 Undergraduate Research: Digital Signal Processing & Quantization Effects Done By: Tanim Taher SID# 10370800 Date: December 19, 2005

Upload: others

Post on 12-Feb-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 1 Tanim Taher

ECE 491-153

Fall 05

Undergraduate Research:

Digital Signal Processing &

Quantization Effects

Done By: Tanim Taher SID# 10370800

Date: December 19, 2005

Page 2: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 2 Tanim Taher

ACKNOWLEDGEMENTS

I would like to express my gratitude towards Dr. Erdal Oruklu and Dr. Jafar Saniiee for

their strong support in making this project successful and a good learning experience for

me. A special thanks to Dr. Saniiee for realizing my potential and suggesting that I

should take up this research course. Thank you professors so much!

Tanim Taher

Page 3: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 3 Tanim Taher

Table Of Contents

1 Abstract……………………………………………………………….4 2 Introduction…………………………………………………...............5 3 Types of Processing……..…………………………………................7 4 DSP Processor Complexity Factors…..……………………................8 5 Processor Architecture……………..…………………………………9 6 Quantization Issues…………..……...……………………..………..10 7 Signal Flow Graphs....……………...………………………………..11 8 Direct Form Transpose Filters………………………………………13 9 Lattice Filters………………………………………….…………….14 10 Results:

a. Simulation Program Overview……………………………….17 b. Software Performance………………………………………..19 c. Software’s Algorithm Description……………………………25 d. Software Applicability………………………………………..27 e. Simulations.…………………………...……………………...28

11 Interpretation………………………………………………………...32 12 Conclusion & Future Work……………………………………...…..33 13 Appendix – Program Listings……………………………………….34

Page 4: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 4 Tanim Taher

1. ABSTRACT

Digital Signal Processing (DSP) is one of the most powerful areas in the electrical

engineering field, that lies at the root of the most exciting technologies today –

communications (wireless and all other kinds), image recognition and processing, and

many others. DSP is so exciting because due to its digital nature, it allows the integration

of computer technology with fields of science and engineering. Many DSP applications

use their own specialized techniques and algorithms, but some basic methods are

common to all and lie really at the core of the subject.

Digital signal processing involves using digital signal processors, which can be

programmed to act as filters to the digitized signals. Quantization is the process of

representing numbers in a digital binary format. The objective of this research project

was to learn how digital quantization parameters affect the performance of different kinds

of digital filters.

Page 5: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 5 Tanim Taher

2. INTRODUCTION

Digital Signal Processing is distinguished from the rest of the digital computer field by

the unique type of data it uses: signals. In most cases, these signals originate as sensory

data from the real world, for example, sound signals being picked up by a microphone.

Digital filters are a very important part of DSP. In fact, their extraordinary performance is

one of the key reasons that DSP has become so popular. Filters have two uses: signal

separation and signal restoration:

1) Signal Separation is the separation of signals that have been combined. Signal

separation is needed when a signal has been contaminated with interference,

noise, or other signals undesired. For example, when talking in a telephone’s

speakerphone, in addition to the person’s voice at this end of the line, the

microphone picks up the sound being produced by the loudspeaker. This would

normally cause the person at the other end of the line to hear echoes of his or her

own voice. Thus a filter is used that separates the sounds and eliminates the echo.

2) Signal restoration is used when a signal has been distorted in some way. For

example, an audio recording made with poor equipment may be filtered to better

represent the sound as it actually occurred.

Filtering can be done with analog filters, but digital filters achieve much better results.

This is because fine tuning analog filters can be difficult, as the analog components have

tolerance issues with their stated property values and the actual ones. On the other hand,

digital filters can be designed with precise characteristics.

However, digital filters have their own limitations. The major limit is due to a finite word

length. This limits the degree of precision with which the signal can be represented.

Digital filtering is carried out by arithmetic operations (processing) that involve ‘filter

Page 6: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 6 Tanim Taher

coefficients’. The finite word length also means that the results of the processing are not

100% accurate, but only an approximation depending on the word length and

representation format.

There are two main kinds of digital filters – infinite impulse response (IIR) filters, and

finite impulse response (FIR) filters. IIR filters have feedback, while FIR filters do not.

Any digital filter can be represented as a polynomial transfer function. The coefficients of

the polynomial represent the digital filter’s coefficients mentioned before. The transfer

function is usually represented as follows:

H(z) = (b0 + b1z-1 + b2z-2 + … bMz-M) / (1 + a1z-1 + a2z-2 + … aNz-N)

Here z-1 is called a unit delay operator. It represents a digital component whouse output

value is the same as the input value of the previous bus cycle.

An FIR filter has the denominator (a) coefficients as all zero, while IIR filters have at

least one non-zero ‘a’ coefficients. The values of the ‘a’ and ‘b’ coefficients are

determined based on the filtering characteristics that are required. Now these coefficients

must also be represented in the computer in a digital format with finite precision, and

hence the response of the filter can vary depending on how precisely these numbers are

represented in the digital signal processor.

In summary, quantization of the signal data, the coefficients and the finite precision

possible in arithmetic processing affect the performance of digital filters. The purpose of

my research was to look at the quantization effects more closely. An important objective

of my work was also to learn more about the field of digital signal processing and digital

signal processors. Hence a large portion of this report has been dedicated to summarizing

what I have learnt.

Page 7: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 7 Tanim Taher

3. TYPES OF PROCESSING

There are two main types of processing in computer science – real-time processing and

off-line processing. Off-line processing is where data has been obtained before, but the

processing is done later. Real-time processing is where the data is processed immediately

after input. Off-line processing is used in situations where the results of the operations on

the data collected are not immediately required. However, in real-time processing, the

results need to be obtained immediately. For example, in speech to text computer

systems, the spoken voice (input signal) must be processed in real-time to generate the

text (result) when preparing a document in a word processing software.

Time is a very crucial factor in real-time systems. Since the results are required

immediately, digital filters in real-time applications need to be very fast. Hence,

sometimes shortcuts may need to be made during processing, in order to produce results

faster. If a computer system is being used in an application as a real time digital filter,

this may mean that single precision word format is used instead of double precision, as

single precision processing is faster.

On the other-hand, time is not so crucial a factor in off-line processing. Hence, if we use

the same computer in an off-line application that needs to use a digital filter, we can now

use double precision arithmetic. Thus we can use higher precision in the digital filter

which makes quantization effects minimal.

Thus depending on the type of processing required – real-time or off-line, the

quantization effects on digital filters can vary. Off course we can use the same amount of

higher precision in real-time digital filters, but this may make the design of the digital

processor more complex, and hence more expensive.

Page 8: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 8 Tanim Taher

4. DSP PROCESSOR COMPLEXITY FACTORS

There are several factors which determine the design complexity and hence the cost of

digital signal processors. Ideally, we want maximum performance at lowest cost. Since

this is not always possible, we can hope to achieve optimum performance that meets the

application requirements, at a reasonably low cost.

The following are the main factors involved in determining this complexity:

Processing Frequency

Precision

The type of filter used

The number of coefficients in the filter

1) Obtaining higher processing frequency affects the processor complexity as follows.

Processor uses more power

Complex algorithms may need to be used to make the processing faster and more

efficient. The algorithm complexity and the processor complexity are correlated.

Processor may require more memory.

Processor may need more adders, multipliers, etc to increase the amount of

calculations possible per bus cycle.

Hence more cost and more complexity.

2) Higher precision in a digital filter involves using longer word lengths. For high

precision work, we would prefer a 32-bit processor over a 16-bit. Hence, the cost and

complexity of the processor is more.

3) Type of filter Used:

IIR filters usually have far fewer coefficients than FIR. Hence they are faster in

processing, or require fewer arithmetic components in the processor.

Lattice filters require a lot of multiplication and addition stages, and this means

added processor complexity.

Page 9: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 9 Tanim Taher

4) More the number of coefficients in a digital filter, the more calculations that need to be

performed. Hence complexity and cost increases as does the number of coefficients. If

we are to use the same processor for two filters – one with more coefficients than

another one, the former would require more processing time and hence will be slower.

5. PROCESSOR ARCHITECTURE

DSP processors in the market commonly use the Super Harvard Architecture. This

architecture lets the processor achieve more per bus cycle. The key features of this type

of architecture are support for separate data and program buses, instruction cache and

support for independent data management in memory. The following figure shows this

architecture:

As can be seen the processor (CPU) uses separate buses for instructions and data. Also

there is an instruction cache that stores the common instruction sequences required for

signal processing. And finally, we can see data in the memory being written or read by an

I/O controller independent of the processor. This architecture, as is obvious, allows for

faster processing.

Reference: The Scientist and Engineer's Guide to Digital Signal Processing (2nd Edition)

Page 10: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 10 Tanim Taher

6. QUANTIZATION ISSUES

No digital system in the world can give us infinite precision while representing all

numbers. There is always a size limit for a digital word.

Rounding is the term used to describe the process of approximating a number in the

digital domain. This is done by rounding the least significant bit such that the value of the

digital number is closest to the actual value of the number. We are loosing some degree

of precision by this approximation, and generating what is called a ‘roundoff error’. On

average, this error amount is equal to a quarter of the least significant bit value. During

analog to digital sampling, the roundoff error can be quite significant if the amount of

noise inherent in the original signal is close to the average error amount.

Another type of error is truncation error which occurs when the results of arithmetic

operations like division are truncated after the word length has been reached.

In recursive filtering operations, the quantization errors simply keep adding up each time

with the digitally filtered data. Furthermore, roundoff error can cause stability issues

when it comes to IIR filters. Rounding, as it causes loss in precision, changes the filter

coefficients such that they no longer are the ideal values. If the pole (denominator in the

transfer function) coefficients are close to 1, and rounding produces a coefficient equal to

1 or more, then the digital filter is unstable.

There are three binary number formats that are in common use. The type of format used

changes the impact of quantization effects in digital filters. The following are the formats:

1) Single Precision floating point – this uses a 32 bit word. The format for storing

the number is standard number format (e.g +1.234 X 103 is a standard format

number).

2) Double Precision floating point – 64 bit, standard number format.

3) Fixed Point – varying word lengths are in use (common 16, 32, 64 bits), where the

decimal point is fixed and does not vary.

Page 11: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 11 Tanim Taher

7. SIGNAL FLOW GRAPHS

Previously, the transfer function for any digital filer was shown. The transfer function can

be used to draw the direct form system diagrams. The system diagram simply represents

the flow of data within a digital filter, and shows the operations carried out at each stage,

like multiplication by the coefficients, and summation of the results. The direct form

diagrams are very common, and I was introduced to them while taking the course, Digital

Signal Processing (ECE 437). Since my focus in this report is on material that I learnt

afterwards, we will not get into the details of the direct form diagrams. However, we shall

look into signal flow graphs, which are another way of representing the digital filter

pictorially.

Signal flow graphs use nodes to represent the adders and as signal sources. If an arrow is

pointing towards a node, it means that that node is adding that signal, or performing a

process on the signal (like multiplication). If the signal arrow is pointing away, then it

simply means that the result of the node’s operation is taken out in that data path.

Multiplication coefficients are represented simply as numbers on the data flow lines, and

the unit delay as z-1on the line. The following diagram makes this clear:

Page 12: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 12 Tanim Taher

Based on this principle, an N order FIR filter would look like the following if shown in

the form of a signal flow graph:

Reference: http://www.onesmartclick.com/engineering/digital-signal-processing.html

Page 13: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 13 Tanim Taher

8. DIRECT FORM TRANSPOSE FILTERS

Direct form filters are filters that realize the filter structure by using the same coefficients

directly from the transfer function into the filter. I learned about direct form 1 and direct

form 2 filters from ECE 437. However, during the course of my research, I came across

Direct Form Transpose filter structures. These structures are generated from the signal

flow graphs of their direct form counterparts. The procedure for converting from direct

form to direct form transpose is as follows:

1) Draw the signal flow graph for the original direct form structure.

2) Redraw the signal flow graph, but reverse the arrow directions of the data

flow paths. Physically this amounts to changing the nodes into adders and the

adders into nodes.

3) Now interchange the input and the output. Thus the new signal flow graph

represents the direct form transpose structure.

The next two system diagrams shows the difference between direct form II and direct

form II transpose filters. The small triangles are the coefficient multipliers. Source:

http://ccrma.stanford.edu/~jos/filters/Direct_Form_II.html

Direct Form II

Direct Form II Transposed

Page 14: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 14 Tanim Taher

9. LATTICE FILTERS

Lattice filters are very different from the direct form structures. They use entirely new

coefficients called K coefficients. The following diagram shows how a lattice IIR filter

would look like.

Source: http://www.mathworks.com/access/helpdesk/help/toolbox/signal/basics27.html

The K and V coefficients are a new set of coefficients that are generated from the IIR

transfer function. The K coefficients are generated from the denominator (pole)

coefficients, and they form the lattice structure. The V coefficients are generated from the

numerator (zero) coefficients and form part of the ladder structure.

The following example shows how to determine the K and V coefficients and also shows

the equations employed. In order to find the coefficients, an FIR filter A(z) is first created

by simply copying the denominator coefficients of the IIR filter. In the following

notations, sub-script m denotes the order of the filter:

αm(n) is the impulse response an ‘m’ order FIR filter at time nT. i.e. hm(n)=αm(n)

βm(n) = αm(m - n)

Page 15: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 15 Tanim Taher

Example: if A2(z) = 1 + 0.2z-1 + 0.3z-2

then B2(z) = 0.3 + 0.2z-1 + 1z-2

so α2(2) = β 2(0) = 0.3

And α2(1) = β 2(1) = 0.2

And α2(0) = β 2(2) = 1

First Step: Km = αm(m)

Since A2(z) = 1 + 0.2z-1 + 0.3z-2

Km = K2 = αm(m) = α2(2) = 0.3

Second: αm-1(n) = [αm(n) - Km βm(n)] / (1 – K2m)

α1(0) = α2(0) – K2 β2(0)] / (1 – K22)

= [1 – (.3 X .3)] / [1 - .32] = 1

α1(1) = α2(1) – K2 β2(1)] / (1 – K22)

= [.2 – (.3 X .2)] / [1 - .32] = .1538

So K1 = α1(1) = 0.1538

All the Am(n), for n = 0 to m, need to be determined so that the Bm(n) can be found for all

required filter orders. Bm(z) is also required later on to find the V coefficients.

Now that Bm(z) is known for all m orders, we can find the V coefficients. To do this, we

first create an FIR filter by copying the numerator of the transfer function of the original

IIR. Let us call this filter Cm(z). Then:

Let cm(n) be the numerator coefficients. It is the response of an ‘m’ order FIR

filter at time nT. i.e. hm(n)=cm(n)

First Step: vm = cm(m)

Second: Cm-1(n) = Cm(n) - vm βm(n)

Thus both the V and K coefficients can be found in this way.

Page 16: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 16 Tanim Taher

Now the question is why use lattice filters. It is indeed quite a lot of work to determine

the K and V coefficients initially. However, during processing, a bigger problem occurs

as is evident from the system diagram shown earlier. The problem is that a large number

of extra multiplication and summation operations need to be carried out in lattice filters in

comparison to their direct form counterparts. So there needs to be a good reason for

choosing lattice filters. The answer is that the output of a lattice filter is less sensitive to

the parameterization of the filter coefficients. This is specially relevant to stability, as

otherwise slight changes in the coefficients during parameterization can make the entire

filter system unstable. The following two diagrams explain this concept. Source of

diagrams: homes.esat.kuleuven.ac.be/~moonen/

This figure shows all the possible

pole locations for any IIR filter

where the coefficient numbers are

represented in 5 bits. Note due to

areas of concentration, generally,

slight changes in coefficients equal

a significant change in the filter’s

characteristics.

This figure shows all the possible

pole locations for any IIR lattice

filter where the K coefficient

numbers are represented in 5 bits.

Note due to uniform concentration,

slight changes in coefficients here

do not affect much the filter’s

characteristics.

-1.5 -1 -0.5 0 0.5 1 1.5-1.5

-1

-0.5

0

0.5

1

1.5

-1.5 -1 -0.5 0 0.5 1 1.5-1.5

-1

-0.5

0

0.5

1

1.5

Page 17: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 17 Tanim Taher

10a. RESULTS: Simulation Program Overview

In an effort to explore the quantization effects in digital filters and to obtain a first-hand

experience in programming digital filters in a computer, I developed a Digital Filter

Simulator and Analysis Tool. I used Matlab to develop the software, and used Matlab’s

Graphics User Interface Design Environment (GUIDE) to make the user interface. Most

of my research time was spent on this software development, and in my opinion the

investment was worthwhile. The software lets the user easily change the quantization

parameters, and at the click of a button change the structure of a digital filter. The output

graphs are very useful in analyzing the performance of the filters in response to

quantization parameter changes.

The software has to GUIDE forms/menus. The first form is called the “Waveform

Generator” and it is used to generate sample data to be fed into the digital filter. The

advantage of this form is that virtually any periodic waveform can be used as input

provided the fourier series coefficients are known. This is also very helpful to the

electrical engineering student for two reasons:

a) He/she can observe firsthand how the fourier series can be used to generate any

waveform of choice, and hence understand the power of Fourier series.

b) He/she can also observe the effect of aliasing which occurs significantly if the

sampling rate is below the Nyquist frequency.

The other form is the “Digital Filter” form. The user inputs the coefficients of a digital

filter (FIR or IIR) from the transfer function coefficients. The user also chooses the filter

structure to be used. The quantization parameters like word length, fixed-point/floating

point can be changed at will. Then at the click of a button, the software simulates the

digital filter. There are two outputs that can be produced by the program:

a) Data output: Using the data-set of the waveform generated by the “Waveform

Generator”, an output data-set is created and displayed by digitally filtering the

Page 18: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 18 Tanim Taher

data input. Thus this represents the output of the digital filter. This means that the

user can know how the output from the filter would be distorted or changed as the

word length or other quantization parameters are changed. This is sometimes

more useful than the frequency response, as it shows how easily the output can

saturate even though the system may be stable.

b) Frequency Response: The user can see the magnitude and phase responses of the

digital filter.

Page 19: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 19 Tanim Taher

10b. RESULTS: Software Performance

In the following, all the waveforms were compared to their Matlab DSP tool

counterparts. The results in each case match with each other. As per the discussion in the

previous section, the following screenshot shows the “Waveform Generator”.

The entry fields on the left let the user input the fourier series fundamental frequency (1st

harmonic frequency) and the coefficients (amplitudes) for various harmonics. The

sampling frequency is important as it determines the aliasing effects. To demonstrate the

aliasing effect, the sampling frequency was lowered. The resulted distorted input

waveform is shown in the next screenshot.

Page 20: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 20 Tanim Taher

The entry fields on the right side let the user determine the length of the data-set by

specifying the start and stop times for the input waveform. The “Total Window Points”

field is used to zoom in or zoom out, and it refers to the number of data points to be

plotted on screen.

This screenshot

is that of the

“Digital Filter”

form.

Page 21: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 21 Tanim Taher

The quantization options lets the user to select the word type (fixed point, single or

double floating points), and the word length for the fixed point type. The output shown is

that produced by the digital filter with input being the waveform shown in the

“Waveform Generator” shown before. The digital filter is specified by the ‘a’ and ‘b’

coefficients that the user can input. The filter structure is chosen by selecting any bullet

from the “Implementation Type” box area. The most important thing to note is that as

the quantization options are changed, the actual filter coefficients used by the digital

filter changes, and these are displayed in the two list boxes at the bottom of the form (A

Coefficients, B coefficients).

The following screenshot shows the magnitude phase response which is the other output.

Page 22: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 22 Tanim Taher

Note, the aliasing effect can be observed here also, by lowering the ‘Fs(Hz)’ entry-field

value. This is shown in the next screenshot:

The third output waveform is for the phase response:

Page 23: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 23 Tanim Taher

Also the lattice coefficients generated by the software are displayed if the implementation

type is Lattice.

Finally, the poles of any filter system can be plotted by clicking “Plot system poles”

button. For a filter, the poles are shown in the next figure. Red color indicates instability.

Page 24: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 24 Tanim Taher

So we see that the user is given many options to play around with the digital filter and the

input waveform. The results are good at experimentally demonstrating many principles of

digital signal processing and quantization effects to the user.

Page 25: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 25 Tanim Taher

10c. RESULTS: Software Algorithm Description

1) The Waveform Generator uses the following algorithm to generate the input

waveform:

Time Period, T = 1 / Sampling Frequency

a. For sample n, input x(n) = 0

b. T0 = Start time

c. Start with harmonic number h = 1

d. For harmonic number h,

x(n) = [h’th fourier Coeff] * Sin (h * fundamental frequency * n * T + T0) + x(n)

e. Increment h by 1

f. If h <= highest harmonic number, repeat steps c to f.

g. Increment n

h. If n not last sample, then repeat steps c to h. The last sample count is

determined from the start and stop times, and the time period T.

2) Once the quantization options are chosen, the program creates a fixed point data object

type is created. Then the program uses this object type for all arithmetic, if the word type

is fixed point. Otherwise, it uses single or double floating point objects (as specified by

the user) for all calculations. Hence during filtering, the simulated digital filter is fully

exposed to quantization effects.

3) Direct Form 1 Filter Implementation: The software uses two separate loops to perform

the two separate summation operations of the IIR direct form 1 implementation equation

shown below:

Page 26: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 26 Tanim Taher

4) Direct Form 2 Filter Implementation: The software uses two separate loops to perform

this filter function. Each of the loops perform one of the summation functions as

specified by the IIR direct form 2 implementation equations shown below:

5) Generating Lattice Filter Coefficients: The software uses several loops to generate the

lattice coefficients recursively using the equations given before in the Lattice Filters

sections. While I was able to successfully generate the lattice coefficients, I faced

constant difficulty recreating the filter model for the lattice structure, and never was able

to successfully implement it. Hence, I used the lattice coefficient vectors generated by my

algorithm, with Matlab’s built in lattice filter function (keyword latcfilt()) for the

filtering.

6) In order to generate the magnitude response, the software uses complex arithmetic.

This algorithm uses the quantized coefficients, and replaces ‘z’ within the filter’s transfer

function (given before) by

z = e-jwt

where, w is the sweep frequency being used. Then the value of H(z) , i.e. H(e-jwt) is

calculated. This complex value’s magnitude represents the magnitude response, and its

phase represents the phase response.

Page 27: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 27 Tanim Taher

10d. RESULTS: Software Applicability

This was briefly mentioned before. Basically, the software is a powerful learning tool. I

certainly learned a lot as the programmer. By simulating a lot of filters using the

software, I was also able to learn and see the quantization effects, and to classify how

different filter structures hold up to quantization effects.

For an electrical engineering student, the software can help for the following:

Learning tools for digital signal processing – aliasing, fourier series, filter

structures, stability, magnitude and phase response, etc.

Students can use and modify this code to learn about:

MATLAB

Simulation

Filter structure

Students can test Quantization effects First Hand. By examining code, then can

appreciate how Quantization errors build up.

I would like to allow the source to be used as freeware, that can be modified by students

and users. The condition is that the comments with my ID information are not modified.

Page 28: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 28 Tanim Taher

10e. RESULTS: Simulation Results

From my software, I was able to see how drastic changes occur when the word length is

increased by even a few bits. The effects of moving from small word fixed point to single

floating point, and then to double floating point were visible. From the simulations, it

seems clear that as long as the word length is adequate to hold the maximum and

minimum input and output values, word format did not matter. By playing around with

the program, I was able to learn more about the general quantization effects.

Anyways, I also carried out several simulations in MATLAB’s DSP tools. Some output

waveforms are shown as follows. LP is low pass, HP is high pass. The conditions and

constraints used are mentioned too:

1100kkHHzz LLoowwppaassss,, OOrrddeerr 2255 FFIIRR,, DDoouubbllee,, FFllooaattiinngg PPooiinntt

8 Bit, Fixed Point

Page 29: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 29 Tanim Taher

1100kkHHzz LLoowwppaassss,, OOrrddeerr 1100 FFIIRR,, DDoouubbllee,, FFllooaattiinngg PPooiinntt

8 Bit, Fixed Point

Page 30: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 30 Tanim Taher

1100kkHHzz LLPP,, OOrrddeerr 44 CChheebbyysshheevv IIIIRR,, DDoouubbllee,, FFllooaattiinngg PPooiinntt,, DDiirreecctt FFoorrmm11

8 Bit, Fixed Point, DF1

Page 31: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 31 Tanim Taher

1100kkHHzz HHPP,,OOrrddeerr 1133 CChheebbyysshheevv IIIIRR,,66 BBiitt,, FFiixxeedd PPooiinntt,, LLaattttiiccee

8 Bit, Fixed Point, Lattice

Page 32: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 32 Tanim Taher

11. INTERPRETATION:

A lot was learned about quantization effects from the simulations. Listed below are the

interpretation of the simulation results:

Lattice Structure is most stable.

Direct Form 1 and Direct Form 2 can give issue with stability.

Direct Form 1 and Direct Form2 sensitive to slight parameterization changes.

Cascade and Parallel structures (done in MATLAB) more stable than Direct Form

1 and Direct Form 2. This is due to the fact that the lower order structural units

used in the cascade and parallel forms have lesser feedback continuation (only

one level of feedback for second order units).

The total number of coefficients in the IIR and FIR filter is the main factor that

determines the filter performance. That is the order is a very important factor

affecting filter performance.

As long as the word length is adequate to hold the maximum and minimum input

and output values, word format has little effect on performance. However, for

input and output values with large ranges, the floating point data formats were

decidedly better than the fixed point ones. The fixed point outputs often saturated.

Even though lattice structure is more stable, the extra processing required (more

multiplication and addition) cannot be really justified. This is because today,

having large word lengths an inexpensive feat, and with large words, the direct

form filters give performance close to that of lattice filters when parameterization

effects are considered.

Page 33: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 33 Tanim Taher

12. COCNCLUSION AND FUTURE WORK

I must repeat again how valuable a learning experience this project was to me. On the one

hand, I learnt how to use MATLAB, GUIDE, improved my simulation program

development skills – skills which will come in handy even in fields outside DSP. In the

other, I learned a lot more about digital signal processing and processors. I feel confident

now of my knowledge in this field, and many of the concepts that were not very clear to

me before are now at the back of my head! I also gained experience in independent study.

As a person, I feel much more confident – something which came due to the multiple

presentations that I had to give during the course. For all this, I would again like to give

special thanks to Dr. Oruklu and Dr. Saniiee.

However much I learned and worked, there is always a lot of learning that can always be

done. Directly related to this project, I believe that this project has the following future

scope:

Further development of Software

Cascade Realization

Parallel Realization

Lattice Filter implementation

Transpose direct forms: although it was my goal to add this functionality, I

did not get any time to work on this.

Generating the coefficients automatically using Chebyshev’s/Butterworth

analog filter coefficients, followed by Bilinear transformation.

Examine quantization effects on Multi-rate and adaptive filters.

Page 34: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 34 Tanim Taher

GENERAL REFERENCES: The Scientist and Engineer's Guide to Digital Signal Processing (2nd Edition)

DSP (Proakis, Manolakis)

http://www.onesmartclick.com/engineering/digital-signal-processing.html

(Excellent Resources here)

Other interesting finds:

DSP Lab manual detailing chip programming and algorithms: Rutgers

University, ADSP-2181 Experiments, Sophocles J. Orfanidis

Page 35: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 35 Tanim Taher

13. APPENDIX: Program Listing

The following is the listing for the “Waveform Generator” form. %Programmer Name: Tanim Taher %e-mail: [email protected] %Done as coursework for ECE 491, undergraduate research work %Fall 2005 function varargout = wavegen(varargin) % WAVEGEN M-file for wavegen.fig % WAVEGEN, by itself, creates a new WAVEGEN or raises the existing % singleton*. % % H = WAVEGEN returns the handle to a new WAVEGEN or the handle to % the existing singleton*. % % WAVEGEN('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in WAVEGEN.M with the given input arguments. % % WAVEGEN('Property','Value',...) creates a new WAVEGEN or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before wavegen_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to wavegen_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Copyright 2002-2003 The MathWorks, Inc. % Edit the above text to modify the response to help wavegen % Last Modified by GUIDE v2.5 07-Dec-2005 16:00:42 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @wavegen_OpeningFcn, ... 'gui_OutputFcn', @wavegen_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1})

Page 36: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 36 Tanim Taher

gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before wavegen is made visible. function wavegen_OpeningFcn(hObject, eventdata, handles, varargin) global h_max ampl h_no Amp %intialize harmonic values h_max = 1; ampl = 1; Amp(1) = 1 h_no = 1; % Choose default command line output for wavegen handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes wavegen wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = wavegen_OutputFcn(hObject, eventdata, handles) % Get default command line output from handles structure varargout{1} = handles.output; function edit4_Callback(hObject, eventdata, handles) % --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles) % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function edit5_Callback(hObject, eventdata, handles) global h_max ampl h_no Amp h_no = str2double(get(handles.edit5,'String')); % Get entered harmonic no value

Page 37: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 37 Tanim Taher

if h_no <= h_max set(handles.edit6,'String',Amp(h_no)); else set(handles.edit6,'String',0); end % --- Executes during object creation, after setting all properties. function edit5_CreateFcn(hObject, eventdata, handles) % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function edit6_Callback(hObject, eventdata, handles) global h_max ampl h_no Amp %redundant % --- Executes during object creation, after setting all properties. function edit6_CreateFcn(hObject, eventdata, handles) % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) global h_max ampl h_no Amp ampl = str2double(get(handles.edit6,'String')); % Get entered amplitude value h_no = str2double(get(handles.edit5,'String')); % Get entered harmonic no value Amp(h_no)=ampl; set(handles.edit5,'string',h_no+1); set(handles.edit6,'string',0); if h_no > h_max h_max = h_no set(handles.maxH,'string',h_max) end; %Find Nyquist frequency and set it as default value min_samp = (2 * h_max * str2double(get(handles.edit4,'String'))) / (2 * pi); w = str2double(get(handles.edit7,'String')); if w < min_samp set(handles.edit7,'string',min_samp); end

Page 38: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 38 Tanim Taher

% --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) global h_max ampl h_no Amp %Global makes these variables accessible throughout the MATLAB workspace clear dpts xs global st_time end_time pts tot_pts xs dpts T% for slider T = 1 / str2double(get(handles.edit7,'String')); %Calculate the period w = str2double(get(handles.edit4,'String')); %fundamental frequency st_time = str2double(get(handles.edit9,'String')); %Get start and stop times end_time = str2double(get(handles.edit10,'String')); tot_pts = ((end_time - st_time) / (T * 1000)) %Calculate total number of points for m = 0:tot_pts n = int16(m+1); dpts(n)=0; xs(n) = m * T + (st_time / 1000); %nT sample for i = 1:h_max dpts(n) = dpts(n) + Amp(i)*sin(w*i*xs(n)); %Generate the waveform end; end; pts = str2num(get(handles.edit8,'String')); % Adjust Slider so that the plot is displayed properly if (tot_pts > pts) frac = pts / (tot_pts - pts) else frac = 500 end Min = st_time; Max = st_time + T * 1000 * (tot_pts-pts); pcnt = (pts/tot_pts) * .05; set(handles.slider1,'Min',Min); %Set values and limits for the axes set(handles.slider1,'Max',Max); set(handles.slider1,'value',st_time); set(handles.slider1,'SliderStep',[pcnt frac]); xst = (get(handles.slider1,'value')) / 1000; xend = xst + pts * T %determine density of pts in plot plot(xs(1:tot_pts),dpts(1:tot_pts),'--.b') axis ([xst xend 0 10]) axis 'auto_y' function edit7_Callback(hObject, eventdata, handles) % --- Executes during object creation, after setting all properties. function edit7_CreateFcn(hObject, eventdata, handles) % Hint: edit controls usually have a white background on Windows.

Page 39: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 39 Tanim Taher

% See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on slider movement. function slider1_Callback(hObject, eventdata, handles) global st_time end_time pts tot_pts xstp dpts T xst = (get(handles.slider1,'value')) / 1000; xend = xst + T * pts %determine density of pts in plot axis ([xst xend 0 10]) %Redraw waveform as specified by slider position axis 'auto_y' % --- Executes during object creation, after setting all properties. function slider1_CreateFcn(hObject, eventdata, handles) % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end function edit8_Callback(hObject, eventdata, handles) % --- Executes during object creation, after setting all properties. function edit8_CreateFcn(hObject, eventdata, handles) % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function edit9_Callback(hObject, eventdata, handles) % --- Executes during object creation, after setting all properties. function edit9_CreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function edit10_Callback(hObject, eventdata, handles) % --- Executes during object creation, after setting all properties. function edit10_CreateFcn(hObject, eventdata, handles) % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER.

Page 40: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 40 Tanim Taher

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in clearH. function clearH_Callback(hObject, eventdata, handles) global h_max ampl h_no Amp global st_time end_time pts tot_pts xs dpts T% for slider pts = 0; %Make all the fourier coefficients = 0, clear the plot tot_pts = 0; clear dpts; clear xs; h_max = 1; clear Amp; h_no = 1; set(handles.edit5,'string',1); set(handles.edit6,'string',1); set(handles.maxH,'string',1); Amp(1) = 1; plot(0,0); THE FOLLOWING IS THE CODE FOR THE DIGITAL FILTER SIMULATOR %Programmer Name: Tanim Taher %e-mail: [email protected] %Done as coursework for ECE 491, undergraduate research work %Fall 2005 function varargout = Filter_Coefficients_Parameterization(varargin) % FILTER_COEFFICIENTS_PARAMETERIZATION M-file for Filter_Coefficients_Parameterization.fig % FILTER_COEFFICIENTS_PARAMETERIZATION, by itself, creates a new FILTER_COEFFICIENTS_PARAMETERIZATION or raises the existing % singleton*. % % H = FILTER_COEFFICIENTS_PARAMETERIZATION returns the handle to a new FILTER_COEFFICIENTS_PARAMETERIZATION or the handle to % the existing singleton*. % % FILTER_COEFFICIENTS_PARAMETERIZATION('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in FILTER_COEFFICIENTS_PARAMETERIZATION.M with the given input arguments. % % FILTER_COEFFICIENTS_PARAMETERIZATION('Property','Value',...) creates a new FILTER_COEFFICIENTS_PARAMETERIZATION or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before Filter_Coefficients_Parameterization_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application

Page 41: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 41 Tanim Taher

% stop. All inputs are passed to Filter_Coefficients_Parameterization_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Copyright 2002-2003 The MathWorks, Inc. % Edit the above text to modify the response to help Filter_Coefficients_Parameterization % Last Modified by GUIDE v2.5 14-Dec-2005 19:24:45 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Filter_Coefficients_Parameterization_OpeningFcn, ... 'gui_OutputFcn', @Filter_Coefficients_Parameterization_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before Filter_Coefficients_Parameterization is made visible. function update_listbox(handles) %This functions displays the A and B coefficients global a_no a_max b_no b_max c_A c_B a_string b_string oCA oCB oCK oCV clear a_string b_string a2_string b2_string k_string v_string b2_string(1)={' '}; a2_string(1) = {'A0 = 1'}; for n=1:a_max %Show the a coeff tmp = {strcat('A',num2str(n),' = ',num2str(c_A(n)))}; a_string(n) = tmp; end for n=1:b_max %Show the b coeffs tmp = {strcat('B',num2str(n-1),' = ',num2str(c_B(n)))}; b_string(n) = tmp;

Page 42: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 42 Tanim Taher

end if get(handles.Latt, 'Value') == 0 set(handles.paramAK, 'Title','A Coefficients'); set(handles.paramBV, 'Title','B Coefficients'); for n=1:numel(oCA) tmp = {strcat('A',num2str(n),' = ',num2str(oCA(n)))}; a2_string(n+1) = tmp; end for n=1:numel(oCB) tmp = {strcat('B',num2str(n-1),' = ',num2str(oCB(n)))}; b2_string(n) = tmp; end set(handles.listAK,'String',a2_string); set(handles.listBV,'String',b2_string); else %Show the K and V coeffs if filter type is lattice set(handles.paramAK, 'Title','K Coefficients'); set(handles.paramBV, 'Title','V Coefficients'); for n=1:numel(oCK) tmp = {strcat('K',num2str(n),' = ',num2str(oCK(n)))}; k_string(n) = tmp; end for n=1:numel(oCV) tmp = {strcat('V',num2str(n-1),' = ',num2str(oCV(n)))}; v_string(n) = tmp; end set(handles.listAK,'String',k_string); set(handles.listBV,'String',v_string); end set(handles.a_list,'String',a_string); set(handles.b_list,'String',b_string); function Filter_Coefficients_Parameterization_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % varargin command line arguments to Filter_Coefficients_Parameterization (see VARARGIN) global a_no a_max b_no b_max c_A c_B PT WL FL oCA oCB %Initialize oCA=[]; oCB=[]; PT = 1; b_no = 1; b_max = 2; a_no = 1; a_max = 2; WL = 8; FL = 4; %c_B = [0.028 0.053 0.071 0.053 0.028] % c_A = [-2.026 2.148 -1.159 0.279] c_B = [0.5 .8125] %Initialize the A and B coefficients to any desired value c_A = [0.5 .3125] pushbutton4_Callback(0, 0, handles)

Page 43: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 43 Tanim Taher

% Choose default command line output for Filter_Coefficients_Parameterization handles.output = hObject; % Update handles structure guidata(hObject, handles); axes(handles.axes3); % UIWAIT makes Filter_Coefficients_Parameterization wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = Filter_Coefficients_Parameterization_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % Get default command line output from handles structure varargout{1} = handles.output; % -------------------------------------------------------------------- function FileMenu_Callback(hObject, eventdata, handles) % hObject handle to FileMenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function OpenMenuItem_Callback(hObject, eventdata, handles) % hObject handle to OpenMenuItem (see GCBO) file = uigetfile('*.fig'); if ~isequal(file, 0) open(file); end % -------------------------------------------------------------------- function PrintMenuItem_Callback(hObject, eventdata, handles) % hObject handle to PrintMenuItem (see GCBO) printdlg(handles.figure1) % -------------------------------------------------------------------- function CloseMenuItem_Callback(hObject, eventdata, handles) % hObject handle to CloseMenuItem (see GCBO) selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],... ['Close ' get(handles.figure1,'Name') '...'],... 'Yes','No','Yes'); if strcmp(selection,'No')

Page 44: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 44 Tanim Taher

return; end delete(handles.figure1) % --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1 % --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'bar(1:.5:10)', 'plot(membrane)', 'surf(peaks)'}); % --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes during object creation, after setting all properties. function listbox1_CreateFcn(hObject, eventdata, handles)

Page 45: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 45 Tanim Taher

% hObject handle to listbox1 (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes during object creation, after setting all properties. function listbox2_CreateFcn(hObject, eventdata, handles) % hObject handle to listbox2 (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function wordlength_Callback(hObject, eventdata, handles) % hObject handle to wordlength (see GCBO) global WL FL WL = str2num(get(hObject,'String')) FL = str2num(get(handles.frac,'String')) if WL <= FL WL = FL + 2; set(hObject,'String',WL) end % --- Executes during object creation, after setting all properties. function wordlength_CreateFcn(hObject, eventdata, handles) % hObject handle to wordlength (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end

Page 46: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 46 Tanim Taher

function frac_Callback(hObject, eventdata, handles) % hObject handle to frac (see GCBO) global WL FL WL = str2num(get(handles.wordlength,'String')) FL = str2num(get(handles.frac,'String')) if WL <= FL WL = FL + 2; set(handles.wordlength,'String',WL) end % --- Executes during object creation, after setting all properties. function frac_CreateFcn(hObject, eventdata, handles) % hObject handle to frac (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in togglebutton1. function togglebutton1_Callback(hObject, eventdata, handles) % Hint: get(hObject,'Value') returns toggle state of togglebutton1 global PT %Change the word type used for processing PT = PT + 1 % Sets the Float or fixed Point data types if PT > 3 PT = 1 end if PT == 1 set(handles.togglebutton1,'String','Fixed Point'); elseif PT == 2 set(handles.togglebutton1,'String','Single, Floating Point'); elseif PT == 3 set(handles.togglebutton1,'String','Double, Floating Point'); end if PT == 1 set(handles.frac,'Enable','on'); set(handles.wordlength,'Enable','on'); else set(handles.frac,'Enable','off'); set(handles.wordlength,'Enable','off'); end function f_start_Callback(hObject, eventdata, handles) % hObject handle to f_start (see GCBO) % --- Executes during object creation, after setting all properties. function f_start_CreateFcn(hObject, eventdata, handles) % hObject handle to f_start (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

Page 47: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 47 Tanim Taher

set(hObject,'BackgroundColor','white'); end function f_end_Callback(hObject, eventdata, handles) % hObject handle to f_end (see GCBO) % --- Executes during object creation, after setting all properties. function f_end_CreateFcn(hObject, eventdata, handles) % hObject handle to f_end (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) sweep_freq(handles,'magnitude'); % hObject handle to pushbutton6 (see GCBO) % --- Executes on button press in pushbutton7. function pushbutton7_Callback(hObject, eventdata, handles) sweep_freq(handles,'phase'); % hObject handle to pushbutton7 (see GCBO) %Calculates the lattice coefficients function calc_latt(handles) global oCK oCV a_max b_max c_A c_B K_C V_C Km Am K_C = []; V_C = []; oCK = []; oCV = []; % X_C are the ones used for calculations, oCX are used for printing Km=[]; Am=[]; Bm=[]; %My Code for determining K and V Am(a_max,1) = 1; for i=1:a_max Am(a_max,i+1) = c_A(i); end for m=a_max:-1:1 %From m'th order to 1st order Am(m,1) = 1; %Am(0)=1 Km(m) = Am(m,m+1); %Km=Am(m) if m>1 %If there is any lower order coefficients to find for n=1:(m-1) %for Am-1(1) to Am-1(m-1) Am(m-1,n+1)=(Am(m,n+1)-Km(m)*Am(m,m-n+1))/(1-(Km(m)^2)); %The Equation for finding lower order A coeff's end end end %This finds all values of Bm(k):

Page 48: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 48 Tanim Taher

mx_len = 1; for mm = 1:a_max mx_len=mx_len+1; for mmi = 1:mx_len Bm(mm,mmi) = Am(mm,mx_len-mmi+1); end end Cm=[]; %Create a square matrix and copy the numerator coefficents to last row. for j=1:(a_max+1) %Calculate the ladder (V) coefficients if j>b_max Cm(a_max+1,j)=0; else Cm(a_max+1,j) = c_B(j); %Cm_max(z) end end if b_max>a_max for j=1:(b_max) if j>a_max Km(j)=0; end Cm(b_max+1,j) = c_B(j); %Cm_max(z) end end for m=(a_max+1):-1:1 %From N'th order to 1st order y Vm(m) = Cm(m,m); %Vm=Cm(m) if m>1 %Check if there is any lower order C(z) coefficients to find for n=1:(m-1) %for calculating Cm-1(1) to Cm-1(m-1) Cm(m-1,n)=Cm(m,n) - Vm(m)*Bm(m-1,n); %The Equation for finding lower order C coeff's end end end oCK = []; oCK = Km; oCV = []; oCV = Vm; % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) %This parameterizes all the coefficients global a_no a_max b_no b_max c_A c_B PT WL FL A_C B_C F T1 A_CF B_CF oCA oCB oCV oCK K_C V_C oCB = []; oCA = []; A_C = []; B_C = []; t=0; for i = a_max:-1:1 %get vectors for the coefficients if c_A(i) == 0 && t==0 a_max = a_max-1; else

Page 49: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 49 Tanim Taher

t=1; end end t=0; for i = b_max:-1:1 if c_B(i) == 0 && t==0 b_max = b_max-1; else t=1; end end c_A = c_A(1:a_max) c_B = c_B(1:b_max) global pts tot_pts xs dpts T calc_latt(handles); %Find K and V coefficients if PT == 1 %If chosen data format is Fixed Point F = fimath('OverflowMode','saturate','RoundMode','convergent') T1 = numerictype('WordLength',WL,'FractionLength',FL) for i = 1:a_max A_C(i) = fi(c_A(i),T1,F); oCA(i) = double(A_C(i)); end for i = 1:b_max B_C(i) = fi(c_B(i),T1,F); oCB(i) = double(B_C(i)); end for i = 1:numel(oCK) K_C(i) = fi(oCK(i),T1,F); oCK(i) = double(K_C(i)); end for i = 1:numel(oCV) V_C(i) = fi(oCV(i),T1,F); oCV(i) = double(V_C(i)); end elseif PT == 2 %If chosen data format is Single Floating Point for i = 1:a_max A_C(i) = single(c_A(i)); oCA(i) = double(A_C(i)); end for i = 1:b_max B_C(i) = single(c_B(i)); oCB(i) = double(B_C(i)); end for i = 1:numel(oCK) K_C(i) = single(oCK(i)); oCK(i) = double(K_C(i)); end for i = 1:numel(oCV) V_C(i) = single(oCV(i)); oCV(i) = double(V_C(i)); end elseif PT == 3 for i = 1:a_max A_C(i) = double(c_A(i)); oCA(i) = A_C(i); end

Page 50: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 50 Tanim Taher

for i = 1:b_max B_C(i) = double(c_B(i)); oCB(i) = B_C(i); end for i = 1:numel(oCK) K_C(i) = oCK(i); end for i = 1:numel(oCV) V_C(i) = oCV(i); end end update_listbox(handles) % hObject handle to pushbutton4 (see GCBO) % --- Executes on button press in del_coeff. function del_coeff_Callback(hObject, eventdata, handles) clear c_A c_B global a_no b_no a_max b_max c_A c_B b_no = 1; b_max = 1; a_no = 1; a_max = 1; c_A(1) = 0; c_B(1) = 0; set(handles.ac_no,'String',1) %set(handles.ac,'String',0) set(handles.bc_no,'String',0) %set(handles.bc,'String',0) update_listbox(handles); % plot(xs(1:tot_pts),dpts(1:tot_pts)) function ac_no_Callback(hObject, eventdata, handles) function ac_Callback(hObject, eventdata, handles) %Store a coefficient values global a_no a_max b_no b_max c_A c_B if a_no > 0 c_A(a_no) = str2double(get(hObject,'String')); a_no = a_no + 1; set(handles.ac_no,'String',a_no); if a_no <= a_max set(handles.ac,'String',c_A(a_no)); else set(handles.ac,'String',0); a_max = a_no; c_A(a_no) = 0; end end update_listbox(handles) % hObject handle to ac (see GCBO)

Page 51: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 51 Tanim Taher

function bc_no_Callback(hObject, eventdata, handles) % hObject handle to bc_no (see GCBO) function bc_Callback(hObject, eventdata, handles) %Store B coefficient values from guide global a_no a_max b_no b_max c_A c_B if b_no > 0 c_B(b_no) = str2double(get(hObject,'String')); b_no = b_no + 1; if b_no < b_max set(handles.bc_no,'String',b_no-1); set(handles.bc,'String',c_B(b_no)); else set(handles.bc,'String',0); b_max = b_no; c_B(b_no) = 0; end end update_listbox(handles) % --- Executes on selection change in a_list. function a_list_Callback(hObject, eventdata, handles) global a_max a_no c_A; a_no = get(hObject,'Value'); set(handles.ac_no,'String',a_no); set(handles.ac,'String',c_A(a_no)); % --- Executes on selection change in b_list. function b_list_Callback(hObject, eventdata, handles) global b_max b_no c_B; b_no = get(hObject,'Value'); set(handles.bc_no,'String',b_no-1); set(handles.bc,'String',c_B(b_no)); function param_obj(handles) global PT FL if strcmp(get(handles.togglebutton1,'String'),'Fixed Point') PT = 1; else PT = 2; end function find_lattice_op(handles) %Failed function that tried to implement lattice structure %Calculate the lattice output global oCK oCV PT FL T1 dpts tot_pts F otpt xs oCK oCV [oCK,oCV]=tf2latc(c_B,[1, c_A]); %Fm(n)=Fo(n), G(1,n)=Go(1,n)

Page 52: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 52 Tanim Taher

if PT == 1 otpt = fi([],T1,F); inpt = fi([],T1,F); elseif PT==2 otpt = single([]); inpt = single([]); w = single([]) elseif PT==3 otpt = double([]) inpt = double([]) w = double([]) end for i = 1:tot_pts if PT == 1 xn = fi(dpts(i),T1,F); elseif PT==2 xn = single(dpts(i)); elseif PT==3 xn = double(dpts(i)); end Fm(numel(oCK)+1)=xn; for jj=1:numel(oCK) if i < 2 G(jj,2)=0; else G(jj,2)=G(jj,1); end end for j=(numel(oCK)+1):-1:2 Fm(j-1) = Fm(j) - oCK(j-1) * G(j-1,2); G(j,1)=oCK(j-1) * Fm(j-1) + G(j-1,2); end G(1,1)=Fm(1); for n=1:numel(oCV) otpt(i)=0; otpt(i) = otpt(i) + G(n,1) * oCV(n); end end otpt = latc plot(xs(1:tot_pts), otpt(1:tot_pts)); function find_FP_output(handles) %Calculate the output waveform global a_no a_max b_no b_max c_A c_B PT WL FL A_C B_C F T1 global pts tot_pts xs dpts T % for slider otpt = []; inpt = []; % Set the data types if PT == 1 %Quantize the input values otpt = fi([],T1,F); inpt = fi([],T1,F); w = fi([],T1,F); elseif PT==2 otpt = single([]) inpt = single([])

Page 53: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 53 Tanim Taher

w = single([]) elseif PT==3 otpt = double([]) inpt = double([]) w = double([]) end %F = fimath('OverflowMode','saturate','RoundMode','convergent') %T1 = numerictype('WordLength',WL,'FractionLength',FL) if get(handles.Latt,'Value')==0 for i = 1:tot_pts if get(handles.DF1,'Value')==1 % Direct Form1 structure if PT == 1 % Fixed Point Processing inpt(i)= fi(dpts(i),T1,F); otpt(i) = fi(0,T1,F); elseif PT == 2 % Single Floating Point Processing inpt(i)= single(dpts(i)); otpt(i) = single(0); elseif PT == 3 %Double Floating Point Processing inpt(i)= double(dpts(i)); otpt(i) = double(0); end otpt(i) = inpt(i) * B_C(1); for bi = 2:b_max if i > (bi-1) if PT == 1 % Fixed Point Processing otpt(i) = fi(otpt(i) + B_C(bi) * inpt(i-bi+1),T1,F); elseif PT == 2 % Single Floating Point Processing otpt(i) = single(otpt(i) + B_C(bi) * inpt(i-bi+1)); elseif PT == 3 %Double Floating Point Processing otpt(i) = double(otpt(i) + B_C(bi) * inpt(i-bi+1)); end %otpt(i) = otpt(i) + B_C(bi) * inpt(i-bi+1); end end for ai = 1:a_max if i > (ai) if PT == 1 % Fixed Point Processing otpt(i) = fi(otpt(i) - A_C(ai) * otpt(i-ai),T1,F); elseif PT == 2 % Single Floating Point Processing otpt(i) = single(otpt(i) - A_C(ai) * otpt(i-ai)); elseif PT == 3 %Double Floating Point Processing otpt(i) = double(otpt(i) - A_C(ai) * otpt(i-ai)); end %otpt(i) = otpt(i) - A_C(ai) * otpt(i-ai); end end elseif get(handles.DF2,'Value')==1 % Direct Form2 Structure if PT == 1 % Fixed Point Processing w(i) = fi(dpts(i),T1,F); otpt(i) = fi(0,T1,F); elseif PT == 2 % Single Floating Point Processing

Page 54: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 54 Tanim Taher

w(i) = single(dpts(i)); otpt(i) = single(0); elseif PT == 3 %Double Floating Point Processing w(i) = double(dpts(i)); otpt(i) = double(0); end otpt(i) = 0; for ki = 1:a_max if i > ki if PT == 1 % Fixed Point Processing w(i) = fi(w(i) - A_C(ki) * w(i-ki),T1,F); elseif PT == 2 % Single Floating Point Processing w(i) = single(w(i) - A_C(ki) * w(i-ki)); elseif PT == 3 %Double Floating Point Processing w(i) = double(w(i) - A_C(ki) * w(i-ki)); end end end for ki = 1:b_max if i >= ki if PT == 1 % Fixed Point Processing otpt(i) = fi(otpt(i) + B_C(ki) * w(i-ki + 1),T1,F); elseif PT == 2 % Single Floating Point Processing otpt(i) = single(otpt(i) + B_C(ki) * w(i-ki + 1)); elseif PT == 3 %Double Floating Point Processing otpt(i) = double(otpt(i) + B_C(ki) * w(i-ki + 1)); end end end end fin_OP(i) = double(otpt(i)); end plot(xs(1:tot_pts), fin_OP(1:tot_pts)) else % Use lattice coefficients if get(handles.latttype,'Value')==0 global k v % Lattice coefficient vector tmpA = [1]; for aaai = 1: a_max tmpA(aaai+1) = A_C(aaai); end for i = 1: tot_pts if PT == 1 inpt(i) = fi(dpts(i),T1,F); elseif PT == 2 inpt(i) = single(dpts(i)); elseif PT == 3 inpt(i) = double(dpts(i)); end inpt2(i)=double(inpt(i)); end

Page 55: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 55 Tanim Taher

[k,v] = tf2latc(B_C,tmpA); [otpt,tmp_g] = latcfilt(k,v,inpt2); otpt = filter(B_C,tmpA, inpt2); for i = 1: tot_pts if PT == 1 otpt2(i) = fi(otpt(i),T1,F); elseif PT == 2 otpt2(i) = single(otpt(i)); else otpt2(i) = double(otpt(i)); end otpt2(i)=double(otpt(i)); end plot(xs(1:tot_pts), otpt2(1:tot_pts)) else find_lattice_op(handles); end end otpt = []; inpt = []; w = []; tmp_g = []; % --- Executes on button press in show_Output. function show_Output_Callback(hObject, eventdata, handles) pushbutton4_Callback(0, 0, handles) find_FP_output(handles) function sweep_freq(handles, plot_type) %This calculates the frequency response pushbutton4_Callback(0, 0, handles) global a_no a_max b_no b_max oCA oCB PT WL FL A_C B_C F T1 c_A = oCA; c_B = oCB; %only in this function due to later change T = 1 / str2num(get(handles.samplerate,'String')); freq = str2num(get(handles.f_start,'String')); fend = str2num(get(handles.f_end,'String')); f_step = (fend - freq) / 60; fn = 0; for fn = 1:60 frequ(fn) = freq; comp_sum_n = complex(0,0); w = 2 * pi * freq; %Vary the frequency for bk = 1:b_max %Calculate the complex value of numerator of transfer function with z=e^jw comp_sum_n = comp_sum_n + c_B(bk) * exp(complex(0,-(bk-1) * w * T));

Page 56: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 56 Tanim Taher

end comp_sum_d = complex(1,0); for ak = 1:a_max %Calculate the complex value of denominator of transfer function with z=e^jw comp_sum_d = comp_sum_d + c_A(ak) * exp(complex(0,-ak * w * T)); end mag(fn) = abs(comp_sum_n / comp_sum_d); %Find the magnitude phs(fn) = angle(comp_sum_n / comp_sum_d) * (180 / pi); %Find the phase freq = freq + f_step; end %Plot the frequency response if strcmp(plot_type, 'magnitude') && get(handles.dBplot,'Value') == 0 plot(frequ(1:fn),mag(1:fn),'--.b'); elseif strcmp(plot_type, 'magnitude') && get(handles.dBplot,'Value') == 1 plot(frequ(1:fn),20 * log(mag(1:fn)),'--.b'); else plot(frequ(1:fn),phs(1:fn),'--*r'); end % --- Executes when user attempts to close figure1. function figure1_CloseRequestFcn(hObject, eventdata, handles) delete(hObject) % Hint: delete(hObject) closes the figure % --- Executes on button press in dBplot. function dBplot_Callback(hObject, eventdata, handles) % Hint: get(hObject,'Value') returns toggle state of dBplot function samplerate_Callback(hObject, eventdata, handles) % hObject handle to samplerate (see GCBO) % --- Executes during object creation, after setting all properties. function samplerate_CreateFcn(hObject, eventdata, handles) % hObject handle to samplerate (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes during object creation, after setting all properties. function axes3_CreateFcn(hObject, eventdata, handles) % hObject handle to axes3 (see GCBO) % Hint: place code in OpeningFcn to populate axes3 % --- Executes on selection change in listAK. function listAK_Callback(hObject, eventdata, handles)

Page 57: ECE 491-153 Fall 05 Undergraduate Research

ECE 491 6/5/2007

DSP & Quantization 57 Tanim Taher

% hObject handle to listAK (see GCBO) % --- Executes during object creation, after setting all properties. function listAK_CreateFcn(hObject, eventdata, handles) % hObject handle to listAK (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on selection change in listBV. function listBV_Callback(hObject, eventdata, handles) % hObject handle to listBV (see GCBO) % --- Executes during object creation, after setting all properties. function listBV_CreateFcn(hObject, eventdata, handles) % hObject handle to listBV (see GCBO) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in poles. function poles_Callback(hObject, eventdata, handles) %This plots the poles of the system pushbutton4_Callback(0, 0, handles) global oCA oCB a_max A = [1 oCA]'; % random polynomial poles = roots(A); % system poles for i = 1:(a_max) if abs(poles(i))>=1 %For unstable pole use red color plot(poles(i),'*r'); else plot(poles,'+b'); %For stable pole use blue color end hold on; end axis ([-5 5 -2 2]); grid on; hold off; % --- Executes on button press in latttype. function latttype_Callback(hObject, eventdata, handles)