comparison base/ColumnOp.h @ 1190:f6998e304b36 spectrogram-minor-refactor

Comments and naming
author Chris Cannam
date Thu, 23 Jun 2016 14:43:00 +0100
parents d9698ee93659
children 927d329252bf
comparison
equal deleted inserted replaced
1189:63b73a21bccd 1190:f6998e304b36
18 18
19 #include "BaseTypes.h" 19 #include "BaseTypes.h"
20 20
21 #include <cmath> 21 #include <cmath>
22 22
23 /**
24 * Class containing static functions for simple operations on data
25 * columns, for use by display layers.
26 */
23 class ColumnOp 27 class ColumnOp
24 { 28 {
25 public: 29 public:
30 /**
31 * Column type.
32 */
26 typedef std::vector<float> Column; 33 typedef std::vector<float> Column;
27 34
35 /**
36 * Normalization types.
37 *
38 * NormalizeColumns means to normalize to max value = 1.
39 * NormalizeHybrid means normalize to max = 1 and then multiply by
40 * log10 of the max value, to retain some difference between
41 * levels of neighbouring columns.
42 *
43 * NormalizeVisibleArea is ignored here and is included only so as
44 * to match the set of normalization options historically provided
45 * in the SV spectrogram layer.
46 */
28 enum Normalization { 47 enum Normalization {
29 NoNormalization, 48 NoNormalization,
30 NormalizeColumns, 49 NormalizeColumns,
31 NormalizeVisibleArea, 50 NormalizeVisibleArea,
32 NormalizeHybrid 51 NormalizeHybrid
33 }; 52 };
34 53
54 /**
55 * Scale an FFT output by half the FFT size.
56 */
35 static Column fftScale(const Column &in, int fftSize) { 57 static Column fftScale(const Column &in, int fftSize) {
36 58
37 Column out; 59 Column out;
38 out.reserve(in.size()); 60 out.reserve(in.size());
39 float scale = 2.f / float(fftSize); 61 float scale = 2.f / float(fftSize);
42 } 64 }
43 65
44 return out; 66 return out;
45 } 67 }
46 68
69 /**
70 * Determine whether an index points to a local peak.
71 */
47 static bool isPeak(const Column &in, int ix) { 72 static bool isPeak(const Column &in, int ix) {
48 73
49 if (!in_range_for(in, ix-1)) return false; 74 if (!in_range_for(in, ix-1)) return false;
50 if (!in_range_for(in, ix+1)) return false; 75 if (!in_range_for(in, ix+1)) return false;
51 if (in[ix] < in[ix+1]) return false; 76 if (in[ix] < in[ix+1]) return false;
52 if (in[ix] < in[ix-1]) return false; 77 if (in[ix] < in[ix-1]) return false;
53 78
54 return true; 79 return true;
55 } 80 }
56 81
82 /**
83 * Return a column containing only the local peak values (all
84 * others zero).
85 */
57 static Column peakPick(const Column &in) { 86 static Column peakPick(const Column &in) {
58 87
59 std::vector<float> out(in.size(), 0.f); 88 std::vector<float> out(in.size(), 0.f);
60 for (int i = 0; in_range_for(in, i); ++i) { 89 for (int i = 0; in_range_for(in, i); ++i) {
61 if (isPeak(in, i)) { 90 if (isPeak(in, i)) {
64 } 93 }
65 94
66 return out; 95 return out;
67 } 96 }
68 97
98 /**
99 * Return a column normalized from the input column according to
100 * the given normalization scheme.
101 */
69 static Column normalize(const Column &in, Normalization n) { 102 static Column normalize(const Column &in, Normalization n) {
70 103
71 if (n == NoNormalization || n == NormalizeVisibleArea) { 104 if (n == NoNormalization || n == NormalizeVisibleArea) {
72 return in; 105 return in;
73 } 106 }
96 out.push_back(v * scale); 129 out.push_back(v * scale);
97 } 130 }
98 return out; 131 return out;
99 } 132 }
100 133
134 /**
135 * Scale the given column using the given gain multiplier.
136 */
101 static Column applyGain(const Column &in, float gain) { 137 static Column applyGain(const Column &in, float gain) {
102 138
103 if (gain == 1.f) { 139 if (gain == 1.f) {
104 return in; 140 return in;
105 } 141 }
109 out.push_back(v * gain); 145 out.push_back(v * gain);
110 } 146 }
111 return out; 147 return out;
112 } 148 }
113 149
150 /**
151 * Distribute the given column into a target vector of a different
152 * size, optionally using linear interpolation. The binfory vector
153 * contains a mapping from y coordinate (i.e. index into the
154 * target vector) to bin (i.e. index into the source column).
155 */
114 static Column distribute(const Column &in, 156 static Column distribute(const Column &in,
115 int h, 157 int h,
116 const std::vector<double> &binfory, 158 const std::vector<double> &binfory,
117 int minbin, 159 int minbin,
118 bool interpolate) { 160 bool interpolate) {