]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/quantiz.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / quantiz.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{qidx} = } quantiz (@var{x}, @var{table})
18 ## @deftypefnx {Function File} {[@var{qidx}, @var{q}] = } quantiz (@var{x}, @var{table}, @var{codes})
19 ## @deftypefnx {Function File} {[ @var{qidx}, @var{q}, @var{d}] = } quantiz (@var{...})
20 ##
21 ## Quantization of an arbitrary signal relative to a paritioning.
22 ##
23 ## @table @code
24 ## @item qidx = quantiz(x, table)
25 ##   Determine position of x in strictly monotonic table.  The first
26 ##   interval, using index 0, corresponds to x <= table(1).
27 ##   Subsequent intervals are table(i-1) < x <= table(i).
28 ##
29 ## @item [qidx, q] = quantiz(x, table, codes)
30 ##   Associate each interval of the table with a code.  Use codes(1) 
31 ##   for x <= table(1) and codes(n+1) for table(n) < x <= table(n+1).
32 ##
33 ## @item [qidx, q, d] = quantiz(...)
34 ##   Compute distortion as mean squared distance of x from the
35 ##   corresponding quantization values.
36 ## @end table
37 ## @end deftypefn
38
39 function [qidx, q, d] = quantiz (x, table, codes)
40   if (nargin < 2 || nargin > 3)
41     usage("[qidx, q, d] = quantiz(x, table, codes)");
42   endif
43
44   qidx = length(table) - lookup(flipud(table(:)), x(:));
45   if (nargin > 2 && nargout > 1)
46     q = codes(qidx + 1);
47   endif
48   if (nargout > 2)
49     table = [table(1) ; table(:) ];
50     d = sumsq (x(:) - q(:)) / length(x);
51   endif
52 endfunction