I am here asking how does one transfer a difference equation into a MCU? I have never done it personally and looking into this topic I was never able to find a good answer. Its usually used with Matlab but never a MCU.
I have a continuous transfer function:
$$LPF_A = \frac{1}{6.416\cdot 10^{-11}s^2+1.133\cdot 10^{-5}s+1}$$
Its a 2nd Order Butterworth filter with a \$F_c = 20\text{kHz}\$.
Using the Tustin method I have the discrete equation:
$$LPF_D=\frac{0.1441z^2+0.2881z+0.1441}{z^2-0.6777z+0.254}$$ With a sample of \$T_s = \$ 8e-6 seconds, in scientific E notation.
The difference equation:
\$Y\$ = Output
\$U\$ = Input
$$Y_i =0.1441U_{i}+0.2281U_{i-1}+0.1441U_{i-2}+0.6777Y_{i-1}-0.254Y_{i-2}$$
I know I have to make my ADC output = \$U_i\$ and initialize \$U_{i-1}\$, \$U_{i-2}\$, \$Y_{i-1}\$, \$Y_{i-2} = 0\$, But after that no idea.
I have no idea what to do with this after. I have always used Matlab's embedded ways of doing it but never have transferred a difference equation into a MCU before. Any help would be appreciated.
Edit: Here's the code based on what I think
unsigned short x;
intdouble Y_i[10];Y_i[10] = {0,0,0,0,0,0,0,0,0,0};
intdouble U_i[10];U_i[10] = {0,0,0,0,0,0,0,0,0,0};
intdouble Y_i_1 = 0;
intdouble Y_i_2 = 0;
intdouble U_i_1 = 0;
intdouble U_i_2 = 0;
void setup() {
ADMUX |= 0xC0;
ADCSRA |= 0x8F;
ADCSRA |= 0x40;
Serial.begin(9600);
}
ISR(ADC_vect) {
x = ADCL << 2;
x |= ADCH;
ADCSRA |= 0x40;
}
void loop() {
//Yi = 0.1441Ui + 0.2281Ui-1 + 0.1441Ui-2 + 0.6777Yi-1 - 0.254Yi-2
for (int i = 1; i < 3;4; i++){
if (i == 1) {
Y_i[i] = 0.1441*x144*x + 0.2281*U_i_1 + 0.1441*U_i_2 + 0.6777*Y_i_1 - 0.254*Y_i_2;
U_i[i] = x;
}
else if (i == 2) {
Y_i[i] = 0.1441*x144*x + 0.2281*U_i[i-1] + 0.1441*U_i[i-2] + 0.6777*Y_i[i-1] - 0.254*Y_i[i-2];
U_i[i] = x;
} else {
Y_i[i] = 0.1441*x144*x + 0.2281*U_i[i-1] + 0.1441*U_i[i-2] + 0.6777*Y_i[i-1] - 0.254*Y_i[i-2];
U_i[i] = x;
}
}
}
What happens after the iteration is done? It resets? thats it?