% Verify proposed fix to Goertzel filter configuration
%
% Output frquency index for desired term:
k = 6

% "Time sequence" to transform (arbitrary)
for  n=1:10
    x(n)= rand;
end
% Additional padding sample (if needed)
x(11)=0.0;

% Multiplier coefficient value for filter
W = exp(-j*2*pi*k/10);
Wc = conj(W);

% Reference case, brute force complex DFT:
dftk = 0.0;
for  n=1:10
    dftk = dftk + W^(n-1)*x(n);
end
dftk

% Original filter missing the required final filter pass.
d1 = 0.0;
d2 = 0.0;
for n=1:10
    y(n) = x(n)+2*real(Wc)*d1-d2;
    d2 = d1;
    d1 = y(n);
end
wrong = d1 - W*d2

% Original filter corrected with artificial extra pass.
d1 = 0.0;
d2 = 0.0;
for n=1:11
    y(n) = x(n)+2*real(Wc)*d1-d2;
    d2 = d1;
    d1 = y(n);
end
goert1 = d1 - W*d2

% Modified filter termination omitting the extra pass.
d1 = 0.0;
d2 = 0.0;
for n=1:10
    y(n) = x(n)+2*real(Wc)*d1-d2;
    d2 = d1;
    d1 = y(n);
end
goert2 = Wc*d1 - d2

