## Digital Derivative Filter Using Integrators. C Code and Octave Script

### 3. Second-Order Low-Pass Filter

##### The magnitude, phase and step responses of the analog low pass filter are showed on the Fugures 3-1, 3-2, 3-3

Figure 3-1: Magnitude Response of the Low Pass Second Order Analog Filter

Figure 3-2: Phase Response of the Low-Pass Second Order Analog Filter

Figure 3-3: Step Response of the Low-Pass Second Order Analog Filter

### 4. Derivative Filter

##### It can be implemented on the basis of Low Pass filter (12). See the Figure 4-1. 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 4-2.

Figure 4-1: Low Pass Analog Filter

Figure 4-2: Derivative Analog Filter

##### The magnitude, phase responses of the analog derivative filter are showed on the Fugures 4-3, 4-4

Figure 4-3: Magnitude Response of the Derivative Analog Filter

##### Figure 4-3 shows that the signal is differentiated up to a frequency of 1kHz, and then the upper frequencies along with noise are suppressed.

Figure 4-4: Phase Response of the Derivative Analog Filter

• ##### Backward Euler Transform:Moving from z transform to samples:The digital derivative filter with the Backward Euler approximation is shown in Figure 4-5

Figure 4-5: Derivative Digital Filter with Backward Euler Approximation

• ##### Bilinear Transform:Moving from z transform to samples:The digital derivative filter realization with the bilinear approximation is showed on the Figure 4-6

Figure 4-6: Derivative Digital Filter with Bilinear Approximation

2. ##### Both transformations (17), (22) are nonlinear and are not exact copies of the analog filter.

Figure 4-7: Magnitude Response of the Derivative Digital Filter with Backward Euler

Figure 4-8: Phase Response of the Derivative Digital Filter with Backward Euler

Figure 4-9: Magnitude Response of the Derivative Digital Filter with bilinear

Figure 4-10: Phase Response of the Derivative Digital Filter with bilinear

##### This is the result obtained using the digital derivative filter in Figure 4-11. See the DerivativeFilter.m

Figure 4-11: 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 4-12. This leads to phase delays (distortions) between the input x(n) signal and the derivative y’(n) at the filter output.

Figure 4-12: Input/Output of the Low Pass Digital Filter

### 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 Backward 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 C-Code
% Input parameters:
% ParameterVector – data array
% FileNameString – output file name
function FloatParamVector2file(ParameterVector, FileNameString)

### 6. C Language 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(n-1) */
float Integrator1_in_previous;
/* Integrator 1: Out(n-1): y'(n) – output of the derivative filter */
float Integrator1_out_previous;
/* Integrator 2: In(n-1) */
float Integrator2_in_previous;
/* Integrator 2: Out(n-1): 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