comparison base/LogRange.cpp @ 1038:cc27f35aa75c cxx11

Introducing the signed 64-bit frame index type, and fixing build failures from inclusion of -Wconversion with -Werror. Not finished yet.
author Chris Cannam
date Tue, 03 Mar 2015 15:18:24 +0000
parents 1424aa29ae95
children 31f01931b781
comparison
equal deleted inserted replaced
1037:bf0e5944289b 1038:cc27f35aa75c
72 LogRange::unmap(float value) 72 LogRange::unmap(float value)
73 { 73 {
74 return powf(10.0, value); 74 return powf(10.0, value);
75 } 75 }
76 76
77 static float 77 static double
78 sd(const std::vector<float> &values, size_t start, size_t n) 78 sd(const std::vector<float> &values, int start, int n)
79 { 79 {
80 float sum = 0.f, mean = 0.f, variance = 0.f; 80 double sum = 0.f, mean = 0.f, variance = 0.f;
81 for (size_t 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 }
84 mean = sum / n; 84 mean = sum / n;
85 for (size_t i = 0; i < n; ++i) { 85 for (int i = 0; i < n; ++i) {
86 float diff = values[start + i] - mean; 86 double diff = values[start + i] - mean;
87 variance += diff * diff; 87 variance += diff * diff;
88 } 88 }
89 variance = variance / n; 89 variance = variance / n;
90 return sqrtf(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<float> 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
100 if (values.size() < 4) return false; 100 int n = int(values.size());
101 if (n < 4) return false;
101 std::sort(values.begin(), values.end()); 102 std::sort(values.begin(), values.end());
102 size_t mi = values.size() / 2; 103 int mi = n / 2;
103 104
104 float sd0 = sd(values, 0, mi); 105 double sd0 = sd(values, 0, mi);
105 float sd1 = sd(values, mi, values.size() - mi); 106 double sd1 = sd(values, mi, n - mi);
106 107
107 SVDEBUG << "LogRange::useLogScale: sd0 = " 108 SVDEBUG << "LogRange::useLogScale: sd0 = "
108 << sd0 << ", sd1 = " << sd1 << endl; 109 << sd0 << ", sd1 = " << sd1 << endl;
109 110
110 if (sd0 == 0 || sd1 == 0) return false; 111 if (sd0 == 0 || sd1 == 0) return false;
111 112
112 // I wonder what method of determining "one sd much bigger than 113 // I wonder what method of determining "one sd much bigger than
113 // the other" would be appropriate here... 114 // the other" would be appropriate here...
114 if (std::max(sd0, sd1) / std::min(sd0, sd1) > 10.f) return true; 115 if (std::max(sd0, sd1) / std::min(sd0, sd1) > 10.) return true;
115 else return false; 116 else return false;
116 } 117 }
117 118