LAB4_02_16_2007

advertisement

ECE 3551 MICROCOMPUTER SYSTEMS 1

Lab 4—Learn to process audio data

OBJECTIVE:

Learn how to process audio data in ADSP-BF533 EZ-Kit Lite.

Learn how to design Infinite Impulse Response ( iir ) filters with MATLAB fdatool.

 Learn how to implement digital iir filter in DSP using:

32-bit floating point emulation

16-bit integer arithmetic

16-bit fractional arithmetic

Learn how to control the audio input/output.

Create the Project

1.

Before power on the board , make sure that switch SW9 pin1,pin2, pin3, pin4, pin5 and pin6 are turned on., which means all pins in SW9 must be on.

1 2 3 4 5 6

2.

Copy the project of the previous completed Lab #3 to your u-drive

“u:\ece3551\labs\lab4”.

Modify the Project

In this lab, you need to learn how to filter audio data and compare the original audio and the filtered audio. In this Lab exercise only stereo input 1 and stereo output 1 will be used.

Duplicating the input & controlling the output:

 Read the data from the input and save it’s copy: one is the original data and another will

be the filtered data.

PF8 & PF9 is used to control which data is sent to output:

In the beginning, LED4 & LED5 are off, and the original data is send to output. So what is heard is the original audio.

When PF8 is pressed:

The LED4 should turn on, and

The low-pass filtered data is send to output. What is heard therefore is filtered audio.

By pushing PF8 again, the LED4 should turn off, and the original data is send to output; what is heard is the original audio again.

When PF9 is pressed:

The LED5 should turn on, and

The high-pass filtered data is send to output. What is heard therefore is high-pass filtered audio.

By pushing PF9 again, the LED5 should turn off, and the original data is send to output; what is heard is the original audio again.

Designing IIR Filters:

MATLAB exports IIR filters in a direct-form II sections each being of second order. The general relationship of output and input given in Z transform is of the form:

H

Y

X

 

 

G k

K 

1

H k

G k

K 

1 b

0 k

1

 b a

1 k

1 k z z

1

1

 b a

2 k

2 k z z

2

2

Note that z

-1

denotes unit delay D operator and z

-2

, denotes 2 unit delays, i.e, D

2

. Y(z) is z-transform of the output, X(z) is z-transform of the input. The block diagram realization of a filter with K=2 sections is given below: x(n) d

1

(n) a

1

1

D

z -1 d

1

(n-1) b

1

1 d

2

(n) a

1

2

D

z -1 d

2

(n-1) b

1

2 y(n)

D

z -1 D

z -1 a

2

1 b

2

1 a

2

2 b

2

2 d

1

(n-2) d

2

(n-2)

Example of a fourth-order IIR filter realized by cascading of two bi-quads.

Implementation Algorithm

H

 b

0

1

 b a

1

1 z z

1

1

 b a

2

2 z z

2

2

Where d y

 

 

 x b

2

  d

 m

 a

2

2 d

 m

 b

1 d

2

 m

 a

1 d

1

 m b

0

 d

1

 

And m={0,1,2,…}

H(z) above is one bi-quad stage of the filter. The overall filter may be implemented with multiple bi-quad as presented in the figure above.

Use MATLAB to design 2 types of filters:

1. Low-Pass IIR Filter with following properties:

Response Type = Lowpass

Design Method = IIR, Chebyshev Type II

Filter Order (i.e., Number of Filter Coefficients): Check “Minimum Order” Radio

Button

Sampling Frequencey Fs = 48000 Hz

Pass-band Cutoff Frequency Fpass = 5000 Hz

Stop-band Cutoff Frequency Fstop = 8000 Hz

This Figure depicts default output of the MATLAB fdatool. Exporting the result into a C file following the procedure depicted below from the fdatool toolbar:

File->Export to C Header File o Select Single Precision float should give you the following result:

Low-Pass Filter

/*

* Filter Coefficients (C Source) generated by the Filter Design and Analysis Tool

*

* Generated by MATLAB(R) 6.5.1 and the Signal Processing Toolbox 6.1.

*

* Generated on: 16-Feb-2007 11:11:23

*

*/

/*

* Discrete-Time IIR Filter (real)

* -------------------------------

* Filter Structure : Direct-Form II, Second-Order Sections

* Filter Order : 4

* Stable : Yes

* Linear Phase : No

*/

/* General type conversion for MATLAB generated C-code */

#include "tmwtypes.h"

/*

* Expected path to tmwtypes.h

* C:\MATLAB6p5p1\extern\include\tmwtypes.h

*/

/*

* Warning - Filter coefficients were truncated to fit specified data type.

* The resulting response may not match generated theoretical response.

* Use the Filter Design & Analysis Tool to design accurate fixed-point

* filter coefficients.

*/

#define MWSPT_NSEC 3 const int NL[MWSPT_NSEC] = { 1,3,3 }; const real32_T NUM[MWSPT_NSEC][3] = {

{

0.002140694065, 0, 0

},

{

1, 2, 1

},

{

1, 2, 1

}

}; const int DL[MWSPT_NSEC] = { 1,3,3 }; const real32_T DEN[MWSPT_NSEC][3] = {

{

1, 0, 0

},

{

1, -1.535104036, 0.6372369528

},

{

1, -1.466933489, 0.8432114124

}

};

