Created
June 27, 2014 04:57
-
-
Save uhho/dddd61edc0fdfa1c28e6 to your computer and use it in GitHub Desktop.
Two-dimensional Gaussian function in JavaScript
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
/** | |
* Two-dimensional Gaussian function | |
* | |
* @param {number} amplitude | |
* @param {number} x0 | |
* @param {number} y0 | |
* @param {number} sigmaX | |
* @param {number} sigmaY | |
* @returns {Function} | |
*/ | |
function makeGaussian(amplitude, x0, y0, sigmaX, sigmaY) { | |
return function(amplitude, x0, y0, sigmaX, sigmaY, x, y) { | |
var exponent = -( | |
( Math.pow(x - x0, 2) / (2 * Math.pow(sigmaX, 2))) | |
+ ( Math.pow(y - y0, 2) / (2 * Math.pow(sigmaY, 2))) | |
); | |
return amplitude * Math.pow(Math.E, exponent); | |
}.bind(null, amplitude, x0, y0, sigmaX, sigmaY); | |
} | |
// USAGE | |
var gaussian = makeGaussian(300, 200, 200, 50, 50); | |
console.log(gaussian(100,100)); // ~5 | |
console.log(gaussian(200, 200)); // 300 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love it! I'm using it an a modular kernel filter system to implement gaussian blur.
Here's my contribution for better intellisense: