home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / signal / stft.m < prev    next >
Text File  |  1997-02-19  |  3KB  |  115 lines

  1. ## Copyright (C) 1995, 1996, 1997  Andreas Weingessel
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  [Y, c] = stft (X [, win_size [, inc [, num_coef [, w_type]]]])
  18. ##
  19. ## Computes the short-term Fourier transform of the vector X with
  20. ## "num_coef" coefficients by applying a window of "win_size" data
  21. ## points and an increment of "inc" points.
  22. ##
  23. ## Before computing the Fourier transform, one of the following windows
  24. ## is applied:  "hanning" (w_type = 1), "hamming" (w_type = 2),
  25. ## "rectangle" (w_type = 3).  The window names can be passed as strings
  26. ## or by the w_type number.
  27. ##
  28. ## If not all arguments are specified, the following defaults are used:
  29. ## win_size = 80, inc = 24, num_coef = 64, w_type = 1.
  30. ##
  31. ## Y = stft (X [, ...]) returns the absolute values of the Fourier
  32. ## coefficients according to the num_coef positive frequencies.
  33. ## [Y, c] = stft (X [, ...]) returns the entire STFT-matrix Y and a
  34. ## vector c = [win_size, inc, w_type] which is needed by the synthesis
  35. ## function.
  36.  
  37. ## Author:  AW <Andreas.Weingessel@ci.tuwien.ac.at>
  38. ## Description:  Short-term Fourier transform
  39.  
  40. function [Y, c] = stft(X, win, inc, coef, w_type)
  41.   
  42.   ## default values of unspecified arguments
  43.   if (nargin < 5)
  44.     w_type = 1;
  45.     if (nargin < 4)
  46.       coef = 64;
  47.       if (nargin < 3)
  48.     inc = 24;
  49.     if (nargin < 2)
  50.       win = 80;
  51.     endif
  52.       endif
  53.     endif
  54.   elseif (nargin == 5)  
  55.     if (isstr (w_type))
  56.       if (strcmp (w_type, "hanning"))
  57.     w_type = 1;
  58.       elseif (strcmp (w_type, "hamming"))
  59.     w_type = 2;
  60.       elseif (strcmp (w_type, "rectangle"))
  61.     w_type = 3;
  62.       else
  63.     error (["stft:  unknown window type `", w_type, "'"])
  64.       endif
  65.     endif
  66.   else
  67.     usage ("[Y [, c]] = ", ...
  68.        "stft(X [, win_size [, inc [, num_coef [, w_type]]]])");
  69.   endif
  70.  
  71.   ## check whether X is a vector
  72.   [nr, nc] = size (X);
  73.   if (nc != 1)
  74.     if (nr == 1)
  75.       X = X'; 
  76.       nr = nc;
  77.     else
  78.       error ("stft:  X must be a vector");
  79.     endif
  80.   endif
  81.  
  82.   num_coef = 2 * coef;
  83.   if (win > num_coef)
  84.     win = num_coef;
  85.     printf ("stft:  window size adjusted to %f\n", win);
  86.   endif
  87.   num_win = fix ((nr - win) / inc);
  88.  
  89.   ## compute the window coefficients
  90.   if (w_type == 3)        # rectangular window 
  91.     WIN_COEF = ones (win, 1);
  92.   elseif (w_type == 2)        # Hamming window
  93.     WIN_COEF = hamming (win);
  94.   else                # Hanning window
  95.     WIN_COEF = hanning (win);
  96.   endif
  97.   
  98.   ## create a matrix Z whose columns contain the windowed time-slices
  99.   Z = zeros (num_coef, num_win + 1);
  100.   start = 1;
  101.   for i = 0:num_win
  102.     Z(1:win, i+1) = X(start:start+win-1) .* WIN_COEF;
  103.     start = start + inc;
  104.   endfor
  105.  
  106.   Y = fft (Z);
  107.  
  108.   if (nargout == 1)
  109.     Y = abs (Y(1:coef, :));
  110.   else
  111.     c = [win, inc, w_type];
  112.   endif
  113.  
  114. endfunction
  115.