Note that the designed filter is actually cascade of 2 sections since the first section anly applys specifed gain to the input signal as noted from the coefficients. You may ommit the gain adjustment if the overflow/undeflow is not an issue with your implementation.

Use the same design to generate the output coefficients for two other data types, 16-bit integer and fractional (fract16) data types using export feature of File menu of the fdatool.

2.

High-Pass IIR Filter with following properties:

Response Type = Highpass

Design Method = IIR - Chebyshev Type II

Filter Order (i.e., Number of Filter Coefficients): Check Minimum Order Radio

Button.

Sampling Frequencey Fs = 48000 Hz

Stop-Band Cutoff Frequency Fstop = 1500 Hz

Pass-Band Cutoff Frequency Fpass = 2000 Hz

Sidelobe attenuation Astop = 20

/*

Exporting Filter coefficients: The MATLAB should generate the following Filter

Coefficients:

* Filter Coefficients (C Source) generated by the Filter Design and Analysis

Tool

*

* Generated by MATLAB(R) 6.5.1 and the Signal Processing Toolbox 6.1.

*

* Generated on: 16-Feb-2007 11:43:15

*

*/

/*

* Discrete-Time IIR Filter (real)

* -------------------------------

* Filter Structure : Direct-Form II, Second-Order Sections

* Filter Order : 5

* Stable : Yes

* Linear Phase : No

*/

/* General type conversion for MATLAB generated C-code */

#include "tmwtypes.h"

/*

* Expected path to tmwtypes.h

* C:\MATLAB6p5p1\extern\include\tmwtypes.h

*/

/*

* Warning - Filter coefficients were truncated to fit specified data type.

* The resulting response may not match generated theoretical response.

* Use the Filter Design & Analysis Tool to design accurate fixed-point

* filter coefficients.

*/

#define MWSPT_NSEC 4 const int NL[MWSPT_NSEC] = { 1,2,3,3 }; const real32_T NUM[MWSPT_NSEC][3] = {

{

0.8173958659, 0, 0

},

{

1, -1, 0

},

{

1, -1.986638904, 1

},

{

1, -1.965208292, 1

}

}; const int DL[MWSPT_NSEC] = { 1,2,3,3 }; const real32_T DEN[MWSPT_NSEC][3] = {

{

1, 0, 0

},

{

1, -0.8822702169, 0

},

{

1, -1.791189313, 0.8174005151

},

{

1, -1.878196359, 0.9264635444

}

};

Note that the designed filter is actually cascade of 3 sections since the first section anly applys specifed gain to the signal noted from the coefficients.

Use the same design to generate the output coefficients for two other data types, 16-bit integer and fractional (fract16) data types using export feature of File menu of the fdatool.

Useful hints

ADI VisualDSP++ Blackfin library

While you may implement your own iir , ADI VisualDSP++ Blackfin library provides several functions that implement iir filters. The following is taken from “VisualDSP++ 4.5 C/C++

Compiler and Library Manual for Blackfin Processors”, page 4-126.

iir - infinite impulse response filter2

Synopsis

#include <filter.h> void iir_fr16(const fract16 input[],

fract16 output[], int length, iir_state_fr16 *filter_state);

The function uses the following structure to maintain the state of the filter. typedef struct { fract16 *c; /* coefficients */ fract16 *d; /* start of delay line */ int k; /* number of biquad stages */

} iir_state_fr16;

Description

The iir_fr16 function implements a biquad, transposed direct form II, infinite impulse response

(IIR) filter. It generates the filtered response of the input data input and stores the result in the

output vector output. The number of input samples and the length of the output vector are specified by the argument length.

The function maintains the filter state in the structured variable filter_state, which must be declared and initialized before calling the function. The macro iir_init, defined in the filter.h header file, is available to initialize the structure and is defined as:

#define iir_init(state, coeffs, delay, stages) \

(state).c = (coeffs);

\

(state).d = (delay); \

(state).k = (stages)

The characteristics of the filter are dependent upon filter coefficients and the number of stages.

Each stage has five coefficients which must be stored in the order A2, A1, B2, B1, and B0 . The value of A0 is implied to be 1.0 and A1 and A2 should be scaled accordingly. This requires that the value of the A0 coefficient be greater than both A1 and A2 for all the stages.

A pointer to the coefficients should be stored in filter_state->c, and filter_state->k should be set to the number of stages. Each filter should have its own delay line which is a vector of type fract16 and whose length is equal to twice the number of stages. The vector should be initially cleared to zero and should not otherwise be modified by the user program. The structure member filter_state->d should be set to the start of the delay line.

Algorithm

H

B

0

1

B

A

1

1 z z

1

1

B

A

2

2 z z

2

2

Where d y

 

 

 x

B

2

  d

 m

A

2

2 d

 m

B

1 d

2

 m

A

1 d

1

 m

B

0

 d

1

 

And m={0,1,2,…,length-1}

H(z) above is one bi-quad stage of the filter. Note that A i

coefficients are inverted in sign compared to a i

coefficients provided by MATLAB. Also the coefficients are in fractional format frac16 for iir_fr16.

Lab report and the complete VisualDSP++ project is needed for this lab.

Specifically I would like you to pay attention to the difference in the sound quality when applying IIR low-pass filters compared to high-pass filters and among various implementations. Describe your observations and make an attempt to explain why you are hearing/noticing this difference if any.

Download