10 #include "common/filter.h"
11 #include "common/maths.h"
12 #include "common/utils.h"
16 #endif /* __ARM_ACLE */
19 void luluFilterInit(luluFilter_t
*filter
, int N
)
21 filter
->N
= constrain(N
, 1, 15);
22 filter
->windowSize
= filter
->N
* 2 + 1;
23 filter
->windowBufIndex
= 0;
25 memset(filter
->luluInterim
, 0, sizeof(float) * (filter
->windowSize
));
26 memset(filter
->luluInterimB
, 0, sizeof(float) * (filter
->windowSize
));
29 FAST_CODE
float fixRoad(float *series
, float *seriesB
, int index
, int filterN
, int windowSize
)
33 for (int N
= 1; N
<= filterN
; N
++)
35 int indexNeg
= (index
+ windowSize
- 2 * N
) % windowSize
;
36 int curIndex
= (indexNeg
+ 1) % windowSize
;
37 float prevVal
= series
[indexNeg
];
38 float prevValB
= seriesB
[indexNeg
];
39 int indexPos
= (curIndex
+ N
) % windowSize
;
41 for (int i
= windowSize
- 2 * N
; i
< windowSize
- N
; i
++)
43 if (indexPos
>= windowSize
)
47 if (curIndex
>= windowSize
)
51 // curIndex = (2 - 1) % 3 = 1
52 curVal
= series
[curIndex
];
53 curValB
= seriesB
[curIndex
];
54 float nextVal
= series
[indexPos
];
55 float nextValB
= seriesB
[indexPos
];
56 // onbump (s, 1, 1, 3)
57 // if(onBump(series, curIndex, N, windowSize))
58 if (prevVal
< curVal
&& curVal
> nextVal
)
60 float maxValue
= MAX(prevVal
, nextVal
);
62 series
[curIndex
] = maxValue
;
64 for (int j
= 1; j
< N
; j
++)
66 if (++k
>= windowSize
)
74 if (prevValB
< curValB
&& curValB
> nextValB
)
76 float maxValue
= MAX(prevValB
, nextValB
);
79 seriesB
[curIndex
] = maxValue
;
81 for (int j
= 1; j
< N
; j
++)
83 if (++k
>= windowSize
)
87 seriesB
[k
] = maxValue
;
96 curIndex
= (indexNeg
+ 1) % windowSize
;
97 prevVal
= series
[indexNeg
];
98 prevValB
= seriesB
[indexNeg
];
99 indexPos
= (curIndex
+ N
) % windowSize
;
100 for (int i
= windowSize
- 2 * N
; i
< windowSize
- N
; i
++)
102 if (indexPos
>= windowSize
)
106 if (curIndex
>= windowSize
)
110 // curIndex = (2 - 1) % 3 = 1
111 curVal
= series
[curIndex
];
112 curValB
= seriesB
[curIndex
];
113 float nextVal
= series
[indexPos
];
114 float nextValB
= seriesB
[indexPos
];
116 if (prevVal
> curVal
&& curVal
< nextVal
)
118 float minValue
= MIN(prevVal
, nextVal
);
121 series
[curIndex
] = minValue
;
123 for (int j
= 1; j
< N
; j
++)
125 if (++k
>= windowSize
)
129 series
[k
] = minValue
;
133 if (prevValB
> curValB
&& curValB
< nextValB
)
135 float minValue
= MIN(prevValB
, nextValB
);
137 seriesB
[curIndex
] = minValue
;
139 for (int j
= 1; j
< N
; j
++)
141 if (++k
>= windowSize
)
145 seriesB
[k
] = minValue
;
154 return (curVal
- curValB
) / 2;
157 FAST_CODE
float luluFilterPartialApply(luluFilter_t
*filter
, float input
)
159 // This is the value N of the LULU filter.
160 int filterN
= filter
->N
;
161 // This is the total window size for the rolling buffer
162 int filterWindow
= filter
->windowSize
;
164 int windowIndex
= filter
->windowBufIndex
;
165 float inputVal
= input
;
166 int newIndex
= (windowIndex
+ 1) % filterWindow
;
167 filter
->windowBufIndex
= newIndex
;
168 filter
->luluInterim
[windowIndex
] = inputVal
;
169 filter
->luluInterimB
[windowIndex
] = -inputVal
;
170 return fixRoad(filter
->luluInterim
, filter
->luluInterimB
, windowIndex
, filterN
, filterWindow
);
173 FAST_CODE
float luluFilterApply(luluFilter_t
*filter
, float input
)
175 // This is the UL filter
176 float resultA
= luluFilterPartialApply(filter
, input
);
177 // We use the median interpretation of this filter to remove bias in the output