## Digital Integrators with an Examples. C Code and Octave Script

### 2. Introduction

##### Then the magnitude and phase responses of the ideal integrator has the form:

Figure 2-1: Bode Diagram of the Analog Ideal Integrator

##### See the Figure 2-2:

Figure 2-2: Integrator Response on the Constant Signal

### 3. Backward Euler Integrator

##### In this case the area is calculated using the rectangle rule. See the Figure 3-1

Figure 3-1: Support of the Backward Euler Integrator

##### Magnitude and Phase Response of the Euler Integrator are calculated from (13) using substitution (8):

Figure 3-2: Magnitude and Phase Resposes of the Backward Euler Integrator

##### Now let’s calculate the integral using the Backward Euler Integrator. To do this, 5 samples of the sine function will be taken as a test signal:

Figure 3-3: Input Test Signal: Five Samples of the Sine Function: x(0), x(1), x(2), x(3), x(4)

##### Now calculate the area using the formula (14). See the DigitalIntegrators.m:

Euler’s Integrator Quality
Half Period Samples Number = 3
exactArea = 1.354866575281059e-03
calculationArea = 8.191520442889925e-04
relativeMistakeInPercent = 39.5%

### 4. Bilinear Integrator

##### Here the area is calculated using the trapezoid rule. See the Figure 4-1

Figure 4-1: Support of the Bilinear Integrator

##### The Magnitude and Phase Responses of the Bilinear Integrator is calculated from (24) using substitution (8):

Figure 4-2: Magnitude and Phase Resposes of the Bilinear Integrator

##### Now the integral is calculated with the Bilinear Integrator using the test signal (17) – (23) and Figure 3-3. See also the Digital Integrators.m:

Bilinear Integrator Quality
Half Period Samples Number = 3
exactArea = 1.354866575281059e-03
calculationArea = 1.272305937807317e-03
relativeMistakeInPercent = 6.1%

### 5. Simpson’s Integrator

##### To approximate the calculation of an integral, the area under the graph shown in Figure 5-1, Simpson’s formula (28) can be used:

Figure 5-1: Support of the Simpson’s Formula

##### Now using the Simpson’s formula, the area is calculated under the graph x(t) in the range t0…t10 shown in Figure 5-2

Figure 5-2: Support of the Simpson’s Integrator

##### The Magnitude and Phase Responses of the Simpson’s Integrator is calculated from (31) using substitution (8):

Figure 5-3: Magnitude and Phase Resposes of the Simpson’s Integrator

##### which corresponds to the frequencies f1 = 0 and f2= fs/2. Instability at zero frequency is the norm for an integrator and was discussed above. Instability at the fs/2 frequency is a problem and can lead to incorrect integral calculations. Therefore it is important that there are no high frequencies in the input signal spectrum. If a short signal with a frequency of fs/2 is applied to this integrator input then continuous oscillations will occur:

(a) Short input signal with a frequency of fs/2

(b) Continuous oscillations at the output of the Simpson’s integrator

Figure 5-4: Simpson’s Integrator: Instability at the fs/2 frequency

##### Now let’s calculate the integral with Simpson’s Integrator, using the test signal (17) – (23) in the Figure 3-3. See also the Digital Integrators.m:

Simpson’s Integrator Quality
Half Period Samples Number = 3
exactArea = 1.354866575281059e-03
calculationArea = 1.394305321397539e-03
relativeMistakeInPercent = 2.9%

### 6. Tick’s Integrator

##### The Magnitude and Phase Responses of the Tick’s Integrator is calculated from (34) using substitution (8):

Figure 6-1: Magnitude and Phase Resposes of the Tick’s Integrator

##### Now calculate the integral with the Tick’s Integrator using the test signal (17) – (23) in the Figure 3-3. See also the DigitalIntegrators.m:

Tick’s Integrator Quality
Half Period Samples Number = 3
exactArea = 1.354866575281059e-03
calculationArea = 1.375956614105570e-03
relativeMistakeInPercent = 1.6%

### 7. Conclusions

##### Figure 7-1 shows all four graphs together from (37) – (40):

Figure 7-1: Integrators Quality Compare

### 8. Octave GNU file DigitalIntegrators.m

##### The .m script generates the digital integrator solutions and plots. To do this, the following functions are defined in this file:

% Plotting the Magnitude Response of the Second Order Analog Filter: (b1*s^2+b2*s+b3)/(a1*s^2+a2*s+a3)
% Input parameters:
% SB – numerator of the second order section
% SA – denominator of the second order section
% FhighInHz – Frequency space in Hz
function AnalogMagnitudeResponse(SB, SA, FhighInHz, Text)
% Plotting the Phase Response of the Second Order Analog Filter: (b1*s^2+b2*s+b3)/(a1*s^2+a2*s+a3)
% Input parameters:
% SB – numerator of the second order section
% SA – denominator of the second order section
% FhighInHz – Frequency space in Hz
function AnalogPhaseResponse(SB, SA, FhighInHz, Text)

% Plotting the Magnitude Response of the Digital Second Order filter: (b1+b2*z^-1+b3*z^-2)/(a1+a2*z^-1+a3*z^-2)
% Input parameters:
% ZB – numerator of the second order section
% ZA – denominator of the second order section
% Tsample – sampling time in seconds
function dig_func_dmr = plot_DigitalMagnitudeResponse(ZB, ZA, Tsample, Text)

% Plotting the Phase Response of the Digital Second Order Filter: (b1+b2*z^-1+b3*z^-2)/(a1+a2*z^-1+a3*z^-2)
% Input parameters:
% ZB – numerator of the second order section
% ZA – denominator of the second order section
% Tsample – sampling time in seconds
function dig_func_angle = plot_DigitalPhaseResponse(ZB, ZA, Tsample, Text)

% Plotting the Ratio: abs(DigitalIntegrator/IdealIntegrator) = w*abs(DigitalIntegrator) = 2*pi*f*abs([(b1+b2*z^-1+b3*z^-2)/(a1+a2*z^-1+a3*z^-2)])
% Input parameters:
% ZB – numerator of the second order digital integrator
% ZA – denominator of the second order digital integrator
% Tsample – sampling time in seconds
function dig_func_ratio = plot_DigitalIntgratorQuality(ZB, ZA, Tsample)

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

### 9. C Language DigitalIntegrators.c/h

##### The demo SW is realized the digital integrator solutions. See the files DigitalIntegrators.c/hFunctions:

/*
IIR Filter with Transposed Form II
Input
float input – filter input
Return
float – filter output
*/
float iir_filter(float input, float const* all_coef_ptr, float* all_history_ptr, uint8 sos_nmb)

/*
Initialization of the Integrators
Clear the working arrays
Input: void
Return: void
*/
void IntegratorsInit(void)

/*
Euler’s Integrator
Input
float input – input value
Return
float – output value
*/
float EulerIntegrator(float input)

/*
Bilinear Integrator
Input
float input – input value
Return
float – output value
*/
float BilinearIntegrator(float input)

/*
Simpson’s Integrator
Input
float input – input value
Return
float – output value
*/
float SimpsonIntegrator(float input)

/*
Tick’s Integrator
Input
float input – input value
Return
float – output value
*/
float TickIntegrator(float input)