function [mY, pY] = stft(x, w, N, H)
% Analysis/synthesis of a sound using the short-time fourier transform
% x: input sound, w: analysis window (odd size), N: FFT size, H: hop size
% y: output sound
M = length(w); % analysis window size
N2 = N/2+1; % size of positive spectrum
soundlength = length(x); % length of input sound array
hM = (M-1)/2; % half analysis window size
pin = 1+hM; % initialize sound pointer in middle of analysis window
pend = soundlength-hM; % last sample to start a frame
fftbuffer = zeros(N,1); % initialize buffer for FFT or reset buffer
w = w/sum(w); % normalize analysis window
nframes=mod(soundlength-hM,H);
frame=1;
%fftbuffer=zeros(N,1);
mY=zeros(N2,nframes);
pY=zeros(N2,nframes);

while pin<pend
%-----analysis-----%
xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound
fftbuffer(:) = 0; % reset buffer
fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase window in fftbuffer
fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
X = fft(fftbuffer); % compute FFT
mX = 20*log10(abs(X(1:N2))); % magnitude spectrum of positive frequencies
pX = unwrap(angle(X(1:N2))); % unwrapped phase spect. of positive freq.
mY(:,frame)=mX;
pY(:,frame)=pX;
frame=frame+1;
pin = pin+H; % advance sound pointer
end
% Copied from Carles
mY_norm=mY-(mY./mY).*max(max(mY)); 
%Spectrogram mode respresentation
surface(1:frame-1, linspace(0,pi,N2),mY_norm(:,1:frame-1),'LineStyle','none');
%Axes adjustment
xlim([1 frame-1]);
ylim([0 pi]);



[x,fs,nbit]=wavread('Ocarina.wav');
