Created
October 12, 2014 03:28
-
-
Save Youka/a9114a274ce70bad4de5 to your computer and use it in GitHub Desktop.
Goertzel frequency analyzer
This file contains hidden or 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
| -- Frequency analyzer | |
| local function goertzel(samples, sample_rate, freq) | |
| -- Get coefficient from target and maximal frequency | |
| local coeff = 2 * math.cos(2 * math.pi * freq / sample_rate) | |
| -- Process samples | |
| local s_prev, s_prev2, s = 0, 0 | |
| for i=1, #samples do | |
| s = samples[i] + coeff * s_prev - s_prev2 | |
| s_prev2, s_prev = s_prev, s | |
| end | |
| -- Calculate power of target frequency | |
| return s_prev2 * s_prev2 + s_prev * s_prev - coeff * s_prev * s_prev2 | |
| end | |
| -- Audio data (sample_type: double; channels: 1) | |
| local sample_rate, seconds, frequency = 22050, 3, 400 | |
| local samples = {} | |
| for i=1, seconds * sample_rate do | |
| samples[i] = math.sin(2 * math.pi * (i-1) / sample_rate * frequency) | |
| end | |
| -- Test | |
| print( | |
| "Power: " .. goertzel(samples, sample_rate, 400) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment