Analog filter to digital.

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
Hello,

I've made an analog filter with the following specs:
4th order lowpass butterworth
Passband 500hz
Stopband 4500hz
-75dB at stopband

To now get this into digital i have performed a BLT transformation to get the coefficients.
However implementing the filter to have the same specs is still a bit difficult.

I have a 32mhz MCU and a 12 bit adc which is the input of the filter.
To get this filter to work is it just a matter of sampling fast enough en then applying the digital filter with the direct form 1 or 2.
Or is there something else that's needed?
And will i need an anti aliasing filter if i sample at about 10000 samples.
 

MrAl

Joined Jun 17, 2014
11,389
Hello,

I've made an analog filter with the following specs:
4th order lowpass butterworth
Passband 500hz
Stopband 4500hz
-75dB at stopband

To now get this into digital i have performed a BLT transformation to get the coefficients.
However implementing the filter to have the same specs is still a bit difficult.

I have a 32mhz MCU and a 12 bit adc which is the input of the filter.
To get this filter to work is it just a matter of sampling fast enough en then applying the digital filter with the direct form 1 or 2.
Or is there something else that's needed?
And will i need an anti aliasing filter if i sample at about 10000 samples.
Did you mention what the Band Width is of your input signal? That determines the need for an anti aliasing filter right? What can you say about this?

If you have the BLT transformed equation, why is it you cant apply it? Is it because you dont know what the individual terms mean?

If you post your analog filter function and your digital, we can check it for you and give some more hints.
 

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
The filter will be tested using a signal generator 1v sine wave from 1 hz to 5hz to test the specs basically.

What im wondering is whether the filter will straigh up work with a 12bit adc with 10k samples per second or if there is something else that i need. Like fixed point or floating point to achieve the required speed.

View attachment 177410

This is from mathlab.
 

Attachments

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
Something like this, single chip solution -


Regards, Dana.
Thank you dana i appreciate the effort.
But shouldn't the digital filter have a somewhat similar plot as the analog filter?
Also does having 128 taps mean that there are more than the amount of coefficients i get with the BLT method?

And lastly what software is that?

Kind regards Chris
 

danadak

Joined Mar 10, 2018
4,057
That software is Cypress PSOC Creator, free. PSOC is a SOC device.

https://www.cypress.com/products/psoc-creator-integrated-design-environment-ide

Plot may be different because I did a FIR approach, rather than a BiQuad
which would be more like typical analog filter. And I did it as 128 taps,
less will result in a lesser ideal response.

I did it with the DelSig, at 8 bits, DelSig can go to 20 bits, DMA has to be modified
to handle a larger word size. Could also have been done with onchip 12 bit SAR.


Regards, Dana.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
The filter will be tested using a signal generator 1v sine wave from 1 hz to 5hz to test the specs basically.

What im wondering is whether the filter will straigh up work with a 12bit adc with 10k samples per second or if there is something else that i need. Like fixed point or floating point to achieve the required speed.

View attachment 177410

This is from mathlab.

Hi,

Well your attachment drawing does not make anything clear.

For one, you show two analog filters. Which one are you using for the digital rendition?
If you use both, how are they connected together?

Do you know how to implement the digital filter equation in software or no?
 

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
The transfer function that is used is the middle 1 for the analog filter
And the bottom function is used to get my coefficients for the digital filter.

I have some code that i used for my microcontroller for a 3rd order filter but i'm afraid that this filter won't work with the same code and that i'll have to implement it with fixed point. As the teacher has been hinting at implementing fixed point calculations for the 4th order filter.
 

MrAl

Joined Jun 17, 2014
11,389
The transfer function that is used is the middle 1 for the analog filter
And the bottom function is used to get my coefficients for the digital filter.

I have some code that i used for my microcontroller for a 3rd order filter but i'm afraid that this filter won't work with the same code and that i'll have to implement it with fixed point. As the teacher has been hinting at implementing fixed point calculations for the 4th order filter.
Hi,

This wont be very difficult once we clear a few little things up.

Is there an error on the far right for the value of w2 ?
That does not seem to follow the second middle transfer function.

