Browse Source

Using median rather than max for pretty frame

Noah 8 years ago
parent
commit
d04730b15c
1 changed files with 22 additions and 16 deletions
  1. 22 16
      audiogram/waveform.js

+ 22 - 16
audiogram/waveform.js View File

@@ -36,32 +36,38 @@ function processSamples(samples, numFrames, samplesPerFrame) {
36 36
       perPoint = Math.floor(perFrame / samplesPerFrame),
37 37
       range = d3.range(samplesPerFrame),
38 38
       maxFrame,
39
-      min = max = 0;
39
+      maxMedian = min = max = 0;
40 40
 
41 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 45
 
45
-    return range.map(function(point){
46
+          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
47
+              localMin = localMax = 0;
46 48
 
47
-      var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
48
-          localMin = localMax = 0;
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
+          }
49 53
 
50
-      for (var i = 0, l = pointSamples.length; i < l; i++) {
51
-        localMin = Math.min(localMin, pointSamples[i]);
52
-        localMax = Math.max(localMax, pointSamples[i]);
53
-      }
54
+          if (localMin < min) min = localMin;
55
+          if (localMax > max) max = localMax;
54 56
 
55
-      min = Math.min(min, localMin);
57
+          // Min value, max value, and midpoint value
58
+          return [localMin, localMax, pointSamples[Math.floor(pointSamples.length / 2)]];
56 59
 
57
-      if (localMax > max) {
58
-        max = localMax;
59
-        maxFrame = frame;
60
-      }
60
+        }),
61
+        median = d3.median(points.map(function(point){
62
+          return point[1] - point[0];
63
+        }));
61 64
 
62
-      return [localMin, localMax];
65
+    if (median > maxMedian) {
66
+      maxMedian = median;
67
+      maxFrame = frame;
68
+    }
63 69
 
64
-    });
70
+    return points;
65 71
 
66 72
   });
67 73