Last active
December 15, 2015 12:29
-
-
Save kevincennis/5260119 to your computer and use it in GitHub Desktop.
spacing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return an array containing (len) linearly-spaced values from (min) to (max) | |
function linspace(from, to, len){ | |
var arr = new Array(len) | |
, step = ( to - from ) / ( len - 1 ) | |
, i = 0 | |
for ( ; i < len; ++i ) | |
arr[i] = from + ( i * step ) | |
return arr | |
} | |
linspace(0, 10, 3) // [0, 5, 10] | |
linspace(20, 40, 5) // [20, 25, 30, 35, 40] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return an array containing (len) logarithmically-spaced values from (min) to (max) | |
// | |
// useful for frequency response graphs | |
function logspace(from, to, len){ | |
var err = 'from must be greater than zero' | |
, base = Math.pow( to / from, 1 / len ) | |
, arr = new Array(len) | |
, i = 1 | |
if ( isNaN(base) ) throw new TypeError(err) | |
for ( ; i <= len; ++i ) | |
arr[i - 1] = from * Math.pow(base, i) | |
return arr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment