annotate base/ColumnOp.h @ 1194:238780e92f86 spectrogram-minor-refactor

Toward updating mag cache
author Chris Cannam
date Wed, 20 Jul 2016 13:40:23 +0100
parents 927d329252bf
children c118d2022ffa
rev   line source
Chris@1187 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1187 2
Chris@1187 3 /*
Chris@1187 4 Sonic Visualiser
Chris@1187 5 An audio file viewer and annotation editor.
Chris@1187 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1188 7 This file copyright 2006-2016 Chris Cannam and QMUL.
Chris@1187 8
Chris@1187 9 This program is free software; you can redistribute it and/or
Chris@1187 10 modify it under the terms of the GNU General Public License as
Chris@1187 11 published by the Free Software Foundation; either version 2 of the
Chris@1187 12 License, or (at your option) any later version. See the file
Chris@1187 13 COPYING included with this distribution for more information.
Chris@1187 14 */
Chris@1187 15
Chris@1187 16 #ifndef COLUMN_OP_H
Chris@1187 17 #define COLUMN_OP_H
Chris@1187 18
Chris@1187 19 #include "BaseTypes.h"
Chris@1187 20
Chris@1187 21 #include <cmath>
Chris@1187 22
Chris@1190 23 /**
Chris@1193 24 * Display normalization types for columns in e.g. grid plots.
Chris@1193 25 *
Chris@1193 26 * Max1 means to normalize to max value = 1.0.
Chris@1193 27 * Sum1 means to normalize to sum of values = 1.0.
Chris@1193 28 *
Chris@1193 29 * Hybrid means normalize to max = 1.0 and then multiply by
Chris@1193 30 * log10 of the max value, to retain some difference between
Chris@1193 31 * levels of neighbouring columns.
Chris@1193 32 *
Chris@1193 33 * Area normalization is handled separately.
Chris@1193 34 */
Chris@1193 35 enum class ColumnNormalization {
Chris@1193 36 None,
Chris@1193 37 Max1,
Chris@1193 38 Sum1,
Chris@1193 39 Hybrid
Chris@1193 40 };
Chris@1193 41
Chris@1193 42 /**
Chris@1190 43 * Class containing static functions for simple operations on data
Chris@1190 44 * columns, for use by display layers.
Chris@1190 45 */
Chris@1187 46 class ColumnOp
Chris@1187 47 {
Chris@1187 48 public:
Chris@1190 49 /**
Chris@1190 50 * Column type.
Chris@1190 51 */
Chris@1187 52 typedef std::vector<float> Column;
Chris@1187 53
Chris@1190 54 /**
Chris@1190 55 * Scale an FFT output by half the FFT size.
Chris@1190 56 */
Chris@1187 57 static Column fftScale(const Column &in, int fftSize) {
Chris@1187 58
Chris@1187 59 Column out;
Chris@1187 60 out.reserve(in.size());
Chris@1187 61 float scale = 2.f / float(fftSize);
Chris@1187 62 for (auto v: in) {
Chris@1187 63 out.push_back(v * scale);
Chris@1187 64 }
Chris@1187 65
Chris@1187 66 return out;
Chris@1187 67 }
Chris@1187 68
Chris@1190 69 /**
Chris@1190 70 * Determine whether an index points to a local peak.
Chris@1190 71 */
Chris@1187 72 static bool isPeak(const Column &in, int ix) {
Chris@1187 73
Chris@1187 74 if (!in_range_for(in, ix-1)) return false;
Chris@1187 75 if (!in_range_for(in, ix+1)) return false;
Chris@1187 76 if (in[ix] < in[ix+1]) return false;
Chris@1187 77 if (in[ix] < in[ix-1]) return false;
Chris@1187 78
Chris@1187 79 return true;
Chris@1187 80 }
Chris@1187 81
Chris@1190 82 /**
Chris@1190 83 * Return a column containing only the local peak values (all
Chris@1190 84 * others zero).
Chris@1190 85 */
Chris@1187 86 static Column peakPick(const Column &in) {
Chris@1187 87
Chris@1187 88 std::vector<float> out(in.size(), 0.f);
Chris@1187 89 for (int i = 0; in_range_for(in, i); ++i) {
Chris@1187 90 if (isPeak(in, i)) {
Chris@1187 91 out[i] = in[i];
Chris@1187 92 }
Chris@1187 93 }
Chris@1187 94
Chris@1187 95 return out;
Chris@1187 96 }
Chris@1187 97
Chris@1190 98 /**
Chris@1190 99 * Return a column normalized from the input column according to
Chris@1190 100 * the given normalization scheme.
Chris@1190 101 */
Chris@1193 102 static Column normalize(const Column &in, ColumnNormalization n) {
Chris@1187 103
Chris@1193 104 if (n == ColumnNormalization::None) {
Chris@1187 105 return in;
Chris@1187 106 }
Chris@1187 107
Chris@1193 108 float scale = 1.f;
Chris@1193 109
Chris@1193 110 if (n == ColumnNormalization::Sum1) {
Chris@1193 111
Chris@1193 112 float sum = 0.f;
Chris@1193 113
Chris@1193 114 for (auto v: in) {
Chris@1193 115 sum += v;
Chris@1193 116 }
Chris@1193 117
Chris@1193 118 if (sum != 0.f) {
Chris@1193 119 scale = 1.f / sum;
Chris@1193 120 }
Chris@1193 121 } else {
Chris@1193 122
Chris@1193 123 float max = *max_element(in.begin(), in.end());
Chris@1193 124
Chris@1193 125 if (n == ColumnNormalization::Max1) {
Chris@1193 126 if (max != 0.f) {
Chris@1193 127 scale = 1.f / max;
Chris@1193 128 }
Chris@1193 129 } else if (n == ColumnNormalization::Hybrid) {
Chris@1193 130 if (max > 0.f) {
Chris@1193 131 scale = log10f(max + 1.f) / max;
Chris@1193 132 }
Chris@1193 133 }
Chris@1193 134 }
Chris@1187 135
Chris@1187 136 std::vector<float> out;
Chris@1187 137 out.reserve(in.size());
Chris@1187 138
Chris@1187 139 for (auto v: in) {
Chris@1187 140 out.push_back(v * scale);
Chris@1187 141 }
Chris@1193 142
Chris@1187 143 return out;
Chris@1187 144 }
Chris@1187 145
Chris@1190 146 /**
Chris@1190 147 * Scale the given column using the given gain multiplier.
Chris@1190 148 */
Chris@1187 149 static Column applyGain(const Column &in, float gain) {
Chris@1187 150
Chris@1187 151 if (gain == 1.f) {
Chris@1187 152 return in;
Chris@1187 153 }
Chris@1187 154 Column out;
Chris@1187 155 out.reserve(in.size());
Chris@1187 156 for (auto v: in) {
Chris@1187 157 out.push_back(v * gain);
Chris@1187 158 }
Chris@1187 159 return out;
Chris@1187 160 }
Chris@1187 161
Chris@1190 162 /**
Chris@1190 163 * Distribute the given column into a target vector of a different
Chris@1190 164 * size, optionally using linear interpolation. The binfory vector
Chris@1190 165 * contains a mapping from y coordinate (i.e. index into the
Chris@1190 166 * target vector) to bin (i.e. index into the source column).
Chris@1190 167 */
Chris@1187 168 static Column distribute(const Column &in,
Chris@1187 169 int h,
Chris@1187 170 const std::vector<double> &binfory,
Chris@1187 171 int minbin,
Chris@1187 172 bool interpolate) {
Chris@1187 173
Chris@1187 174 std::vector<float> out(h, 0.f);
Chris@1187 175 int bins = int(in.size());
Chris@1187 176
Chris@1187 177 for (int y = 0; y < h; ++y) {
Chris@1187 178
Chris@1187 179 double sy0 = binfory[y] - minbin;
Chris@1187 180 double sy1 = sy0 + 1;
Chris@1187 181 if (y+1 < h) {
Chris@1187 182 sy1 = binfory[y+1] - minbin;
Chris@1187 183 }
Chris@1187 184
Chris@1187 185 if (interpolate && fabs(sy1 - sy0) < 1.0) {
Chris@1187 186
Chris@1187 187 double centre = (sy0 + sy1) / 2;
Chris@1187 188 double dist = (centre - 0.5) - rint(centre - 0.5);
Chris@1187 189 int bin = int(centre);
Chris@1187 190
Chris@1187 191 int other = (dist < 0 ? (bin-1) : (bin+1));
Chris@1187 192
Chris@1187 193 if (bin < 0) bin = 0;
Chris@1187 194 if (bin >= bins) bin = bins-1;
Chris@1187 195
Chris@1187 196 if (other < 0 || other >= bins) {
Chris@1187 197 other = bin;
Chris@1187 198 }
Chris@1187 199
Chris@1187 200 double prop = 1.0 - fabs(dist);
Chris@1187 201
Chris@1187 202 double v0 = in[bin];
Chris@1187 203 double v1 = in[other];
Chris@1187 204
Chris@1187 205 out[y] = float(prop * v0 + (1.0 - prop) * v1);
Chris@1187 206
Chris@1187 207 } else { // not interpolating this one
Chris@1187 208
Chris@1187 209 int by0 = int(sy0 + 0.0001);
Chris@1187 210 int by1 = int(sy1 + 0.0001);
Chris@1187 211 if (by1 < by0 + 1) by1 = by0 + 1;
Chris@1187 212
Chris@1187 213 for (int bin = by0; bin < by1; ++bin) {
Chris@1187 214
Chris@1187 215 float value = in[bin];
Chris@1187 216
Chris@1187 217 if (value > out[y]) {
Chris@1187 218 out[y] = value;
Chris@1187 219 }
Chris@1187 220 }
Chris@1187 221 }
Chris@1187 222 }
Chris@1187 223
Chris@1187 224 return out;
Chris@1187 225 }
Chris@1187 226
Chris@1187 227 };
Chris@1187 228
Chris@1187 229 #endif
Chris@1187 230