svcore  1.9
LogRange.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "LogRange.h"
17 #include "system/System.h"
18 
19 #include <algorithm>
20 #include <iostream>
21 #include <cmath>
22 
23 void
24 LogRange::mapRange(float &min, float &max, float logthresh)
25 {
26  if (min > max) std::swap(min, max);
27  if (max == min) max = min + 1;
28 
29 // SVDEBUG << "LogRange::mapRange: min = " << min << ", max = " << max << endl;
30 
31  if (min >= 0.f) {
32 
33  max = log10f(max); // we know max != 0
34 
35  if (min == 0.f) min = std::min(logthresh, max);
36  else min = log10f(min);
37 
38 // SVDEBUG << "LogRange::mapRange: positive: min = " << min << ", max = " << max << endl;
39 
40  } else if (max <= 0.f) {
41 
42  min = log10f(-min); // we know min != 0
43 
44  if (max == 0.f) max = std::min(logthresh, min);
45  else max = log10f(-max);
46 
47  std::swap(min, max);
48 
49 // SVDEBUG << "LogRange::mapRange: negative: min = " << min << ", max = " << max << endl;
50 
51  } else {
52 
53  // min < 0 and max > 0
54 
55  max = log10f(std::max(max, -min));
56  min = std::min(logthresh, max);
57 
58 // SVDEBUG << "LogRange::mapRange: spanning: min = " << min << ", max = " << max << endl;
59  }
60 
61  if (min == max) min = max - 1;
62 }
63 
64 float
65 LogRange::map(float value, float thresh)
66 {
67  if (value == 0.f) return thresh;
68  return log10f(fabsf(value));
69 }
70 
71 float
72 LogRange::unmap(float value)
73 {
74  return powf(10.0, value);
75 }
76 
77 static float
78 sd(const std::vector<float> &values, size_t start, size_t n)
79 {
80  float sum = 0.f, mean = 0.f, variance = 0.f;
81  for (size_t i = 0; i < n; ++i) {
82  sum += values[start + i];
83  }
84  mean = sum / n;
85  for (size_t i = 0; i < n; ++i) {
86  float diff = values[start + i] - mean;
87  variance += diff * diff;
88  }
89  variance = variance / n;
90  return sqrtf(variance);
91 }
92 
93 bool
94 LogRange::useLogScale(std::vector<float> values)
95 {
96  // Principle: Partition the data into two sets around the median;
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.
99 
100  if (values.size() < 4) return false;
101  std::sort(values.begin(), values.end());
102  size_t mi = values.size() / 2;
103 
104  float sd0 = sd(values, 0, mi);
105  float sd1 = sd(values, mi, values.size() - mi);
106 
107  SVDEBUG << "LogRange::useLogScale: sd0 = "
108  << sd0 << ", sd1 = " << sd1 << endl;
109 
110  if (sd0 == 0 || sd1 == 0) return false;
111 
112  // I wonder what method of determining "one sd much bigger than
113  // the other" would be appropriate here...
114  if (std::max(sd0, sd1) / std::min(sd0, sd1) > 10.f) return true;
115  else return false;
116 }
117 
static float sd(const std::vector< float > &values, size_t start, size_t n)
Definition: LogRange.cpp:78
static void mapRange(float &min, float &max, float thresh=-10)
Map a linear range onto a logarithmic range.
Definition: LogRange.cpp:24
static bool useLogScale(std::vector< float > values)
Estimate whether a set of values would be more properly shown using a logarithmic than a linear scale...
Definition: LogRange.cpp:94
static float map(float value, float thresh=-10)
Map a value onto a logarithmic range.
Definition: LogRange.cpp:65
static float unmap(float value)
Map a value from the logarithmic range back again.
Definition: LogRange.cpp:72
#define SVDEBUG
Definition: Debug.h:42