Newer
Older
/* cspline
Cubic spline fitting and interpolation
Tim Behrens, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* CCOPYRIGHT */
#include <string>
#include <iostream>
#include <fstream>
#include "newmatap.h"
#include "newmatio.h"
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
#include "cspline.h"
#define WANT_STREAM
#define WANT_MATH
using namespace NEWMAT;
using namespace std;
///////////////////////////////////////////////////////
namespace MISCMATHS{
// void Cspline::Cspline(){}
void Cspline::set(ColumnVector& pnodes,ColumnVector& pvals){
nodes=pnodes;vals=pvals;
fitted=false;
n=vals.Nrows();
}
void Cspline::set(ColumnVector& pnodes, Matrix& pcoefs){
nodes=pnodes;coefs=pcoefs;
fitted=false;
n=vals.Nrows();
}
void Cspline::diff(const ColumnVector& x, ColumnVector& dx ){
// dx should be of length length(x)-1
dx.ReSize(x.Nrows()-1);
for(int i=2;i<=x.Nrows();i++){
dx(i-1)=x(i)-x(i-1);
}
}
void Cspline::fit(){
if(vals.Nrows()<4){
cerr<<"Cspline::fit - You have less than 4 data pts for spline fitting."<<endl;
exit(-1);
}
if(nodes.Nrows()!=vals.Nrows()){
cerr<<"Nodes and VALS must be the same length in your spline"<<endl;
exit(-1);
}
int n=vals.Nrows();
ColumnVector s(n);
ColumnVector dx,dy,dydx(n-1);
diff(nodes,dx);
diff(vals,dy);
for(int i=1;i<=n-1;i++){
dydx(i)=dy(i)/dx(i);
}
ColumnVector b(n);
b=0;
Mark Jenkinson
committed
for(int i=2;i<=b.Nrows()-1;i++){
b(i)=3*(dx(i)*dydx(i-1)+dx(i-1)*dydx(i));
}
float x31=nodes(3)-nodes(1),xn=nodes(n)-nodes(n-2);
b(1)=((dx(1)+2*x31)*dx(2)*dydx(1)+dx(1)*dx(1)*dydx(2))/x31;
b(n)=(dx(n-1)*dx(n-1)*dydx(n-2)+(2*xn+dx(n-1))*dx(n-2)*dydx(n-1))/xn;
Matrix tridiag(n,n);
tridiag=0;
ColumnVector y3(n);
for(int j=2;j<=n-1;j++){
tridiag(j,j-1)=dx(j);
tridiag(j,j)=2*(dx(j)+dx(j-1));
tridiag(j,j+1)=dx(j-1);
}
tridiag(1,1)=dx(2);tridiag(1,2)=x31;
tridiag(n,n-1)=xn;tridiag(n,n)=dx(n-2);
s=tridiag.i()*b;
ColumnVector d(n-1),c(n-1);
Mark Jenkinson
committed
for(int j=1;j<n;j++){
d(j)=(s(j)+s(j+1)-2*dydx(j))/dx(j);
c(j)=(dydx(j)-s(j))/dx(j)-d(j);
Mark Jenkinson
committed
for(int j=1;j<n;j++){
coefs(j,1)=vals(j);
coefs(j,2)=s(j);
coefs(j,3)=c(j);
coefs(j,4)=d(j)/dx(j);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
float Cspline::interpolate(float xx) const{
// nodes must be monotonically increasing. I don't check this.
// On your head be it if you don't.
if(nodes.Nrows()!=vals.Nrows()){
cerr<<"Cspline:interpolate: Nodes and Vals should be the same length"<<endl;
exit(-1);
}
float ret;
if(!fitted){
cerr<<"Cspline::interpolate - Cspline has not been fitted"<<endl;
exit(-1);
}
else{
bool stop=false;
int ind=0;
if(xx<nodes(1)){
ind=1;
}
else if(xx>nodes(nodes.Nrows())){
ind=nodes.Nrows()-1;
}
else{
for(int i=1;i<nodes.Nrows();i++){
if(!stop){
if( (nodes(i)<=xx) && (nodes(i+1)>xx) ){
ind=i;
stop=true;
}
}
}
}
float a=coefs(ind,1);
float b=coefs(ind,2);
float c=coefs(ind,3);
float d=coefs(ind,4);
float t=xx-nodes(ind);
ret=a+b*t+c*t*t+d*t*t*t;
}
return ret;
}
float Cspline::interpolate(float xx, int ind) const{
float ret;
if(!fitted){
cerr<<"Cspline::interpolate - Cspline has not been fitted"<<endl;
exit(-1);
}
else{
if(ind>n-1){
cerr<<"Cspline::interpolate - segment index is greater than number of segments - exiting"<<endl;
exit(-1);
}
else if(ind<1){
cerr<<"Cspline::interpolate - segment index is less than 1 - exiting"<<endl;
exit(-1);
}
float a=coefs(ind,1);
float b=coefs(ind,2);
float c=coefs(ind,3);
float d=coefs(ind,4);
float t=xx-nodes(ind);
ret=a+b*t+c*t*t+d*t*t*t;
}
return ret;
}
ColumnVector Cspline::interpolate(const ColumnVector& x) const{
// nodes must be monotonically increasing. I don't check this.
// On your head be it if you don't.
if(nodes.Nrows()!=vals.Nrows()){
cerr<<"Cspline::interpolate - Nodes and Vals should be the same length"<<endl;
exit(-1);
}
ColumnVector ret(x.Nrows());
if(!fitted){
cerr<<"Cspline::interpolate - Cspline has not been fitted"<<endl;
exit(-1);
}
else{
for(int xnum=1;xnum<=x.Nrows();xnum++){
float xx=x(xnum);
bool stop=false;
int ind=0;
if(xx<nodes(1)){
ind=1;
}
else if(xx>=nodes(nodes.Nrows())){
ind=nodes.Nrows()-1;
}
else{
for(int i=1;i<nodes.Nrows();i++){
if(!stop){
if( (nodes(i)<=xx) && (nodes(i+1)>xx) ){
ind=i;
stop=true;
}
}
}
}
float a=coefs(ind,1);
float b=coefs(ind,2);
float c=coefs(ind,3);
float d=coefs(ind,4);
float t=xx-nodes(ind);
ret(xnum)=a+b*t+c*t*t+d*t*t*t;
}
}
return ret;
}
ColumnVector Cspline::interpolate(const ColumnVector& x,const ColumnVector& indvec) const{
// nodes must be monotonically increasing. I don't check this.
// On your head be it if you don't.
if(nodes.Nrows()!=vals.Nrows()){
cerr<<"Cspline::interpolate - Nodes and Vals should be the same length"<<endl;
exit(-1);
}
ColumnVector ret(x.Nrows());
if(!fitted){
cerr<<"Cspline::interpolate - Cspline has not been fitted"<<endl;
exit(-1);
}
else{
for(int xnum=1;xnum<=x.Nrows();xnum++){
float xx=x(xnum);
int ind=int(indvec(xnum));
float a=coefs(ind,1);
float b=coefs(ind,2);
float c=coefs(ind,3);
float d=coefs(ind,4);
float t=xx-nodes(ind);
ret(xnum)=a+b*t+c*t*t+d*t*t*t;
}
}
return ret;
}
}