1 ## Copyright (C) 2003 Motorola Inc
2 ## Copyright (C) 2003 David Bateman
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.
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.
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/>.
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.
29 function y = lookup_table (table, x, interp, extrap);
31 error("lookup_table: needs two or more arguments");
33 idx = lookup(table.x, x);
35 y = table.y (max(1,idx));
37 len = length(table.x);
38 before = find(idx == 0);
39 after = find(idx == len);
40 idx = max(1,min(idx, len-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));
49 y(before) = table.y(1);
50 y(after) = table.y(len);