TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcmodext
weight_model.c
Go to the documentation of this file.
1
5
/*****************************************************************************/
6
7
/*****************************************************************************/
8
#include "
libtpcmodext.h
"
9
/*****************************************************************************/
10
11
/*****************************************************************************/
20
int
dftWeightByFreq
(
23
DFT
*dft
24
) {
25
int
fi, fi1, fi2;
26
double
f, t, t1, t2, sumw=0.0;
27
28
if
(dft==NULL || dft->
frameNr
<1)
return
1;
29
if
(dft->
frameNr
==1) {
30
dft->
w
[0]=1.0;
31
dft->
isweight
=1;
32
return
0;
33
}
34
35
if
(dft->
timetype
==
DFT_TIME_STARTEND
) {
/* weights by frame lengths */
36
37
for
(fi=0; fi<dft->
frameNr
; fi++) {
38
dft->
w
[fi]=dft->
x2
[fi]-dft->
x1
[fi];
39
sumw+=dft->
w
[fi];
40
}
41
/* scale weights so that sum of weights equals sample number */
42
sumw/=(double)dft->
frameNr
;
43
for
(fi=0; fi<dft->
frameNr
; fi++) dft->
w
[fi]/=sumw;
44
45
}
else
if
(dft->
timetype
==
DFT_TIME_MIDDLE
) {
/* weights by sample distance */
46
47
for
(fi=0; fi<dft->
frameNr
; fi++) {
48
t=t1=t2=dft->
x
[fi];
//printf("fi=%d t=%g\n", fi, t);
49
/* Find the closest sample time before this one */
50
for
(fi1=fi; fi1>=0; fi1--) {t1=dft->
x
[fi1];
if
(t1<t)
break
;}
51
/* Find the closest sample time after this one */
52
for
(fi2=fi; fi2<dft->
frameNr
; fi2++) {t2=dft->
x
[fi2];
if
(t2>t)
break
;}
53
//printf(" t1=%g t2=%g\n", t1, t2);
54
/* Mean sample distance */
55
f=0.0;
56
if
(t1<t) f+=t-t1;
else
f+=t2-t;
57
if
(t2>t) f+=t2-t;
else
f+=t-t1;
58
f*=0.5;
if
(f<=0.0) f=1.0;
59
/* Set initial weight */
60
//printf(" f=%g\n", f);
61
dft->
w
[fi]=f; sumw+=f;
62
}
63
/* Scale weights */
64
sumw/=(double)dft->
frameNr
;
65
for
(fi=0; fi<dft->
frameNr
; fi++) dft->
w
[fi]/=sumw;
66
67
}
else
68
return
2;
69
70
dft->
isweight
=1;
71
return
(0);
72
}
73
/*****************************************************************************/
74
75
/*****************************************************************************/
85
int
imgSetWeights
(
88
IMG
*img,
91
int
wmet,
93
int
verbose
94
) {
95
if
(verbose>0) printf(
"imgSetWeights(*img, %d, ...)\n"
, wmet);
96
/* Check the function input */
97
if
(img==NULL)
return
(1);
98
if
(img->
status
!=
IMG_STATUS_OCCUPIED
)
return
(2);
99
if
(img->
dimt
<1 || img->
dimx
<1 || img->
dimy
<1 || img->
dimz
<1)
return
(3);
100
if
(wmet<0 || wmet>2)
return
(4);
101
img->
isWeight
=0;
102
103
/* If one time frame, then weight is 1 */
104
if
(img->
dimt
==1) {
105
img->
weight
[0]=1.0;
106
if
(wmet==0 || wmet==1) img->
isWeight
=1;
107
return
(0);
108
}
109
110
int
fi, ret;
111
double
f, fm;
112
113
if
(wmet==2) {
/* If weights are to be set to 1 (removed) */
114
for
(fi=0; fi<img->
dimt
; fi++) img->
weight
[fi]=1.0;
115
return
(0);
116
}
117
118
if
(wmet==0) {
/* weight based on frame mean values and durations */
119
SIF
sif;
120
sifInit
(&sif);
121
ret=
sifAllocateWithIMG
(&sif, img, 1, verbose);
if
(ret)
return
(100+ret);
122
sifModerateTrues
(&sif, 100.0);
123
sifWeight
(&sif, 0.0);
if
(verbose>2)
sifPrint
(&sif);
124
for
(fi=0; fi<img->
dimt
; fi++) img->
weight
[fi]=sif.
weights
[fi];
125
sifEmpty
(&sif);
126
}
else
if
(wmet==1) {
/* weight based on frame durations */
127
for
(fi=0, fm=0.0; fi<img->
dimt
; fi++) {
128
f=img->
end
[fi]-img->
start
[fi]; fm+=f;
129
img->
weight
[fi]=f;
130
}
131
/* scale weights so that sum of weights equals sample number */
132
if
(fm>0.0) f=(double)img->
dimt
/fm;
else
f=1.0;
133
for
(fi=0; fi<img->
dimt
; fi++) img->
weight
[fi]*=f;
134
}
135
img->
isWeight
=1;
136
return
(0);
137
}
138
/*****************************************************************************/
139
140
/*****************************************************************************/
147
int
dftWSampleNr
(
149
DFT
*tac
150
) {
151
if
(tac==NULL || tac->
voiNr
<1 || tac->
frameNr
<1)
return
(0);
152
if
(tac->
isweight
==0)
return
(tac->
frameNr
);
153
int
n=0;
154
for
(
int
i=0; i<tac->
frameNr
; i++)
if
(tac->
w
[i]>0.0) n++;
155
return
(n);
156
}
157
/*****************************************************************************/
158
159
/*****************************************************************************/
DFT_TIME_MIDDLE
#define DFT_TIME_MIDDLE
Definition
libtpccurveio.h:433
DFT_TIME_STARTEND
#define DFT_TIME_STARTEND
Definition
libtpccurveio.h:439
sifModerateTrues
void sifModerateTrues(SIF *sif, double limit)
Definition
weight.c:131
IMG_STATUS_OCCUPIED
#define IMG_STATUS_OCCUPIED
Definition
libtpcimgio.h:1479
sifInit
void sifInit(SIF *data)
Definition
sif.c:17
sifPrint
void sifPrint(SIF *data)
Definition
sifio.c:234
sifWeight
void sifWeight(SIF *data, double halflife)
Calculate weights for frames in SIF data based on true counts. Weights are normalized to have an aver...
Definition
weight.c:28
sifEmpty
void sifEmpty(SIF *data)
Definition
sif.c:33
libtpcmodext.h
Header file for libtpcmodext.
sifAllocateWithIMG
int sifAllocateWithIMG(SIF *sif, IMG *img, int doCounts, int verbose)
Definition
misc_model.c:418
DFT
Definition
libtpccurveio.h:73
DFT::timetype
int timetype
Definition
libtpccurveio.h:99
DFT::w
double * w
Definition
libtpccurveio.h:110
DFT::x1
double * x1
Definition
libtpccurveio.h:104
DFT::voiNr
int voiNr
Definition
libtpccurveio.h:78
DFT::x2
double * x2
Definition
libtpccurveio.h:106
DFT::frameNr
int frameNr
Definition
libtpccurveio.h:76
DFT::isweight
int isweight
Definition
libtpccurveio.h:112
DFT::x
double * x
Definition
libtpccurveio.h:102
IMG
Definition
libtpcimgio.h:1657
IMG::dimx
unsigned short int dimx
Definition
libtpcimgio.h:1780
IMG::status
char status
Definition
libtpcimgio.h:1665
IMG::dimt
unsigned short int dimt
Definition
libtpcimgio.h:1778
IMG::weight
float * weight
Definition
libtpcimgio.h:1827
IMG::start
float * start
Definition
libtpcimgio.h:1815
IMG::dimz
unsigned short int dimz
Definition
libtpcimgio.h:1784
IMG::dimy
unsigned short int dimy
Definition
libtpcimgio.h:1782
IMG::end
float * end
Definition
libtpcimgio.h:1817
IMG::isWeight
char isWeight
Definition
libtpcimgio.h:1825
SIF
Definition
libtpcimgio.h:1443
SIF::weights
double * weights
Definition
libtpcimgio.h:1467
dftWSampleNr
int dftWSampleNr(DFT *tac)
Definition
weight_model.c:147
dftWeightByFreq
int dftWeightByFreq(DFT *dft)
Definition
weight_model.c:20
imgSetWeights
int imgSetWeights(IMG *img, int wmet, int verbose)
Definition
weight_model.c:85
Generated on
for TPCCLIB by
1.17.0