Advertisement
neo01124

Peak Interpolation

Nov 6th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.64 KB | None | 0 0
  1. function y = stft(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. peak = [0 0];
  28. trans=100;
  29.  
  30. %while pin<pend
  31.   %-----analysis-----%
  32.   %clf;
  33.   xw = x(pin-hM:pin+hM).*w(1:M);             % window the input sound
  34. %   subplot(3,1,1);
  35. %   plot(xw);
  36.  
  37.  % pause;
  38.   %plot(freq(1:M), abs(xw));
  39.   %pause;
  40.   fftbuffer(:) = 0;                          % reset buffer
  41.   fftbuffer(1:(M+1)/2) = xw((M+1)/2:M);      % zero-phase window in fftbuffer
  42.   fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
  43.   %plot(fftbuffer);
  44.   %pause;
  45.   X = fft(fftbuffer);                        % compute FFT
  46.   mX = 20*log10(abs(X(1:N2)));    % magnitude spectrum of positive frequencies
  47.   ploc = 1 + find((mX(2:N2-1)>t).*(mX(2:N2-1)>mX(3:N2)).*(mX(2:N2-1)>mX(1:N2-2)));
  48. %   d=size(ploc);
  49. %   d(1);
  50. %   size(ploc)
  51. %   for d(1)
  52. %       if((ploc-pin)+trans)<M
  53. %           ploc=ploc+trans;
  54. %       end
  55.   %end
  56.    pX = unwrap(angle(X(1:N2)));    % unwrapped phase spect. of positive freq.
  57.    [iploc ipmag ipphase] = peakinterp (mX, pX, ploc);
  58.    subplot(2,1,1);
  59.    plot(freq(1:N2),mX);
  60.    hold on;
  61.    plot(fs*iploc/N,ipmag,'X');
  62.    hold off;
  63.        
  64.    subplot(2,1,2);
  65.    plot(freq(1:N2),pX);
  66.    hold on;
  67.    plot(fs*iploc/N,ipphase,'X');
  68.    hold off;
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement