# Second-order IIR Filters will support cascade implementations - 5 Part digital audio application tutorial

Until recently, audio systems have largely been analog. Now the trend is moving rapidly to digital implementations. While the literature contains most of the information needed to design and implement digital filters, it might not be easily found in the most useful forms. This paper gives the system designer a concise tutorial containing enough information to quickly get started with the design and implementation of digital filters.

Digital filters are much more flexible than analog filters since they do not depend upon the availability of special components to implement the desired mathematical function. Rather, digital second-order filters have 5 degrees of freedom that can be exploited to accomplish the desired filtering task. Unfortunately, it is not always clear how to find the coefficients to fully exploit this flexibility. As a partial answer to that dilemma, this paper surveys many of the common techniques and gives practical implementation tips.

The coefficients designed in this paper are those for the so-called Direct Form digital filters.

Although focusing on second-order filters, this paper also discusses some first-order filters as well, since some applications require them. Furthermore, there is no problem implementing first-order filters even in systems that offer only coefficient-configurable second-order sections. As this paper explains, the first-order filter can be implemented individually in the second-order section, or combined with another first-order filter to form a second-order filter.

It is possible to design filters in the analog domain and convert them for use in digital systems, and these techniques are also discussed in this paper. But it is also possible to design many filters directly in the digital domain. Usually a direct approach, if available, will be favored since it offers the designer more direct control of the filter transfer function and, often, ease of design.

**Nomenclature**

For simplification, this is also expressed as

Note that the sample-domain equation for Eq. (1) is as follows:

where the relationship back to Eq. (1) is understood by converting Eq. (3) back to z-transform notation by observing the following rules:

Lower case x's and y's become upper case. n's are substituted with z's. Delays, n-delta, become z-delta.

Making these substitutions, Eq. (3) becomes:

Which becomes Eq. (1) upon application of Eq. (4) and some additional manipulation.

A common audio implementation of this filter is shown in Fig. 1 1.

Notice the negative signs in the figure. These come directly out of Eq. (3). Obviously these can be absorbed into the a coefficients, leading to another standard version of Eq. (1), this time with the + signs in the denominator substituted with - signs. For purposes of this paper, the following convention is used: when the coefficients are directly applicable to Eq. (1), they will be written as shown in Eq. (2). When the sign has been absorbed into the a coefficients, the following nomenclature is used:

**Figure 1. Direct Form I second-order IIR Filter**

**Implementation issues**

*Stability*

Obviously, stability is a key issue in filter design, and the criterion for digital filters is simple: the filter is stable if its poles lie within the so-called unit circle. Practically speaking it is sufficient to test the coefficients (at implementation precision) to assure that the roots of the denominator polynomial have magnitudes lower than 1. For example, consider the denominator polynomial

A = 1.0 -1.96297931671143 0.96365261077881

or

Its roots, or the poles of the filter, are

0.98148965835571 0.01818409523718i

which have magnitude 0.98165809260598 < 1,="" so="" this="" filter="" is="" stable.="">

If the filter implementation in use uses A' instead of A, that is, if the signs of the a1 and a2 coefficients are reversed such that

A' = 1.0 1.96297931671143 -.96365261077881

the signs must be re-reversed prior to finding the roots. Due to the implementation, A' results in the same stable filter as A, but would not be a stable filter, with poles of

-2.36964475064116

0.40666543392974

except in those implementations where the numerator coefficient sign reversal is absorbed in the coefficients, as discussed in Section 2.

*Coefficient Quantization*

For fixed point implementations, the filter coefficients must be quantized to the appropriate word size. For most filters these can be either truncated or rounded. However, in the author's experience, magnitude truncation, or truncation toward 0, is the safest method in cases where the available precision is only marginally sufficient. This has not been the object of intense study, but, as a practical matter, the author uses magnitude truncation on coefficients, and tries other options if the transfer function deviates significantly from the desired result.

A fixed-point digital filter coefficient will have some number of integer bits, i, and some number of fraction bits, f. It is common to call this an i.f format, where the point indicates the binary point. For example, a 4.20 coefficient has 4 integer bits and 20 fraction bits, and a 5.23 coefficient has 5 integer bits and 23 fraction bits.

Coefficient quantization can be accomplished in the following way,

which is a fancy way of denoting an up shift of the coefficient, C, by the number of fraction bits, f, truncating toward 0, and then a down shift again by f to produce the quantized coefficient CQ.

Notice that, since the number of bits available to represent the coefficient is limited, it is also necessary to saturate the coefficient to the maximum and minimum values.

Since coefficients must, in general, take on negative or positive values, the 4.20 or 5.23 coefficients of the example above will be two's complement numbers. Therefore, the maximum positive number will be 2i-1-2-f, while the maximum negative coefficient will be -2i-1. This saturation is accomplished in the following manner:

For example, Table 1 shows some quantized and saturated 4.20 coefficients, and Table 2 shows the same coefficients for the 5.23 case:

**Table 1. 4.20 Quantization**

**Table 2. 5.23 Quantization**

*Conversion to Hexadecimal*

While conversion of decimal numbers to hexadecimal is commonplace, and there exist tools for accomplishing this task, converting i.f, twos-complement numbers is a slightly different wrinkle, that sometimes causes confusion.

The easiest way to accomplish this, and the way that works for the standard filtering devices in the marketplace, is as shown below:

This equation makes each coefficient the hexadecimal equivalent non-negative integer that is required for most standard dec2hex commands. In the absence of a standard dec2hex command, the non-negative integer arising from Eq. (8) can be converted to hexadecimal by first converting to binary, and then grouping the bits into groups of 4 and making a hexadecimal conversion.

By way of simple example, consider the value -1.25 to be represented in 2.2 hexadecimal. Quantizing according to Eq. (7) does not change the value. Then, applying Eq. (9) results in the following:

The binary representation of 11 is 1011, which results in the hex code B.

Had this been 1.25, Eq. (9) returns , which is 0101 in binary, and, of course, simply 5 in hexadecimal.

For more realistic examples, consider -3.97999954223633 from Table 1, and 5.29999995231628 from Table 2 above. Using Eq. (8) for the former, a 4.20 value

= 110000000101000111101100 in binary and

C051EC in hexadecimal.

Likewise, 5.29999995231628 for the 5.23 case produces 0010101001100110011001100110 in binary, and

2A66666 in hexadecimal.