diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-12-19 22:19:50 +0300 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-12-19 22:19:50 +0300 |
commit | d845d87e2c7fa7afb2ac0ecb239ae1cc0b341937 (patch) | |
tree | a883e4bb20e21ce6180b63a8456909bcdd76c681 /examples/fft.lua | |
parent | 7059fffa6e55cb0fc406307122dd0688f34ee4fc (diff) | |
download | applause2-d845d87e2c7fa7afb2ac0ecb239ae1cc0b341937.tar.gz |
added example for pitch tracking via FFT
Diffstat (limited to 'examples/fft.lua')
-rw-r--r-- | examples/fft.lua | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/examples/fft.lua b/examples/fft.lua index 5930f46..1f8687d 100644 --- a/examples/fft.lua +++ b/examples/fft.lua @@ -36,3 +36,20 @@ noisy:FFT(1024, Hamming):map(function(spectrum) end return spectrum end):IFFT(1024):play() + +-- Pitch Tracking +-- See also: http://blog.bjornroche.com/2012/07/frequency-detection-using-fft-aka-pitch.html +opera = SndfileStream("examples/opera.flac") +opera:LPF(10000):FFT(1024, Hanning):map(function(spectrum) + local size = (#spectrum-1)*2 + local peak_i, peak_val + for i = 1, #spectrum do + -- We don't have to take the square root to find the peak + local val = spectrum[i].re^2 + spectrum[i].im^2 + if not peak_val or val > peak_val then + peak_i, peak_val = i, val + end + end + -- Return peak as frequency + return tostream((peak_i-1)*samplerate/size):sub(1, size) +end):ravel():div(10000):crush(7):mul(10000):SqrOsc():crush():play() |