From 1aca907e744a925cc285a6abcf31f78a436e0832 Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Wed, 15 Nov 2023 13:04:58 -0500 Subject: [PATCH] first commit --- LICENSE | 14 ++++++++++++++ README.md | 3 +++ reverse_goertzel.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 reverse_goertzel.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee7d6a5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..3fca784 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Reverse Goertzel + +Code using the Goertzel algorithm in reverse. diff --git a/reverse_goertzel.py b/reverse_goertzel.py new file mode 100644 index 0000000..43f5def --- /dev/null +++ b/reverse_goertzel.py @@ -0,0 +1,44 @@ +import math +import matplotlib.pyplot as plt +import numpy as np + +def goertzel_sinusoid(freq, duration, sample_rate, amplitude): + n = int(duration * sample_rate) + omega = (2.0 * math.pi * freq) / sample_rate + coeff = 2.0 * math.cos(omega) + + # Initialize state variables + q1 = 1.0 + q2 = 1.0 + result = [] + + for i in range(n): + sample = coeff * q1 - q2 + q2 = q1 + q1 = sample + result.append(sample * amplitude) + + return result + +def sinusoid(n, cycles=1): + result = [] + for i in range(1, n+1): + sample = math.cos(2.0 * cycles * math.pi * (i/n)) + result.append(sample) + return result + +def main(): + cycles = 2 + signal = goertzel_sinusoid(cycles, 1, 1024, 1) + reference = np.array(sinusoid(1024, cycles)) + error = reference - signal + print("max error:", error.max()) + x = np.array(range(0, 1024)) + plt.plot(x, signal) + plt.plot(x, reference) + plt.plot(x, error) + #plt.plot(x, error*1000/3) + plt.show() + +if __name__ == "__main__": + main()