annotate extra codes/Lab4.m @ 4:aeccfa83c7a4

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