annotate base/ColumnOp.h @ 1192:9884efa1f88a spectrogram-minor-refactor

Fix potential crash in Dense3dModelPeakCache owing to combination of wrong width calculation and wrong handling of width overrun
author Chris Cannam
date Thu, 14 Jul 2016 14:49:04 +0100
parents f6998e304b36
children 927d329252bf
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@1190 24 * Class containing static functions for simple operations on data
Chris@1190 25 * columns, for use by display layers.
Chris@1190 26 */
Chris@1187 27 class ColumnOp
Chris@1187 28 {
Chris@1187 29 public:
Chris@1190 30 /**
Chris@1190 31 * Column type.
Chris@1190 32 */
Chris@1187 33 typedef std::vector<float> Column;
Chris@1187 34
Chris@1190 35 /**
Chris@1190 36 * Normalization types.
Chris@1190 37 *
Chris@1190 38 * NormalizeColumns means to normalize to max value = 1.
Chris@1190 39 * NormalizeHybrid means normalize to max = 1 and then multiply by
Chris@1190 40 * log10 of the max value, to retain some difference between
Chris@1190 41 * levels of neighbouring columns.
Chris@1190 42 *
Chris@1190 43 * NormalizeVisibleArea is ignored here and is included only so as
Chris@1190 44 * to match the set of normalization options historically provided
Chris@1190 45 * in the SV spectrogram layer.
Chris@1190 46 */
Chris@1187 47 enum Normalization {
Chris@1187 48 NoNormalization,
Chris@1187 49 NormalizeColumns,
Chris@1187 50 NormalizeVisibleArea,
Chris@1187 51 NormalizeHybrid
Chris@1187 52 };
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@1187 102 static Column normalize(const Column &in, Normalization n) {
Chris@1187 103
Chris@1187 104 if (n == NoNormalization || n == NormalizeVisibleArea) {
Chris@1187 105 return in;
Chris@1187 106 }
Chris@1187 107
Chris@1187 108 float max = *max_element(in.begin(), in.end());
Chris@1187 109
Chris@1187 110 if (n == NormalizeColumns && max == 0.f) {
Chris@1187 111 return in;
Chris@1187 112 }
Chris@1187 113
Chris@1187 114 if (n == NormalizeHybrid && max <= 0.f) {
Chris@1187 115 return in;
Chris@1187 116 }
Chris@1187 117
Chris@1187 118 std::vector<float> out;
Chris@1187 119 out.reserve(in.size());
Chris@1187 120
Chris@1187 121 float scale;
Chris@1187 122 if (n == NormalizeHybrid) {
Chris@1187 123 scale = log10f(max + 1.f) / max;
Chris@1187 124 } else {
Chris@1187 125 scale = 1.f / max;
Chris@1187 126 }
Chris@1187 127
Chris@1187 128 for (auto v: in) {
Chris@1187 129 out.push_back(v * scale);
Chris@1187 130 }
Chris@1187 131 return out;
Chris@1187 132 }
Chris@1187 133
Chris@1190 134 /**
Chris@1190 135 * Scale the given column using the given gain multiplier.
Chris@1190 136 */
Chris@1187 137 static Column applyGain(const Column &in, float gain) {
Chris@1187 138
Chris@1187 139 if (gain == 1.f) {
Chris@1187 140 return in;
Chris@1187 141 }
Chris@1187 142 Column out;
Chris@1187 143 out.reserve(in.size());
Chris@1187 144 for (auto v: in) {
Chris@1187 145 out.push_back(v * gain);
Chris@1187 146 }
Chris@1187 147 return out;
Chris@1187 148 }
Chris@1187 149
Chris@1190 150 /**
Chris@1190 151 * Distribute the given column into a target vector of a different
Chris@1190 152 * size, optionally using linear interpolation. The binfory vector
Chris@1190 153 * contains a mapping from y coordinate (i.e. index into the
Chris@1190 154 * target vector) to bin (i.e. index into the source column).
Chris@1190 155 */
Chris@1187 156 static Column distribute(const Column &in,
Chris@1187 157 int h,
Chris@1187 158 const std::vector<double> &binfory,
Chris@1187 159 int minbin,
Chris@1187 160 bool interpolate) {
Chris@1187 161
Chris@1187 162 std::vector<float> out(h, 0.f);
Chris@1187 163 int bins = int(in.size());
Chris@1187 164
Chris@1187 165 for (int y = 0; y < h; ++y) {
Chris@1187 166
Chris@1187 167 double sy0 = binfory[y] - minbin;
Chris@1187 168 double sy1 = sy0 + 1;
Chris@1187 169 if (y+1 < h) {
Chris@1187 170 sy1 = binfory[y+1] - minbin;
Chris@1187 171 }
Chris@1187 172
Chris@1187 173 if (interpolate && fabs(sy1 - sy0) < 1.0) {
Chris@1187 174
Chris@1187 175 double centre = (sy0 + sy1) / 2;
Chris@1187 176 double dist = (centre - 0.5) - rint(centre - 0.5);
Chris@1187 177 int bin = int(centre);
Chris@1187 178
Chris@1187 179 int other = (dist < 0 ? (bin-1) : (bin+1));
Chris@1187 180
Chris@1187 181 if (bin < 0) bin = 0;
Chris@1187 182 if (bin >= bins) bin = bins-1;
Chris@1187 183
Chris@1187 184 if (other < 0 || other >= bins) {
Chris@1187 185 other = bin;
Chris@1187 186 }
Chris@1187 187
Chris@1187 188 double prop = 1.0 - fabs(dist);
Chris@1187 189
Chris@1187 190 double v0 = in[bin];
Chris@1187 191 double v1 = in[other];
Chris@1187 192
Chris@1187 193 out[y] = float(prop * v0 + (1.0 - prop) * v1);
Chris@1187 194
Chris@1187 195 } else { // not interpolating this one
Chris@1187 196
Chris@1187 197 int by0 = int(sy0 + 0.0001);
Chris@1187 198 int by1 = int(sy1 + 0.0001);
Chris@1187 199 if (by1 < by0 + 1) by1 = by0 + 1;
Chris@1187 200
Chris@1187 201 for (int bin = by0; bin < by1; ++bin) {
Chris@1187 202
Chris@1187 203 float value = in[bin];
Chris@1187 204
Chris@1187 205 if (value > out[y]) {
Chris@1187 206 out[y] = value;
Chris@1187 207 }
Chris@1187 208 }
Chris@1187 209 }
Chris@1187 210 }
Chris@1187 211
Chris@1187 212 return out;
Chris@1187 213 }
Chris@1187 214
Chris@1187 215 };
Chris@1187 216
Chris@1187 217 #endif
Chris@1187 218