SlewRateLimiterFilter_update

Digital Slew Rate Limiter Filter and C Implementation

1. Abbreviation

SLRF – Slew Rate Limiter Filter

 

2. Slew Rate Limiter Filter (SRLF)

In most applications, you can define the maximum possible (acceptable) the speed of the measured signal. This is determined by the frequency band of the amplifying path, the characteristics of the used sensors, and the external conditions. For example, the temperature and humidity of the environment change quite slowly and the corresponding sensor generates a low-frequency signal. If the measuring device detects an excessive change in the signal, it is caused by the presence of interference. The Slew Rate Limiter Filter (SLRF) is a non-linear filter designed to eliminate such interference.
The device of such an SLRF is quite simple. The signal change compared to the previous step is calculated, and if the signal jump is greater than the allowed one, the filter restricts this change at the output. See the Figure 2-1

SlewRateLimiterFilter_update

                                                         Figure 2-1: Slew Rate Limiter Filter

 

C Source Code is simple:

/*
Slew Rate Limiter Filter
Input
sint16 x_input – input x(n)
Return
sint16 – filter output y(n)
*/

#define SRL_MAX_SPEED_POSITIVE 1
#define SRL_MAX_SPEED_NEGATIVE (-2)

sint16 srl_filter(sint16 x_input)
{

sint16 deltaValue;

deltaValue = x_input – srl_filter_history;

if(SRL_MAX_SPEED_POSITIVE < deltaValue)
{/* Check on the positive speed */

deltaValue = SRL_MAX_SPEED_POSITIVE;

}

if(SRL_MAX_SPEED_NEGATIVE > deltaValue)
{/* Check on the negative speed */

deltaValue = SRL_MAX_SPEED_NEGATIVE;

}

/* Filter output and update filter history */
srl_filter_history += deltaValue;

return srl_filter_history;

}

Let the input value of the x_input signal change from INPUT1 to INPUT2
Variant 1: INPUT2 > INPUT1

Slew_formula1

Variant 2: INPUT2 < INPUT1
Slew_formula2
where CyclicNumber – the number of filter function calls through which the output value will take the value INPUT2,
MaxSpeedPositive / MaxSpeedNegative – see the defines SRL_MAX_SPEED_POSITIVE / SRL_MAX_SPEED_NEGATIVE in C Code
Example 1:
INPUT1 = 100
INPUT2 = 200
MaxSpeedPositive = 1
Then:
CycleNumber = (200-100)/1 = 100
Example 2:
INPUT1 = 200
INPUT2 = 100
MaxSpeedNegative = -2
Then:
CycleNumber = (200-100)/2 = 50

 

3. Slow Slew Rate Limiter Filter. Variant 1.

There are applications where the signal changes are extremely slow. The output signal changes by one only after few filter execute cycles. Therefore the filter can be called Slow SRLF.
Let’s consider the first variant of the filter.
C Source Code:

/*
Slow Slew Rate Limiter Filter
Input
sint16 x_input – input x(n)
Return
sint16 – filter output y(n)
*/

#define SRL_SLOW_V1_VALUE_POSITIVE 20
#define SRL_SLOW_V1_VALUE_NEGATIVE 10

sint16 slow_v1_srl_filter(sint16 x_input)
{

sint16 deltaValue;

deltaValue = x_input – slow_v1_srl_filter_history;

if(0 < deltaValue)
{/* Positive delta */

slow_v1_srl_value_positive -= 1;
if(0 >= slow_v1_srl_value_positive)
{/* Counter is underflow */

slow_v1_srl_filter_history += 1;
slow_v1_srl_value_positive =  SRL_SLOW_V1_VALUE_POSITIVE;

}
/* Initialization the negative retarder value */
slow_v1_srl_value_negative = SRL_SLOW_V1_VALUE_NEGATIVE;

}

if(0 > deltaValue)
{/* Negative delta */

slow_v1_srl_value_negative -= 1;
if(0 >= slow_v1_srl_value_negative)
{/* Counter is underflow */

slow_v1_srl_filter_history -= 1;
slow_v1_srl_value_negative = SRL_SLOW_V1_VALUE_NEGATIVE;

}

/* Initialization the positive retarder value */
slow_v1_srl_value_positive = SRL_SLOW_V1_VALUE_POSITIVE;

}

if(0 == deltaValue)
{/* Delta is zero */

slow_v1_srl_value_positive = SRL_SLOW_V1_VALUE_POSITIVE;
slow_v1_srl_value_negative = SRL_SLOW_V1_VALUE_NEGATIVE;

}

return slow_v1_srl_filter_history;

}

The program uses for slowing down the slow_v1_srl_value_positive = SRL_SLOW_V1_VALUE_POSITIVE / slow_v1_srl_value_negative = SRL_SLOW_V1_VALUE_NEGATIVE. First these variables are reset by subtracting 1 in each call of the filter function, and then the filter output is changed by one after which the process is repeated again.
Let the input value of the x_input signal change from INPUT1 to INPUT2. Then
Δ = INPUT2 – INPUT1
Slew_formula3
where SlowValue – see the defines SRL_SLOW_V1_VALUE_POSITIVE / SRL_SLOW_V1_VALUE_NEGATIVE in C Code
Example:
INPUT1 = 100
INPUT2 = 200
SlowValue = SRL_SLOW_V1_VALUE_POSITIVE = 20
Then:
Δ = INPUT2 – INPUT1 = 200 -100 = 100
CycleNumber = 20×100 = 2000
 

