Created
January 18, 2015 22:36
-
-
Save matthewpalmer/ba3cbf731c5d8d726bae to your computer and use it in GitHub Desktop.
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
module Spectrum ( computeSpectrum | |
, sumSpectrum | |
) where | |
import Data.Complex | |
{- | |
N samples | |
R sample rate | |
So frequency spacing is R / N | |
-} | |
-- http://stackoverflow.com/questions/604453/analyze-audio-using-fast-fourier-transform?rq=1 | |
computeSpectrum :: [Complex Double] -> Double -> [(Double, Double)] | |
computeSpectrum input sampleRate = normalize(zip frequencies (magnitudes input)) | |
where frequencies = [i * resolution | i <- [0..(halfNumSamples - 1)]] | |
endBound = (numSamples - 1) | |
ratio = (numSamples + 1) / sampleRate | |
resolution = 1 / ratio | |
numSamples = fromIntegral(length input) | |
halfNumSamples = numSamples / 2 | |
normalize :: [(Double, Double)] -> [(Double, Double)] | |
normalize complexes = map (\(f, x) -> (f, 10 * (logBase 10 (x + 1e-20)))) complexes | |
sumSpectrum :: [Complex Double] -> Double -> Double | |
sumSpectrum input sampleRate = sum firsts | |
where firsts = map fst $ computeSpectrum input sampleRate | |
magnitudes zs = map Data.Complex.magnitude zs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment