67 if(initStep<=0.0 || endTime<=0.0 || endTime<initStep)
return(n);
68 if(maxStep<initStep) maxStep=endTime;
70 double x1=0.0, s=initStep;
80 for(
int i=0; i<10 && x1<endTime; i++, n++) {
84 s*=2.0;
if(s>maxStep) s=maxStep;
85 for(
int i=0; i<5 && x1<endTime; i++, n++) {
89 s*=2.5;
if(s>maxStep) s=maxStep;
90 for(
int i=0; i<2 && x1<endTime; i++, n++) {
94 s*=2.0;
if(s>maxStep) s=maxStep;
100 for(
int i=0; i<20 && x1<endTime; i++, n++) {
104 s*=2.0;
if(s>maxStep) s=maxStep;
105 for(
int i=0; i<10 && x1<endTime; i++, n++) {
109 s*=2.5;
if(s>maxStep) s=maxStep;
110 for(
int i=0; i<5 && x1<endTime; i++, n++) {
114 s*=2.0;
if(s>maxStep) s=maxStep;
180 if(verbose>0) printf(
"%s(x, y, %d, nx, ny, ni, ni2, %d, %d, %d)\n", __func__, nr, newnr, se, ee);
182 if(nr<1 || newnr<1)
return 1;
183 if(x==NULL || y==NULL || newx==NULL)
return 1;
186 double ty, tyi, tyii;
187 double ox1, ox2, oy1, oy2, oi1, oi2, oii1, oii2, dt, ndt;
191 if(x[0]<0.0) {ox1=ox2=x[0]; oy1=oy2=y[0];}
192 else {ox1=0.0; ox2=x[0]; oy1=0.0; oy2=0.0;}
194 if(x[0]<0.0) {ox1=ox2=x[0]; oy1=oy2=y[0];}
195 else {ox1=0.0; ox2=x[0]; oy1=oy2=y[0];}
201 if(ox1>0.0 && se>=2) {
205 }
else if(se==3 && y[0]>0.0) {
209 }
else if(se==4 && y[0]>0.0) {
221 oi2=oi1 + (ox2-ox1)*(oy1+oy2)/2.0;
222 oii2=oii1 + (ox2-ox1)*(oi1+oi2)/2.0;
224 printf(
"ox1=%g oy1=%g oi1=%g oii1=%g\n", ox1, oy1, oi1, oii1);
225 printf(
"ox2=%g oy2=%g oi2=%g oii2=%g\n", ox2, oy2, oi2, oii2);
229 if(verbose>2) printf(
"before input start\n");
230 j=0;
while(j<newnr && newx[j]<ox1) {
231 ty=tyi=tyii=0.0;
if(verbose>2) printf(
" ndt=%g\n", ox1-newx[j]);
232 if(verbose>2) printf(
" j=%d newx=%g ty=%g tyi=%g tyii=%g\n", j, newx[j], ty, tyi, tyii);
233 if(newy!=NULL) newy[j]=ty;
234 if(newyi!=NULL) newyi[j]=tyi;
235 if(newyii!=NULL) newyii[j]=tyii;
240 if(verbose>2) printf(
"before input start #2\n");
242 dt=ox2-ox1;
if(dt>0.0)
while(j<newnr && newx[j]<ox2) {
243 ndt=newx[j]-ox1;
if(verbose>2) printf(
" ndt=%g\n", ndt);
244 ty=((oy2-oy1)/dt)*ndt + oy1;
if(newy!=NULL) newy[j]=ty;
245 tyi=oi1 + 0.5*(ty+oy1)*ndt;
if(newyi!=NULL) newyi[j]=tyi;
246 if(newyii!=NULL) newyii[j]=tyii= oii1 + 0.5*(tyi+oi1)*ndt;
247 if(verbose>2) printf(
" j=%d newx=%g ty=%g tyi=%g tyii=%g\n", j, newx[j], ty, tyi, tyii);
252 if(verbose>2) printf(
"inside input range\n");
253 for(i=0; i<nr && j<newnr; i++) {
255 ox1=ox2; oy1=oy2; oi1=oi2; oii1=oii2;
257 oi2=oi1 + (ox2-ox1)*(oy1+oy2)/2.0;
258 oii2=oii1 + (ox2-ox1)*(oi1+oi2)/2.0;
260 printf(
"ox1=%g oy1=%g oi1=%g oii1=%g\n", ox1, oy1, oi1, oii1);
261 printf(
"ox2=%g oy2=%g oi2=%g oii2=%g\n", ox2, oy2, oi2, oii2);
265 dt=ox2-ox1;
if(dt<0.0)
return 3;
else if(dt==0.0)
continue;
268 while(j<newnr && newx[j]<=ox2) {
270 ty=((oy2-oy1)/dt)*ndt + oy1;
if(newy!=NULL) newy[j]=ty;
271 tyi=oi1 + 0.5*(ty+oy1)*ndt;
if(newyi!=NULL) newyi[j]=tyi;
272 if(newyii!=NULL) newyii[j]=tyii= oii1 + 0.5*(tyi+oi1)*ndt;
274 printf(
" j=%d newx=%g ndt=%g ty=%g tyi=%g tyii=%g\n", j, newx[j], ndt, ty, tyi, tyii);
281 if(verbose>2) printf(
"after input range\n");
284 if(ee==0) ty=0.0;
else ty=oy2;
285 if(ee==0) tyi=oi2;
else tyi=oi2 + oy2*ndt;
286 tyii=oii2 + 0.5*(tyi+oi2)*ndt;
287 if(newy!=NULL) newy[j]=ty;
288 if(newyi!=NULL) newyi[j]=tyi;
289 if(newyii!=NULL) newyii[j]=tyii;
290 if(verbose>4) printf(
" j=%d newx=%g ty=%g tyi=%g tyii=%g\n", j, newx[j], ty, tyi, tyii);
294 if(verbose>3) printf(
"%s() ready\n", __func__);
363 printf(
"%s(x, y, %d, nx1, nx2, ny, ni, ni2, %d, %d, %d)\n", __func__, nr, newnr, se, ee);
366 if(nr<1 || newnr<1)
return 1;
367 if(x==NULL || y==NULL || newx1==NULL || newx2==NULL)
return 1;
369 if(newx2[newnr-1]<=x[0] || newx1[0]>=x[nr-1])
return 3;
372 if(newy==NULL && newyi==NULL && newyii==NULL)
return 0;
374 int ret, fi, zeroframe=0, overlap=0;
378 for(fi=0; fi<newnr; fi++) {
380 fdur=newx2[fi]-newx1[fi];
382 if(fdur<0.0)
return 4;
383 if(fdur==0.0) zeroframe++;
385 if(fi>0 && newx2[fi-1]>newx1[fi]) overlap++;
388 printf(
"overlap := %d\n", overlap);
389 printf(
"zeroframe := %d\n", zeroframe);
399 if(overlap>0 || zeroframe>0 || newnr<=3 || newy==NULL )
401 double petx[3], pety[3], petyi[3], petyii[3];
403 if(verbose>1) printf(
"frame-by-frame interpolation/integration\n");
404 for(fi=0; fi<newnr; fi++) {
406 petx[0]=newx1[fi]; petx[2]=newx2[fi]; petx[1]=0.5*(petx[0]+petx[2]);
408 fdur=petx[2]-petx[0];
410 if(fdur<0.0)
return 4;
413 ret=
liInterpolate(x, y, nr, petx, pety, petyi, petyii, 1, se, ee, verbose-1);
414 if(ret)
return 10+ret;
415 if(newy!=NULL) newy[fi]=petyi[0];
416 if(newyi!=NULL) newyi[fi]=petyi[0];
417 if(newyii!=NULL) newyii[fi]=petyii[0];
421 ret=
liInterpolate(x, y, nr, petx, NULL, petyi, petyii, 3, se, ee, verbose-1);
422 if(ret)
return 20+ret;
424 if(newyi!=NULL) newyi[fi]=petyi[1];
425 if(newyii!=NULL) newyii[fi]=petyii[1];
427 if(newy!=NULL) newy[fi]=(petyi[2]-petyi[0])/fdur;
432 if(verbose>1) printf(
"all-frames-at-once interpolation/integration\n");
434 double *tp=NULL; tp=malloc(newnr*
sizeof(
double));
if(tp==NULL)
return 7;
436 ret=
liInterpolate(x, y, nr, newx1, NULL, tp, NULL, newnr, se, ee, verbose-1);
437 if(ret) {free(tp);
return 10+ret;}
439 ret=
liInterpolate(x, y, nr, newx2, NULL, newy, NULL, newnr, se, ee, verbose-1);
440 if(ret) {free(tp);
return 10+ret;}
442 for(fi=0; fi<newnr; fi++)
443 newy[fi]=(newy[fi]-tp[fi])/(newx2[fi]-newx1[fi]);
445 if(newyi!=NULL || newyii!=NULL) {
446 for(fi=0; fi<newnr; fi++) tp[fi]=0.5*(newx2[fi]+newx1[fi]);
447 ret=
liInterpolate(x, y, nr, tp, NULL, newyi, newyii, newnr, se, ee, verbose-1);
448 if(ret) {free(tp);
return 10+ret;}
453 if(verbose>1) printf(
"%s() ready\n", __func__);
int liInterpolate(double *x, double *y, const int nr, double *newx, double *newy, double *newyi, double *newyii, const int newnr, const int se, const int ee, const int verbose)
Linear interpolation and/or integration with trapezoidal method.
int liInterpolateForPET(double *x, double *y, const int nr, double *newx1, double *newx2, double *newy, double *newyi, double *newyii, const int newnr, const int se, const int ee, const int verbose)
Linear TAC interpolation and/or integration to PET frames.