function [f, freq, amp, iMaxAmp] = computeMaxFrequency(xdata, ydata, rbw, df)
%  return estimated frequency and amplitude for maximum peak in spectrum
% 
%  This file is for internal use only.

%   Copyright 2013 The MathWorks, Inc.
%#codegen

% Search line data for maximum value
[~, iMaxAmp] = max(ydata);
iOffset = max(ceil(rbw/df), 1);

% compute the central moment in the neighborhood of the peak
Ffund = xdata(max(1,iMaxAmp-iOffset):min(iMaxAmp+iOffset,numel(xdata)));
Pfund = ydata(max(1,iMaxAmp-iOffset):min(iMaxAmp+iOffset,numel(ydata))); 

f = dot(Ffund, Pfund) ./ sum(Pfund);

% return local peak value.  We do not account for scallop loss for
% consistency with peak finder
freq = xdata(iMaxAmp);
amp = rbw*ydata(iMaxAmp);

