Mercurial > octave-nkf
annotate scripts/audio/lin2mu.m @ 11472:1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 09 Jan 2011 21:33:04 -0800 |
parents | be55736a0783 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1999, 2000, 2002, 2004, 2005, 2006, |
2 ## 2007 John W. Eaton | |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2303 | 19 |
3332 | 20 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
21 ## @deftypefn {Function File} {} lin2mu (@var{x}) |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
22 ## @deftypefnx {Function File} {} lin2mu (@var{x}, @var{n}) |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
23 ## Convert audio data from linear to mu-law. Mu-law values use 8-bit |
3911 | 24 ## unsigned integers. Linear values use @var{n}-bit signed integers or |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
25 ## floating point values in the range -1 @leq{} @var{x} @leq{} 1 if |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
26 ## @var{n} is 0. |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
27 ## If @var{n} is not specified it defaults to 0, 8, or 16 depending on |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
28 ## the range of values in @var{x}. |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
29 ## @seealso{mu2lin, loadaudio, saveaudio} |
3332 | 30 ## @end deftypefn |
5642 | 31 |
2311 | 32 |
3911 | 33 ## Author: Andreas Weingessel <Andreas.Weingessel@ci.tuwien.ac.at> |
2312 | 34 ## Created: 17 October 1994 |
35 ## Adapted-By: jwe | |
36 | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
37 function y = lin2mu (x, n) |
2325 | 38 |
3911 | 39 if (nargin == 1) |
40 range = max (abs (x (:))); | |
41 if (range <= 1) | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
42 n = 0; |
3911 | 43 elseif (range <= 128) |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
44 n = 8; |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
45 warning ("lin2mu: no precision specified, so using %d", n); |
3911 | 46 else |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
47 n = 16; |
3911 | 48 endif |
49 elseif (nargin == 2) | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
50 if (n != 0 && n != 8 && n != 16) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
51 error ("lin2mu: N must be either 0, 8 or 16"); |
3911 | 52 endif |
53 else | |
6046 | 54 print_usage (); |
1636 | 55 endif |
2325 | 56 |
3911 | 57 ## Transform real and n-bit format to 16-bit. |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
58 if (n == 0) |
3911 | 59 ## [-1,1] -> [-32768, 32768] |
60 x = 32768 * x; | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
61 elseif (n != 16) |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
62 x = 2^(16-n) .* x; |
1636 | 63 endif |
64 | |
3911 | 65 ## Determine sign of x, set sign(0) = 1. |
1636 | 66 sig = sign(x) + (x == 0); |
67 | |
3911 | 68 ## Take absolute value of x, but force it to be smaller than 32636; |
69 ## add bias. | |
70 x = min (abs (x), 32635) + 132; | |
1636 | 71 |
3911 | 72 ## Find exponent and fraction of bineary representation. |
1636 | 73 [f, e] = log2 (x); |
74 | |
75 y = 64 * sig - 16 * e - fix (32 * f) + 335; | |
76 | |
77 endfunction |