Created
July 19, 2014 19:59
-
-
Save cheery/72c47526f9d9bbefd734 to your computer and use it in GitHub Desktop.
Simple high pass and low pass filters with frequency cutoff
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
/* | |
simple high pass and low pass filters with frequency cutoff. | |
*/ | |
var tau = Math.PI*2; | |
var v = 0; | |
lp = LP(); | |
hp = HP(); | |
function dsp(t) { | |
return lp(hp(noise(), 5000+4000*Math.cos(tau*t*0.1)), 5000+4000*Math.sin(tau*t*0.1)) * 0.5; | |
} | |
function noise() { | |
return Math.random(); | |
} | |
function LP() { | |
var dout=0, din=0; | |
return function (x, cutoff) { | |
var rc = 1.0/(cutoff*2*tau); | |
var dt = 1.0/sampleRate; | |
var alpha = dt/(rc+dt); | |
dout = dout + alpha*(x - dout); | |
din = x; | |
return dout; | |
}; | |
} | |
function HP() { | |
var dout=0, din=0; | |
return function (x, cutoff) { | |
var rc = 1.0/(cutoff*2*tau); | |
var dt = 1.0/sampleRate; | |
var alpha = rc/(rc+dt); | |
dout = alpha*(dout + x - din); | |
din = x; | |
return dout; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment