annotate base/LogRange.cpp @ 1393:04abe8f73b22

Remove commented-out code
author Chris Cannam
date Tue, 28 Feb 2017 11:26:24 +0000
parents 667e369cfeab
children 7e3532d56abb
rev   line source
Chris@224 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@224 2
Chris@224 3 /*
Chris@224 4 Sonic Visualiser
Chris@224 5 An audio file viewer and annotation editor.
Chris@224 6 Centre for Digital Music, Queen Mary, University of London.
Chris@224 7 This file copyright 2006 Chris Cannam.
Chris@224 8
Chris@224 9 This program is free software; you can redistribute it and/or
Chris@224 10 modify it under the terms of the GNU General Public License as
Chris@224 11 published by the Free Software Foundation; either version 2 of the
Chris@224 12 License, or (at your option) any later version. See the file
Chris@224 13 COPYING included with this distribution for more information.
Chris@224 14 */
Chris@224 15
Chris@224 16 #include "LogRange.h"
Chris@573 17 #include "system/System.h"
Chris@224 18
Chris@224 19 #include <algorithm>
Chris@464 20 #include <iostream>
Chris@224 21 #include <cmath>
Chris@224 22
Chris@224 23 void
Chris@1044 24 LogRange::mapRange(double &min, double &max, double logthresh)
Chris@224 25 {
Chris@1385 26 // ensure that max > min:
Chris@224 27 if (min > max) std::swap(min, max);
Chris@224 28 if (max == min) max = min + 1;
Chris@224 29
Chris@1385 30 if (min >= 0.0) {
Chris@224 31
Chris@1385 32 // and max > min, so we know min >= 0 and max > 0
Chris@1385 33
Chris@1385 34 max = log10(max);
Chris@224 35
Chris@1385 36 if (min == 0.0) min = std::min(logthresh, max);
Chris@1044 37 else min = log10(min);
Chris@224 38
Chris@1385 39 } else if (max <= 0.0) {
Chris@1385 40
Chris@1385 41 // and max > min, so we know min < 0 and max <= 0
Chris@224 42
Chris@1385 43 min = log10(-min);
Chris@1385 44
Chris@1385 45 if (max == 0.0) max = std::min(logthresh, min);
Chris@1044 46 else max = log10(-max);
Chris@224 47
Chris@224 48 std::swap(min, max);
Chris@464 49
Chris@224 50 } else {
Chris@224 51
Chris@224 52 // min < 0 and max > 0
Chris@224 53
Chris@1044 54 max = log10(std::max(max, -min));
Chris@224 55 min = std::min(logthresh, max);
Chris@224 56 }
Chris@224 57
Chris@224 58 if (min == max) min = max - 1;
Chris@224 59 }
Chris@224 60
Chris@1044 61 double
Chris@1044 62 LogRange::map(double value, double thresh)
Chris@224 63 {
Chris@1385 64 if (value == 0.0) return thresh;
Chris@1044 65 return log10(fabs(value));
Chris@224 66 }
Chris@224 67
Chris@1044 68 double
Chris@1044 69 LogRange::unmap(double value)
Chris@266 70 {
Chris@1044 71 return pow(10.0, value);
Chris@266 72 }
Chris@478 73
Chris@1038 74 static double
Chris@1044 75 sd(const std::vector<double> &values, int start, int n)
Chris@478 76 {
Chris@1385 77 double sum = 0.0, mean = 0.0, variance = 0.0;
Chris@1038 78 for (int i = 0; i < n; ++i) {
Chris@478 79 sum += values[start + i];
Chris@478 80 }
Chris@478 81 mean = sum / n;
Chris@1038 82 for (int i = 0; i < n; ++i) {
Chris@1038 83 double diff = values[start + i] - mean;
Chris@478 84 variance += diff * diff;
Chris@478 85 }
Chris@478 86 variance = variance / n;
Chris@1038 87 return sqrt(variance);
Chris@478 88 }
Chris@478 89
Chris@478 90 bool
Chris@1392 91 LogRange::shouldUseLogScale(std::vector<double> values)
Chris@478 92 {
Chris@478 93 // Principle: Partition the data into two sets around the median;
Chris@478 94 // calculate the standard deviation of each set; if the two SDs
Chris@478 95 // are very different, it's likely that a log scale would be good.
Chris@478 96
Chris@1038 97 int n = int(values.size());
Chris@1038 98 if (n < 4) return false;
Chris@478 99 std::sort(values.begin(), values.end());
Chris@1038 100 int mi = n / 2;
Chris@478 101
Chris@1038 102 double sd0 = sd(values, 0, mi);
Chris@1038 103 double sd1 = sd(values, mi, n - mi);
Chris@478 104
Chris@690 105 SVDEBUG << "LogRange::useLogScale: sd0 = "
Chris@687 106 << sd0 << ", sd1 = " << sd1 << endl;
Chris@478 107
Chris@478 108 if (sd0 == 0 || sd1 == 0) return false;
Chris@478 109
Chris@478 110 // I wonder what method of determining "one sd much bigger than
Chris@478 111 // the other" would be appropriate here...
Chris@1038 112 if (std::max(sd0, sd1) / std::min(sd0, sd1) > 10.) return true;
Chris@478 113 else return false;
Chris@478 114 }
Chris@478 115