annotate extra/Lab4.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 = stpt(x, w, N, H, t)
yading@10 2 % Analysis/synthesis of a sound using the peaks
yading@10 3 % of the short-time fourier transform
yading@10 4 % x: input sound,
yading@10 5 % w: analysis window (odd size),
yading@10 6 % N: FFT size,
yading@10 7 % H: hop size,
yading@10 8 % t: threshold in negative dB,
yading@10 9 % y: output sound
yading@10 10 M = length(w); % analysis window size
yading@10 11 N2 = N/2+1; % size of positive spectrum
yading@10 12 soundlength = length(x); % length of input sound array
yading@10 13 hM = (M-1)/2; % half analysis window size
yading@10 14 pin = 1+hM; % initialize sound pointer at the middle of analysis window
yading@10 15 pend = soundlength-hM; % last sample to start a frame
yading@10 16 fftbuffer = zeros(N,1); % initialize buffer for FFT
yading@10 17 yw = zeros(M,1); % initialize output sound frame
yading@10 18 y = zeros(soundlength,1); % initialize output array
yading@10 19 w = w/sum(w); % normalize analysis window
yading@10 20 sw = hanning(M); % synthesis window
yading@10 21 sw = sw./sum(sw);
yading@10 22 while pin<pend
yading@10 23 %-----analysis-----%
yading@10 24 xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound
yading@10 25 fftbuffer(:) = 0; % reset buffer
yading@10 26 fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase fftbuffer
yading@10 27 fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
yading@10 28 X = fft(fftbuffer); % compute the FFT
yading@10 29 mX = 20*log10(abs(X(1:N2))); % magnitude spectrum of positive frequencies
yading@10 30 pX = unwrap(angle(X(1:N2))); % unwrapped phase spectrum
yading@10 31 ploc = 1 + find((mX(2:N2-1)>t) .* (mX(2:N2-1)>mX(3:N2)) ...
yading@10 32 .* (mX(2:N2-1)>mX(1:N2-2))); % peakss
yading@10 33 pmag = mX(ploc); % magnitude of peaks
yading@10 34 pphase = pX(ploc); % phase of peaks
yading@10 35 %-----synthesis-----%
yading@10 36 Y = zeros(N,1); % initialize output spectrum
yading@10 37 Y(ploc) = 10.^(pmag/20).*exp(i.*pphase); % generate positive freq.
yading@10 38 Y(N+2-ploc) = 10.^(pmag/20).*exp(-i.*pphase); % generate negative freq.
yading@10 39 fftbuffer = real(ifft(Y)); % real part of the inverse FFT
yading@10 40 yw((M+1)/2:M) = fftbuffer(1:(M+1)/2); % undo zero phase window
yading@10 41 yw(1:(M-1)/2) = fftbuffer(N-(M-1)/2+1:N);
yading@10 42 y(pin-hM:pin+hM) = y(pin-hM:pin+hM) + H*N*sw.*yw(1:M); % overlap-add
yading@10 43 pin = pin+H; % advance sound pointer
yading@10 44 end