Chris@224: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@224: Chris@224: /* Chris@224: Sonic Visualiser Chris@224: An audio file viewer and annotation editor. Chris@224: Centre for Digital Music, Queen Mary, University of London. Chris@224: This file copyright 2006 Chris Cannam. Chris@224: Chris@224: This program is free software; you can redistribute it and/or Chris@224: modify it under the terms of the GNU General Public License as Chris@224: published by the Free Software Foundation; either version 2 of the Chris@224: License, or (at your option) any later version. See the file Chris@224: COPYING included with this distribution for more information. Chris@224: */ Chris@224: Chris@224: #include "LogRange.h" Chris@573: #include "system/System.h" Chris@224: Chris@224: #include Chris@464: #include Chris@224: #include Chris@224: Chris@224: void Chris@1044: LogRange::mapRange(double &min, double &max, double logthresh) Chris@224: { Chris@1395: static double eps = 1e-10; Chris@1395: Chris@1385: // ensure that max > min: Chris@224: if (min > max) std::swap(min, max); Chris@224: if (max == min) max = min + 1; Chris@224: Chris@1385: if (min >= 0.0) { Chris@224: Chris@1385: // and max > min, so we know min >= 0 and max > 0 Chris@1385: Chris@1385: max = log10(max); Chris@224: Chris@1385: if (min == 0.0) min = std::min(logthresh, max); Chris@1044: else min = log10(min); Chris@224: Chris@1385: } else if (max <= 0.0) { Chris@1385: Chris@1385: // and max > min, so we know min < 0 and max <= 0 Chris@224: Chris@1385: min = log10(-min); Chris@1385: Chris@1385: if (max == 0.0) max = std::min(logthresh, min); Chris@1044: else max = log10(-max); Chris@224: Chris@224: std::swap(min, max); Chris@464: Chris@224: } else { Chris@224: Chris@224: // min < 0 and max > 0 Chris@224: Chris@1044: max = log10(std::max(max, -min)); Chris@224: min = std::min(logthresh, max); Chris@224: } Chris@224: Chris@1395: if (fabs(max - min) < eps) min = max - 1; Chris@224: } Chris@224: Chris@1044: double Chris@1044: LogRange::map(double value, double thresh) Chris@224: { Chris@1385: if (value == 0.0) return thresh; Chris@1044: return log10(fabs(value)); Chris@224: } Chris@224: Chris@1044: double Chris@1044: LogRange::unmap(double value) Chris@266: { Chris@1044: return pow(10.0, value); Chris@266: } Chris@478: Chris@1038: static double Chris@1044: sd(const std::vector &values, int start, int n) Chris@478: { Chris@1385: double sum = 0.0, mean = 0.0, variance = 0.0; Chris@1038: for (int i = 0; i < n; ++i) { Chris@478: sum += values[start + i]; Chris@478: } Chris@478: mean = sum / n; Chris@1038: for (int i = 0; i < n; ++i) { Chris@1038: double diff = values[start + i] - mean; Chris@478: variance += diff * diff; Chris@478: } Chris@478: variance = variance / n; Chris@1038: return sqrt(variance); Chris@478: } Chris@478: Chris@478: bool Chris@1392: LogRange::shouldUseLogScale(std::vector values) Chris@478: { Chris@478: // Principle: Partition the data into two sets around the median; Chris@478: // calculate the standard deviation of each set; if the two SDs Chris@478: // are very different, it's likely that a log scale would be good. Chris@478: Chris@1038: int n = int(values.size()); Chris@1038: if (n < 4) return false; Chris@478: std::sort(values.begin(), values.end()); Chris@1038: int mi = n / 2; Chris@478: Chris@1038: double sd0 = sd(values, 0, mi); Chris@1038: double sd1 = sd(values, mi, n - mi); Chris@478: Chris@690: SVDEBUG << "LogRange::useLogScale: sd0 = " Chris@687: << sd0 << ", sd1 = " << sd1 << endl; Chris@478: Chris@478: if (sd0 == 0 || sd1 == 0) return false; Chris@478: Chris@478: // I wonder what method of determining "one sd much bigger than Chris@478: // the other" would be appropriate here... Chris@1038: if (std::max(sd0, sd1) / std::min(sd0, sd1) > 10.) return true; Chris@478: else return false; Chris@478: } Chris@478: