-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltering.cpp
executable file
·51 lines (44 loc) · 1022 Bytes
/
filtering.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
/*
* accel.cpp
*
* Created on: Oct 23, 2016
* Author: mike
*/
#include "filtering.h"
void Filters::begin(int sample_size_bits)
{
// default values set to something reasonable but very permissive
maximum = (1<<sample_size_bits)-1;
minimum = -maximum;
max_slew = maximum / 2;
int midpoint = minimum + ((maximum - minimum)/2);
for (int i = 0; i < PREV_SAMPLES_RETAINED; i++)
prev[i] = midpoint;
}
void Filters::setup(int initial_sample, int min, int max, int slew)
{
for (int i = 0; i < PREV_SAMPLES_RETAINED; i++)
prev[i] = initial_sample;
maximum = max;
minimum = min;
max_slew = slew;
}
int Filters::clip(int sample)
{
if (sample < minimum)
return(minimum);
else if (sample > maximum)
return(maximum);
return(sample);
}
int Filters::limit_slew(int sample)
{
int slewed = sample - prev[0];
if (slewed < -max_slew)
prev[0] = prev[0] - max_slew;
else if (slewed > max_slew)
prev[0] = prev[0] + max_slew;
else
prev[0] = sample;
return prev[0];
}