comparison base/LogRange.cpp @ 1044:31f01931b781 cxx11

Move to using double rather than float for floating-point calculations (float only for storage); more build fixes
author Chris Cannam
date Mon, 09 Mar 2015 12:02:10 +0000
parents cc27f35aa75c
children d4212687520e
comparison
equal deleted inserted replaced
1043:fe39581d249b 1044:31f01931b781
19 #include <algorithm> 19 #include <algorithm>
20 #include <iostream> 20 #include <iostream>
21 #include <cmath> 21 #include <cmath>
22 22
23 void 23 void
24 LogRange::mapRange(float &min, float &max, float logthresh) 24 LogRange::mapRange(double &min, double &max, double logthresh)
25 { 25 {
26 if (min > max) std::swap(min, max); 26 if (min > max) std::swap(min, max);
27 if (max == min) max = min + 1; 27 if (max == min) max = min + 1;
28 28
29 // SVDEBUG << "LogRange::mapRange: min = " << min << ", max = " << max << endl; 29 // SVDEBUG << "LogRange::mapRange: min = " << min << ", max = " << max << endl;
30 30
31 if (min >= 0.f) { 31 if (min >= 0.f) {
32 32
33 max = log10f(max); // we know max != 0 33 max = log10(max); // we know max != 0
34 34
35 if (min == 0.f) min = std::min(logthresh, max); 35 if (min == 0.f) min = std::min(logthresh, max);
36 else min = log10f(min); 36 else min = log10(min);
37 37
38 // SVDEBUG << "LogRange::mapRange: positive: min = " << min << ", max = " << max << endl; 38 // SVDEBUG << "LogRange::mapRange: positive: min = " << min << ", max = " << max << endl;
39 39
40 } else if (max <= 0.f) { 40 } else if (max <= 0.f) {
41 41
42 min = log10f(-min); // we know min != 0 42 min = log10(-min); // we know min != 0
43 43
44 if (max == 0.f) max = std::min(logthresh, min); 44 if (max == 0.f) max = std::min(logthresh, min);
45 else max = log10f(-max); 45 else max = log10(-max);
46 46
47 std::swap(min, max); 47 std::swap(min, max);
48 48
49 // SVDEBUG << "LogRange::mapRange: negative: min = " << min << ", max = " << max << endl; 49 // SVDEBUG << "LogRange::mapRange: negative: min = " << min << ", max = " << max << endl;
50 50
51 } else { 51 } else {
52 52
53 // min < 0 and max > 0 53 // min < 0 and max > 0
54 54
55 max = log10f(std::max(max, -min)); 55 max = log10(std::max(max, -min));
56 min = std::min(logthresh, max); 56 min = std::min(logthresh, max);
57 57
58 // SVDEBUG << "LogRange::mapRange: spanning: min = " << min << ", max = " << max << endl; 58 // SVDEBUG << "LogRange::mapRange: spanning: min = " << min << ", max = " << max << endl;
59 } 59 }
60 60
61 if (min == max) min = max - 1; 61 if (min == max) min = max - 1;
62 } 62 }
63 63
64 float 64 double
65 LogRange::map(float value, float thresh) 65 LogRange::map(double value, double thresh)
66 { 66 {
67 if (value == 0.f) return thresh; 67 if (value == 0.f) return thresh;
68 return log10f(fabsf(value)); 68 return log10(fabs(value));
69 } 69 }
70 70
71 float 71 double
72 LogRange::unmap(float value) 72 LogRange::unmap(double value)
73 { 73 {
74 return powf(10.0, value); 74 return pow(10.0, value);
75 } 75 }
76 76
77 static double 77 static double
78 sd(const std::vector<float> &values, int start, int n) 78 sd(const std::vector<double> &values, int start, int n)
79 { 79 {
80 double sum = 0.f, mean = 0.f, variance = 0.f; 80 double sum = 0.f, mean = 0.f, variance = 0.f;
81 for (int i = 0; i < n; ++i) { 81 for (int i = 0; i < n; ++i) {
82 sum += values[start + i]; 82 sum += values[start + i];
83 } 83 }
89 variance = variance / n; 89 variance = variance / n;
90 return sqrt(variance); 90 return sqrt(variance);
91 } 91 }
92 92
93 bool 93 bool
94 LogRange::useLogScale(std::vector<float> values) 94 LogRange::useLogScale(std::vector<double> values)
95 { 95 {
96 // Principle: Partition the data into two sets around the median; 96 // Principle: Partition the data into two sets around the median;
97 // calculate the standard deviation of each set; if the two SDs 97 // calculate the standard deviation of each set; if the two SDs
98 // are very different, it's likely that a log scale would be good. 98 // are very different, it's likely that a log scale would be good.
99 99