weight.c
Go to the documentation of this file.
00001 /******************************************************************************
00002 
00003   Copyright (c) 2005,2008 Turku PET Centre
00004 
00005   File:        weight.c
00006   Description: Functions for setting weight factors based on SIF.
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016   See the GNU Lesser General Public License for more details:
00017   http://www.gnu.org/copyleft/lesser.html
00018 
00019   You should have received a copy of the GNU Lesser General Public License
00020   along with this library/program; if not, write to the Free Software
00021   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
00022 
00023   Turku PET Centre, Turku, Finland, http://www.turkupetcentre.fi
00024 
00025   Modification history:
00026   2005-01-15 Vesa Oikonen
00027     First created. Functions from sif.c.
00028   2005-01-16 VO
00029     Weights are scaled so that average is 1.0.
00030   2005-04-26 CL
00031     Merged libsif to libtpcimgio.
00032   2008-07-11 VO
00033     Added sifModerate().
00034 
00035 ******************************************************************************/
00036 
00037 /*****************************************************************************/
00038 #include "sif.h"
00039 /*****************************************************************************/
00040 
00041 /*****************************************************************************/
00061 void sifWeight(SIF *data, double halflife) {
00062   int i;
00063   double f, d;
00064 
00065   if(SIF_TEST) printf("sifWeight(*sif, %g)\n", halflife);
00066   /* Calculate weights */
00067   for(i=0; i<data->frameNr; i++) {
00068     if(data->trues[i]<1.0) data->trues[i]=1.0;
00069     f=data->x2[i]-data->x1[i]; if(f<=0.0) f=1.0;
00070     if(halflife<=1.0E-8) 
00071       d=1.0;
00072     else
00073       d=exp( ((data->x1[i]+data->x2[i])/2.0)*0.693147/halflife );
00074     data->weights[i]=(f*f)/(d*data->trues[i]);
00075     /*printf("%3d %g %g\n", i, data->trues[i], data->weights[i]);*/
00076   }
00077 
00078 #if(0)
00079   /* Scale weights between [0,1] */
00080   f=data->weights[0];
00081   for(i=1; i<data->frameNr; i++) if(data->weights[i]>f) f=data->weights[i];
00082   for(i=0; i<data->frameNr; i++) data->weights[i]/=f;
00083 #else
00084   /* Scale weights so that average weight is 1.0 */
00085   for(i=0, f=0.0; i<data->frameNr; i++) f+=data->weights[i];
00086   f/=(double)data->frameNr;
00087   for(i=0; i<data->frameNr; i++) data->weights[i]/=f;
00088 #endif
00089 
00090   return;
00091 }
00092 /*****************************************************************************/
00093 
00094 /*****************************************************************************/
00102 void sifModerate(
00104   SIF *sif,
00106   double limit
00107 ) {
00108   int fi;
00109   double w, f;
00110 
00111   if(sif==NULL || sif->frameNr<2) return;
00112   if(limit<=1.0) return;
00113   for(w=f=sif->trues[0], fi=1; fi<sif->frameNr; fi++) {
00114     if(sif->trues[fi]>w) w=sif->trues[fi];
00115     else if(sif->trues[fi]<f) f=sif->trues[fi];
00116   }
00117   if(f*limit<w) {
00118     for(w/=limit, fi=0; fi<sif->frameNr; fi++)
00119       if(sif->trues[fi]>0.0) sif->trues[fi]+=w; else sif->trues[fi]=w;
00120   } else {
00121     for(fi=0; fi<sif->frameNr; fi++)
00122       if(sif->trues[fi]<0.0) sif->trues[fi]=0.0;
00123   }
00124 }
00125 /*****************************************************************************/
00126 
00127 /*****************************************************************************/
00128