Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function y = stft(x, w, N, H, fs, t)
- % Analysis/synthesis of a sound using the short-time Fourier transform
- % Authors: J. Bonada, X. Serra, X. Amatriain, A. Loscos
- % x: input sound, w: analysis window (odd size), N: FFT size, H: hop size
- % y: output sound
- %
- %--------------------------------------------------------------------------
- % This source code is provided without any warranties as published in
- % DAFX book 2nd edition, copyright Wiley & Sons 2011, available at
- % http://www.dafx.de. It may be used for educational purposes and not
- % for commercial applications without further permission.
- %--------------------------------------------------------------------------
- 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
- yw = zeros(M,1); % initialize output sound frame
- y = zeros(soundlength,1); % initialize output array
- w = w/sum(w); % normalize analysis window
- k = [0:1:N];
- freq = fs*k/N;
- %t = -80 ; % threshold
- peak = [0 0];
- trans=100;
- %while pin<pend
- %-----analysis-----%
- %clf;
- xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound
- % subplot(3,1,1);
- % plot(xw);
- % pause;
- %plot(freq(1:M), abs(xw));
- %pause;
- 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);
- %plot(fftbuffer);
- %pause;
- X = fft(fftbuffer); % compute FFT
- mX = 20*log10(abs(X(1:N2))); % magnitude spectrum of positive frequencies
- ploc = 1 + find((mX(2:N2-1)>t).*(mX(2:N2-1)>mX(3:N2)).*(mX(2:N2-1)>mX(1:N2-2)));
- % d=size(ploc);
- % d(1);
- % size(ploc)
- % for d(1)
- % if((ploc-pin)+trans)<M
- % ploc=ploc+trans;
- % end
- %end
- pX = unwrap(angle(X(1:N2))); % unwrapped phase spect. of positive freq.
- [iploc ipmag ipphase] = peakinterp (mX, pX, ploc);
- subplot(2,1,1);
- plot(freq(1:N2),mX);
- hold on;
- plot(fs*iploc/N,ipmag,'X');
- hold off;
- subplot(2,1,2);
- plot(freq(1:N2),pX);
- hold on;
- plot(fs*iploc/N,ipphase,'X');
- hold off;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement