-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinearInter1D.m
55 lines (43 loc) · 1.13 KB
/
LinearInter1D.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function Vx = LinearInter1D(xBP,V,x)
% Perform linear interpolation with clipping for out-of-bound data.
%
% Input arguments:
% xBP: Break points on x-axis, monotonically increasing, size m x 1
% V: Function values at break points, size m x 1
% x: Interpolation points on x-axis, size q x 1
%
% Output arguments:
% Vx: Interpolated points at x, size q x 1
%
% W. D. Widanage 04/06/2013 (Riffs!)
% Vectorize
xBP = xBP(:);
% Get dimensions
m = length(xBP);
q = length(x);
% Initialise
Vx = zeros(q,1);
for qq = 1:q
% Find indicies of nearest neighbours in x
diffx = x(qq) - xBP;
if (diffx < 0)
ix0 = 1;
ix1 = 1;
elseif (diffx > 0)
ix0 = m;
ix1 = m;
else
diffx_0 = diffx;
diffx_0(diffx < 0) = inf;
[~,ix0] = min(diffx_0);
diffx_1 = diffx;
diffx_1(diffx > 0) = -inf;
[~,ix1] = max(diffx_1);
end
x0 = xBP(ix0);
x1 = xBP(ix1);
if x0 == x1, xd = x0; else xd = (x(qq)-x0)/(x1-x0); end
% Interplolate along x
Vx(qq) = V(ix0)*(1-xd) + V(ix1)*xd;
end
end