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@1265
|
22 #include <vector>
|
Chris@1265
|
23 #include <algorithm>
|
Chris@1265
|
24 #include <iostream>
|
Chris@1187
|
25
|
Chris@1190
|
26 /**
|
Chris@1193
|
27 * Display normalization types for columns in e.g. grid plots.
|
Chris@1193
|
28 *
|
Chris@1193
|
29 * Max1 means to normalize to max value = 1.0.
|
Chris@1193
|
30 * Sum1 means to normalize to sum of values = 1.0.
|
Chris@1193
|
31 *
|
Chris@1193
|
32 * Hybrid means normalize to max = 1.0 and then multiply by
|
Chris@1193
|
33 * log10 of the max value, to retain some difference between
|
Chris@1193
|
34 * levels of neighbouring columns.
|
Chris@1193
|
35 *
|
Chris@1193
|
36 * Area normalization is handled separately.
|
Chris@1193
|
37 */
|
Chris@1193
|
38 enum class ColumnNormalization {
|
Chris@1193
|
39 None,
|
Chris@1193
|
40 Max1,
|
Chris@1193
|
41 Sum1,
|
Chris@1193
|
42 Hybrid
|
Chris@1193
|
43 };
|
Chris@1193
|
44
|
Chris@1193
|
45 /**
|
Chris@1190
|
46 * Class containing static functions for simple operations on data
|
Chris@1190
|
47 * columns, for use by display layers.
|
Chris@1190
|
48 */
|
Chris@1187
|
49 class ColumnOp
|
Chris@1187
|
50 {
|
Chris@1187
|
51 public:
|
Chris@1190
|
52 /**
|
Chris@1190
|
53 * Column type.
|
Chris@1190
|
54 */
|
Chris@1187
|
55 typedef std::vector<float> Column;
|
Chris@1187
|
56
|
Chris@1190
|
57 /**
|
Chris@1195
|
58 * Scale the given column using the given gain multiplier.
|
Chris@1195
|
59 */
|
Chris@1197
|
60 static Column applyGain(const Column &in, double gain) {
|
Chris@1195
|
61
|
Chris@1197
|
62 if (gain == 1.0) {
|
Chris@1195
|
63 return in;
|
Chris@1195
|
64 }
|
Chris@1195
|
65 Column out;
|
Chris@1195
|
66 out.reserve(in.size());
|
Chris@1195
|
67 for (auto v: in) {
|
Chris@1197
|
68 out.push_back(float(v * gain));
|
Chris@1195
|
69 }
|
Chris@1195
|
70 return out;
|
Chris@1195
|
71 }
|
Chris@1195
|
72
|
Chris@1195
|
73 /**
|
Chris@1265
|
74 * Scale an FFT output downward by half the FFT size.
|
Chris@1190
|
75 */
|
Chris@1187
|
76 static Column fftScale(const Column &in, int fftSize) {
|
Chris@1197
|
77 return applyGain(in, 2.0 / fftSize);
|
Chris@1187
|
78 }
|
Chris@1187
|
79
|
Chris@1190
|
80 /**
|
Chris@1190
|
81 * Determine whether an index points to a local peak.
|
Chris@1190
|
82 */
|
Chris@1187
|
83 static bool isPeak(const Column &in, int ix) {
|
Chris@1265
|
84 if (!in_range_for(in, ix)) {
|
Chris@1265
|
85 return false;
|
Chris@1265
|
86 }
|
Chris@1265
|
87 if (ix == 0) {
|
Chris@1265
|
88 return in[0] >= in[1];
|
Chris@1265
|
89 }
|
Chris@1265
|
90 if (!in_range_for(in, ix+1)) {
|
Chris@1265
|
91 return in[ix] > in[ix-1];
|
Chris@1265
|
92 }
|
Chris@1265
|
93 if (in[ix] < in[ix+1]) {
|
Chris@1265
|
94 return false;
|
Chris@1265
|
95 }
|
Chris@1265
|
96 if (in[ix] <= in[ix-1]) {
|
Chris@1265
|
97 return false;
|
Chris@1265
|
98 }
|
Chris@1187
|
99 return true;
|
Chris@1187
|
100 }
|
Chris@1187
|
101
|
Chris@1190
|
102 /**
|
Chris@1190
|
103 * Return a column containing only the local peak values (all
|
Chris@1190
|
104 * others zero).
|
Chris@1190
|
105 */
|
Chris@1187
|
106 static Column peakPick(const Column &in) {
|
Chris@1187
|
107
|
Chris@1187
|
108 std::vector<float> out(in.size(), 0.f);
|
Chris@1187
|
109 for (int i = 0; in_range_for(in, i); ++i) {
|
Chris@1187
|
110 if (isPeak(in, i)) {
|
Chris@1187
|
111 out[i] = in[i];
|
Chris@1187
|
112 }
|
Chris@1187
|
113 }
|
Chris@1187
|
114
|
Chris@1187
|
115 return out;
|
Chris@1187
|
116 }
|
Chris@1187
|
117
|
Chris@1190
|
118 /**
|
Chris@1190
|
119 * Return a column normalized from the input column according to
|
Chris@1190
|
120 * the given normalization scheme.
|
Chris@1190
|
121 */
|
Chris@1193
|
122 static Column normalize(const Column &in, ColumnNormalization n) {
|
Chris@1187
|
123
|
Chris@1265
|
124 if (n == ColumnNormalization::None || in.empty()) {
|
Chris@1187
|
125 return in;
|
Chris@1187
|
126 }
|
Chris@1187
|
127
|
Chris@1193
|
128 float scale = 1.f;
|
Chris@1193
|
129
|
Chris@1193
|
130 if (n == ColumnNormalization::Sum1) {
|
Chris@1193
|
131
|
Chris@1193
|
132 float sum = 0.f;
|
Chris@1193
|
133
|
Chris@1193
|
134 for (auto v: in) {
|
Chris@1193
|
135 sum += v;
|
Chris@1193
|
136 }
|
Chris@1193
|
137
|
Chris@1193
|
138 if (sum != 0.f) {
|
Chris@1193
|
139 scale = 1.f / sum;
|
Chris@1193
|
140 }
|
Chris@1193
|
141 } else {
|
Chris@1193
|
142
|
Chris@1193
|
143 float max = *max_element(in.begin(), in.end());
|
Chris@1193
|
144
|
Chris@1193
|
145 if (n == ColumnNormalization::Max1) {
|
Chris@1193
|
146 if (max != 0.f) {
|
Chris@1193
|
147 scale = 1.f / max;
|
Chris@1193
|
148 }
|
Chris@1193
|
149 } else if (n == ColumnNormalization::Hybrid) {
|
Chris@1193
|
150 if (max > 0.f) {
|
Chris@1193
|
151 scale = log10f(max + 1.f) / max;
|
Chris@1193
|
152 }
|
Chris@1193
|
153 }
|
Chris@1193
|
154 }
|
Chris@1187
|
155
|
Chris@1197
|
156 return applyGain(in, scale);
|
Chris@1187
|
157 }
|
Chris@1187
|
158
|
Chris@1190
|
159 /**
|
Chris@1190
|
160 * Distribute the given column into a target vector of a different
|
Chris@1190
|
161 * size, optionally using linear interpolation. The binfory vector
|
Chris@1190
|
162 * contains a mapping from y coordinate (i.e. index into the
|
Chris@1265
|
163 * target vector) to bin (i.e. index into the source column). The
|
Chris@1265
|
164 * source column ("in") may be a partial column; it's assumed to
|
Chris@1265
|
165 * contain enough bins to span the destination range, starting
|
Chris@1265
|
166 * with the bin of index minbin.
|
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@1198
|
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@1265
|
184
|
Chris@1265
|
185 std::cerr << "y = " << y << " of " << h << ", sy0 = " << sy0 << ", sy1 = " << sy1 << std::endl;
|
Chris@1187
|
186
|
Chris@1187
|
187 if (interpolate && fabs(sy1 - sy0) < 1.0) {
|
Chris@1187
|
188
|
Chris@1187
|
189 double centre = (sy0 + sy1) / 2;
|
Chris@1187
|
190 double dist = (centre - 0.5) - rint(centre - 0.5);
|
Chris@1187
|
191 int bin = int(centre);
|
Chris@1187
|
192
|
Chris@1187
|
193 int other = (dist < 0 ? (bin-1) : (bin+1));
|
Chris@1187
|
194
|
Chris@1187
|
195 if (bin < 0) bin = 0;
|
Chris@1187
|
196 if (bin >= bins) bin = bins-1;
|
Chris@1187
|
197
|
Chris@1187
|
198 if (other < 0 || other >= bins) {
|
Chris@1187
|
199 other = bin;
|
Chris@1187
|
200 }
|
Chris@1187
|
201
|
Chris@1187
|
202 double prop = 1.0 - fabs(dist);
|
Chris@1187
|
203
|
Chris@1187
|
204 double v0 = in[bin];
|
Chris@1187
|
205 double v1 = in[other];
|
Chris@1187
|
206
|
Chris@1187
|
207 out[y] = float(prop * v0 + (1.0 - prop) * v1);
|
Chris@1187
|
208
|
Chris@1187
|
209 } else { // not interpolating this one
|
Chris@1187
|
210
|
Chris@1187
|
211 int by0 = int(sy0 + 0.0001);
|
Chris@1187
|
212 int by1 = int(sy1 + 0.0001);
|
Chris@1187
|
213 if (by1 < by0 + 1) by1 = by0 + 1;
|
Chris@1253
|
214 if (by1 >= bins) by1 = bins - 1;
|
Chris@1198
|
215
|
Chris@1253
|
216 for (int bin = by0; bin <= by1; ++bin) {
|
Chris@1187
|
217
|
Chris@1187
|
218 float value = in[bin];
|
Chris@1187
|
219
|
Chris@1201
|
220 if (bin == by0 || value > out[y]) {
|
Chris@1187
|
221 out[y] = value;
|
Chris@1187
|
222 }
|
Chris@1187
|
223 }
|
Chris@1187
|
224 }
|
Chris@1187
|
225 }
|
Chris@1187
|
226
|
Chris@1187
|
227 return out;
|
Chris@1187
|
228 }
|
Chris@1187
|
229
|
Chris@1187
|
230 };
|
Chris@1187
|
231
|
Chris@1187
|
232 #endif
|
Chris@1187
|
233
|