annotate extra/stft_primer_ejemplo.m @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 function y = stft(x, w, N, H)
yading@10 2 % Analysis/synthesis of a sound using the short-time fourier transform
yading@10 3 % x: input sound, w: analysis window (odd size), N: FFT size, H: hop size
yading@10 4 % y: output sound
yading@10 5 M = length(w); % analysis window size
yading@10 6 N2 = N/2+1; % size of positive spectrum
yading@10 7 soundlength = length(x); % length of input sound array
yading@10 8 hM = (M-1)/2; % half analysis window size
yading@10 9 pin = 1+hM; % initialize sound pointer in middle of analysis window
yading@10 10 pend = soundlength-hM; % last sample to start a frame
yading@10 11 fftbuffer = zeros(N,1); % initialize buffer for FFT
yading@10 12 yw = zeros(M,1); % initialize output sound frame
yading@10 13 y = zeros(soundlength,1); % initialize output array
yading@10 14 w = w/sum(w); % normalize analysis window
yading@10 15 while pin<pend
yading@10 16 %-----analysis-----%
yading@10 17 xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound
yading@10 18 fftbuffer(:) = 0; % reset buffer
yading@10 19 fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase window in fftbuffer
yading@10 20 fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
yading@10 21 X = fft(fftbuffer); % compute FFT
yading@10 22 mX = 20*log10(abs(X(1:N2))); % magnitude spectrum of positive frequencies
yading@10 23 pX = unwrap(angle(X(1:N2))); % unwrapped phase spect. of positive freq.
yading@10 24 %-----synthesis-----%
yading@10 25 Y = zeros(N,1); % initialize output spectrum
yading@10 26 Y(1:N2) = 10.^(mX/20).*exp(i.*pX); % generate positive freq.
yading@10 27 Y(N2+1:N) = 10.^(mX(N2-1:-1:2)/20).*exp(-i.*pX(N2-1:-1:2));
yading@10 28 % generate neg.freq.
yading@10 29 fftbuffer = real(ifft(Y)); % inverse FFT
yading@10 30 yw(1:(M-1)/2) = fftbuffer(N-(M-1)/2+1:N); % undo zero-phase window
yading@10 31 yw((M+1)/2:M) = fftbuffer(1:(M+1)/2);
yading@10 32 y(pin-hM:pin+hM) = y(pin-hM:pin+hM) + H*yw(1:M); % overlap-add
yading@10 33 pin = pin+H; % advance sound pointer
yading@10 34 end