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{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{...})
21 ## Quantization of an arbitrary signal relative to a paritioning.
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).
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).
33 ## @item [qidx, q, d] = quantiz(...)
34 ## Compute distortion as mean squared distance of x from the
35 ## corresponding quantization values.
39 function [qidx, q, d] = quantiz (x, table, codes)
40 if (nargin < 2 || nargin > 3)
41 usage("[qidx, q, d] = quantiz(x, table, codes)");
44 qidx = length(table) - lookup(flipud(table(:)), x(:));
45 if (nargin > 2 && nargout > 1)
49 table = [table(1) ; table(:) ];
50 d = sumsq (x(:) - q(:)) / length(x);