## IIR Filter and C Implementation Using Octave GNU Tool

### 3. IIR Filter in direct Form

##### Using formula (1), you can implement an IIR filter, as shown in Figure 3-1. This implementation is called direct Form 1 and requires storing the x(n-1),…,x(n-M) prehistory of the input signal, as well as y(n-1), y(n-2), y(n-3), …, y (n-N) – output signal. In total, you need to reserve (N+M) memory cells.

Figure 3-1 IIR Filter. Direct Form 1

##### Using (9) and (11) , we can do the canonic implementation of the filter shown in Figure 3-2

Figure 3-2 IIR Filter. Direct Form 2 canonic

##### Using the signal flow graph theory and direct form 2 we can get the transposed direct form 2. See the Figure 3-3

Figure 3-3 IIR Filter. Transposed Direct Form 2 canonic

### 4. IIR Filter in cascade Form

##### Thus, we get the following filter chain, as shown in Figure 4-1.

Figure 4-1: IIR Filter. Cascade Realization.

### 6. Using Example of the IIR Filter

##### In my demo SW (iir_filter. m/c/h) as an example, I use Bessel IIR filter from the article “Bessel Filter in Digital Form?“: Low Pass Filter, Fband=1kHz, Tsampling=83.33us (Fsampling≈12kHz) with the z-Transform:

Figure 6-1: Low Pass Bessel IIR Filter 4th Order. Magnitude Response.

### 7. Octave GNU file IIR_Filter.m

##### In this demo SW, where I analyze the resulting Bessel IIR filter after quantizing and rounding filter coefficients, generate test signals and pass them through the filter. To do this, the following functions are defined in this file:

% Plotting the Magnitude Response for the 4th order digital filter: (b11+b12*z^-1+b13*z^-2)(b21+b22*z^-1+b23*z^-2)/(a11+a12*z^-1+a13*z^-2)(a21+a22*z^-1+a23*z^-2)
% Input parameters:
% ZB1 – numerator 1 of the second order section
% ZA1 – denominator 1 of the second order section
% ZB2 – numerator 2 of the second order section
% ZA2 – denominator 2 of the second order section
% Tsample – sampling time in seconds
function plot_DigitalMagnitudeResponse(ZB1, ZA1, ZB2, ZA2, Tsample)

% Plotting the Phase Response for the 4th order digital filter: (b11+b12*z^-1+b13*z^-2)(b21+b22*z^-1+b23*z^-2)/(a11+a12*z^-1+a13*z^-2)(a21+a22*z^-1+a23*z^-2)
% Input parameters:
% ZB1 – numerator 1 of the second order section
% ZA1 – denominator 1 of the second order section
% ZB2 – numerator 2 of the second order section
% ZA2 – denominator 2 of the second order section
% Tsample – sampling time in seconds
function plot_DigitalPhaseResponse(ZB1, ZA1, ZB2, ZA2, Tsample)

% Plotting the Signal Response for the 4th order digital filter: (b11+b12*z^-1+b13*z^-2)(b21+b22*z^-1+b23*z^-2)/(a11+a12*z^-1+a13*z^-2)(a21+a22*z^-1+a23*z^-2)
% Input parameters:
% ZB1 – numerator 1 of the second order section
% ZA1 – denominator 1 of the second order section
% ZB2 – numerator 2 of the second order section
% ZA2 – denominator 2 of the second order section
% SampleNumber – Sample number of the step function
function plot_SignalResponseDigitalFilter(ZB1, ZA1, ZB2, ZA2, x_input, SampleNumber)

% IIR Filter Coefficients Quantization
% Input parameters:
% ZA, ZB – IIR coefficients
% ScaleFactor – integer scale
% Output parameters:
% ZA_quantization – integer format
% ZB_quantization – integer format
function [ZA_quantization ZB_quantization] = IirFilterQuantization(ZA, ZB, ScaleFactor)

% Integer Test Signal to File
% Support C-Code
% Input parameters:
% SignalAmpl – amplitude of the test signal
% SignalFreqInHz – frequency of the test signal in Hz
% SignalLength – table length
% Tsample – sampling time in s
% FileNameString – output file name
% Output parameters:
% TestSignal – test signal vector
function TestSignal = IntegerTestSignals2file(SignalAmpl, SignalFreqInHz, SignalLength, Tsample, FileNameString)

% Integer Table to File
% Support C-Code
% Input parameters:
% ParameterVector – data array
% FileNameString – output file name
function IntegerParamVector2file(ParameterVector, FileNameString)

% Float Table to File
% Support C-Code
% Input parameters:
% ParameterVector – data array
% FileNameString – output file name
function FloatParamVector2file(ParameterVector, FileNameString)

• ##### Sum of the two sine curves 500Hz and 5kHz

Figure 7-1: Low Pass Bessel IIR Filter 4th Order. Step Response.
Blue – input signal, red – output signal

Figure 7-2: Low Pass Bessel IIR Filter 4th Order. 500Hz Response.
Blue – input signal, red – output signal

Figure 7-3: Low Pass Bessel IIR Filter 4th Order. 500Hz and 5kHz
Response. Blue – input signal, red – output signal