![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
cv1
SampleFilter.c@1:1567f5a4e96f, 2017-10-30 (annotated)
- Committer:
- zdenka
- Date:
- Mon Oct 30 16:12:27 2017 +0000
- Revision:
- 1:1567f5a4e96f
accelerometer samplefilter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zdenka | 1:1567f5a4e96f | 1 | #include "Samplefilter.h" |
zdenka | 1:1567f5a4e96f | 2 | |
zdenka | 1:1567f5a4e96f | 3 | static double filter_taps[SAMPLEFILTER_TAP_NUM] = { |
zdenka | 1:1567f5a4e96f | 4 | 0.009364555465021395, |
zdenka | 1:1567f5a4e96f | 5 | 0.04162548629009952, |
zdenka | 1:1567f5a4e96f | 6 | 0.08783132195564536, |
zdenka | 1:1567f5a4e96f | 7 | 0.14608632119801232, |
zdenka | 1:1567f5a4e96f | 8 | 0.19260258113649567, |
zdenka | 1:1567f5a4e96f | 9 | 0.21122159144894026, |
zdenka | 1:1567f5a4e96f | 10 | 0.19260258113649567, |
zdenka | 1:1567f5a4e96f | 11 | 0.14608632119801232, |
zdenka | 1:1567f5a4e96f | 12 | 0.08783132195564536, |
zdenka | 1:1567f5a4e96f | 13 | 0.04162548629009952, |
zdenka | 1:1567f5a4e96f | 14 | 0.009364555465021395 |
zdenka | 1:1567f5a4e96f | 15 | }; |
zdenka | 1:1567f5a4e96f | 16 | |
zdenka | 1:1567f5a4e96f | 17 | void SampleFilter_init(SampleFilter* f) { |
zdenka | 1:1567f5a4e96f | 18 | int i; |
zdenka | 1:1567f5a4e96f | 19 | for(i = 0; i < SAMPLEFILTER_TAP_NUM; ++i) |
zdenka | 1:1567f5a4e96f | 20 | f->history[i] = 0; |
zdenka | 1:1567f5a4e96f | 21 | f->last_index = 0; |
zdenka | 1:1567f5a4e96f | 22 | } |
zdenka | 1:1567f5a4e96f | 23 | |
zdenka | 1:1567f5a4e96f | 24 | void SampleFilter_put(SampleFilter* f, double input) { |
zdenka | 1:1567f5a4e96f | 25 | f->history[f->last_index++] = input; |
zdenka | 1:1567f5a4e96f | 26 | if(f->last_index == SAMPLEFILTER_TAP_NUM) |
zdenka | 1:1567f5a4e96f | 27 | f->last_index = 0; |
zdenka | 1:1567f5a4e96f | 28 | } |
zdenka | 1:1567f5a4e96f | 29 | |
zdenka | 1:1567f5a4e96f | 30 | double SampleFilter_get(SampleFilter* f) { |
zdenka | 1:1567f5a4e96f | 31 | double acc = 0; |
zdenka | 1:1567f5a4e96f | 32 | int index = f->last_index, i; |
zdenka | 1:1567f5a4e96f | 33 | for(i = 0; i < SAMPLEFILTER_TAP_NUM; ++i) { |
zdenka | 1:1567f5a4e96f | 34 | index = index != 0 ? index-1 : SAMPLEFILTER_TAP_NUM-1; |
zdenka | 1:1567f5a4e96f | 35 | acc += f->history[index] * filter_taps[i]; |
zdenka | 1:1567f5a4e96f | 36 | }; |
zdenka | 1:1567f5a4e96f | 37 | return acc; |
zdenka | 1:1567f5a4e96f | 38 | } |