home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-bin.lha / lib / octave / 1.1.1 / m / signal / fftfilt.m < prev    next >
Text File  |  1996-10-12  |  3KB  |  101 lines

  1. # Copyright (C) 1995 John W. Eaton
  2. # This file is part of Octave.
  3. # Octave is free software; you can redistribute it and/or modify it
  4. # under the terms of the GNU General Public License as published by the
  5. # Free Software Foundation; either version 2, or (at your option) any
  6. # later version.
  7. # Octave is distributed in the hope that it will be useful, but WITHOUT
  8. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  10. # for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with Octave; see the file COPYING.  If not, write to the Free
  13. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14.  
  15. function y = fftfilt (b, x, N)
  16.  
  17. # usage:  fftfilt (b, x [, N])
  18. #
  19. # y = fftfilt (b, x) filters x with the FIR filter b using the FFT.
  20. # y = fftfilt (b, x, N) uses the overlap-add method to filter x with
  21. # b using an N-point FFT.
  22.  
  23. # Written by KH (Kurt.Hornik@ci.tuwien.ac.at) on Sep 3, 1994
  24.  
  25. # Reference:  Oppenheim & Schafer (1989).  Discrete-time Signal
  26. # Processing (Chapter 8).  Prentice-Hall.
  27.  
  28. # If N is not specified explicitly, we do not use the overlap-add 
  29. # method at all because loops are really slow.  Otherwise, we only
  30. # ensure that the number of points in the FFT is the smallest power
  31. # of two larger than N and length(b).  This could result in length
  32. # one blocks, but if the user knows better ...
  33.   
  34.   if (nargin < 2 || nargin > 3)
  35.     usage (" fftfilt (b, x [, N])");
  36.   endif
  37.   
  38.   [r_x, c_x] = size (x);
  39.   [r_b, c_b] = size (b);
  40.   if (! (min ([r_x c_x]) == 1 || min ([r_b c_b]) == 1))
  41.     error ("fftfilt: both x and b should be vectors");
  42.   endif
  43.   l_x  = r_x * c_x;
  44.   l_b  = r_b * c_b;
  45.  
  46.   if ((l_x == 1) && (l_b == 1))
  47.     y = b * x;  
  48.     return;
  49.   endif
  50.   
  51.   x = reshape (x, 1, l_x);
  52.   b = reshape (b, 1, l_b);
  53.   
  54.   if (nargin == 2)
  55. # Use FFT with the smallest power of 2 which is >= length (x) +
  56. # length (b) - 1 as number of points ...
  57.     N    = 2^(ceil (log (l_x + l_b - 1) / log(2)));
  58.     y    = ifft (fft (x, N) .* fft(b, N));
  59.   else
  60. # Use overlap-add method ...
  61.     if !(is_scalar (N))
  62.       error ("fftfilt: N has to be a scalar");
  63.     endif
  64.     N = 2^(ceil (log (max ([N l_b])) / log(2)));
  65.     L = N - l_b + 1;
  66.     B = fft (b, N);
  67.     R = ceil (l_x / L);
  68.     y = zeros (1, l_x);
  69.     for r = 1:R;
  70.       lo  = (r - 1) * L + 1;
  71.       hi  = min (r * L, l_x);
  72.       tmp = ifft (fft (x(lo:hi), N) .* B);
  73.       hi  = min (lo+N-1, l_x);
  74.       y(lo:hi) = y(lo:hi) + tmp(1:(hi-lo+1));
  75.     endfor  
  76.   endif
  77.     
  78.   y = reshape (y(1:l_x), r_x, c_x);
  79.  
  80. # Final cleanups:  if both x and b are real respectively integer, y
  81. # should also be
  82.  
  83.   if (! (any (imag (x)) || any (imag (b))))
  84.     y = real (y);
  85.   endif
  86.   if (! (any (x - round (x)) || any (b - round (b))))
  87.     y = round (y);
  88.   endif
  89.  
  90. endfunction
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.