1 ## Copyright (C) 2001 Paul Kienzle
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{y} = } compand (@var{x}, @var{mu}, @var{V}, 'mu/compressor')
18 ## @deftypefnx {Function File} {@var{y} = } compand (@var{x}, @var{mu}, @var{V}, 'mu/expander')
19 ## @deftypefnx {Function File} {@var{y} = } compand (@var{x}, @var{mu}, @var{V}, 'A/compressor')
20 ## @deftypefnx {Function File} {@var{y} = } compand (@var{x}, @var{mu}, @var{V}, 'A/expander')
22 ## Compresses and expanding the dynamic range of a signal using a mu-law or
23 ## or A-law algorithm.
25 ## The mu-law compressor/expander for reducing the dynamic range, is used
26 ## if the fourth argument of @dfn{compand} starts with 'mu/'. Whereas the
27 ## A-law compressor/expander is used if @dfn{compand} starts with 'A/'.
28 ## The mu-law algorithm uses the formulation
33 ## y = {V log (1 + \\mu / V \\|x\\|) \\over log (1 + \\mu)} sgn(x)
40 ## V log (1 + \mu/V |x|)
41 ## y = -------------------- sgn(x)
47 ## while the A-law algorithm used the formulation
52 ## y = { \\left\{ \\matrix{ {A / (1 + log A) x}, & 0 <= \\|x\\| <= V/A \\cr
54 ## {V log (1 + log(A/V \\|x\\|) ) \\over 1 + logA}, &
55 ## V/A < \\|x\\| <= V} \\right. }
62 ## / A / (1 + log A) x, 0 <= |x| <= V/A
64 ## y = < V ( 1 + log (A/V |x|) )
65 ## | ----------------------- sgn(x), V/A < |x| <= V
70 ## Neither converts from or to audio file ulaw format. Use mu2lin or lin2mu
74 ## @seealso{m2ulin, lin2mu}
76 function y = compand(x, mu, V, stype)
78 if (nargin != 3 && nargin != 4)
79 usage('y=compand(x,[mu|A],V,stype);');
82 stype = 'mu/compressor';
84 stype = tolower(stype);
87 if strcmp(stype, 'mu/compressor')
88 y = (V/log(1+mu)) * log(1+(mu/V)*abs(x)) .* sign(x);
89 elseif strcmp(stype, 'mu/expander')
90 y = (V/mu) * ( exp (abs(x) * (log(1+mu)/V)) - 1 ) .* sign(x);
91 elseif strcmp(stype, 'a/compressor')
93 idx = find (abs(x) <= V/mu);
95 y(idx) = (mu/(1+log(mu))) * abs(x(idx));
97 idx = find (abs(x) > V/mu);
99 y(idx) = (V/(1+log(mu))) * (1 + log ((mu/V) * abs(x(idx))));
102 elseif strcmp(stype, 'a/expander')
104 idx = find (abs(x) <= V/(1+log(mu)));
106 y(idx) = ((1+log(mu))/mu) * abs(x(idx));
108 idx = find (abs(x) > V/(1+log(mu)));
110 y(idx) = exp (((1+log(mu))/V) * abs(x(idx)) - 1) * (V/mu);