# What's wrong with my Goertzel algorithm implementation?

I've implemented Goertzel algorithm, now very simply in Python. But I cannot obtain the correct answer it's supposed to produce for a single DTF frequency. here is my code in Python:

import numpy as np

omega = 0.23

# length of the data to generate
length = 200

# updated parameters
dft = 0.0
s1 = 0.0
s2 = 0.0

# initial phase
for i in range(length):

# generate signal value

# goertzel algorithm update
s0 = value + 2.0 * np.cos(omega) * s1  - s2
s2 = s1
s1 = s0

# dft sum update
dft += value * np.exp(-1j * omega * i)

# next value

# final goertzel algorithm update
s0 = 2.0 * s1 * np.cos(omega) - s2
goe = s0 - np.exp(-1j * omega) * s1

# print results
print "DFT: {:.4f}".format(dft / length)
print "GOE: {:.4f}".format(goe / length)


Output:

DFT: 0.4979-0.0097j
GOE: -0.2065+0.4531j


The correct answer (with infinite data) is 0.5 + 0.0j.

DFT is therefore providing the correct result.

What's wrong with my Goertzel algorithm implementation?

https://en.wikipedia.org/wiki/Goertzel_algorithm

• Here are two implementations for MatLab - mathworks.com/matlabcentral/fileexchange/… – Juha P Aug 20 '18 at 12:58
• Omg! All this time spent puzzling over this, only now I realise I’m trying to use Goertzel algorithm for non integer frequency bin! Please post your comment as answer and I will accept it! – akuz Aug 20 '18 at 13:02
• Interestingly, in that article they propose to apply correction np.exp (-1j * omega * (length - 1)), whereas the correct one seems to be np.exp (-1j * omega * length) as proposed in Peter K's answer. – akuz Aug 20 '18 at 13:36 I believe your misunderstanding is to not take account of the $e^{j\omega_0n}$ term.