This repository has been archived by the owner on Apr 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MathMf.cpp
129 lines (99 loc) · 3.17 KB
/
MathMf.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Math library
*
* This source file can be found under:
* http://www.github.com/microfarad-de/MathMf
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MathMf.h"
void FirFilterClass::initialize (int16_t *memory, uint16_t size) {
this->memory = memory;
this->size = size;
this->index = 0;
this->initialized = true;
}
int16_t FirFilterClass::process (int16_t input) {
uint16_t i;
int32_t output = 0;
if (!initialized) return 0;
memory[index] = input;
index++;
if (index >= size) index = 0;
for (i = 0; i < size; i++) {
output = output + memory[i];
}
return (int16_t)(output / size);
}
/*#######################################################################################*/
int16_t IirFilterClass::process (int16_t input, uint16_t size) {
output = ((size - 1) * output + (int32_t)input) / size;
return (int16_t)output;
}
/*#######################################################################################*/
uint32_t HysteresisClass::apply (uint32_t value, int32_t threshold) {
int32_t delta = value - lastValue;
int8_t sign = sgn (delta);
uint32_t result;
if ( abs (delta) >= threshold || sign == lastSign) {
result = value;
lastSign = sign;
lastValue = value;
}
else {
result = lastValue;
}
return result;
}
/*#######################################################################################*/
uint32_t crcCalc(uint8_t *buf, uint16_t bufSize) {
const uint32_t crcTable[16] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
uint32_t crc = ~0L;
for (uint16_t index = 0 ; index < bufSize ; ++index) {
crc = crcTable[(crc ^ buf[index]) & 0x0f] ^ (crc >> 4);
crc = crcTable[(crc ^ (buf[index] >> 4)) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return crc;
}
/*#######################################################################################*/
int8_t sgn (int val) {
if (val < 0) return -1;
else return 1;
}
/*#######################################################################################*/
uint8_t dec2bcdLow (uint8_t value) {
while ( value >= 10 ) value -= 10;
return value;
}
uint8_t dec2bcdHigh (uint8_t value) {
uint8_t rv = 0;
while ( value >= 100) value -= 100;
while ( value >= 10 ) {
value -= 10;
rv++;
}
return rv;
}