comparison base/LogRange.cpp @ 1527:710e6250a401 zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:14 +0100
parents 7e3532d56abb
children
comparison
equal deleted inserted replaced
1324:d4a28d1479a8 1527:710e6250a401
21 #include <cmath> 21 #include <cmath>
22 22
23 void 23 void
24 LogRange::mapRange(double &min, double &max, double logthresh) 24 LogRange::mapRange(double &min, double &max, double logthresh)
25 { 25 {
26 static double eps = 1e-10;
27
28 // ensure that max > min:
26 if (min > max) std::swap(min, max); 29 if (min > max) std::swap(min, max);
27 if (max == min) max = min + 1; 30 if (max == min) max = min + 1;
28 31
29 // cerr << "LogRange::mapRange: min = " << min << ", max = " << max << endl; 32 if (min >= 0.0) {
30
31 if (min >= 0.f) {
32 33
33 max = log10(max); // we know max != 0 34 // and max > min, so we know min >= 0 and max > 0
35
36 max = log10(max);
34 37
35 if (min == 0.f) min = std::min(logthresh, max); 38 if (min == 0.0) min = std::min(logthresh, max);
36 else min = log10(min); 39 else min = log10(min);
37 40
38 // cerr << "LogRange::mapRange: positive: min = " << min << ", max = " << max << endl; 41 } else if (max <= 0.0) {
39 42
40 } else if (max <= 0.f) { 43 // and max > min, so we know min < 0 and max <= 0
41 44
42 min = log10(-min); // we know min != 0 45 min = log10(-min);
43 46
44 if (max == 0.f) max = std::min(logthresh, min); 47 if (max == 0.0) max = std::min(logthresh, min);
45 else max = log10(-max); 48 else max = log10(-max);
46 49
47 std::swap(min, max); 50 std::swap(min, max);
48
49 // cerr << "LogRange::mapRange: negative: min = " << min << ", max = " << max << endl;
50 51
51 } else { 52 } else {
52 53
53 // min < 0 and max > 0 54 // min < 0 and max > 0
54 55
55 max = log10(std::max(max, -min)); 56 max = log10(std::max(max, -min));
56 min = std::min(logthresh, max); 57 min = std::min(logthresh, max);
57
58 // cerr << "LogRange::mapRange: spanning: min = " << min << ", max = " << max << endl;
59 } 58 }
60 59
61 if (min == max) min = max - 1; 60 if (fabs(max - min) < eps) min = max - 1;
62 } 61 }
63 62
64 double 63 double
65 LogRange::map(double value, double thresh) 64 LogRange::map(double value, double thresh)
66 { 65 {
67 if (value == 0.f) return thresh; 66 if (value == 0.0) return thresh;
68 return log10(fabs(value)); 67 return log10(fabs(value));
69 } 68 }
70 69
71 double 70 double
72 LogRange::unmap(double value) 71 LogRange::unmap(double value)
75 } 74 }
76 75
77 static double 76 static double
78 sd(const std::vector<double> &values, int start, int n) 77 sd(const std::vector<double> &values, int start, int n)
79 { 78 {
80 double sum = 0.f, mean = 0.f, variance = 0.f; 79 double sum = 0.0, mean = 0.0, variance = 0.0;
81 for (int i = 0; i < n; ++i) { 80 for (int i = 0; i < n; ++i) {
82 sum += values[start + i]; 81 sum += values[start + i];
83 } 82 }
84 mean = sum / n; 83 mean = sum / n;
85 for (int i = 0; i < n; ++i) { 84 for (int i = 0; i < n; ++i) {
89 variance = variance / n; 88 variance = variance / n;
90 return sqrt(variance); 89 return sqrt(variance);
91 } 90 }
92 91
93 bool 92 bool
94 LogRange::useLogScale(std::vector<double> values) 93 LogRange::shouldUseLogScale(std::vector<double> values)
95 { 94 {
96 // Principle: Partition the data into two sets around the median; 95 // Principle: Partition the data into two sets around the median;
97 // calculate the standard deviation of each set; if the two SDs 96 // 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. 97 // are very different, it's likely that a log scale would be good.
99 98