function Xinter = interpolate(X, timeBase, hopSize)
% Xinter = interpolate(X, timeBase, hopSize) 
% X  - input Hanning Window
% timeBase - Vector of the desired Hanning Window size
% hopSize - number of sample indexes to shift over when calculating the next Hann Window
% the interpolate function performs a linear interpolation on the Hanning
% Window X in steps as defined by the timeBase vector.  The interpolator
% also calculates the phase advance and new phase of all the samples.  This
% is as perscribed by the Phase Vocoder.  The hopSize defines the size of the 
% hanning window shift and is used to calculate the phases of the new signal, the initial 
% phase advance of the bins.
% written by John Arroyo, ja2124@columbia.edu

Xrows = size(X,1);
N = 2*(Xrows-1);

% Phase accumulator, set the initial phase accumulation to the values in
% the first windowed bin of the encoded signal
phaseX = angle(X(:,1));

winIndex = 1; %begin the window index with 1

for i = timeBase
    % Get the 2 columns of X to interpolate from
    X1 = X(:,floor(i)+1);
    X2 = X(:,floor(i)+2);
    
    % find the interpolated magnitude from the two columns
    % e.g.: to interpolate 2 points into 4, X(3) = (1/3)X1 + (2/3)X2
    scale = i - floor(i); % finds the scaling factor of each of the 2 columns being interpolated, 0 >= scale < 1
    Xmag = (1-scale)*abs(X1) + scale*(abs(X2)); % scale1*X1 + scale2*X2 
       
    % Generate the the current column of Xinter (the interpolated Hann
    % Window)
    Xinter(:,winIndex) = Xmag .* exp(j*phaseX); %x = mag*e^jw
    winIndex = winIndex+1;
    
    % Generate the phase advance
    % the phase advance is the difference of the original phases minus the
    % expected phase advance
    phaseAdv = angle(X2) - angle(X1);
    
    % Accumulate the phase
    phaseX = phaseX + phaseAdv;
end
