function [demods, Eflag] = hdedop(samples, VonC, FConFS, start_scan, n_scans)
% Function to frequency shift and adjust sample times to account for
% Doppler compression/expansion when processing HIFT data sets.
%
% samples      Column vector of sample values extracted from a "standard"
%              HIFT data set.
%
% VonC         Ratio of estimated source velocity as seen at the site that
%              generated the sample data set divided by the estimated speed
%              of sound.
%
% FConFS       The ratio of the carrier frequency (FC) divided by the sample
%              rate (FS).
%
% start_scan   The scan to start the output at.  This is relative to the
%              start of the data set AFTER doppler correction.  For example
%              if there are N samples per period of reception.  The starting
%              indices of successive periods are 1, N, 2N, 3N, ... .
%              
% n_scans      The number of dedoppered values to generate
%
% demods       A column vector of complex downshifted, resampled to compensate
%              for doppler.  These have not been filtered!!!  There remains
%              the -2fc term.
%
% Eflag        This is zero if the requested number of values has been
%              generated.  Otherwise it is nonzero.
%
%              22April2000 initial production version...K.Metzger

Eflag = 0;

d_start_scan = (start_scan-1)/(1+VonC);        % start scan in doppler data
d_end_scan = (start_scan+n_scans-1)/(1+VonC);  % end scan in doppler data

d_start = floor(d_start_scan);
d_end = floor(d_end_scan)+1;
d_N = d_end-d_start+1;

if d_end >= length(samples)
  demods = [];
  Eflag = 1;
  return;
end

demods = exp(-2i*pi*(1+VonC)*[d_start:d_end]*FConFS).'.*samples([d_start:d_end]+1);

d_index = [start_scan-1:start_scan-1+n_scans-1]'/(1+VonC)-d_start;

temp = demods(floor(d_index)+1);
demods = temp + ...
  (d_index-floor(d_index)).*(demods(floor(d_index+1)+1)-temp)*(1+VonC);

return;