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