How can I design a low pass filter using Z transform in Microcontroller?

I have generated a signal which is a mixture of 50 Hz and 250 Hz sine waves using a microcontroller and DAC. Check the screenshot of the excel file:

The values of row 4 (i.e. f1+f2) are feed to an 8-bit DAC to form the mixed signal. Now, if I read the Analog signal using another microcontroller at a 0.0002-second interval then I'll get back these data again.

So for the receiver, x(n) = {120,132,144,155,163, ... ... , 90th value }.
And the Z transform should be, X(z) = {120 + 132z^(-1) + 144z^(-2) + 155z^(-3) + ... ... }

Again, the transfer function of low pass RC filter is H(s)= (1/(sRC+1)) = a(1/(s+a)); where, a= 1/RC.

And the z transform will be H(z) = a(z/(z- e^(-a)))

So, how can I calculate the output response Y(z) = X(z)H(z), as X(z) is a very long series. My aim is to use the output response equation in a microcontroller to reproduce the low-frequency signal.

It is solved according to the answer of @Warren Hill

• 4th column? The "D" column? Commented Apr 2, 2020 at 12:54
• @Andyaka sorry row .. editing Commented Apr 2, 2020 at 12:55
• I use this book: The Scientist and Engineer's Guide to Digital Signal Processing by Steven W. Smith. Less theoretical and more practical than a classroom textbook. You don't have a lot of separation between 50 Hz and 250 Hz, you are going to need a higher order filter. Commented Apr 2, 2020 at 13:42
• Thank you. I'll check that book. Commented Apr 2, 2020 at 13:47
• If you choose an IIR (infinite impulse response) filter then you won't need to take many terms to obtain a decent low pass filter. Commented Apr 2, 2020 at 14:30

The usual method to design an IIR filter is to start with a frequency response we want, for example.

$$A(s)= \dfrac{1}{1+\dfrac{s}{\omega_p}}$$

Where $$\ \omega_p \$$ is the pole frequency in $$\ \text{rad}\cdot \text{s}^{-1}\$$. We can convert this to time domain by using the Tustin or bi-linear transformation.

$$s = \dfrac{2}{T} \cdot \dfrac{1-z^{-1}}{1+z^{-1}}$$

Where $$\T\$$ is the time between samples. This gives us a new function in $$\z\$$

$$H[z] = \dfrac{y}{x} = \dfrac{1}{1+\dfrac{2}{\omega_p\cdot T} \cdot \dfrac{1-z^{-1}}{1+z^{-1}}}$$

With a bit or arithmetic we can get this of the form:

$$H[z] = \dfrac{y}{x} = \dfrac{B_0+B_1 \cdot z^{-1} + B_2 \cdot z^{-2} + ... }{1-A_1 \cdot z^{-1} - A_2 \cdot z^{-2} - ...}$$

Now we note that $$\ z^{-1} \$$ represents one step back in time, $$\ z^{-2} \$$ represents two steps back in time and so on.

In this particular example we only need to worry about the the current a previous one values.

let $$\y_0\$$ be the current output, $$\y_1\$$ be the previous output, $$\x_0\$$ be the current input, $$\x_1\$$ be the previous input.

At each stage we can calculate a new $$\y_0\$$

$$y_0 = A_1 \cdot y_1 + B_0\cdot x_0 + B_1\cdot x_1$$

You can also design this as an FIR filter but because these do not look at previous outputs you would have significantly more terms.

$$y_0 = B_0 \cdot x_0 + B_1 \cdot x_1 + B_2 \cdot x_2 + ...$$

• Can you kindly explain what is A and B? Commented Apr 3, 2020 at 16:00
• A and B are just numbers you when you rearrange the formula into the standard form. I didn't want to calculate all the terms as I was showing the method. Commented Apr 3, 2020 at 18:06
• o got it .. thanks Commented Apr 3, 2020 at 18:16
• It has worked perfectly. Look at the edit of the question. I've included a screenshot of the result. Commented Apr 3, 2020 at 19:16

How can I design a low pass filter using Z transform in Microcontroller?

A simple IIR 1st order filter can be designed like this from here: -