Skip to content

Instantly share code, notes, and snippets.

@smoge
Created May 10, 2024 07:52
Show Gist options
  • Save smoge/5a337ec4425fdd8277040aaaa5f5f6b8 to your computer and use it in GitHub Desktop.
Save smoge/5a337ec4425fdd8277040aaaa5f5f6b8 to your computer and use it in GitHub Desktop.
faust_recursive_circuits
-- faust
-- import("stdfaust.lib");
-- lowpass(cf, x) = y
-- letrec {
-- 'y = b0 * x - a1 * y;
-- }
-- with {
-- b0 = 1 + a1;
-- a1 = exp(-w(cf)) * -1;
-- w(f) = 2 * ma.PI * f / ma.SR;
-- };
-- process = lowpass;
-- precompute the filter coefficients outside the filtering function, especially since they do not change unless the cutoff
-- frequency changes. This is more efficient when processing a large batch of samples, as it avoids recalculating
-- the coefficients for each sample.
module Main where
import Data.Vector (Vector, scanl')
import qualified Data.Vector as V
samplingRate :: Double
samplingRate = 48000.0
coefficients :: Double -> (Double, Double)
coefficients cf = let w = 2 * pi * cf / samplingRate
a1 = -exp(-w)
in (1 + a1, a1)
lowpass :: Double -> Double -> Vector Double -> Vector Double
lowpass b0 a1 = scanl' (\y x -> b0 * x - a1 * y) 0
main :: IO ()
main = do
let cf = 1000
(b0, a1) = coefficients cf
samples = V.fromList [0.0, 1.0, 0.5, 0.2, 0.1, 0.0]
filteredSamples = lowpass b0 a1 samples
print filteredSamples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment