-
Notifications
You must be signed in to change notification settings - Fork 29
/
benchLookup.ino
102 lines (85 loc) · 1.99 KB
/
benchLookup.ino
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
void setup() {
Serial.begin(115200);
// Create lookup table
float lu[199];
for(int i = -99; i <100;i++){
lu[i+99]=pow10f(i/20.0);
}
// test data ranges between -10 and 10 dB
float temp=-10.0;
float val[200];
for(int i = 0; i <200;i++){
temp+=.1;
val[i]= temp;
}
// exact solution
float outvalEx[200];
unsigned long t1 =micros();
for(int i = 0; i <200;i++){
outvalEx[i]= pow10f(val[i]/20.0f);
}
unsigned long t2 =micros();
Serial.print("Exact Method: ");
Serial.print((t2-t1)/200.0);
Serial.println(" Microseconds per transform");
// approximate method
float outvalAppi[200];
t1 =micros();
for(int i = 0; i <200;i++){
outvalAppi[i]= lu[(int)val[i]+99];
}
t2=micros();
Serial.print("Approximate Method(lookup): ");
Serial.print((t2-t1)/200.0);
Serial.println(" Microseconds per transform");
// approximate method
float frac;
float outvalApp[200];
t1 =micros();
for(int i = 0; i <200;i++){
float input =val[i];
if(input<0){
int integ= (int)input;
integ-=1;
frac= input-integ;
outvalApp[i]= 1.0f+ (0.1146f+0.0074f*frac)*frac;
outvalApp[i]*=lu[integ+99];
}else{
int integ= (int)input;
frac= input-integ;
outvalApp[i]= 1.0f+ (0.1146f+0.0074f*frac)*frac;
outvalApp[i]*=lu[integ+99];
}
}
t2=micros();
Serial.print("Approximate Method: ");
Serial.print((t2-t1)/200.0);
Serial.println(" Microseconds per transform");
float err=0.0;
float maxerr=0.0;
// Approximation error(lookup)
for(int i = 0; i <200;i++){
err= abs((outvalAppi[i]-outvalEx[i])/outvalEx[i]*100);
if(err>maxerr){
maxerr=err;
}
}
Serial.print("Approximate method(lookup) max error is: ");
Serial.print(maxerr);
Serial.println("%");
maxerr=0;
// Approximation error(polynomial)
for(int i = 0; i <200;i++){
err= abs((outvalApp[i]-outvalEx[i])/outvalEx[i]*100);
if(err>maxerr){
maxerr=err;
}
}
Serial.print("Approximate method (polynomial) max error is: ");
Serial.print(maxerr);
Serial.println("%");
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
}