function X = encode(x, winLen, hopSize)
% function X = encode(x, fftPts, winLen, hopSize)
% x - input signal
% fftPts - size of the fft (in general the length of the Hanning Window
% winLen - Length of the Hanning Window
% hopSize - number of sample indexes to shift over when calculating the next Hann Window
% X - output Hanning window
% This is similiar to the spectrogram function in the signal processing
% toolbox.
% written by John Arroyo, ja2124@columbia.edu

signalLen = length(x);
fftPts = winLen; %set the fft size to the size of the window

%create a periodic hanning window the length of f
hannWin = hann(winLen, 'periodic');

% Generate an empty output array
X = zeros((1+fftPts/2),1+fix((signalLen-fftPts)/hopSize));

%index into the Hanning Window.  Each index contains one Window of length hannWin
winIndex = 1;

%calculate the FFT at every window, shift by the hopSize until the entire
%signal x has been looped through
for i = 0:hopSize:(signalLen-fftPts) 
    window = hannWin.*x((i+1):(i+fftPts)); % multiply the samples by the Hann Window
    Xwin = fft(window',fftPts); % Transpose and calculate the FFT of the window (must be transposed since the fft requires a column vector
    X(:,winIndex) = Xwin(1:(1+fftPts/2))'; %store the Window Frame in the Hann Window
    winIndex = winIndex+1; %increment index
end;

%similiar to the filterbank, you are modulating by a sinusoid and then
%breaking the signal into bands.  In this case the bands are the window
%length of the hamming window