My Project
weight.c
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright (c) 2005,2008 Turku PET Centre
4 
5  File: weight.c
6  Description: Functions for setting weight factors based on SIF.
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  See the GNU Lesser General Public License for more details:
17  http://www.gnu.org/copyleft/lesser.html
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with this library/program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  Turku PET Centre, Turku, Finland, http://www.turkupetcentre.fi
24 
25  Modification history:
26  2005-01-15 Vesa Oikonen
27  First created. Functions from sif.c.
28  2005-01-16 VO
29  Weights are scaled so that average is 1.0.
30  2005-04-26 CL
31  Merged libsif to libtpcimgio.
32  2008-07-11 VO
33  Added sifModerate().
34 
35 ******************************************************************************/
36 
37 /*****************************************************************************/
38 #include "sif.h"
39 /*****************************************************************************/
40 
41 /*****************************************************************************/
61 void sifWeight(SIF *data, double halflife) {
62  int i;
63  double f, d;
64 
65  if(SIF_TEST) printf("sifWeight(*sif, %g)\n", halflife);
66  /* Calculate weights */
67  for(i=0; i<data->frameNr; i++) {
68  if(data->trues[i]<1.0) data->trues[i]=1.0;
69  f=data->x2[i]-data->x1[i]; if(f<=0.0) f=1.0;
70  if(halflife<=1.0E-8)
71  d=1.0;
72  else
73  d=exp( ((data->x1[i]+data->x2[i])/2.0)*0.693147/halflife );
74  data->weights[i]=(f*f)/(d*data->trues[i]);
75  /*printf("%3d %g %g\n", i, data->trues[i], data->weights[i]);*/
76  }
77 
78 #if(0)
79  /* Scale weights between [0,1] */
80  f=data->weights[0];
81  for(i=1; i<data->frameNr; i++) if(data->weights[i]>f) f=data->weights[i];
82  for(i=0; i<data->frameNr; i++) data->weights[i]/=f;
83 #else
84  /* Scale weights so that average weight is 1.0 */
85  for(i=0, f=0.0; i<data->frameNr; i++) f+=data->weights[i];
86  f/=(double)data->frameNr;
87  for(i=0; i<data->frameNr; i++) data->weights[i]/=f;
88 #endif
89 
90  return;
91 }
92 /*****************************************************************************/
93 
94 /*****************************************************************************/
104  SIF *sif,
106  double limit
107 ) {
108  int fi;
109  double w, f;
110 
111  if(sif==NULL || sif->frameNr<2) return;
112  if(limit<=1.0) return;
113  for(w=f=sif->trues[0], fi=1; fi<sif->frameNr; fi++) {
114  if(sif->trues[fi]>w) w=sif->trues[fi];
115  else if(sif->trues[fi]<f) f=sif->trues[fi];
116  }
117  if(f*limit<w) {
118  for(w/=limit, fi=0; fi<sif->frameNr; fi++)
119  if(sif->trues[fi]>0.0) sif->trues[fi]+=w; else sif->trues[fi]=w;
120  } else {
121  for(fi=0; fi<sif->frameNr; fi++)
122  if(sif->trues[fi]<0.0) sif->trues[fi]=0.0;
123  }
124 }
125 /*****************************************************************************/
126 
127 /*****************************************************************************/
128 
int SIF_TEST
Definition: sif.h:63
Definition: sif.h:36
double * x1
Definition: sif.h:50
int frameNr
Definition: sif.h:40
double * x2
Definition: sif.h:52
double * weights
Definition: sif.h:60
double * trues
Definition: sif.h:58
void sifModerate(SIF *sif, double limit)
Definition: weight.c:102
void sifWeight(SIF *data, double halflife)
Definition: weight.c:61