Mercurial > octave
annotate scripts/audio/lin2mu.m @ 31042:13a7af4ca0da
Update lin2mu, mu2lin functions.
* lin2mu.m: Rewrite docstring. Use default argument for input 'n' to simply
input validation. Eliminate warning abut no precision certified. Add stricter
input validation 'n' input. Use inplace operators for better performnace.
Update BIST tests.
* mu2lin.m: Rewrite docstring. Use default argument for input 'n' to simply
input validation. Add BIST tests for function.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 28 May 2022 23:21:53 -0700 |
parents | e4f9db6543bd |
children | fd29c7a50a78 |
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 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 1995-2022 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 | 7 ## |
8 ## This file is part of Octave. | |
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 | 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 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
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 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 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 | 25 |
3332 | 26 ## -*- texinfo -*- |
31042 | 27 ## @deftypefn {} {@var{y} =} lin2mu (@var{x}) |
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 | 31 ## Linear values use floating point values in the range -1 @leq{} @var{x} |
32 ## @leq{} 1 if @var{n} is 0 (default), or @var{n}-bit signed integers if @var{n} | |
33 ## is 8 or 16. Mu-law values are 8-bit unsigned integers in the range | |
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 | 36 ## @end deftypefn |
5642 | 37 |
31042 | 38 function y = lin2mu (x, n = 0) |
2325 | 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 | 44 ## Convert to floating point integers per Matlab. |
45 x = double (x); | |
46 | |
47 if (nargin == 2) | |
48 if (! isscalar (n) && ! isreal (n) | |
49 || (n != 0 && n != 8 && n != 16)) | |
50 error ("lin2mu: N must be either 0, 8, or 16"); | |
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 | 53 endif |
1636 | 54 endif |
2325 | 55 |
3911 | 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 | 58 ## [-1,1] -> [-32768, 32768] |
31042 | 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 | 61 x *= 256; |
1636 | 62 endif |
63 | |
3911 | 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 | 66 |
3911 | 67 ## Take absolute value of x, but force it to be smaller than 32636; |
68 ## add bias. | |
69 x = min (abs (x), 32635) + 132; | |
1636 | 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 | 72 [f, e] = log2 (x); |
73 | |
74 y = 64 * sig - 16 * e - fix (32 * f) + 335; | |
75 | |
76 endfunction | |
30974
e4f9db6543bd
test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents:
30564
diff
changeset
|
77 |
31042 | 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'; |
e4f9db6543bd
test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents:
30564
diff
changeset
|
83 %! assert (lin2mu (x), (lin2mu (y))') |
e4f9db6543bd
test: new BISTs for lin2mu.m (patch #10192).
Shreya Gupta <shreyaguptammm@gmail.com>
parents:
30564
diff
changeset
|
84 %! assert (lin2mu (x), [0, 255, 128]) |
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 | 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 | 100 %!error <N must be either 0, 8, or 16> lin2mu (1, 2) |
101 %!error <N must be either 0, 8, or 16> lin2mu (1, [1,2]) | |
102 %!error <N must be either 0, 8, or 16> lin2mu (1, ones (1, 2)) | |
103 %!error <invalid conversion> lin2mu ({2:5}) |