## Digital Slew Rate Limiter Filter and C Implementation

### 2. Slew Rate Limiter Filter (SRLF)

##### 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

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;

}

### 3. Slow Slew Rate Limiter Filter. Variant 1.

##### 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;

}

### 4. Slow Slew Rate Limiter Filter. Variant 2.

##### 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;

}