forked from MarcusNordstrom/C-Step-Counter
/
filterStage.c
executable file
·109 lines (90 loc) · 2.83 KB
/
filterStage.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
The MIT License (MIT)
Copyright (c) 2020 Anna Brondin, Marcus Nordström and Dario Salvi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "filterStage.h"
#include "scoringStage.h"
#ifdef DUMP_FILE
#include <stdio.h>
static FILE
*filteredFile;
static FILE *filteredFile;
#endif
static ring_buffer_t *inBuff;
static ring_buffer_t *outBuff;
static void (*nextStage)(void);
/*
FIR filter designed with
http://t-filter.appspot.com
sampling frequency: 12.5 Hz
fixed point precision: 32 bits
* 0 Hz - 3 Hz
gain = 1
desired ripple = 5 dB
actual ripple = n/a
* 4 Hz - 6.25 Hz
gain = 0
desired attenuation = -10 dB
actual attenuation = n/a
*/
#define FILTER_TAP_NUM 7
static magnitude_t filter_taps[FILTER_TAP_NUM] = {
-2696,
-3734,
11354,
17457,
11354,
-3734,
-2696};
void initFilterStage(ring_buffer_t *pInBuff, ring_buffer_t *pOutBuff, void (*pNextStage)(void))
{
inBuff = pInBuff;
outBuff = pOutBuff;
nextStage = pNextStage;
#ifdef DUMP_FILE
filteredFile = fopen(DUMP_FILTERED_FILE_NAME, "w+");
#endif
}
void filterStage(void)
{
if (ring_buffer_num_items(inBuff) == FILTER_TAP_NUM)
{
accumulator_t sum = 0;
data_point_t dataPoint;
data_point_t out;
for (int8_t i = 0; i < FILTER_TAP_NUM; i++)
{
ring_buffer_peek(inBuff, &dataPoint, i);
if (i == FILTER_TAP_NUM - 1)
out.time = dataPoint.time;
sum += dataPoint.magnitude * filter_taps[i];
}
out.magnitude = sum >> 16;
ring_buffer_dequeue(inBuff, &dataPoint);
ring_buffer_queue(outBuff, out);
(*nextStage)();
#ifdef DUMP_FILE
if (filteredFile)
{
if (!fprintf(filteredFile, "%lld, %lld\n", out.time, out.magnitude))
puts("error writing file");
fflush(filteredFile);
}
#endif
}
}