What you would do next is convert the digital filter into a form that clearly shows the delays. Then it gets very simple to implement. In fact, even a simpler function would clearly show how this works so we can do that next.
We can look at that next but i'd like to know if there was a mistake in w2 or that is the right calculation on the far right for w2.
 

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
butterworth mathlab fixed.png 3de orde.png I've fixed the mistake w2 was indeed wrong.
I've also added how i've previously implemented the 3rd order filter.
I could also add the code for my micontroller if necessary.
However the 3rd order was a lot less heavy on the speed requirement thus not needing fixed point or any fancy implementations.
 

MrAl

Joined Jun 17, 2014
11,389
View attachment 177450 View attachment 177451 I've fixed the mistake w2 was indeed wrong.
I've also added how i've previously implemented the 3rd order filter.
I could also add the code for my micontroller if necessary.
However the 3rd order was a lot less heavy on the speed requirement thus not needing fixed point or any fancy implementations.
Hello,

I dont understand what you need then. You seem to have the system layed out although i did not check it for accuracy, so what do you want to know then? How to implement pseudo floating point? Not sure what you need yet.
 

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
The question would then be how do i make it into floating point math fot inplementation

As an example i could upload my 3rd ordrr filter that used doubles.

But i am unsure as to how to convert the coeficients and math to fixed point using ints.

The only hint that i have gotten is to use 4 1st order filters as i am using a butterworth filter

Kind regards Chris
 

MrAl

Joined Jun 17, 2014
11,389
The question would then be how do i make it into floating point math fot inplementation

As an example i could upload my 3rd ordrr filter that used doubles.

But i am unsure as to how to convert the coeficients and math to fixed point using ints.

The only hint that i have gotten is to use 4 1st order filters as i am using a butterworth filter

Kind regards Chris
Hi,

Your question seems to be nothing about the implementation of the digital filter equation but mostly about how to implement floats using integers.

Very simply, if you had to store pi as 3.14159 you would store it as an integer 314159 then at the very end divide by 100000 using actual floats. So that way you only need to use floats one time.
If you had to multiply by say 4, that would be 400000 in this system, and so the result would be 125663600000
then divide by 100000 (or just truncate) at the end which gives you 1256636, and at the very end divide by 100000 to get a real float value.

See if that helps, and if not i'll have to dig into my own implementation i used on a graphic image processor program several years ago.
 

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
This being the code that was used for the 3rd order filter.
with the implementation that was shown in an earlier image.

From what i've seen online the fixed point is mostly done with shifts and either 16 or 32 bits depending on accuracy.
However my coefficients are already quite big for some reason when everyone one else seems to have fractions.

Also for a 4th order filter which this is about i plan on using the same code but
changing the 1st order function to a 2nd order. But with fixed point to actually achieve the speed.

So my knowledge on this is limited and i'm finding mixed information. But if i were to try then that would basically mean multiplying all my coefficients and having the same mathematics function for the filter then at the end dividing my answer by a certain amount or multiplying by 0.0001 or something.

double xA[3] = {0};
double yA[3] = {0};
double xB[2] = {0};
double yB[2] = {0};
double y2 = 0;
double y = 0;
xA[0] = voltage;
xB[0] = voltage;
//2nd order

double a0 = 1496493.355637112;
double a1 = -1558044.337440127;
double a2 = 945462.3069227610;

double b0 = 220977.8312799367;
double b1 = 441955.6625598734;
double b2 = 220977.8312799367;

//1st order
double a20 = 1275.549091646361;
double a21 = -724.4509083536393;

double b20 = 275.5490916463607;
double b21 = 275.5490916463607;

y2 = b0* xA[0]+ b1 *xA[1]+ b2*xA[2]- a1*yA[1] - a2*yA[2]; //2nd order function
y2 = y2/a0;
yA[0] = y2;

y = b20 xB[0]+ b21 xB[1]-a21 *yB[1]; //1st order function
y = y/a20;
yB[0] = y;


for(uint8_t i = 2; i > 0; i--) // delay for 2nd order
xA = xA[i-1];

for(uint8_t i = 2; i > 0; i--)
yA = yA[i-1];

for(uint8_t i = 1; i > 0; i--) // 1st delay
xB = xB[i-1];

for(uint8_t i = 1; i > 0; i--)
yB = yB[i-1];

voltage = y;
 

MrAl

Joined Jun 17, 2014
11,389
Hi again,

Well you work with whatever register size you can get away with for the required speed and truncate as needed.
For example, if you cant do 3.14159 as 314159 and div by 100k later, then you have to resort to 31416 and div by 10k later.
That's life.
Sometimes you can scale the whole thing though, then upscale before you actually use the result for anything real.
Scaling may reduce accuracy too though when go to upscale later.

