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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1995, 1996, 1997, 1999, 2000, 2002, 2004, 2005, 2006,
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## 2007 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1636
diff changeset
19
3332
7c03933635c6 [project @ 1999-11-02 06:57:12 by jwe]
jwe
parents: 2847
diff changeset
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
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
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
7c03933635c6 [project @ 1999-11-02 06:57:12 by jwe]
jwe
parents: 2847
diff changeset
30 ## @end deftypefn
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
31
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
32
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
33 ## Author: Andreas Weingessel <Andreas.Weingessel@ci.tuwien.ac.at>
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
34 ## Created: 17 October 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
35 ## Adapted-By: jwe
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
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
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
38
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
39 if (nargin == 1)
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
40 range = max (abs (x (:)));
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
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
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
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
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
46 else
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
47 n = 16;
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
48 endif
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
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
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
52 endif
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
53 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
54 print_usage ();
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
55 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
56
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
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
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
59 ## [-1,1] -> [-32768, 32768]
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
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
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
63 endif
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
64
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
65 ## Determine sign of x, set sign(0) = 1.
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
66 sig = sign(x) + (x == 0);
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
67
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
68 ## Take absolute value of x, but force it to be smaller than 32636;
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
69 ## add bias.
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
70 x = min (abs (x), 32635) + 132;
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
71
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
72 ## Find exponent and fraction of bineary representation.
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
73 [f, e] = log2 (x);
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
74
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
75 y = 64 * sig - 16 * e - fix (32 * f) + 335;
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
76
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
77 endfunction