view main/comm/compand.m @ 0:6b33357c7561 octave-forge

Initial revision
author pkienzle
date Wed, 10 Oct 2001 19:54:49 +0000
parents
children 039e2a73aa8d
line wrap: on
line source

## Copyright (C) 2001 Paul Kienzle
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

## y = compand(x, mu, V, 'mu/compressor')
## y = compand(x, mu, V, 'mu/expander')
##   mu-law compressor/expander for reducing the dynamic range of your
##   signal. Uses:
##
##         V log (1 + \mu/V |x|)
##     y = -------------------- sgn(x)
##             log (1 + \mu)
##
##   Does not convert from/to audio file ulaw format.  Use mu2lin/lin2mu
##   instead.
##
## y = compand(x, A, V, 'A/compressor')
## y = compand(x, A, V, 'A/expander')
##   A-law compressor/expander. Uses:
##
##         /    A / (1 + log A) x,               0 <= |x| <= V/A  
##         | 
##     y = <    V ( 1 + log (A/V |x|) )
##         |    ----------------------- sgn(x),  V/A < |x| <= V
##         \        1 + log A

function y = compand(x, mu, V, stype)

  if (nargin != 3 && nargin != 4)
    usage('y=compand(x,[mu|A],V,stype);'); 
  endif
  if (nargin < 4) 
    stype = 'mu/compressor';
  else 
    stype = tolower(stype);
  endif

  if strcmp(stype, 'mu/compressor')
    y = (V/log(1+mu)) * log(1+(mu/V)*abs(x)) .* sign(x);
  elseif strcmp(stype, 'mu/expander')
    y = (V/mu) * ( exp (abs(x) * (log(1+mu)/V)) - 1 ) .* sign(x);
  elseif strcmp(stype, 'a/compressor')
    y = zeros(size(x));
    idx = find (abs(x) <= V/mu);
    if (idx)
      y(idx) = (mu/(1+log(mu))) * abs(x(idx));
    endif
    idx = find (abs(x) > V/mu);
    if (idx)
      y(idx) = (V/(1+log(mu))) * (1 + log ((mu/V) * abs(x(idx))));
    endif
    y = y .* sign(x);
  elseif strcmp(stype, 'a/expander')
    y = zeros(size(x));
    idx = find (abs(x) <= V/(1+log(mu)));
    if (idx)
      y(idx) = ((1+log(mu))/mu) * abs(x(idx));
    endif
    idx = find (abs(x) > V/(1+log(mu)));
    if (idx)
      y(idx) = exp (((1+log(mu))/V) * abs(x(idx)) - 1) * (V/mu);
    endif
    y = y .* sign(x);
  endif

endfunction