function newoffset = adjustoffsetweighted(cols,rows,weights, curve)
% Vertically translates a parabola to best match some given data.
%
% NEWOFFSET = ADJUSTOFFSET(COLS, ROWS, WEIGHTS,CURVE) returns the optimum offset
% to match the parameter CURVE to the data in [ROWS COLS].
% Curve contains coefficients [c b a] from the parabola y = ax^2
% + bx + c. This function gives a weighted least squares regression for the
% parabola ax^2 + bx + c on the points  where the
% coefficients a and b are fixed.  Therefore, it is optimizing the c
% coefficient for the data provided in bottoms (which is simply the
% weighted mean deviation).

% Copyright (c) 2012
% Jerod Weinman
% jerod@acm.org

% $Id: adjustoffsetweighted.m 595 2014-10-24 18:15:54Z weinman $
 
% Jerod Weinman, Zach Butler, Dugan Knoll, Alexander Turner, Yirong Jing
% jerod@acm.org
% Copyright 2011, 2012, 2013, 2014
 
%    This file is part of PIONEER WordNorm.
%
%    PIONEER WordNorm is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    PIONEER WordNorm is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with PIONEER WordNorm.  If not, see <http://www.gnu.org/licenses/>.

if length(curve)==3
  newoffset = sum( (rows - curve(3)*cols.^2 - curve(2)*cols) .* weights) / ...
      sum(weights);
else
  newoffset = sum( (rows - curve(2)*cols) .* weights) / ...
      sum(weights);
end
