home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / DATAFUN.DI$ / NEXTPOW2.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  567 b   |  26 lines

  1. function p = nextpow2(n)
  2. %NEXTPOW2 Next power of two.
  3. %    NEXTPOW2(N) returns the first P such that 2^P >= abs(N).  It is
  4. %    often useful for finding the nearest power of two sequence
  5. %    length for FFT operations.
  6. %    NEXTPOW2(X), if X is a vector, is the same as NEXTPOW2(LENGTH(X)).
  7. %
  8. %       See also LOG2.
  9.  
  10. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  11.  
  12. if length(n) > 1
  13.     n = length(n);
  14. end
  15.  
  16. [f,p] = log2(abs(n));
  17.  
  18. % Check if n is an exact power of 2.
  19. if f == 0.5
  20.     p = p-1;
  21. end
  22.  
  23. % Check for infinities and NaNs
  24. k = ~finite(f);
  25. p(k) = f(k);
  26.