4. Slow Slew Rate Limiter Filter. Variant 2.

The deceleration in the Variant 2 depends on the Δ value. If the Δ is larger, then the filter output changes are faster.
C Source Code:

/*
Slow Slew Rate Limiter Filter
Input
sint16 x_input – input x(n)
Return
sint16 – filter output y(n)
*/

#define SRL_SLOW_V2_VALUE_POSITIVE 300
#define SRL_SLOW_V2_VALUE_NEGATIVE 200

sint16 slow_v2_srl_filter(sint16 x_input)
{

sint16 deltaValue;

deltaValue = x_input – slow_v2_srl_filter_history;

if(0 < deltaValue)
{/* Positive delta */

slow_v2_srl_value_positive -= deltaValue;
if(0 >= slow_v2_srl_value_positive)
{/* Counter is underflow */

slow_v2_srl_filter_history += 1;
slow_v2_srl_value_positive = SRL_SLOW_V2_VALUE_POSITIVE;

}
/* Initialization the negative retarder value */
slow_v2_srl_value_negative = SRL_SLOW_V2_VALUE_NEGATIVE;

}

if(0 > deltaValue)
{/* Negative delta */

slow_v2_srl_value_negative += deltaValue;
if(0 >= slow_v2_srl_value_negative)
{/* Counter is underflow */
slow_v2_srl_filter_history -= 1;
slow_v2_srl_value_negative = SRL_SLOW_V2_VALUE_NEGATIVE;
}
 /* Initialization the positive retarder value */
slow_v2_srl_value_positive = SRL_SLOW_V2_VALUE_POSITIVE;

}

if(0 == deltaValue)
{/* Delta is zero */

slow_v2_srl_value_positive = SRL_SLOW_V2_VALUE_POSITIVE;
slow_v2_srl_value_negative = SRL_SLOW_V2_VALUE_NEGATIVE;

}

return slow_v2_srl_filter_history;

}

The program uses for slowing down the slow_v2_srl_value_positive = SRL_SLOW_V2_VALUE_POSITIVE / slow_v2_srl_value_negative = SRL_SLOW_V2_VALUE_NEGATIVE. First these variables are reset by subtracting the value of the current delta in each call of the filter function, and then the filter output is changed by one after which the process is repeated again.
Let the input value of the x_input signal change from INPUT1 to INPUT2.
Δ = INPUT2 – INPUT1
Then:
Slew_formula4
where ceil(X) – return the smallest integer not less than X,
SlowValue – see the defines SRL_SLOW_V2_VALUE_POSITIVE / SRL_SLOW_V2_VALUE_NEGATIVE in C Code
Making calculations (4) is a difficult task. Let’s use the inequality to evaluate the CycleNumber:
LowBoundary ≤ CycleNumber ≤ HighBoundary (5)
where:
Slew_formula6

Slew_formula7

Formulas (5), (6) can be simplified:

Slew_formula8_9

The partial sums of the harmonic series have logarithmic growth:
Slew_formula10
where γ ≈ 0.5772 – Euler – Mascheroni constant
Using (10), the formulas (5), (8), (9) are rewritten:

Slew_formula11

The formula (10) can be used to estimate the CycleNumber.
Example:
INPUT1 = 100
INPUT2 = 200
SlowValue = SRL_SLOW_V2_VALUE_POSITIVE = 300
Then:
Δ = INPUT2 – INPUT1 = 200 -100 = 100
300(ln(100)+0.5772) ≤ CycleNumber ≤ 300(ln(100)+0.5772) + 100
1554.7 ≤ CycleNumber ≤ 1654.7
Note:
The exact value of CycleNumber is 1601, which corresponds to the our result! The exact value can be got using the C Code SlewRateLimiterFilter.c/h. See the “int main(void)“ function.

 

5. Download the SlewRateLimiterFilter.c/h

The C Code: SlewRateLimiterFilter.c/h
SRLF Functions:
void srl_filter_init(sint16 init_value);
where init_value – start output y(0) initialization
sint16 srl_filter(sint16 x_input);
where x_input – filter x(n) input
return – filter y(n) output
Slow SRLF Functions. Variant 1:
void slow_v1_srl_filter_init(sint16 init_value);
where init_value – start output y(0) initialization
sint16 slow_v1_srl_filter(sint16 x_input);
where x_input – filter x(n) input
return – filter y(n) output
Slow SRLF Functions. Variant 2:
void slow_v2_srl_filter_init(sint16 init_value);
where init_value – start output y(0) initialization
sint16 slow_v2_srl_filter(sint16 x_input);
where x_input – filter x(n) input
return – filter y(n) output
Note:
init_value – обычно берется первое измеренное значение сенсора x(0)
You can download the files with the button: