summaryrefslogtreecommitdiff
path: root/client/lib/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/lib/util.js')
-rw-r--r--client/lib/util.js37
1 files changed, 36 insertions, 1 deletions
diff --git a/client/lib/util.js b/client/lib/util.js
index e47b343..0685b9d 100644
--- a/client/lib/util.js
+++ b/client/lib/util.js
@@ -13,6 +13,7 @@ const browser = { isIphone, isIpad, isMobile, isDesktop }
function choice (a){ return a[ Math.floor(Math.random() * a.length) ] }
function mod(n,m){ return n-(m * Math.floor(n/m)) }
+function norm(n, min, max){ return (n - min) / (max - min) }
function requestAudioContext (fn) {
if (isMobile) {
@@ -92,5 +93,39 @@ function tap (fn) {
}
}
-export { choice, mod, browser, requestAudioContext, ftom, mtof, tap, dataURItoBlob }
+/* get minimum and maximum variance from row-to-row */
+
+function get_diff_bounds(rows){
+ const diffs = rows.map(row => {
+ const row_min = Math.min.apply(Math, row)
+ const row_max = Math.max.apply(Math, row)
+ return row_max - row_min
+ })
+ const min = Math.min.apply(Math, diffs)
+ const max = Math.max.apply(Math, diffs)
+ return { min, max }
+}
+
+/* get minimum and maximum values from a dataset */
+
+function get_bounds(dataset){
+ let rows = dataset.lines
+ // rows.forEach(row => row.shift())
+ rows = rows.map(a => a.map(n => parseFloat(n)))
+ const max = rows.reduce((a,b) => {
+ return b.reduce((z,bb) => {
+ return Math.max(z, bb)
+ }, a)
+ }, -Infinity)
+ const min = rows.reduce((a,b) => {
+ return b.reduce((z,bb) => {
+ return Math.min(z, bb)
+ }, a)
+ }, Infinity)
+ return { rows, max, min }
+}
+
+
+
+export { choice, mod, norm, browser, get_bounds, get_diff_bounds, requestAudioContext, ftom, mtof, tap, dataURItoBlob }