]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/compand.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / compand.m
1 ## Copyright (C) 2001 Paul Kienzle
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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')
21 ##
22 ## Compresses and expanding the dynamic range of a signal using a mu-law or
23 ## or A-law algorithm.
24 ##
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
29 ##
30 ## @iftex
31 ## @tex
32 ## $$
33 ## y = {V log (1 + \\mu / V \\|x\\|) \\over log (1 + \\mu)} sgn(x)
34 ## $$
35 ## @end tex
36 ## @end iftex
37 ## @ifinfo
38 ## @example
39 ##
40 ##         V log (1 + \mu/V |x|)
41 ##     y = -------------------- sgn(x)
42 ##             log (1 + \mu)
43 ##
44 ## @end example
45 ## @end ifinfo
46 ##
47 ## while the A-law algorithm used the formulation
48 ##
49 ## @iftex
50 ## @tex
51 ## $$
52 ## y = { \\left\{  \\matrix{ {A / (1 + log A) x}, & 0 <= \\|x\\| <= V/A \\cr
53 ##                  & \\cr  
54 ##                  {V log (1 + log(A/V \\|x\\|) ) \\over 1 + logA}, &
55 ##                  V/A < \\|x\\| <= V} \\right. }
56 ## $$
57 ## @end tex
58 ## @end iftex
59 ## @ifinfo
60 ## @example
61 ##
62 ##         /    A / (1 + log A) x,               0 <= |x| <= V/A  
63 ##         | 
64 ##     y = <    V ( 1 + log (A/V |x|) )
65 ##         |    ----------------------- sgn(x),  V/A < |x| <= V
66 ##         \        1 + log A
67 ## @end example
68 ## @end ifinfo
69 ##
70 ## Neither converts from or to audio file ulaw format. Use mu2lin or lin2mu
71 ## instead.
72 ##
73 ## @end deftypefn
74 ## @seealso{m2ulin, lin2mu}
75
76 function y = compand(x, mu, V, stype)
77
78   if (nargin != 3 && nargin != 4)
79     usage('y=compand(x,[mu|A],V,stype);'); 
80   endif
81   if (nargin < 4) 
82     stype = 'mu/compressor';
83   else 
84     stype = tolower(stype);
85   endif
86
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')
92     y = zeros(size(x));
93     idx = find (abs(x) <= V/mu);
94     if (idx)
95       y(idx) = (mu/(1+log(mu))) * abs(x(idx));
96     endif
97     idx = find (abs(x) > V/mu);
98     if (idx)
99       y(idx) = (V/(1+log(mu))) * (1 + log ((mu/V) * abs(x(idx))));
100     endif
101     y = y .* sign(x);
102   elseif strcmp(stype, 'a/expander')
103     y = zeros(size(x));
104     idx = find (abs(x) <= V/(1+log(mu)));
105     if (idx)
106       y(idx) = ((1+log(mu))/mu) * abs(x(idx));
107     endif
108     idx = find (abs(x) > V/(1+log(mu)));
109     if (idx)
110       y(idx) = exp (((1+log(mu))/V) * abs(x(idx)) - 1) * (V/mu);
111     endif
112     y = y .* sign(x);
113   endif
114
115 endfunction
116