annotate scripts/audio/lin2mu.m @ 31706:597f3ee61a48 stable

update Octave Project Developers copyright for the new year
author John W. Eaton <jwe@octave.org>
date Fri, 06 Jan 2023 13:11:27 -0500
parents fd29c7a50a78
children 2e484f9f1f18
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
31706
597f3ee61a48 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31551
diff changeset
3 ## Copyright (C) 1995-2023 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
8 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
2313
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 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
19 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
20 ## 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
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1636
diff changeset
25
3332
7c03933635c6 [project @ 1999-11-02 06:57:12 by jwe]
jwe
parents: 2847
diff changeset
26 ## -*- texinfo -*-
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
27 ## @deftypefn {} {@var{y} =} lin2mu (@var{x})
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
28 ## @deftypefnx {} {@var{y} =} lin2mu (@var{x}, @var{n})
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
29 ## Convert audio data from linear to mu-law.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
30 ##
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
31 ## Linear values use floating point values in the range -1 @leq{} @var{x}
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
32 ## @leq{} 1 if @var{n} is 0 (default), or @var{n}-bit signed integers if @var{n}
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
33 ## is 8 or 16. Mu-law values are 8-bit unsigned integers in the range
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
34 ## 0 @leq{} @var{y} @leq{} 255.
19647
0165d9607624 Deprecate audio functions loadaudio, playaudio, saveaudio, and setaudio.
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
35 ## @seealso{mu2lin}
3332
7c03933635c6 [project @ 1999-11-02 06:57:12 by jwe]
jwe
parents: 2847
diff changeset
36 ## @end deftypefn
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
37
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
38 function y = lin2mu (x, n = 0)
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
39
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
40 if (nargin < 1)
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
41 print_usage ();
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
42 endif
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
43
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
44 ## Convert to floating point integers per Matlab.
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
45 x = double (x);
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
46
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
47 if (nargin == 2)
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
48 if (! isscalar (n) && ! isreal (n)
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
49 || (n != 0 && n != 8 && n != 16))
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
50 error ("lin2mu: N must be either 0, 8, or 16");
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
51 elseif (isempty (n))
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
52 n = 0;
3911
8389e78e67d4 [project @ 2002-04-28 02:15:38 by jwe]
jwe
parents: 3408
diff changeset
53 endif
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]
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
59 x *= 32768;
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
60 elseif (n != 16)
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
61 x *= 256;
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.
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
65 sig = sign (x) + (x == 0);
1636
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
30974
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
71 ## Find exponent and fraction of binary 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
30974
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
77
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
78
30974
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
79 ## Test functionality
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
80 %!test
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
81 %! x = -1:1;
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
82 %! y = x';
31551
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 31042
diff changeset
83 %! assert (lin2mu (x), (lin2mu (y))');
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 31042
diff changeset
84 %! assert (lin2mu (x), [0, 255, 128]);
30974
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
85
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
86 %!assert (lin2mu ([0, 1, NaN, inf, -inf], 8), [255, 231, NaN, 128, 0])
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
87 %!assert (lin2mu ([]), [])
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
88 %!assert (lin2mu (0), 255)
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
89 %!assert (lin2mu (0, 0), 255)
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
90 %!assert (lin2mu (0, 8), 255)
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
91 %!assert (lin2mu (0, 16), 255)
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
92 %!assert (lin2mu (2, 8), 219)
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
93 %!assert (lin2mu (3, []), 128)
30974
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
94 %!assert (lin2mu (3, 16), 255)
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
95 %!assert (lin2mu (repmat (-0.23, 1, 1000), 0), repmat (34, 1, 1000))
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
96 %!assert (lin2mu (ones (2, 2), 0), repmat (128, 2))
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
97
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
98 ## Test input validation
e4f9db6543bd test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents: 30564
diff changeset
99 %!error <Invalid call> lin2mu ()
31042
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
100 %!error <N must be either 0, 8, or 16> lin2mu (1, 2)
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
101 %!error <N must be either 0, 8, or 16> lin2mu (1, [1,2])
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
102 %!error <N must be either 0, 8, or 16> lin2mu (1, ones (1, 2))
13a7af4ca0da Update lin2mu, mu2lin functions.
Rik <rik@octave.org>
parents: 30974
diff changeset
103 %!error <invalid conversion> lin2mu ({2:5})