function [a, b, c] = qf1d(f, K)
% [a, b, c] = qf1d(f, K)
% Quadratic fit to 1D function f; K:number of points in estimate
% inc: increment in independent variable; start: first loc of f
% See Haralick and Shapiro: Computer and Robot Vision (Chapter 8)
%
N = length(f);
k = round((K-1)/2);
m2 = 0; 	m4 = 0;
for m = -k:k
  m2 = m2 + m*m;
  m4 = m4 + m*m*m*m;
end
q = m2; r = K; s = m4;
denom = (q-(r*s/q));  k1 = r/q;
a = zeros(N,1); b = zeros(N,1); c = zeros(N,1); 
for n = 1:k
  b(n) = 0; c(n) = 0; a(n) = 0;
end
for n = k+1:N-k
  fs(1) = 0;	fs(2) = 0;	fs(3) = 0;
  for m = -k:k
    fs(1) = fs(1) + f(m+n);
    fs(2) = fs(2) + m*f(m+n);
    fs(3) = fs(3) + m*m*f(m+n);
  end
  b(n) = fs(2)/q;
  c(n) = (fs(1)-(k1*fs(3)))/denom;
  a(n) = (fs(1)-q*c(n))/r;
end
for n = N-k+1:N
  b(n) = 0; c(n) = 0; a(n) = 0;
end
