function [idx] = PickSpeeds(Speed,windowSize,epsilon,speeds)
%This function picks out contiguous values of the array of Speed that are
%centered around the values specified in speeds, +/- epsilon.  

Speed_Filt = loc_filtfilt(ones(1,windowSize)/windowSize,1,Speed);
figure
plot(1:length(Speed),Speed,1:length(Speed_Filt),Speed_Filt);


 for j = 1:2
  if(j == 2)  %Use prescribed Speeds the first time, then re-run with mean speeds from that result
     for i = 1:length(speeds)
       speeds(i) = mean(Speed(idx{i}));       
     end
     epsilon = epsilon/2;
  end
    
  for i = 1:length(speeds);
    foo = find((Speed_Filt < speeds(i)+epsilon) & (Speed_Filt > speeds(i)-epsilon));
    goo = foo((diff(foo)==1));
    while(~isempty(find(diff(goo) > 1)))  %This loop finds the largest contiguous range of indexes in the results.
       foo = goo;   
    goo = foo((diff(foo)==1));
    end  
    idx{i} = goo;
  end
  
 end
 
 
 
end

