-
Notifications
You must be signed in to change notification settings - Fork 1
/
lfiler.c
72 lines (59 loc) · 1.46 KB
/
lfiler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
static void bb_digital_float_filter(float *b, float *a, float *x, float *y, float *Z, int len_b, uint32_t len_x, int stride_X, int stride_Y)
{
float *ptr_x = x, *ptr_y = y;
float *ptr_Z;
float *ptr_b = (float*)b;
float *ptr_a = (float*)a;
float *xn, *yn;
const float a0 = *((float *)a);
int n;
uint32_t k;
/* normalize the filter coefs only once. */
for (n = 0; n < len_b; ++n) {
ptr_b[n] /= a0;
ptr_a[n] /= a0;
}
for (k = 0; k < len_x; k++) {
ptr_b = (float *)b; /* Reset a and b pointers */
ptr_a = (float *)a;
xn = (float *)ptr_x;
yn = (float *)ptr_y;
if (len_b > 1) {
ptr_Z = ((float *)Z);
*yn = *ptr_Z + *ptr_b * *xn; /* Calculate first delay (output) */
ptr_b++;
ptr_a++;
/* Fill in middle delays */
for (n = 0; n < len_b - 2; n++) {
*ptr_Z =
ptr_Z[1] + *xn * (*ptr_b) - *yn * (*ptr_a);
ptr_b++;
ptr_a++;
ptr_Z++;
}
/* Calculate last delay */
*ptr_Z = *xn * (*ptr_b) - *yn * (*ptr_a);
}
else {
*yn = *xn * (*ptr_b);
}
ptr_y += stride_Y; /* Move to next input/output point */
ptr_x += stride_X;
}
}
int main()
{
float b[3] = { 0.00013651, 0.00027302, 0.00013651 };
float a[3] = { 1., -1.96668139 , 0.96722743 };
float x[10] = { 2,5,6,7,8,2,3,5,1,2 };
float delay[2] = { 0 ,0 };
float y[1] = {};
for (int i = 0; i < 10; i++)
{
bb_digital_float_filter(b, a, &x[i], y, delay, 3, 1, 1, 1);
printf("%f\n", y[0]);
}
getchar();
}