I may have actually used 64 bit integers in my graphics processing program because they are fast on a Intel platform and i wanted to get close to at least standard floats. I did have some problems where i had to convert float to int and the standard cast methoid took too long. There is a magic number for this when added to a float yeilds an int.
 

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
double a10 = 1
double a11 = -1.72645939822
double a12 = 0.74789093782
double b10 = 0.0053578849
double b11 = 0.0107157698
double b12 = 0.0053578849

double a20 = 1
double a21 = -1.86410476542
double a22 = 0.88724497662
double b20 = 0.0057850528
double b21 = 0.0115701056
double b22 = 0.0057850528

These are the coefficients after dividing by a10 and a20 which might be better suited.
https://gyazo.com/1e4aa7b3346cc9fe964d67bd4a27153e with this being my resulting plot
 

MrAl

Joined Jun 17, 2014
11,389
Hello again,

Ok before that, something does not look right.
I took a look at the simpler transfer function:
s/(s+d)

and your BLT transform does not look right simply because it does not have any feedback path.

So my question now is, how did you get your BLT transform for that part, and i assume that it is in the block that appears under the analog function s/(s+d) ?

If you want to work with constants it's simpler to just transform s/(s+d) and then you know your constant right away without having to abstract a second time. If you have a reason for doing otherwise let me know. But doing it this way then makes it easy to follow your work because then 'd' in the solution is d=2*pi*90*1.596 which is easy to relate to the analog filter just by simply looking at the transfer function of the analog filter for that section.
 
Last edited:

Thread Starter

Christiaan Buijse

Joined Apr 4, 2019
19
I posted the coefficients of my 4th order filter.
As i only posted the 3rd order example to make it a bit more clear.

The blt coefficients are gained using the formula at the bottom https://gyazo.com/a6ada88cad8667b71a629fc1ab182824 and the plot seems to comply with what i am expecting

I've tried matlab as well which gives me only 10 coefficients rather than the 12 i have now but gives me the exact same plot.
Using this code in matlab gives the exact same plot.


f_s = 20000; % Sample frequency in Hz
f_c = 500; % Cut-off frequency in Hz
order = 4; % Order of the butterworth filter

omega_c = 2 * pi * f_c; % Cut-off angular frequency
omega_c_d = omega_c / f_s; % Normalized cut-off frequency (digital)

[b, a] = butter(order, omega_c_d / pi); % Design the Butterworth filter
disp('a = '); disp(a); % Print the coefficients
disp('b = '); disp(b);
H = tf(b, a, 1 / f_s); % Create a transfer function
bode(H); % Show the Bode plot



a =
1.0000 -3.5897 4.8513 -2.9241 0.6630

b =
1.0e-03 *

0.0312 0.1250 0.1874 0.1250 0.0312
 

MrAl

Joined Jun 17, 2014
11,389
I posted the coefficients of my 4th order filter.
As i only posted the 3rd order example to make it a bit more clear.

The blt coefficients are gained using the formula at the bottom https://gyazo.com/a6ada88cad8667b71a629fc1ab182824 and the plot seems to comply with what i am expecting

I've tried matlab as well which gives me only 10 coefficients rather than the 12 i have now but gives me the exact same plot.
Using this code in matlab gives the exact same plot.


f_s = 20000; % Sample frequency in Hz
f_c = 500; % Cut-off frequency in Hz
order = 4; % Order of the butterworth filter

omega_c = 2 * pi * f_c; % Cut-off angular frequency
omega_c_d = omega_c / f_s; % Normalized cut-off frequency (digital)

[b, a] = butter(order, omega_c_d / pi); % Design the Butterworth filter
disp('a = '); disp(a); % Print the coefficients
disp('b = '); disp(b);
H = tf(b, a, 1 / f_s); % Create a transfer function
bode(H); % Show the Bode plot



a =
1.0000 -3.5897 4.8513 -2.9241 0.6630

b =
1.0e-03 *

0.0312 0.1250 0.1874 0.1250 0.0312

Hi again,

The question was about using the BLT transform not about creating an analog filter right?

So why dont we do this. Let's both transform this transfer function:

y/x=s/(s+d)

using the BLT transform and see if we can both come up with the same result. I will also check my result using some numerical techniques before i post again to be sure my solution is right. After that we can move on to the larger transfer function.
 
Top