• No results found

real part

4.3. MATLAB IMPLEMENTATION 77

Figure 4.22: Block diagram of a direct form II (DF-II) fourth-order elliptic filter.

x[n] + +

Figure 4.23: Block diagram of a the second-order section (SOS) implementation of a fourth-order elliptic filter.

Figure 4.24: Block diagram of a parallel implementation of a fourth-order elliptic filter.

78 CHAPTER 4. IIR DIGITAL FILTERS

data Y. The filter is a "Direct Form II Transposed"

implementation of the standard difference equation:

a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

This function is useful for quickly implementing a filter with minimal programming on your part.

4.3.5 Creating Your Own Filter Algorithm

In this next MATLAB example, we are trying to implement a first-order IIR notch filter (this is not the same notch filter implemented in winDSK). We desire the filter to have a zero at z = 1 and a pole at z = 0.9. The transfer function associated with this pole/zero diagram is

H(z) = 1− z−1 1− 0.9z−1 and the difference equation is

y[n] = 0.9y[n− 1] + x[n] − x[n − 1].

We will use a unit impulse as the input to the system. If we calculate an infinite number of output terms, we will have determined the system’s impulse response. Manually calculating a few terms from the difference equation is very helpful in understanding this process.

1. Label the columns as shown below.

n y[n] y[n− 1] x[n] x[n − 1]

2. Fill in the n = 0 row information.

n y[n] y[n− 1] x[n] x[n − 1]

0 0 1 0

3. Calculate the y[0] term.

n y[n] y[n− 1] x[n] x[n − 1]

0 1 0 1 0

4. Fill in the n = 1 row information. Notice the “down and to the right” flow of the stored values.

n y[n] y[n− 1] x[n] x[n − 1]

0 1 0 1 0

1 1 0 1

5. Calculate the y[1] term.

n y[n] y[n− 1] x[n] x[n − 1]

0 1 0 1 0

1 −0.1 1 0 1

6. Continue this process until you have calculated all of the terms that you need.

4.4. DSK IMPLEMENTATION IN C 79 The MATLAB code shown in the listing below will only calculate the y[1] term. This code more closely implements the algorithm required for the real-time process. While it may seem strange to calculate only a single term, you must remember that this is exactly how sample-by-sample processing works.

Listing 4.1: Simple MATLAB IIR filter example.

1 % begin simulation

3 % Simulation inputs

x = [ 0 1 ] ; % input vector x = x [0] x [ -1]

5 y = [ 1 1 ] ; % output vector y = y [0] y [ -1]

B = [ 1 −1]; % numerator coefficients

7 A = [ 1 −0.9]; % denominator coefficients

9 % Calculated terms

As in the manual calculations, you should find that the output value is −0.1. In sum-mary, the input (receive) ISR provides a new sample to the algorithm, the algorithm calcu-lates the new output value, the algorithm prepares for the arrival of the next sample, and finally, the algorithm gives the new output value to the output (transmit) ISR, so that it may be converted back into an analog value. Notice that for low-order filters, the actual calculation of the output value may be a single line of code!

4.4 DSK Implementation in C

4.4.1 Brute-Force IIR Filtering

This version of the IIR implementation code, similar to the last MATLAB example, takes a brute-force approach. The intention of this approach is understandability, which comes at the expense of efficiency.

The files necessary to run this application are in the ccs\IIRrevA directory of Chapter 4.

The primary file of interest is the IIR_mono_ISRs.c interrupt service routine. This file con-tains the necessary variable declarations and performs the actual IIR filtering operation.

To allow for the use of a stereo codec (e.g., the native codecs on the OMAP-L138 Experi-menter Kit and the C6713 DSK), the program can easily implement independent Left and Right channel filters (see the difference between FIRmono_ISRs.c and FIRstereo_ISRs.c in Chapter 3). For clarity, however, only the Left channel mono version will be discussed below. In the code shown below, N is the filter order, the B array holds the filter’s numer-ator coefficients, the A array holds the filter’s denominnumer-ator coefficients, the x array holds the current input value x[0], and past values of x (namely, x[−1] for this filter), and the y array contains the current output value of the filter, y[0], and past values of y (namely, y[−1] for this filter).

80 CHAPTER 4. IIR DIGITAL FILTERS Listing 4.2: Brute-force IIR filter declarations.

#define N 1 // filter order

2

f l o a t B [ N +1] = { 1 . 0 , −1.0}; // numerator filter coefficients

4 f l o a t A [ N +1] = { 1 . 0 , −0.9}; // denominator filter coefficients f l o a t x [ N + 1 ] ; // input values

6 f l o a t y [ N + 1 ] ; // output values

The code shown below performs the actual filtering operation. The four main steps involved in this operation will be discussed following the code listing.

Listing 4.3: Brute-force IIR filtering for real-time.

/* I added my routine here */

2 x [ 0 ] = CodecDataIn . Channel [ LEFT ] ; // current input value

4 y [ 0 ] =−A [ 1 ] ∗ y [ 1 ] + B [ 0 ] ∗ x [ 0 ] + B [ 1 ] x [ 1 ] ; // calc. the output

6 x [ 1 ] = x [ 0 ] ; // setup for the next input y [ 1 ] = y [ 0 ] ; // setup for the next input

8

CodecDataOut . Channel [ LEFT ] = y [ 0 ] ; // output the result

10 /* end of my routine */

The four real-time steps involved in brute-force IIR filtering An explanation of Listing 4.3 follows.

1. (Line 2): This code receives the next sample from the receive ISR and assigns it to the current input array element, x[0].

2. (Line 4): This code calculates a single value of the difference equation’s output, y[0].

3. (Lines 6–7): These 2 lines of code shift the values in the x and y arrays one element to the right. The equivalent operation is,

x[0]→ x[1]

y[0]→ y[1].

After the shift to the right is complete, the next incoming sample, x[0] can be written into the x[0] memory location without a loss of information.

4. (Line 9): This line of code completes the filtering operation by transferring the result of the filtering operation, y[0], to the CodecDataOut.Channel[LEFT] variable for transfer to the DAC side of the codec via the transmit ISR.

Now that you understand the code. . .

Go ahead and copy all of the files into a separate directory. Open the project in CCS and “Rebuild All.” Once the build is complete, “Load Program” into the DSK and click on “Run.” Your IIR HP filter (actually a D.C. blocking filter) is now running on the DSK. Remember this program would typically be used for audio filtering, so a good way to experience the effects of your filter is to listen to unfiltered and filtered music.6

6You may need to adjust the value ofA[1] from −0.9 to a value such as −0.7 or even −0.5 to hear the effect well.