/
microsmooth.h
97 lines (75 loc) · 3.04 KB
/
microsmooth.h
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
/*
Microsmooth, DSP library for Arduino
Copyright (C) 2013, Asheesh Ranjan, Pranav Jetley
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 2 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*Standard Library Dependencies*/
#include <stdint.h>
//#include <math.h>
#include <stdlib.h>
#ifndef MICROSMOOTH
#define MICROSMOOTH
/*Filter Codes*/
#define SMA 0b00000001
#define CMA 0b00000010
#define EMA 0b00000100
#define SGA 0b00001000
#define KZA 0b00010000
#define RDP 0b00100000
#define KFA 0b01000000
/*Algorithm Parameters*/
/*
These parameters should be tuned depending on need. Each of these parameters affects
run time and signal smoothing obtained. See documentation for specific instructions
on tuning these parameters.
*/
/*Simple Moving Average - Length of window */
#ifndef SMA_LENGTH
#define SMA_LENGTH 5
#endif
/*Exponential Moving Average - Alpha parameter */
#ifndef EMA_ALPHA
#define EMA_ALPHA 10 /*This is in percentage. Should be between 0-99*/
#endif
/*Savitzky Golay Filter - */
#ifndef SGA_LENGTH
#define SGA_LENGTH 5 /* Window may 5, 7 or 9.
For window 5, only quadratic or cubic smoothing may be used */
#endif
#ifndef SGA_DEGREE
#define SGA_DEGREE 3 /* For quadratic or cubic smoothing, enter degree 3.
For quartic or quintic smoothing, enter degree 4.*/
#endif
#ifndef SGA_INDEX
#define SGA_INDEX (SGA_DEGREE - SGA_LENGTH + 2)
#endif
#define SGA_MAX_LENGTH 9 /* Do not change */
/*Ramer Douglas Peucker - */
#ifndef RDP_LENGTH
#define RDP_LENGTH 7
#endif
#ifndef SMOOTH_EPSILON
#define SMOOTH_EPSILON 50
#endif
/*Kolmogorov Zurbenko Filter- */
#ifndef KZA_LENGTH
#define KZA_LENGTH 5 /* Window size may 3, 5, or 7 */
#endif
#ifndef KZA_MAX
#define KZA_MAX 4 /* Number of iterations may be 2, 3 or 4 */
#endif
#define KZA_HISTORY_LENGTH ((KZA_LENGTH-1)*KZA_MAX)
#define KZA_MID (KZA_HISTORY_LENGTH)/2
/*Function Prototypes*/
uint16_t* ms_init(uint8_t );
int sma_filter(int current_value, uint16_t history_SMA[]);
int cma_filter(int current_value, void * ptr); /*Pointer not needed. Maintaining consistent interface*/
int ema_filter(int current_value, void * ptr); /*Pointer not needed. Maintaining consistent interface*/
int sga_filter(int current_value, uint16_t history_SGA[]); /*Needs to be tested*/
int kza_filter(int current_value, uint16_t history_KZA[]);
int rdp_filter(int current_value, uint16_t history_RDP[]);/*Iterative neeeds testing*/
int kfa_filter(int current_value, uint16_t history_KFA[]);/*Iterative neeeds testing*/
void ms_deinit(uint16_t *);
#endif