Mercurial > hg > svcore
comparison data/model/WaveformOversampler.cpp @ 1536:eb10ed56d5a4 zoom
Add custom waveform oversampler method
author | Chris Cannam |
---|---|
date | Wed, 26 Sep 2018 13:03:46 +0100 |
parents | |
children | fa67fbbff8fc |
comparison
equal
deleted
inserted
replaced
1535:22fe34dd7f26 | 1536:eb10ed56d5a4 |
---|---|
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 | |
8 This program is free software; you can redistribute it and/or | |
9 modify it under the terms of the GNU General Public License as | |
10 published by the Free Software Foundation; either version 2 of the | |
11 License, or (at your option) any later version. See the file | |
12 COPYING included with this distribution for more information. | |
13 */ | |
14 | |
15 #include "WaveformOversampler.h" | |
16 | |
17 #include "data/model/DenseTimeValueModel.h" | |
18 | |
19 floatvec_t | |
20 WaveformOversampler::getOversampledData(const DenseTimeValueModel *source, | |
21 int channel, | |
22 sv_frame_t sourceStartFrame, | |
23 sv_frame_t sourceFrameCount, | |
24 int oversampleBy) | |
25 { | |
26 // Oversampled at a fixed ratio of m_filterRatio | |
27 floatvec_t fixedRatio = getFixedRatioData(source, channel, | |
28 sourceStartFrame, | |
29 sourceFrameCount); | |
30 sv_frame_t fixedCount = fixedRatio.size(); | |
31 sv_frame_t targetCount = (fixedCount / m_filterRatio) * oversampleBy; | |
32 | |
33 // And apply linear interpolation to the desired factor | |
34 | |
35 floatvec_t result(targetCount, 0.f); | |
36 | |
37 for (int i = 0; i < targetCount; ++i) { | |
38 double pos = (double(i) / oversampleBy) * m_filterRatio; | |
39 double diff = pos - floor(pos); | |
40 int ix = int(floor(pos)); | |
41 float interpolated = (1.0 - diff) * fixedRatio[ix]; | |
42 if (in_range_for(fixedRatio, ix + 1)) { | |
43 interpolated += diff * fixedRatio[ix + 1]; | |
44 } | |
45 result[i] = interpolated; | |
46 } | |
47 | |
48 return result; | |
49 } | |
50 | |
51 floatvec_t | |
52 WaveformOversampler::getFixedRatioData(const DenseTimeValueModel *source, | |
53 int channel, | |
54 sv_frame_t sourceStartFrame, | |
55 sv_frame_t sourceFrameCount) | |
56 { | |
57 sv_frame_t sourceLength = source->getEndFrame(); | |
58 | |
59 if (sourceStartFrame + sourceFrameCount > sourceLength) { | |
60 sourceFrameCount = sourceLength - sourceStartFrame; | |
61 if (sourceFrameCount <= 0) return {}; | |
62 } | |
63 | |
64 sv_frame_t targetFrameCount = sourceFrameCount * m_filterRatio; | |
65 | |
66 sv_frame_t filterLength = m_filter.size(); // NB this is known to be odd | |
67 sv_frame_t filterTailOut = (filterLength - 1) / 2; | |
68 sv_frame_t filterTailIn = filterTailIn / m_filterRatio; | |
69 | |
70 floatvec_t oversampled(targetFrameCount, 0.f); | |
71 | |
72 sv_frame_t i0 = sourceStartFrame - filterTailIn; | |
73 if (i0 < 0) { | |
74 i0 = 0; | |
75 } | |
76 sv_frame_t i1 = sourceStartFrame + sourceFrameCount + filterTailIn; | |
77 if (i1 > sourceLength) { | |
78 i1 = sourceLength; | |
79 } | |
80 | |
81 floatvec_t sourceData = source->getData(channel, i0, i1 - i0); | |
82 | |
83 for (sv_frame_t i = i0; i < i1; ++i) { | |
84 float v = sourceData[i - i0]; | |
85 sv_frame_t outOffset = | |
86 (i - sourceStartFrame) * m_filterRatio - filterTailOut; | |
87 for (sv_frame_t j = 0; j < filterLength; ++j) { | |
88 sv_frame_t outIndex = outOffset + j; | |
89 if (outIndex < 0 || outIndex >= targetFrameCount) { | |
90 continue; | |
91 } | |
92 oversampled[outIndex] += v * m_filter[j]; | |
93 } | |
94 } | |
95 | |
96 return oversampled; | |
97 } | |
98 | |
99 int | |
100 WaveformOversampler::m_filterRatio = 8; | |
101 | |
102 /// Precalculated windowed sinc FIR filter for oversampling ratio of 8 | |
103 floatvec_t | |
104 WaveformOversampler::m_filter { | |
105 2.0171043153063023E-4, 2.887198196326776E-4, | |
106 3.410439309101285E-4, 3.4267123819805857E-4, | |
107 2.843462511901066E-4, 1.6636986363946504E-4, | |
108 -4.5940658605786285E-18, -1.9299665002484582E-4, | |
109 -3.8279951732549946E-4, -5.357990649609105E-4, | |
110 -6.201170748425957E-4, -6.11531555444137E-4, | |
111 -4.987822892899791E-4, -2.872272251922189E-4, | |
112 -7.822991648518709E-19, 3.2382854144162815E-4, | |
113 6.341027017666046E-4, 8.769331519465396E-4, | |
114 0.001003535186382615, 9.791732608026272E-4, | |
115 7.906678783612421E-4, 4.5101220009813206E-4, | |
116 -3.039648514978356E-18, -4.996532051508215E-4, | |
117 -9.704877518513666E-4, -0.0013318083550190923, | |
118 -0.0015128911871247466, -0.0014658111457301016, | |
119 -0.0011756800671747431, -6.663214707558645E-4, | |
120 1.0713650598357415E-17, 7.292959363289514E-4, | |
121 0.0014084768982220279, 0.0019222969998680237, | |
122 0.0021721723797956524, 0.0020938999751673173, | |
123 0.0016712330766289326, 9.427050283841188E-4, | |
124 -5.656965938821965E-18, -0.0010225643654040554, | |
125 -0.001966437013513757, -0.002672722670880038, | |
126 -0.00300806671037164, -0.00288843624179131, | |
127 -0.0022967244980623574, -0.0012908081494665458, | |
128 -5.1499690577098E-18, 0.0013904094522721, | |
129 0.0026648961861419334, 0.0036103002009868065, | |
130 0.004050469159316014, 0.0038774554290217484, | |
131 0.0030739396559265075, 0.001722603817632299, | |
132 -9.130030250503607E-18, -0.0018451873718735516, | |
133 -0.0035270571169279162, -0.004765847116110058, | |
134 -0.0053332982334767624, -0.005092831604550132, | |
135 -0.00402770012894082, -0.0022517645624319594, | |
136 3.2752446397299053E-17, 0.0024010765839506923, | |
137 0.004579613038976446, 0.006174912111845945, | |
138 0.00689578873526276, 0.006571541332393174, | |
139 0.005186887306036285, 0.002894248521447605, | |
140 -1.336645565990815E-17, -0.0030747336558684963, | |
141 -0.0058540294958507235, -0.007879546416595632, | |
142 -0.008784519668338507, -0.008357645279493864, | |
143 -0.006586046547485615, -0.003669217725935383, | |
144 -1.9348975378752276E-17, 0.0038863208094135626, | |
145 0.007388553022623823, 0.009931080628244226, | |
146 0.011056594746806033, 0.010505398026651453, | |
147 0.008267906564613223, 0.00460048159140493, | |
148 -1.816145184081109E-17, -0.004861124757802925, | |
149 -0.009231379891669668, -0.012394511669674028, | |
150 -0.01378467517229709, -0.013084177592430083, | |
151 -0.010287380585427207, -0.00571879407588959, | |
152 7.520535431851951E-17, 0.006032144534828161, | |
153 0.011445734103106982, 0.015355551390625357, | |
154 0.017065088242935025, 0.016186450815185452, | |
155 0.012718051439340603, 0.0070655888687995785, | |
156 -2.3209664144996714E-17, -0.007444328311482942, | |
157 -0.01411821163125819, -0.018932253281654043, | |
158 -0.02103125585328301, -0.019941019333653123, | |
159 -0.015663002335303704, -0.008699245445932525, | |
160 2.5712475624993567E-17, 0.009161748270723635, | |
161 0.0173729814451255, 0.023294901939595228, | |
162 0.02587678878709242, 0.02453592568963366, | |
163 0.01927365323131565, 0.010706050935569809, | |
164 -2.8133472199037193E-17, -0.011280308241551094, | |
165 -0.02139710071477064, -0.02870170641615764, | |
166 -0.031897218249350504, -0.030260140480986304, | |
167 -0.023784294156618507, -0.013220449772289724, | |
168 3.042099156841831E-17, 0.013951594368737923, | |
169 0.0264884258371512, 0.03556693609945249, | |
170 0.03957036852169639, 0.0375845888664677, | |
171 0.029579845398822833, 0.016465167405787955, | |
172 -3.2524514488155654E-17, -0.017431115375410273, | |
173 -0.03315356952091943, -0.04460179422099746, | |
174 -0.0497244634100025, -0.04733366619358394, | |
175 -0.037341081614037944, -0.020838316594689998, | |
176 3.439626695384943E-17, 0.022185914535618936, | |
177 0.04232958202159685, 0.05713801867687856, | |
178 0.06393033000280622, 0.06109191933191721, | |
179 0.04839482380906132, 0.027127167584840003, | |
180 -3.5992766927138734E-17, -0.029168755716052385, | |
181 -0.055960213335110184, -0.07598693477350407, | |
182 -0.08556575102599769, -0.08233350786406181, | |
183 -0.06571046000158454, -0.03713224848702707, | |
184 3.727625511036616E-17, 0.04066438975848791, | |
185 0.07882920057770397, 0.10826166115536123, | |
186 0.12343378955977465, 0.12040455825217859, | |
187 0.09755344650130694, 0.056053367635801106, | |
188 -3.8215953158245473E-17, -0.0638435745677513, | |
189 -0.12667849902789644, -0.17861887575594584, | |
190 -0.20985333136704623, -0.21188193950868073, | |
191 -0.17867464086818077, -0.10760048593620072, | |
192 3.8789099095340224E-17, 0.13868670259490817, | |
193 0.29927055936918734, 0.46961864377510765, | |
194 0.6358321371992203, 0.7836674214332147, | |
195 0.9000377382311825, 0.9744199685311685, | |
196 1.0000000000000004, 0.9744199685311685, | |
197 0.9000377382311825, 0.7836674214332147, | |
198 0.6358321371992203, 0.46961864377510765, | |
199 0.29927055936918734, 0.13868670259490817, | |
200 3.8789099095340224E-17, -0.10760048593620072, | |
201 -0.17867464086818077, -0.21188193950868073, | |
202 -0.20985333136704623, -0.17861887575594584, | |
203 -0.12667849902789644, -0.0638435745677513, | |
204 -3.8215953158245473E-17, 0.056053367635801106, | |
205 0.09755344650130694, 0.12040455825217859, | |
206 0.12343378955977465, 0.10826166115536123, | |
207 0.07882920057770397, 0.04066438975848791, | |
208 3.727625511036616E-17, -0.03713224848702707, | |
209 -0.06571046000158454, -0.08233350786406181, | |
210 -0.08556575102599769, -0.07598693477350407, | |
211 -0.055960213335110184, -0.029168755716052385, | |
212 -3.5992766927138734E-17, 0.027127167584840003, | |
213 0.04839482380906132, 0.06109191933191721, | |
214 0.06393033000280622, 0.05713801867687856, | |
215 0.04232958202159685, 0.022185914535618936, | |
216 3.439626695384943E-17, -0.020838316594689998, | |
217 -0.037341081614037944, -0.04733366619358394, | |
218 -0.0497244634100025, -0.04460179422099746, | |
219 -0.03315356952091943, -0.017431115375410273, | |
220 -3.2524514488155654E-17, 0.016465167405787955, | |
221 0.029579845398822833, 0.0375845888664677, | |
222 0.03957036852169639, 0.03556693609945249, | |
223 0.0264884258371512, 0.013951594368737923, | |
224 3.042099156841831E-17, -0.013220449772289724, | |
225 -0.023784294156618507, -0.030260140480986304, | |
226 -0.031897218249350504, -0.02870170641615764, | |
227 -0.02139710071477064, -0.011280308241551094, | |
228 -2.8133472199037193E-17, 0.010706050935569809, | |
229 0.01927365323131565, 0.02453592568963366, | |
230 0.02587678878709242, 0.023294901939595228, | |
231 0.0173729814451255, 0.009161748270723635, | |
232 2.5712475624993567E-17, -0.008699245445932525, | |
233 -0.015663002335303704, -0.019941019333653123, | |
234 -0.02103125585328301, -0.018932253281654043, | |
235 -0.01411821163125819, -0.007444328311482942, | |
236 -2.3209664144996714E-17, 0.0070655888687995785, | |
237 0.012718051439340603, 0.016186450815185452, | |
238 0.017065088242935025, 0.015355551390625357, | |
239 0.011445734103106982, 0.006032144534828161, | |
240 7.520535431851951E-17, -0.00571879407588959, | |
241 -0.010287380585427207, -0.013084177592430083, | |
242 -0.01378467517229709, -0.012394511669674028, | |
243 -0.009231379891669668, -0.004861124757802925, | |
244 -1.816145184081109E-17, 0.00460048159140493, | |
245 0.008267906564613223, 0.010505398026651453, | |
246 0.011056594746806033, 0.009931080628244226, | |
247 0.007388553022623823, 0.0038863208094135626, | |
248 -1.9348975378752276E-17, -0.003669217725935383, | |
249 -0.006586046547485615, -0.008357645279493864, | |
250 -0.008784519668338507, -0.007879546416595632, | |
251 -0.0058540294958507235, -0.0030747336558684963, | |
252 -1.336645565990815E-17, 0.002894248521447605, | |
253 0.005186887306036285, 0.006571541332393174, | |
254 0.00689578873526276, 0.006174912111845945, | |
255 0.004579613038976446, 0.0024010765839506923, | |
256 3.2752446397299053E-17, -0.0022517645624319594, | |
257 -0.00402770012894082, -0.005092831604550132, | |
258 -0.0053332982334767624, -0.004765847116110058, | |
259 -0.0035270571169279162, -0.0018451873718735516, | |
260 -9.130030250503607E-18, 0.001722603817632299, | |
261 0.0030739396559265075, 0.0038774554290217484, | |
262 0.004050469159316014, 0.0036103002009868065, | |
263 0.0026648961861419334, 0.0013904094522721, | |
264 -5.1499690577098E-18, -0.0012908081494665458, | |
265 -0.0022967244980623574, -0.00288843624179131, | |
266 -0.00300806671037164, -0.002672722670880038, | |
267 -0.001966437013513757, -0.0010225643654040554, | |
268 -5.656965938821965E-18, 9.427050283841188E-4, | |
269 0.0016712330766289326, 0.0020938999751673173, | |
270 0.0021721723797956524, 0.0019222969998680237, | |
271 0.0014084768982220279, 7.292959363289514E-4, | |
272 1.0713650598357415E-17, -6.663214707558645E-4, | |
273 -0.0011756800671747431, -0.0014658111457301016, | |
274 -0.0015128911871247466, -0.0013318083550190923, | |
275 -9.704877518513666E-4, -4.996532051508215E-4, | |
276 -3.039648514978356E-18, 4.5101220009813206E-4, | |
277 7.906678783612421E-4, 9.791732608026272E-4, | |
278 0.001003535186382615, 8.769331519465396E-4, | |
279 6.341027017666046E-4, 3.2382854144162815E-4, | |
280 -7.822991648518709E-19, -2.872272251922189E-4, | |
281 -4.987822892899791E-4, -6.11531555444137E-4, | |
282 -6.201170748425957E-4, -5.357990649609105E-4, | |
283 -3.8279951732549946E-4, -1.9299665002484582E-4, | |
284 -4.5940658605786285E-18, 1.6636986363946504E-4, | |
285 2.843462511901066E-4, 3.4267123819805857E-4, | |
286 3.410439309101285E-4, 2.887198196326776E-4, | |
287 2.0171043153063023E-4 | |
288 }; | |
289 |