comparison src/app/waveform/waveform.component.ts @ 109:68fe21cfda2a

Avoid NaNs, etc
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 10 Mar 2017 16:45:54 +0000
parents 7740f7fd7c3c
children 9890436bcc9a
comparison
equal deleted inserted replaced
108:7740f7fd7c3c 109:68fe21cfda2a
114 estimatePercentile(matrix, percentile) { 114 estimatePercentile(matrix, percentile) {
115 // our sample is not evenly distributed across the whole data set: 115 // our sample is not evenly distributed across the whole data set:
116 // it is guaranteed to include at least one sample from every 116 // it is guaranteed to include at least one sample from every
117 // column, and could sample some values more than once. But it 117 // column, and could sample some values more than once. But it
118 // should be good enough in most cases (todo: show this) 118 // should be good enough in most cases (todo: show this)
119 if (matrix.length === 0) return 0.0; 119 if (matrix.length === 0) {
120 return 0.0;
121 }
120 const w = matrix.length; 122 const w = matrix.length;
121 const h = matrix[0].length; 123 const h = matrix[0].length;
122 const n = w * h; 124 const n = w * h;
123 const m = (n > 10000 ? 10000 : n); // should base that on the %ile 125 const m = (n > 50000 ? 50000 : n); // should base that on the %ile
124 let m_per = Math.floor(m / w); 126 let m_per = Math.floor(m / w);
125 if (m_per < 1) m_per = 1; 127 if (m_per < 1) m_per = 1;
126 let sample = []; 128 let sample = [];
127 for (let x = 0; x < w; ++x) { 129 for (let x = 0; x < w; ++x) {
128 for (let i = 0; i < m_per; ++i) { 130 for (let i = 0; i < m_per; ++i) {
129 const y = Math.floor(Math.random() * h); 131 const y = Math.floor(Math.random() * h);
130 sample.push(matrix[x][y]); 132 const value = matrix[x][y];
131 } 133 if (!isNaN(value) && value !== Infinity) {
134 sample.push(value);
135 }
136 }
137 }
138 if (sample.length === 0) {
139 console.log("WARNING: No samples gathered, even though we hoped for " +
140 (m_per * w) + " of them");
141 return 0.0;
132 } 142 }
133 sample.sort((a,b) => { return a - b; }); 143 sample.sort((a,b) => { return a - b; });
134 const ix = Math.floor((sample.length * percentile) / 100); 144 const ix = Math.floor((sample.length * percentile) / 100);
135 console.log("Estimating " + percentile + "-%ile of " + 145 console.log("Estimating " + percentile + "-%ile of " +
136 n + "-sample dataset (" + w + " x " + h + ") as value " + ix + 146 n + "-sample dataset (" + w + " x " + h + ") as value " + ix +
415 } 425 }
416 case 'matrix': { 426 case 'matrix': {
417 const stepDuration = (features as FixedSpacedFeatures).stepDuration; 427 const stepDuration = (features as FixedSpacedFeatures).stepDuration;
418 const matrixData = (features.data as Float32Array[]); 428 const matrixData = (features.data as Float32Array[]);
419 if (matrixData.length === 0) return; 429 if (matrixData.length === 0) return;
420 const targetValue = this.estimatePercentile(matrixData, 97); 430 console.log("matrix data length = " + matrixData.length);
431 console.log("height of first column = " + matrixData[0].length);
432 const targetValue = this.estimatePercentile(matrixData, 95);
421 const gain = (targetValue > 0.0 ? (1.0 / targetValue) : 1.0); 433 const gain = (targetValue > 0.0 ? (1.0 / targetValue) : 1.0);
422 console.log("setting gain to " + gain); 434 console.log("setting gain to " + gain);
423 const matrixEntity = new wavesUI.utils.PrefilledMatrixEntity(matrixData); 435 const matrixEntity = new wavesUI.utils.PrefilledMatrixEntity(matrixData);
424 let matrixLayer = new wavesUI.helpers.MatrixLayer(matrixEntity, { 436 let matrixLayer = new wavesUI.helpers.MatrixLayer(matrixEntity, {
425 gain, 437 gain,
426 height, 438 height: height * 0.8,
427 normalise: 'hybrid', 439 top: height * 0.1,
440 normalise: 'none',
428 mapper: this.iceMapper() 441 mapper: this.iceMapper()
429 }); 442 });
430 this.colouredLayers.set(this.addLayer( 443 this.colouredLayers.set(this.addLayer(
431 matrixLayer, 444 matrixLayer,
432 mainTrack, 445 mainTrack,