
% 
% Delays the input signal
% 
% INPUTS
% - input: variable to be delayed
% - sampl_freq: in Hz
% - delay: in seconds (positive or negative)
% 

function delayedInput = MVP_delayCTD(input,samplFreq,delay)

if any(delay ~= 0) && length(input) > 1
    % Build the new delayed time grid
    oldTime = [0:length(input)-1]'*(1/samplFreq);
    if length(delay) == 1 || length(delay) == length(oldTime)
        newTime = sort(oldTime + delay);  % if the delay has both positive and negative values, make new_time monotonic
    else
        error('The delay (based on the speed) has not the same number of elements than CTD data!');
    end
    
    % Interpolate from the old to the new (delayed) time grid
    delayedInput = interp1(newTime,input,oldTime); %,'linear','extrap'); % << Changed by AdeV, extrapolation was making weird negatives...
%     delayedInput(delayedInput < 0) = 0;   %%% ??? %%%
else
    delayedInput = input;
end
