# Rate (gradient) limiter – Xcos modeling and simulation

A rate limiter, also called gradient limiter or slew rate limiter, is a mathematical function which controls the rate at which a signal is raising or falling. With other words, the rate limiter limits the first order derivative of the signal passing through it.

A rate limiter is widely used in embedded systems mainly for two reasons:

• to limit the rate of input signals (e.g. values read by sensors); this is done usually if there are spikes of the input signals or has gradients which can not happen in reality
• to limit the rate of a control signal (actuator input); this is done usually to have a smooth control action of the actuator,

The rate (gradient) R of a sampled signal u(t) is defined as:

$\text{R}=\frac{u[k]-u[k-1]}{t[k]-t[k-1]}\tag{1}$

where:

k – sample (time step)
u[k] – input signal at current time step
u[k-1] – input signal at previous time step
t[k] – current time step
t[k-1] – previous time step

As you can see, the definition of the signal rate (gradient) is the definition of the first order derivative, which is the ratio between signal variation and time variation.

The rate R can be different for rising or falling signals. A rising signal is defined as u[k] > u[k-1], which means that the value of the signal at the current time step is greater than the value at the previous time step. A falling signal is defined as u[k] < u[k-1], which means that the value of the signal at the current time step is less than the value at the previous time step.

If we define (impose) Rr as the maximum rising rate (gradient) and Rf as the minimum falling rate (gradient), a rate limiter works like this:

• if the actual rate R is greater than the rising (slew) rate Rr, u[k] is recalculated as:
$u[k] = R_{r} \cdot \left ( t[k] – t[k-1] \right ) + u[k-1] \tag{2}$
• if the actual rate R is less than the falling (slew) rate Rf, u[k] is recalculated as:
$u[k] = R_{f} \cdot \left ( t[k] – t[k-1] \right ) + u[k-1] \tag{3}$
• if the actual rate R is between falling (slew) rate Rf and rising (slew) rate Rr, u[k] is not recalculated

Example of rate (gradient) calculation. Let’s imagine that we have a position sensor which measures a position at every 10 ms. In the table below we can see the position value, time values and calculated rate (gradient).

 Sample [#] Time [s] Position [m] Rate [m/s] 0 0 0 0 1 0.1 0.05 0.5 2 0.2 0.09 0.4 3 0.3 0.12 0.3 4 0.4 0.08 -0.4 5 0.5 0.03 -0.5

From the physical properties of the system we know that the rate of the position can not be higher than 0.3 m/s and lower than -0.2 m/s. Therefore we need to apply a rate limiter to the measured signal. To perform all the calculation in a fast and efficient manner, we’ll use a Scilab script.

t = [0 0.1 0.2 0.3 0.4 0.5];
u = [0 0.05 0.09 0.12 0.08 0.03];
Rr = 0.3;
Rf = -0.2;
y=0;
for k=1:6
if k>1
R(k) = (u(k)-u(k-1))/(t(k)-t(k-1));
end
end
y=u;
mprintf("\n%s\t%s\t%s\t%s\t%s\n","k","t(k)","u(k)","R(k)","y(k)")
for k=1:6
if R(k) > Rr
y(k) = Rr*(t(k)-t(k-1))+y(k-1);
end
if R(k) < Rf
y(k) = Rf*(t(k)-t(k-1))+y(k-1);
end
mprintf("%d\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n",k,t(k),u(k),R(k),y(k))
end


The first FOR loop calculates the gradient (rate) of the signal. The second FOR loop checks the rate R against rising rate Rr and falling rate Rf. If these rates are exceeded, recalculates the signal and assigns the value to variable y. Variable y is in fact variable u but with the rate limit applied.

By running the Scilab script we get the following results:

k	t(k)	u(k)	R(k)	y(k)
1	0.00	0.00	0.00	0.00
2	0.10	0.05	0.50	0.03
3	0.20	0.09	0.40	0.06
4	0.30	0.12	0.30	0.09
5	0.40	0.08	-0.40	0.07
6	0.50	0.03	-0.50	0.05


As you can see, the output signal, after the gradient limiter, only increases in steps of 0.3 m/s and only decreases in steps of -0.2 m/s.

The gradient limiter can also be modelled as an Xcos block diagram. Applying equations (2) and (3) results in the following block diagram:

There are four inputs in the diagram:

• input raw signal, u(t)
• rising (maximum) rate, Rr
• falling (minimum) rate, Rf
• sample time, dT

The output of the block diagram is the signal y(t), which is u(t) with a rate limit applied to it.

The sample time dT is the time difference between two time iterations:

$dT = t[k] – t[k-1] \tag{4}$

The signal difference is calculated as u[k] – y[k-1], where u[k] is basically u(t) but sampled at dT (0.01 s). The current signal difference is compare against the allowed raising (Rf·(t[k]-t[k-1])) and falling (Rr·(t[k]-t[k-1])) signal difference. If these values are exceeded, the output signal is set at y[k-1]+Rr·(t[k]-t[k-1]) or y[k-1]+Rf·(t[k]-t[k-1]), which is the previous value y[k-1] plus the allowed rising or falling difference.

As example, we are going to have a square signal as input and set the rising rate Rr = 2, falling rate Rf = 2 and sample time dT = 0.01.

The Gradient Limiter block (subsystem) is sampled also at 0.01 s using the Clock block. To see the effect of the gradient limiter we use a MUX block and display the input u(t) and output y(t) on the same plot.

Running the simulation for 10 s outputs the following graphical window.

As you can see the input signal u(t) is no allowed to jump from 0 to 1 in 0.01 s. The increase is limited to 0.02 per 0.01 s and decrease to -0.02 per 0.01 s. So it takes 50 steps (or 0.5 s) for the output signal to reach the the final value (1) of the raw input signal.

We can modify the rising rate Rr to 1 and run the simulation again.

With a lower rising rate, it takes the signal 100 steps (1 s) to reach the value 1 of the raw input signal.

This example can be use to understand how a gradient limiter works and how changing the raising or falling rate affects the output. Also, it can be used for embedded signal applications as a predefined library function.