home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / audio / lin2mu.m next >
Encoding:
Text File  |  1999-11-02  |  1.8 KB  |  61 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} lin2mu (@var{x})
  22. ## If the vector @var{x} represents mono audio data in 8- or 16-bit
  23. ## linear encoding, @code{lin2mu (@var{x})} is the corresponding mu-law
  24. ## encoding.
  25. ## @end deftypefn
  26.  
  27. ## See also: mu2lin, loadaudio, saveaudio, playaudio, setaudio, record
  28.  
  29. ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
  30. ## Created: 17 October 1994
  31. ## Adapted-By: jwe
  32.  
  33. function y = lin2mu (x)
  34.  
  35.   if (nargin != 1)
  36.     usage ("y = lin2mu (x)");
  37.   endif
  38.  
  39.   if (! is_vector (x))
  40.     error ("lin2mu: x must be a vector");
  41.   endif
  42.  
  43.   ## transform 8-bit format to 16-bit
  44.   if (max (abs (x)) <= 128)
  45.     x = 256 .* x;
  46.   endif
  47.  
  48.   ## determine sign of x, set sign(0) = 1.
  49.   sig = sign(x) + (x == 0);
  50.  
  51.   ## take absolute value of x, but force it to be smaller than 32636;
  52.   ## add bias
  53.   x = min (abs (x), 32635 * ones (size (x))) + 132;
  54.  
  55.   ## find exponent and fraction of bineary representation
  56.   [f, e] = log2 (x);
  57.  
  58.   y = 64 * sig - 16 * e - fix (32 * f) + 335;
  59.  
  60. endfunction
  61.