Digital Derivative Filter Using Integrators. C Code and Octave Script
1. Abbreviation
2. Introduction
The implementation of the input signal derivative is always a difficult task. This is due to the fact that the derivative increases at the upper frequencies of the signal spectrum, which also causes an increase in highfrequency interference. This is easy to understand using the Laplace transform. Let there be an input signal f(t) and its Laplace image F(s).
Then the signal derivative f(t) has a Laplace image:
Let f(0) = 0 and substituting s=jω, then the spectrum from the function derivative:
It can be seen from formula (4) that the upper frequencies of the signal spectrum are amplified along with numerous noises. To reduce this negative effect, the input signal is first passed through a low pass filter, which suppresses high frequency interference, and then the derivative is calculated. If we use a second order low pass filter, then the derivative can be implemented on two integrators [1]. This possibility I will consider in this article.
3. SecondOrder LowPass Filter
Consider a second order low pass filter, which can be written in two variants (5) and (6) using the Laplace transfer function:
Notes:

If Q ≤ 1 then the filter magnitude response has not peak on the resonance frequency ω0. Next in my example I will use Q = 0.7

Sometimes is used still the damping factor for the filter analysis

(5) can be written in another form:
Introducing new designations:
Then:
This Laplace transfer function can be rewritten as a linear differential equation:
Example
Q = 0.7
Then:
The magnitude, phase and step responses of the analog low pass filter are showed on the Fugures 31, 32, 33
Figure 31: Magnitude Response of the Low Pass Second Order Analog Filter
Figure 32: Phase Response of the LowPass Second Order Analog Filter
Figure 33: Step Response of the LowPass Second Order Analog Filter
4. Derivative Filter
The analog derivative filter has the form:
It can be implemented on the basis of Low Pass filter (12). See the Figure 41. This implementation contains a derivative signal. If the derivative signal is used as the filter output, then we get an analog derivative filter on the Figure 42.
Figure 41: Low Pass Analog Filter
Figure 42: Derivative Analog Filter
The magnitude, phase responses of the analog derivative filter are showed on the Fugures 43, 44
Figure 43: Magnitude Response of the Derivative Analog Filter
Figure 43 shows that the signal is differentiated up to a frequency of 1kHz, and then the upper frequencies along with noise are suppressed.
Figure 44: Phase Response of the Derivative Analog Filter
There are two ways to switch from an analog filter to a digital one:

Back Euler Transform:
Moving from z transform to samples:
The digital derivative filter with the Back Euler approximation is shown in Figure 45
Figure 45: Derivative Digital Filter with Back Euler Approximation
And the filter ztransform:

Bilinear Transform:
Moving from z transform to samples:
The digital derivative filter realization with the bilinear approximation is showed on the Figure 46
Figure 46: Derivative Digital Filter with Bilinear Approximation
The filter ztransform was done with the bilinear() function from the Octave Tool. See the DerivativeFilter.m
Notes:

I prefer bilinear approximation. This conversion leads to a more accurate reproduction of the analog prototype. This is especially important in control systems. See/compare the Figures 43, 44, 47, 48, 49, 410. Next I will use derivative filter with bilinear approximation.

Both transformations (17), (22) are nonlinear and are not exact copies of the analog filter.
Figure 47: Magnitude Response of the Derivative Digital Filter with Back Euler
Figure 48: Phase Response of the Derivative Digital Filter with Back Euler
Figure 49: Magnitude Response of the Derivative Digital Filter with bilinear
Figure 410: Phase Response of the Derivative Digital Filter with bilinear
Example
Let’s continue the example (16):
Q = 0.7
Let’s choose the sampling frequency based on the fact that the signal band is 10kHz
This is the result obtained using the digital derivative filter in Figure 411. See the DerivativeFilter.m
Figure 411: Output of the Derivative Digital Filter
It should be remembered that the derivative is calculated from the output of the low pass filter. There is a time delay between the input x(n) signal and the y(n) output of the low pass filter. See the Figure 412. This leads to phase delays (distortions) between the input x(n) signal and the derivative y’(n) at the filter output.
Figure 412: Input/Output of the Low Pass Digital Filter
Conclusion:
A simple implementation of the derivative on two integrators makes it possible to suppress interference at high frequencies and obtain a robust system.
5. Octave GNU file DerivativeFilter.m
The m script generates Laplace filter transform, test signals and checks the low pass / derivative 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)
% 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 Step 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
% TimeVector – Time space
function plot_StepResponseAnalogFilter(SB, SA, TimeVector, 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 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 plot_DigitalPhaseResponse(ZB, ZA, Tsample, Text)
% Plotting the Step Response for 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
% SampleNumber – Sample number of the step function
function plot_StepResponseDigitalFilter(ZB, ZA, SampleNumber, Text)
% Calculation of the Digital Derivative Filter with Back Euler
% Input parameters:
% Q_Factor – quality factor
% w0 – filter band in Rad/s
% Tsample – sampling time in s
% x_input – input signal
% y_output – output signal
function [y_filter, y_derivative] = DigitalDerivativeFilterBackEuler(Q_Factor, w0, Tsample, x_input)
% Calculation of the Digital Derivative Filter with Bilinear Transform
% Input parameters:
% Q_Factor – quality factor
% w0 – filter band in Rad/s
% Tsample – sampling time in s
% x_input – input signal
% y_output – output signal
function [y_filter, y_derivative] = DigitalDerivativeFilterBilinear(Q_Factor, w0, Tsample, x_input)
% Float Table to File
% Support CCode
% Input parameters:
% ParameterVector – data array
% FileNameString – output file name
function FloatParamVector2file(ParameterVector, FileNameString)
6. C Language DigitalDerivativeFilter.c/h
The demo SW is realized the digital derivative filter with the bilinear transform. See the files DigitalDerivativeFilter.c/h
Data/Functions:
/* Export structure */
typedef struct derative_filter_data
{
/* Low Pass Filter Band in Rad/s */
float w0_value;
/* Quality Factor */
float Q_value;
/* Sample Time */
float Tsample;
/* Integrator 1: In(n1) */
float Integrator1_in_previous;
/* Integrator 1: Out(n1): y'(n) – output of the derivative filter */
float Integrator1_out_previous;
/* Integrator 2: In(n1) */
float Integrator2_in_previous;
/* Integrator 2: Out(n1): y(n) – output of the low pass filter */
float Integrator2_out_previous;
} derative_filter_data_s;
/* Export functions */
extern void DerivativeFilterInitialization(void); – filter initialization
extern void DerivativeFilter(float, derative_filter_data_s*); – calculate next sample of the derivation filter