Learning Content and Theses

Platform for digital learning at HSHL

Digital Filters

A microcontroller processes instructions sequentially. Data from an AD converter are made available to it sequentially. Filtering in the microcontroller therefore takes place continuously while new measurement data are constantly being provided by the AD converter. Step by step, we create source code that provides filtered output data whenever a new value is available at the ADC as an input quantity.

To derive a simple filter function, we consider an analogue low-pass filter made of a resistor and a capacitor:

RC low-pass: input voltage uEin(t), current i(t), resistor R and capacitor C with uAus(t)
Aus = out (output)
\[ \begin{gathered} \text{Capacitor: } u_{\mathrm{Aus}}(t) = \frac{1}{C} \int i_C(t)\,dt + u_{\mathrm{Aus,Vorher}} \\[6pt] \text{Series connection: } i_C(t) = i_R(t) = i(t) \\[6pt] \text{Ohm's law: } i(t) = \frac{u_R(t)}{R} \\[6pt] \text{Mesh equation: } u_R(t) = u_{\mathrm{Ein}}(t) - u_{\mathrm{Aus}}(t) \\[6pt] \text{Substituting everything: } u_{\mathrm{Aus}}(t) = \frac{1}{C} \int \frac{u_{\mathrm{Ein}}(t) - u_{\mathrm{Aus}}(t)}{R}\,dt + u_{\mathrm{Aus,Vorher}} \\[6pt] u_{\mathrm{Aus}}(t) = \frac{1}{RC} \int \left(u_{\mathrm{Ein}}(t) - u_{\mathrm{Aus}}(t)\right) dt + u_{\mathrm{Aus,Vorher}} \\[6pt] \text{with } RC = \tau \\[6pt] u_{\mathrm{Aus}}(t) = \frac{1}{\tau} \int \left(u_{\mathrm{Ein}}(t) - u_{\mathrm{Aus}}(t)\right) dt + u_{\mathrm{Aus,Vorher}} \end{gathered} \]

We now consider this equation in time-discrete steps of length Δt. As soon as this time interval has passed, a new value is available at the output of the ADC. This new value is added to the digital filter as an input quantity. We consider a current output voltage and a current input voltage at time n. One time step Δt later, we have time n+1.

We consider the change in the output voltage over such a time interval Δt. At time n+1, based on the values at time n:

In the source code, the input voltage uEin(t) is replaced by an input number zEin(n) from the ADC. The integral over the output voltage uAus(t) dt becomes an output number zAus(n) ∙ Δt. At time n, the output voltage uAus,Vorher was present across the capacitor. This corresponds to the output number of the calculation step before the integration, zAus(n).

\[ \begin{gathered} z_{\mathrm{Aus}}(n+1) = \frac{1}{\tau}\left(z_{\mathrm{Ein}}(n) - z_{\mathrm{Aus}}(n)\right) \cdot \Delta t + z_{\mathrm{Aus}}(n) \\[6pt] z_{\mathrm{Aus}}(n+1) = z_{\mathrm{Ein}}(n) \cdot \frac{\Delta t}{\tau} - z_{\mathrm{Aus}}(n) \cdot \frac{\Delta t}{\tau} + z_{\mathrm{Aus}}(n) \\[6pt] z_{\mathrm{Aus}}(n+1) = z_{\mathrm{Ein}}(n) \cdot \frac{\Delta t}{\tau} + z_{\mathrm{Aus}}(n) \cdot \left(1 - \frac{\Delta t}{\tau}\right) \end{gathered} \]

Let us interpret the last line. It says that the number zEin(n) newly provided by the ADC is weighted with the factor Δt / τ. The old output value zAus(n) is also weighted with a factor. Then both terms are added.

This calculation is carried out every time a new ADC value is available. The result of the previous calculation (n) is included in the new calculation (n +1).

Let us assume that Δt is much smaller than τ, e.g. Δt = τ /10. Then 1/10 of the new number is added to the previous filter result. The result then consists of 1 – 1/10 = 9/10 of the previous value. So it takes some time until a change in the input numbers shows up in the filtered output number.

Now let us assume that the input number equals the output number. Then the new output number consists of 1/10 of the input number plus 9/10 of the identical output number. So the output number does not change when the input number equals the output number.

If the input number is smaller than the output number, the output number decreases over time. Conversely, the output number increases if the input number is greater than the output number. But it increases only a little with each calculation step. The speed of the change is determined by the factor Δt / τ. A large tau leads to a slow change, because it makes the factor small.

Setting the cut-off frequency

A large tau is an indicator of a sluggish system. This is analogous to the findings from the RC charging curves (at the very end of the page) in the fundamentals of electrical engineering. Please read up on it again there if you do not see this directly.

What can we influence in the filter? Δt is the time between two samples. We do not change the sampling frequency for filtering, because it has been optimised for the analogue input signals. We can change the parameter τ. This corresponds to changing R and C in an analogue filter circuit (τ = R ∙ C).

Why does this formula have a filtering effect? We have reprogrammed a low-pass filter. The filter should therefore attenuate the peak values of high frequencies and let the peak values of low frequencies pass unchanged. Let us look again at the result of the calculation above:

\[ z_{\mathrm{Aus}}(n+1) = z_{\mathrm{Ein}}(n) \cdot \frac{\Delta t}{\tau} + z_{\mathrm{Aus}}(n) \cdot \left(1 - \frac{\Delta t}{\tau}\right) \]

Let us apply this function to example numbers. The input numbers in the left column come from the ADC. The result of filtering according to the formula above is in the right column. For this example I chose Δt = 1µs. This corresponds to a sampling frequency of 1MHz. For the filter effect I chose τ = 10 ∙ Δt = 10µs. For this example, please ignore the fact that an ADC does not output negative numbers; this is about the basic function.

Input numbers zEin(n) and filtered output numbers zAus(n) as a table and plot: the signal is strongly attenuated
Aus = out (output) · Zu = in (inflow) · Ein = in (input)

On the right of the figure, input and output numbers are plotted over time. Time advances in discrete steps n ∙ Δt. The points are connected in the graph so that you can see the shape of the curve better. In the microcontroller there are only the numbers, and of course they are not connected into a curve.

The filter attenuates the input signal by about a factor of 10. The peak value of the output number is reduced, and the output number is phase-shifted relative to the input number. The signal has the period TSignal = 8 ∙ Δt = 8µs, because it repeats after n = 8 values, each shifted by Δt from the next. Let us check:

\[ \begin{gathered} \text{Set in the example: } \Delta t = 1\,\mathrm{\mu s};\; \tau = 10\,\mathrm{\mu s} \\[6pt] T_{\mathrm{Signal}} = 8 \cdot \Delta t = 8\,\mathrm{\mu s} \\[6pt] f_{\mathrm{Signal}} = \frac{1}{T_{\mathrm{Signal}}} = \frac{1}{8 \cdot \Delta t} = 125\,\mathrm{kHz} \\[6pt] \text{Filter cut-off frequency: } \omega_G = \frac{1}{RC} = \frac{1}{\tau} \\[6pt] f_G = \frac{\omega_G}{2\pi} = \frac{1}{2\pi \cdot 10 \cdot \Delta t} = 16\,\mathrm{kHz} \\[6pt] f_{\mathrm{Signal}} > f_G \rightarrow \text{low-pass filter attenuates} \\[6pt] \text{Attenuation by about a factor of } D = \frac{f_{\mathrm{Signal}}}{f_G} = \frac{125\,\mathrm{kHz}}{16\,\mathrm{kHz}} = 7.8 \approx 10 \end{gathered} \]

Next, we shift the low-pass cut-off frequency in the Bode diagram so far to the right that the signal frequency is lower than the cut-off frequency. Then the signal passes the filter unchanged.

Input numbers zEin(n) and output numbers zAus(n) for τ = Δt: the signal passes the filter almost unchanged
Aus = out (output) · Ein = in (input)
\[ \begin{gathered} f_{\mathrm{Signal}} = \frac{1}{T_{\mathrm{Signal}}} = \frac{1}{8 \cdot \Delta t} = 125\,\mathrm{kHz} \\[6pt] \text{Set in the example: } \tau = \Delta t = 1\,\mathrm{\mu s} \\[6pt] \text{Filter cut-off frequency: } f_G = \frac{1}{2\pi \cdot \tau} = 160\,\mathrm{kHz} \\[6pt] f_{\mathrm{Signal}} < f_G \rightarrow \text{low-pass filter does not attenuate.} \end{gathered} \]

The output signal of the filter is shifted to the right by one calculation step in the time plot. This has nothing to do with the phase shift of the filter, but with the method of calculation. You always use past measured values from the previous calculation step. As a result, the waveform is always shifted to the right by at least one calculation step.

Moving average filter

The filter discussed so far uses the entire history of the input signal. The old signal components are weighted less and less with increasing time distance, but they remain in the calculation forever. An analogue filter does the same. We can also use a moving average for the filter. This uses a firmly defined time window. The following applies:

\[ z_{\mathrm{Aus}}(n) = \frac{1}{m} \sum_{n=1}^{m} z_{\mathrm{Ein}}(n) \]

This filter always uses a fixed number m of input numbers for filtering. As soon as a new value is available at the ADC, it is included in the average. In exchange, the oldest value is removed from the average. This filter has a similar effect.

Think about how you could implement this filter in C code with as few arithmetic operations as possible.

Download course as PDF

The PDF contains all pages of the course. Interactive content is only available on the website.