Advertisement
neo01124

Stpt

Oct 28th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.74 KB | None | 0 0
  1. function y = stpt(x, w, N, H, fs, t)
  2. % Analysis/synthesis of a sound using the short-time Fourier transform
  3. % Authors: J. Bonada, X. Serra, X. Amatriain, A. Loscos
  4. % x: input sound, w: analysis window (odd size), N: FFT size, H: hop size
  5. % y: output sound
  6. %
  7. %--------------------------------------------------------------------------
  8. % This source code is provided without any warranties as published in
  9. % DAFX book 2nd edition, copyright Wiley & Sons 2011, available at
  10. % http://www.dafx.de. It may be used for educational purposes and not
  11. % for commercial applications without further permission.
  12. %--------------------------------------------------------------------------
  13.  
  14. M = length(w);                               % analysis window size
  15. N2 = N/2+1;                                  % size of positive spectrum
  16. soundlength = length(x);                     % length of input sound array
  17. hM = (M-1)/2;                                % half analysis window size
  18. pin = 1+hM;       % initialize sound pointer in middle of analysis window
  19. pend = soundlength-hM;                       % last sample to start a frame
  20. fftbuffer = zeros(N,1);                      % initialize buffer for FFT
  21. yw = zeros(M,1);                             % initialize output sound frame
  22. y = zeros(soundlength,1);                    % initialize output array
  23. w = w/sum(w);                                % normalize analysis window
  24. k = [0:1:N];
  25. freq = fs*k/N;
  26. %t = -80 ;                                    % threshold
  27.  
  28. while pin<pend
  29.   %-----analysis-----%
  30.   %clf;
  31.   xw = x(pin-hM:pin+hM).*w(1:M);             % window the input sound
  32.  
  33.   fftbuffer(:) = 0;                          % reset buffer
  34.   fftbuffer(1:(M+1)/2) = xw((M+1)/2:M);      % zero-phase window in fftbuffer
  35.   fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
  36.   X = fft(fftbuffer);                        % compute FFT
  37.   mX = 20*log10(abs(X(1:N2)));    % magnitude spectrum of positive frequencies
  38.   ploc = 1 + find((mX(2:N2-1)>t).*(mX(2:N2-1)>mX(3:N2)).*(mX(2:N2-1)>mX(1:N2-2)));
  39.   pX = unwrap(angle(X(1:N2)));    % unwrapped phase spect. of positive freq.
  40.   pmag=mX(ploc);
  41.   pphase=pX(ploc);
  42.    %-----synthesis-----%
  43.   Y = zeros(N,1);     % initialize output spectrum
  44.   Y(ploc) = 10.^(pmag/20).*exp(i.*pphase); % generate positive freq.  
  45.   Y(N-ploc) = 10.^(pmag/20).*exp(-i.*pphase);% generate negative frequency          
  46.  
  47.  
  48.   fftbuffer = real(ifft(Y));                 % inverse FFT
  49.   fftbuffer2 = fftbuffer.*hanning(N);        % smoothing with a window
  50.  
  51.   yw(1:(M-1)/2) = fftbuffer(N-(M-1)/2+1:N);  % undo zero-phase window
  52.   yw((M+1)/2:M) = fftbuffer(1:(M+1)/2);
  53.  
  54.   y(pin-hM:pin+hM) = y(pin-hM:pin+hM) + H*yw(1:M);     % overlap-add
  55.   pin  = pin+H;                              % advance sound pointer
  56.   end
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement