#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main() {
double b0 = 0.009765625;
double b1 = 0.01014328003;
double b2 = -0.009014129639;
double b3 = -0.009391784668;
double a1 = -1.641998291;
double a2 = 0.7047691345;
double a3 = -0.06273269653;
int i;
int iteration = 10;
double x = 0, y = 0;
double in_data[10] = {255, 255, 255, 190, 158, 146, 221, 218, 112, 45};
// for direct form I
double y1 = 0, y2 = 0, y3 = 0;
double x1 = 0, x2 = 0, x3 = 0;
printf("Direct form I\n");
for(i=0; i<iteration; i++)
{
x = in_data[i] - 128;
y = b0*x + b1*x1 + b2*x2 + b3*x3 - a1*y1 - a2*y2 - a3*y3;
y = floor(y);
x3 = x2;
x2 = x1;
x1 = x;
y3 = y2;
y2 = y1;
y1 = y;
printf("The %d interation, x = %lf, y = %lf \n", i, x+128, y+128);
}
// for Direct form II Transposed
printf("\n");
printf("Direct form II Transposed\n");
x = 0; y = 0;
double s1z = 0;
double s2z = 0;
double s3z = 0;
for(i=0; i<iteration; i++)
{
x = in_data[i] - 128;
y = s1z + b0*x;
printf("out_sum = %lf\n", y*pow(2,18));
y = floor(y);
printf("b0*x = %lf\n", b0*x*pow(2,18));
printf("b1*x = %lf, a1*y = %lf\n", b1*x*pow(2,18), a1*y*pow(2,18));
printf("b2*x = %lf, a2*y = %lf\n", b2*x*pow(2,18), a2*y*pow(2,18));
printf("b3*x = %lf, a3*y = %lf\n", b3*x*pow(2,18), a3*y*pow(2,18));
printf("s1z = %lf ", s1z*pow(2,18));
printf("s2z = %lf ", s2z*pow(2,18));
printf("s3z = %lf\n", s3z*pow(2,18));
printf("The %d interation, x = %lf, y = %lf \n", i, x+128, y+128);
s1z = s2z + b1*x - a1*y;
s2z = s3z + b2*x - a2*y;
s3z = b3*x - a3*y;
}
return 0;
}