-
Notifications
You must be signed in to change notification settings - Fork 18
/
lowpass_test.cc
85 lines (72 loc) · 1.96 KB
/
lowpass_test.cc
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include "lowpass2.h"
/* 4% overshoot
class LowPass2
{ public:
int32_t Out1, Out;
public:
void Set(uint32_t Out=0) { Out1=Out2=Out; }
int32_t Process( int32_t Inp)
{ Out1 = (Inp-Out2+32*Out1+16)>>5;
Out = ( Out1+15*Out2+ 8)>>4;
return Out; }
} ;
*/
/*
// possibly optimal, (almost) no overshoot
template <class Int>
class LowPass2
{ public:
static const Int InpScale = 16;
static const Int IntegScale1 = 10;
static const Int IntegScale2 = IntegScale1-2;
Int Out1, Out;
public:
void Set(Int Inp=0) { Out1=Out = Inp<<InpScale; }
Int Process( Int Inp)
{ Inp<<=InpScale;
Out1 = ( Inp -Out + (Out1<<IntegScale1) + (1<<(IntegScale1-1)) )>>IntegScale1;
Out = ( Out1-Out + (Out <<IntegScale2) + (1<<(IntegScale2-1)) )>>IntegScale2;
return Out ; } // return fractional result
} ;
// Out = ( Out1-Out + 276*Out + 138)/276;
*/
/*
class LowPass2
{ public:
int32_t Out1, Out2;
public:
void Set(uint32_t Out=0) { Out1=Out2=Out; }
int32_t Process( int32_t Inp)
{ Out1 = (Inp-Out2+256*Out1+128)>>8;
Out2 = ( Out1+ 72*Out2+ 36)/73;
return Out2; }
} ;
*/
/*
class LowPass2 // IIR bilinear ?
{ public:
int32_t Hist1, Hist2;
public:
void Set(uint32_t Inp=0) { Hist1=Hist2=Inp; }
int32_t Process( int32_t Inp)
{ int32_t Hist = ( Inp + (Hist1<<5) - Hist2 + 16)>>5;
int32_t Out = ( Hist + (Hist1<<2) + Hist2 + 2)>>2;
Hist2 = Hist1; Hist1 = Hist; return Out; }
} ;
*/
int main(int argc, char *argv[])
{ LowPass2<uint32_t,8,6,8> LowPass;
LowPass.Set(2*115);
time_t Now; time(&Now); srand(Now);
for(int T=(-1000); T<2000; T++)
{ int32_t Inp = (T>=0) ? 2*115:0;
// Inp+= (rand()%33)-16;
uint32_t Out = LowPass.Process(Inp);
printf("%+8d: %+10d %+15.3f (%10d,%10d)\n",
T, Inp, (double)LowPass.Out/(1<<8), LowPass.Out1, LowPass.Out);
}
return 0; }