
% segment audio data into overlapping frames and take fft of each frame

function fftframes = myframefft(audiodata, winlength, overlap, windowkind, varargin)

if nargin > 4
    nPad = varargin{1};
    zeropadding = true;
else
    zeropadding = false;
end

fprintf(1,'');   

win = winlength;

% Pad audio with zeros so that first analysis frame corresponds with first
% audio sample

pad = zeros((win/2),1);

%audiodata = [pad; audiodata; pad];

% find size of input
ilength = length(audiodata);


%pad audio data
audiodata = [pad; audiodata; pad]; 

% set hop size
hop = fix(win.*overlap); 

% set number of windows
num_win = ceil((ilength+1)/win/overlap);

start=1;
if ~zeropadding
    fftframes = zeros(win,num_win);
else
    fftframes = zeros(win*(nPad+1),num_win);
end

check = ceil(num_win/10);

switch windowkind 
    case 'hamming'
        window = hamming(win);
    case 'rect'
        window = rectwin(win);
    case 'kaiser'
        window = kaiser(win);
end


% do fft analysis for each window:
for i = 1 : num_win;
    start = floor(win * overlap * (i-1) + 1);
    if ~zeropadding
        segment = window .* audiodata(start:(start + win - 1));
        fftframes(:,i) = fft(segment, win)';
    else
        segment = window .* audiodata(start:(start + win - 1));
        fftframes(:,i) = fft(segment, win * (nPad+1))';
    end
  
%    start = start + hop
    
    if mod(i,check)  == 0
        fprintf(1,'.');
    end
    
end;
fprintf(1,'\n');


