function avgFFT = averageSpectrum(x, frameSize, fftSize)

if mod(frameSize, 2) == 1
    frameSize = frameSize + 1;
end

lenX = length(x);
window = hamming(frameSize)';

zerosToPad = floor(frameSize/2);

z = zeros(1, zerosToPad);
x = [z x z];

center = zerosToPad + 1;
index = 1;

frameX = zeros(1, frameSize);

while center <= lenX + zerosToPad
    frameX(:) = x(center - zerosToPad : center + zerosToPad - 1);
    windowedFrame = frameX .* window;
    outputFFTs(:,index) = fft(windowedFrame, fftSize);
    index = index + 1;
    center = center + zerosToPad;
end

avgFFT = mean(outputFFTs, 2)';

end