annotate scripts/audio/lin2mu.m @ 11587:c792872f8942

all script files: untabify and strip trailing whitespace
author John W. Eaton <jwe@octave.org>
date Thu, 20 Jan 2011 17:35:29 -0500
parents fd0a3ac60b0e
children 605ed5bbb643
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11472
diff changeset
1 ## Copyright (C) 1995-2011 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ## 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
6 ## 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
7 ## 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
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ## 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
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## 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
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ## 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
16 ## 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
17 ## <http://www.gnu.org/licenses/>.
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1636
diff changeset
18
3332
7c03933635c6 [project @ 1999-11-02 06:57:12 by jwe]
jwe
parents: 2847
diff changeset
19 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
20 ## @deftypefn {Function File} {} lin2mu (@var{x})
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
21 ## @deftypefnx {Function File} {} lin2mu (@var{x}, @var{n})
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
22 ## Convert audio data from linear to mu-law. Mu-law values use 8-bit
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
23 ## 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
24 ## floating point values in the range -1 @leq{} @var{x} @leq{} 1 if
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
25 ## @var{n} is 0.
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
26 ## If @var{n} is not specified it defaults to 0, 8, or 16 depending on
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
27 ## the range of values in @var{x}.
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
28 ## @seealso{mu2lin, loadaudio, saveaudio}
3332
7c03933635c6 [project @ 1999-11-02 06:57:12 by jwe]
jwe
parents: 2847
diff changeset
29 ## @end deftypefn
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
30
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
31
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
32 ## 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
33 ## Created: 17 October 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
34 ## Adapted-By: jwe
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
35
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
36 function y = lin2mu (x, n)
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
37
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
38 if (nargin == 1)
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
39 range = max (abs (x (:)));
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
40 if (range <= 1)
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
41 n = 0;
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
42 elseif (range <= 128)
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
43 n = 8;
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
44 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
45 else
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
46 n = 16;
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
47 endif
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
48 elseif (nargin == 2)
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
49 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
50 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
51 endif
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
52 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
53 print_usage ();
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
54 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
55
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
56 ## 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
57 if (n == 0)
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
58 ## [-1,1] -> [-32768, 32768]
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
59 x = 32768 * x;
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
60 elseif (n != 16)
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
61 x = 2^(16-n) .* x;
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
62 endif
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
63
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
64 ## Determine sign of x, set sign(0) = 1.
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
65 sig = sign(x) + (x == 0);
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
66
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
67 ## 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
68 ## add bias.
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
69 x = min (abs (x), 32635) + 132;
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
70
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
71 ## Find exponent and fraction of bineary representation.
1636
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
72 [f, e] = log2 (x);
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
73
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
74 y = 64 * sig - 16 * e - fix (32 * f) + 335;
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
75
21fe2afb3692 [project @ 1995-11-16 19:16:11 by jwe]
jwe
parents:
diff changeset
76 endfunction