]> Creatis software - CreaPhase.git/blob - octave_packages/fixed-0.7.10/lookup_table.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / fixed-0.7.10 / lookup_table.m
1 ## Copyright (C) 2003  Motorola Inc
2 ## Copyright (C) 2003  David Bateman
3 ##
4 ## This program is free software; you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2 of the License, or
7 ## (at your option) any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {@var{y} =} lookup_table (@var{table}, @var{x})
19 ## @deftypefnx {Function File} {@var{y} =} lookup_table (@var{table}, @var{x}, @var{interp}, @var{extrap})
20 ## Using the lookup table created by @dfn{create_lookup_table}, find the value
21 ## @var{y} corresponding to @var{x}. With two arguments the lookup is done
22 ## to the nearest value below in the table less than the desired value. With
23 ## three arguments a simple linear interpolation is performed. With four
24 ## arguments an extrapolation is also performed. The exact values of arguments
25 ## three and four are irrelevant, as only there presence detremines whether
26 ## interpolation and/or extrapolation are used.
27 ## @end deftypefn
28
29 function y = lookup_table (table, x, interp, extrap);
30   if (nargin < 2)
31     error("lookup_table: needs two or more arguments");
32   else 
33     idx = lookup(table.x, x);
34     if (nargin == 2)
35       y = table.y (max(1,idx));
36     else
37       len = length(table.x);
38       before = find(idx == 0);
39       after = find(idx == len);
40       idx = max(1,min(idx, len-1));
41       xl = table.x(idx);
42       yl = table.y(idx);
43       xu = table.x(idx+1);
44       yu = table.y(idx+1);
45       ## Careful with the order of this interpolation to avoid underflow
46       ## in fixed point calculations.
47       y = ( yl + (yu - yl) ./ (xu - xl) .* (x - xl));
48       if (nargin < 4)
49         y(before) = table.y(1);
50         y(after) = table.y(len);
51       endif
52     endif
53   endif
54 endfunction