hsk_libs-dev
163:b63ae088cc97
High Speed Karlsruhe XC878 library collection
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Macros
Groups
Pages
hsk_filter.h
Go to the documentation of this file.
1
/** \file
2
* HSK Filter generator
3
*
4
* This file offers preprocessor macros to filter analogue values, by
5
* calculating the average of a set of a given length.
6
*
7
* The buffer for the filter is stored in xdata memory.
8
*
9
* @author kami
10
*/
11
12
#ifndef _HSK_FILTER_H_
13
#define _HSK_FILTER_H_
14
15
#include <Infineon/XC878.h>
16
17
#include <string.h>
/* memset() */
18
19
/**
20
* Generates a filter.
21
*
22
* The filter can be accessed with:
23
* - void <prefix>_init(void)
24
* - Initializes the filter with 0
25
* - <valueType> <prefix>_update(const <valueType> value)
26
* - Update the filter and return the current average
27
*
28
* @param prefix
29
* A prefix for the generated internals and functions
30
* @param valueType
31
* The data type of the stored values
32
* @param sumType
33
* A data type that can contain the sum of all buffered values
34
* @param sizeType
35
* A data type that can hold the length of the buffer
36
* @param size
37
* The length of the buffer
38
*/
39
#define FILTER_FACTORY(prefix, valueType, sumType, sizeType, size) \
40
\
41
/** Holds the buffer and its current state. */
\
42
struct { \
43
/** The value buffer. */
\
44
valueType values[size]; \
45
/** The sum of the buffered values. */
\
46
sumType sum; \
47
/** The index of the oldest buffered value. */
\
48
sizeType current; \
49
} xdata prefix; \
50
\
51
/** Initializes the buffer with 0. */
\
52
void prefix##_init(void) { \
53
memset(&prefix, 0, sizeof(prefix)); \
54
} \
55
\
56
/** Updates the filter and returns the current sliding average of
57
buffered values.
58
@param value
59
The value to add to the buffer
60
@return
61
The average of the buffed values */
\
62
valueType prefix##_update(const valueType value) { \
63
prefix.sum -= prefix.values[prefix.current]; \
64
prefix.values[prefix.current++] = value; \
65
prefix.sum += value; \
66
prefix.current %= size; \
67
return prefix.sum / size; \
68
} \
69
70
71
/**
72
* Generates a group of filters.
73
*
74
* The filters can be accessed with:
75
* - void <prefix>_init(void)
76
* - Initializes all filters with 0
77
* - <valueType> <prefix>_update(const ubyte filter, const <valueType> value)
78
* - Update the given filter and return the current average
79
*
80
* @param prefix
81
* A prefix for the generated internals and functions
82
* @param filters
83
* The number of filters
84
* @param valueType
85
* The data type of the stored values
86
* @param sumType
87
* A data type that can contain the sum of all buffered values
88
* @param sizeType
89
* A data type that can hold the length of the buffer
90
* @param size
91
* The length of the buffer
92
*/
93
#define FILTER_GROUP_FACTORY(prefix, filters, valueType, sumType, sizeType, size) \
94
\
95
/** Holds the buffers and their current states. */
\
96
struct { \
97
/** The value buffer. */
\
98
valueType values[size]; \
99
/** The sum of the buffered values. */
\
100
sumType sum; \
101
/** The index of the oldest buffered value. */
\
102
sizeType current; \
103
} xdata prefix[filters]; \
104
\
105
/** Initializes all buffers with 0. */
\
106
void prefix##_init(void) { \
107
memset(&prefix, 0, sizeof(prefix)); \
108
} \
109
\
110
/** Updates the given filter and returns the current sliding average of
111
buffered values.
112
@param filter
113
The filter to update
114
@param value
115
The value to add to the buffer
116
@return
117
The average of the buffed values */
\
118
valueType prefix##_update(const ubyte filter, const valueType value) { \
119
prefix[filter].sum -= prefix[filter].values[prefix[filter].current]; \
120
prefix[filter].values[prefix[filter].current++] = value; \
121
prefix[filter].sum += value; \
122
prefix[filter].current %= size; \
123
return prefix[filter].sum / size; \
124
} \
125
126
127
#endif
/* _HSK_FILTER_H_ */
128
hsk_filter
hsk_filter.h
Generated on Fri Oct 11 2013 12:54:53 for hsk_libs-dev by
1.8.3.1