TPCCLIB
Loading...
Searching...
No Matches
noise.c File Reference

Noise simulation for PET modelling. More...

#include "libtpcmodext.h"

Go to the source code of this file.

Functions

int noiseSD4Simulation (double y, double t1, double dt, double hl, double a, double *sd, char *status, int verbose)
 
int noiseSD4SimulationFromDFT (DFT *dft, int index, double pc, double *sd, char *status, int verbose)
 

Detailed Description

Noise simulation for PET modelling.

Author
Vesa Oikonen

Definition in file noise.c.

Function Documentation

◆ noiseSD4Simulation()

int noiseSD4Simulation ( double y,
double t1,
double dt,
double hl,
double a,
double * sd,
char * status,
int verbose )

Calculate SD for PET radioactivity concentration data to be used to simulate noise.

Note that SD is dependent on the time units.

Reference: Varga & Szabo. J Cereb Blood Flow Metab 2002;22(2):240-244.

Returns
Returns 0 when successful, otherwise <>0.
Parameters
ySample radioactivity concentration (decay corrected to zero time)
t1Radioactivity measurement (frame) start time (in same units as the halflife)
dtRadioactivity measurement (frame) duration (in same units as the halflife)
hlIsotope halflife (in same units as the sample time); enter 0 if not to be considered
aProportionality factor. Note that it is inside square root.
sdPointer in which SD is written
statusPointer to a string (allocated for at least 64 chars) where error message or other execution status will be written; enter NULL, if not needed
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 21 of file noise.c.

42 {
43 double d, lambda, f;
44
45 if(verbose>0) printf("noiseSD4Simulation(%g, %g, %g, %g, %g, *sd, ...)\n",
46 y, t1, dt, hl, a);
47 if(status!=NULL) strcpy(status, "invalid data");
48 if(sd==NULL) return 1;
49 if(t1<0.0) return 2;
50 if(dt<=0.0) return 3;
51
52 /* Decay factor (<=1) */
53 if(hl<=0.0) {
54 d=1.0; lambda=0.0;
55 } else {
56 if(status!=NULL) strcpy(status, "invalid half-life");
57 lambda=hl2lambda(hl); if(lambda<0.0) return 4;
58 d=hlLambda2factor(-lambda, t1, dt); if(d<0.0) return 5;
59 }
60 /* SD */
61 f=y*d*dt;
62 if(f<=0.0) *sd=0.0;
63 else *sd=y*sqrt(a/f);
64
65 if(status!=NULL) strcpy(status, "ok");
66 return(0);
67}
double hl2lambda(double halflife)
Definition halflife.c:84
double hlLambda2factor(double lambda, double frametime, double framedur)
Definition halflife.c:98

Referenced by noiseSD4SimulationFromDFT().

◆ noiseSD4SimulationFromDFT()

int noiseSD4SimulationFromDFT ( DFT * dft,
int index,
double pc,
double * sd,
char * status,
int verbose )

Calculate SD for noise simulation from TAC data.

Sample times will be converted to minutes if necessary.

Reference: Varga & Szabo. J Cereb Blood Flow Metab 2002;22(2):240-244.

Returns
Returns 0 when successful, otherwise <>0.
Parameters
dftPointer to TAC data in DFT struct, based on which the SD for each frame is calculated. Contents are not changed. Struct must contain correct values for the isotope, time unit, and frame times (start and end). Status of decay correction is used, and if not known, then data is assumed to be decay corrected.
indexTAC index [0..voiNr-1] which is used to calculate the SD in case DFT contains more than one TAC. Non-effective if DFT contains only one TAC. Set to <0 if mean of all TACs is to be used.
pcProportionality factor
sdPointer to allocated array in which SD is written
statusPointer to a string (allocated for at least 64 chars) where error message or other execution status will be written; enter NULL, if not needed
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 79 of file noise.c.

99 {
100 int ret, fi;
101 double hl=0.0, t1, deltat;
102 double *y;
103 DFT mean;
104
105 if(verbose>0) printf("noiseSD4SimulationFromDFT(DFT, %d, %g, sd[], ...)\n",
106 index, pc);
107 if(status!=NULL) strcpy(status, "invalid data");
108 if(dft==NULL || sd==NULL) return 1;
109 if(dft->voiNr<1 || dft->frameNr<1) return 2;
110 if(dft->voiNr>1 && index>=dft->voiNr) return 3;
111
112 /* Check that valid frame times are available */
113 if(status!=NULL) strcpy(status, "invalid frame times");
114 for(fi=0; fi<dft->frameNr; fi++)
115 if(dft->x2[fi]<=dft->x1[fi] || dft->x1[fi]<0.0) return 4;
116 if(status!=NULL) strcpy(status, "missing time unit");
117 if(dft->timeunit!=TUNIT_SEC && dft->timeunit!=TUNIT_MIN) return 5;
118
119 /* Check if isotope is available, if it is needed */
122 {
123 hl=hlFromIsotope(dft->isotope);
124 if(status!=NULL) strcpy(status, "missing isotope halflife");
125 if(hl<=0.0) return 6;
126 }
127 if(verbose>1) printf("halflife := %g\n", hl);
128
129 /* Set pointer to activity concentration data */
130 dftInit(&mean);
131 if(dft->voiNr==1) {
132 y=dft->voi[0].y;
133 } else if(index>=0) {
134 y=dft->voi[index].y;
135 } else {
136 /* Compute TAC mean */
137 ret=dftMeanTAC(dft, &mean); if(ret!=0) {
138 if(status!=NULL) strcpy(status, "cannot calculate mean TAC");
139 return 8;
140 }
141 /* and use that */
142 y=mean.voi[0].y;
143 }
144
145 /* Compute SD for each sample time */
146 if(pc<=0.0) pc=1.0;
147 for(fi=0; fi<dft->frameNr; fi++) {
148 t1=dft->x1[fi]; deltat=dft->x2[fi]-dft->x1[fi];
149 if(dft->timeunit==TUNIT_SEC) {t1/=60.0; deltat/=60.0;}
150 ret=noiseSD4Simulation(y[fi], t1, deltat, hl, pc, sd+fi, status, verbose);
151 if(ret!=0) break;
152 }
153 dftEmpty(&mean);
154 if(ret!=0) {
155 if(status!=NULL)
156 sprintf(status, "cannot calculate SD for noise simulation (%d)", ret);
157 return(100+ret);
158 }
159
160 if(status!=NULL) strcpy(status, "ok");
161 return 0;
162}
void dftInit(DFT *data)
Definition dft.c:38
int dftMeanTAC(DFT *dft, DFT *mean)
Definition dft.c:1580
void dftEmpty(DFT *data)
Definition dft.c:20
double hlFromIsotope(char *isocode)
Definition halflife.c:55
#define DFT_DECAY_UNKNOWN
#define DFT_DECAY_CORRECTED
int mean(double *x, double *y, int nr, double *xmean, double *xsd, double *ymean, double *ysd)
Definition pearson.c:341
int noiseSD4Simulation(double y, double t1, double dt, double hl, double a, double *sd, char *status, int verbose)
Definition noise.c:21
char decayCorrected
Voi * voi
int timeunit
double * x1
int voiNr
double * x2
int frameNr
char isotope[8]
double * y