Noah 8 years ago
parent
commit
eb03a5a575
1 changed files with 11 additions and 12 deletions
  1. 11 12
      audiogram/waveform.js

+ 11 - 12
audiogram/waveform.js View File

36
       perPoint = Math.floor(perFrame / samplesPerFrame),
36
       perPoint = Math.floor(perFrame / samplesPerFrame),
37
       range = d3.range(samplesPerFrame),
37
       range = d3.range(samplesPerFrame),
38
       maxFrame,
38
       maxFrame,
39
-      maxMedian = min = max = 0;
39
+      maxMedian = max = 0;
40
 
40
 
41
   var unadjusted = d3.range(numFrames).map(function(frame){
41
   var unadjusted = d3.range(numFrames).map(function(frame){
42
 
42
 
43
     var frameSamples = samples.slice(frame * perFrame, (frame + 1) * perFrame),
43
     var frameSamples = samples.slice(frame * perFrame, (frame + 1) * perFrame),
44
         points = range.map(function(point){
44
         points = range.map(function(point){
45
 
45
 
46
-          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
47
-              localMin = localMax = 0;
46
+          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint);
48
 
47
 
49
-          for (var i = 0, l = pointSamples.length; i < l; i++) {
50
-            if (pointSamples[i] < localMin) localMin = pointSamples[i];
51
-            if (pointSamples[i] > localMax) localMax = pointSamples[i];
52
-          }
48
+          var rms = Math.sqrt(d3.sum(pointSamples.map(function(d){
49
+            return d * d;
50
+          })) / perPoint);
53
 
51
 
54
-          if (localMin < min) min = localMin;
55
-          if (localMax > max) max = localMax;
52
+          if (rms > max) {
53
+            max = rms;
54
+          }
56
 
55
 
57
           // Min value, max value, and midpoint value
56
           // Min value, max value, and midpoint value
58
-          return [localMin, localMax, pointSamples[Math.floor(pointSamples.length / 2)]];
57
+          return [-rms, rms, pointSamples[Math.floor(pointSamples.length / 2)]];
59
 
58
 
60
         }),
59
         }),
61
         median = d3.median(points.map(function(point){
60
         median = d3.median(points.map(function(point){
62
-          return point[1] - point[0];
61
+          return point[1];
63
         }));
62
         }));
64
 
63
 
65
     if (median > maxMedian) {
64
     if (median > maxMedian) {
72
   });
71
   });
73
 
72
 
74
   // Scale up to -1 / 1
73
   // Scale up to -1 / 1
75
-  var adjustment = 1 / Math.max(Math.abs(min), Math.abs(max));
74
+  var adjustment = 1 / max;
76
 
75
 
77
   var adjusted = unadjusted.map(function(frame){
76
   var adjusted = unadjusted.map(function(frame){
78
     return frame.map(function(point){
77
     return frame.map(function(point){