-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcspline.m
executable file
·94 lines (87 loc) · 3.17 KB
/
cspline.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function [xx,yy] = cspline(x,y,npts,varargin)
%
% [xx,yy] = cspline(x,y,npts)
% [xx,yy] = cspline(x,y,npts,stype,<type>)
% [xx,yy] = cspline(x,y,npts,pscale,<scale>)
% [xx,yy] = cspline(x,y,npts,<Property>,<Value>,...)
%
% Reconstruct curve of generic set of points and any shape, on a substrate
% of "npts" points. DO NOT EXTRAPOLATE.
%
% Input arguments:
% - x : x-data array of size 1-by-n
% - y : y-data array of same size of x or size n-by-k
% - npts : number of points to consider in the (x,y) domain.
% - varargin : Use 'option',<val> for optional input arguments (see ProduceCorrectVarargin).
% Accepted 'option' strings are:
% + stype : {'pchip'} | 'spline' Spline interpolator type.
% + xscale, yscale: {'linear'} or {'lin'} | 'log' Use linear or
% logarithmic spacing for the npts points to
% interpolate.
%
% see also PCHIP, SPLINE, PRODUCECORRECTVARARGIN.
%
% v1.1
% Revised and added help.
% Maurizio De Pitta', The University of Chicago, Chicago, April 28th, 2016.
%
% v1.0
% Maurizio De Pitta', Tel Aviv University, Tel Aviv, Israel, January 28th, 2012.
%
% https://sites.google.com/site/mauriziodepitta/home
%--------------------------------------------------------------------------
% Defaults
%--------------------------------------------------------------------------
opts.xscale = 'lin';
opts.yscale = 'lin';
opts.stype = 'pchip';
%--------------------------------------------------------------------------
% User-defined values
%--------------------------------------------------------------------------
if ~isempty(varargin)
varargin = ProduceCorrectVarargin(varargin);
for i = 1:length(varargin)/2
if isfield(opts,varargin{2*i-1})
opts.(genvarname(varargin{2*i-1})) = varargin{2*i};
end
end
end
%--------------------------------------------------------------------------
% Initialize auxiliary substrate
%--------------------------------------------------------------------------
% Auxiliary variable
t = 1:length(x);
% Define interpolant substrate (X-scale)
switch lower(opts.xscale)
case {'linear','lin'}
ttx = linspace(t(1),t(end),npts);
case 'log'
ttx = logspace(log10(t(1)),log10(t(end)),npts);
end
% Define interpolant substrate (Y-scale)
switch lower(opts.yscale)
case {'linear','lin'}
tty = linspace(t(1),t(end),npts);
case 'log'
tty = logspace(log10(t(1)),log10(t(end)),npts);
end
%--------------------------------------------------------------------------
% Effective spline interpolation
%--------------------------------------------------------------------------
switch opts.stype
case 'pchip'
% pchip length(x)==size(y,1) for correct interpolation, i.e. each trial is
% given in columns
xx = pchip(t,x,ttx);
yy = pchip(t,y',tty);
case 'spline'
% spline length(x)==size(y,2) for correct interpolation, i.e. each trial is
% given in rows
xx = spline(t,x,ttx);
% flip y data
yy = spline(t,y',tty)';
end
% Restore format
xx = xx';
yy = yy';