%
%  Name:  rpbinfit.m
%
%  Usage:  rpbinfit.m
%
%  Selects and plots the frequency bins to use in
%	the rpfit
%

%  Version SWD970709
%
%  Author(s): Scott W. Doebling, doebling@lanl.gov
%
%  Institution: Los Alamos National Laboratory,
%               ESA-EA
%
%  Copyright (c) 1996-1997, The Regents of the University
%       of California. (See top level of code for
%       full copyright statement.)

nresp = size(G,1)/nlines;
nref = size(G,2);
nboot = size(G,3);

%
%  Separate the portion of the FRF matrix inside the cursor band
%

n1=floor(f1/df)+1;
n2=floor(f2/df)+1;

nband = n2 - n1 + 1;

Gband = zeros(nresp*nband,nref,nboot);

for i = 1:nresp,
    Gband( (i-1)*nband+1 : i*nband ,:,:) = G( (i-1)*nlines+n1 : (i-1)*nlines+n2 ,:,:);
end

%
%  Sort the bins in order of ascending CMIF
%

[temp,asrt] = sort(cmif(Gband,nband));

%fitpts= n1 + sort(asrt(nband-nfitbin+1:nband));

fitpts = n1 - 1 + sort(asrt(end-nfitbin+1:end));

Gfit = zeros(nfitbin*nresp,nref,nboot);

for i = 1:nresp,
   
   Gfit((i-1)*nfitbin+1:i*nfitbin,:,:) = G((i-1)*nlines+fitpts,:,:);
   
end
 
freqfit = freq(fitpts);

axes(haxes)

if length(hline)>3, delete(hline(4)), end

if strcmp(name,'CMIF'),
   hold on
   hline(4) = plot(freqfit,cmif(Gfit,nfitbin),'ro');
   hold off
else
   hold on
   hline(4) = plot(freqfit,G((iresp-1)*nfitbin+1:iresp*nfitbin,iref,1),'ro');
   hold off
end

   lnst(4,:) = 'o ';
   lnco(4,:) = 'r';

   set(hplttl(11),'string','Primary Data|Low Cursor|High Cursor|Bins to fit')
