Advertisement
neo01124

fft and synthesis

Oct 19th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.70 KB | None | 0 0
  1. function y = stft(x, w, N, H, fs)
  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. while pin<pend
  27.   %-----analysis-----%
  28.   %clf;
  29.   xw = x(pin-hM:pin+hM).*w(1:M);             % window the input sound
  30.   %plot(freq(1:M), abs(xw));
  31.   %pause
  32.   fftbuffer(:) = 0;                          % reset buffer
  33.   fftbuffer(1:(M+1)/2) = xw((M+1)/2:M);      % zero-phase window in fftbuffer
  34.   fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
  35.   X = fft(fftbuffer);                        % compute FFT
  36.   mX = 20*log10(abs(X(1:N2)));    % magnitude spectrum of positive frequencies
  37.   %subplot(2,1,1)
  38.   %plot(freq(1:N2),mX);
  39.   pX = unwrap(angle(X(1:N2)));    % unwrapped phase spect. of positive freq.
  40.   %subplot(2,1,2)
  41.   %plot(freq(1:N2),pX);
  42.    %-----synthesis-----%
  43.   Y = zeros(N,1);                            % initialize output spectrum
  44.   Y(1:N2) = 10.^(mX/20).*exp(i.*pX);         % generate positive freq.
  45.   Y(N2+1:N) = 10.^(mX(N2-1:-1:2)/20).*exp(-i.*pX(N2-1:-1:2));
  46.                                              % generate neg.freq.
  47.   fftbuffer = real(ifft(Y));                 % inverse FFT
  48.   yw(1:(M-1)/2) = fftbuffer(N-(M-1)/2+1:N);  % undo zero-phase window
  49.   yw((M+1)/2:M) = fftbuffer(1:(M+1)/2);
  50.   y(pin-hM:pin+hM) = y(pin-hM:pin+hM) + H*yw(1:M);     % overlap-add
  51.   pin  = pin+H;                              % advance sound pointer
  52. end
  53. wavwrite(y, fs,'out.wav');
  54. end
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement