19 #define TYPE__DOUBLE 3
26 int go4fit_position(
int l_len,
double *pa_data,
double *pr_pos,
double *pr_sig,
double *pr_sum);
51 int lMaxIndex,lMinIndex,lIndexMax,lDirection,lIndex,lLastUp,lLastDown,ii,i,lMax,lLeft,lRight,
52 *plMinimaL,*plMeanL,*plMinimaR,*plMeanR,lPrint,*pl;
54 double dValue,dLow,dHigh,dDelta,dAver,*pdData,*pdNoise,dMax,dMin,dPos,dSig,dSum,*pd;
58 pdData = (
double *)malloc(lPoints*8);
59 pdNoise = (
double *)malloc(lPoints*8);
60 memset(pdData,0,lPoints*8);
61 memset(pdNoise,0,lPoints*8);
65 pw = ((
short *)pfData) + lFirstChan;
66 pl = ((
int *)pfData) + lFirstChan;
67 pf = ((
float *)pfData) + lFirstChan;
68 pd = ((
double *)pfData) + lFirstChan;
69 if(lAver == 0)lAver=1;
70 lPoints=lPoints/lAver;
71 dAver = (double) lAver;
76 { *(pdData+ii) = (
double)*(pw+lAver*ii);
77 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pw+lAver*ii+i);
78 *(pdData+ii) = *(pdData+ii)/dAver;
79 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
80 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
82 { *(pdData+ii) = (
double)*(pl+lAver*ii);
83 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pl+lAver*ii+i);
84 *(pdData+ii) = *(pdData+ii)/dAver;
85 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
86 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
88 { *(pdData+ii) = (
double)*(pf+lAver*ii);
89 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pf+lAver*ii+i);
90 *(pdData+ii) = *(pdData+ii)/dAver;
91 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
92 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
94 { *(pdData+ii) = (
double)*(pd+lAver*ii);
95 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pd+lAver*ii+i);
96 *(pdData+ii) = *(pdData+ii)/dAver;
97 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
98 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
99 default: printf(
"Data type %d not supported!\n",lType);
100 printf(
"Type 0: short, 1: int, 2: float, 3: double\n");
107 for(ii=0;ii<lPoints;ii++)
109 if(*(pdData+ii) == 0.0) *(pdNoise+ii) = dDeltaFact;
110 else *(pdNoise+ii) = sqrt(*(pdData+ii)) * dDeltaFact;
111 if(dDeltaMin > *(pdNoise+ii)) *(pdNoise+ii)=dDeltaMin;
114 for(ii=0;ii<lPoints;ii++)
115 { *(pdNoise+ii) = *(dNoise+lFirstChan+lAver*ii);
116 for(i=1;i<lAver;i++)*(pdNoise+ii) += *(dNoise+lFirstChan+lAver*ii+i);
117 *(pdNoise+ii) = *(pdNoise+ii)/dAver;
123 plMinimaL = (
int *)malloc(lPeaksMax*16);
124 plMeanL = (
int *)(plMinimaL+lPeaksMax);
125 plMinimaR = (
int *)(plMinimaL+2*lPeaksMax);
126 plMeanR = (
int *)(plMinimaL+3*lPeaksMax);
127 memset(plMinimaL,0,lPeaksMax*16);
130 dHigh = *pdData + dDelta;
131 dLow = *pdData - dDelta;
132 if(pfLowNoise)*pfLowNoise=dLow;
133 if(pfHighNoise)*pfHighNoise=dHigh;
140 while ((lIndex <= lIndexMax) & (lMaxIndex < lPeaksMax-2))
142 dValue = *(pdData+lIndex);
145 if (lDirection !=
UP)
147 if(lPrint)printf(
"%5d - %5d - %5d d %8.1f\n",
152 *(plMinimaR+lMinIndex)=lIndex;
153 *(plMinimaL+lMinIndex)=lLastDown;
161 if (lDirection ==
UP)
163 if(lPrint)printf(
"%5d + %5d - %5d d %8.1f\n",
168 *(plMeanR+lMaxIndex)=lIndex;
169 *(plMeanL+lMaxIndex)=lLastUp;
176 if((dValue < dLow)|(dValue > dHigh))
178 dDelta= *(pdNoise+lIndex);
179 dHigh = dValue + dDelta;
180 dLow = dValue - dDelta;
183 if(pfLowNoise)*(pfLowNoise+lIndex)=dLow;
184 if(pfHighNoise)*(pfHighNoise+lIndex)=dHigh;
186 if(lDirection ==
DOWN)
188 *(plMinimaL+lMinIndex)=lLastDown;
189 *(plMinimaR+lMinIndex)=lIndexMax;
190 if(lPrint)printf(
"%5d - %5d - %5d\n",
197 if(lPrint)printf(
"-------------------------\n");
198 if(lPrint)printf(
"Minima %d Maxima %d \n",lMinIndex,lMaxIndex);
200 if(lMinIndex != (lMaxIndex+1))
206 if(lPrint)printf(
"Error, wrong minima\n");
211 if(lPrint)printf(
" [ Left Right ] \n");
212 *plMinima = (*plMinimaL + *plMinimaR)/2;
214 for(ii = *plMinimaL;ii <= *plMinimaR;ii++) *pdMinima += *(pdData+ii);
215 *pdMinima = *pdMinima/(double)(*plMinimaR - *plMinimaL + 1);
216 if(lPrint)printf(
"- [%6d %6d] %6d %6.1f\n",
221 for(lIndex=0;lIndex < lMaxIndex;lIndex++)
224 *(plMean+lIndex)=(*(plMeanL+lIndex)+*(plMeanR+lIndex))/2;
225 if(lPrint)printf(
"+ [%6d %6d] %6d\n",
230 *(plMinima+lIndex+1) = (*(plMinimaL+lIndex+1) + *(plMinimaR+lIndex+1))/2;
231 *(pdMinima+lIndex+1)=0.;
232 for(ii = *(plMinimaL+lIndex+1);ii <= *(plMinimaR+lIndex+1);ii++)
233 *(pdMinima+lIndex+1) += *(pdData+ii);
234 *(pdMinima+lIndex+1) = *(pdMinima+lIndex+1)/(double)(*(plMinimaR+lIndex+1) - *(plMinimaL+lIndex+1)+1);
235 if(lPrint)printf(
"- [%6d %6d] %6d %6.1f\n",
236 *(plMinimaL+lIndex+1),
237 *(plMinimaR+lIndex+1),
238 *(plMinima+lIndex+1),
239 *(pdMinima+lIndex+1));
241 ii = *(plMinimaL+lIndex+1) - *(plMinimaR+lIndex) + 1;
246 *(plMean+*plPeaks)=(
int)((dPos+(double)(*(plMinimaR+lIndex)))*dAver)+lFirstChan;
247 *(plWidth+*plPeaks) =(
int)(dSig*dAver + 0.5);
248 if(*(pdMinima+lIndex) < *(pdMinima+lIndex+1))
249 dSum=dSum-((double)ii * *(pdMinima+lIndex)+ (double)ii * (*(pdMinima+lIndex+1)-*(pdMinima+lIndex))/2.);
251 dSum=dSum-((double)ii * *(pdMinima+lIndex)- (double)ii * (*(pdMinima+lIndex)-*(pdMinima+lIndex+1))/2.);
252 *(pdIntegral+*plPeaks) = dSum*dAver;
261 if(lPrint)printf(
"Error, negative interval\n");
265 for(i=0;i<*plPeaks;i++)
269 pd=pdData+*(plMinima+i);
270 for(ii=*(plMinima+i);ii<*(plMinima+i+1);ii++){
if(dMax < *pd)dMax = *pd;
if(dMin > *pd)dMin = *pd; pd++;}
271 dMin=dMin+(dMax-dMin)/2.;
272 pd=pdData+*(plMinima+i);
273 for(lMax=*(plMinima+i);lMax<*(plMinima+i+1);lMax++){
if(dMax == *pd)
break; pd++;}
275 lRight=*(plMinima+i+1);
278 for(ii=lMax;ii<lRight;ii++){
if(dMin > *pd){lRight=ii;
break;}pd++;}
280 for(ii=lMax;ii>lLeft;ii--){
if(dMin > *pd){lLeft=ii;
break;}pd--;}
284 *(plMinima) = (
int)((double)(*(plMinima))*dAver + dAver/2.)+lFirstChan;
285 for(i=0;i<*plPeaks;i++)
288 *(plMaximum+i) = (
int)((double)(*(plMaximum+i))*dAver + dAver/2.)+lFirstChan;
289 *(plLeft+i) = (
int)((double)(*(plLeft+i))*dAver + dAver/2.)+lFirstChan;
290 *(plRight+i) = (
int)((double)(*(plRight+i))*dAver + dAver/2.)+lFirstChan;
291 *(plMinima+i+1) = (
int)((double)(*(plMinima+i+1))*dAver + dAver/2.)+lFirstChan;
293 if(lPrint)printf(
"peaks found %d\n",*plPeaks);
300 int go4fit_position(
int l_len,
double *pa_data,
double *pr_pos,
double *pr_sig,
double *pr_sum)
321 for(J = 1; J <= l_len; J++)
323 *pr_sum = *pr_sum + *pl_data;
324 d_sum_prod = d_sum_prod + J * *pl_data;
330 *pr_pos = (d_sum_prod/ *pr_sum + 0.5);
332 for(J = 1; J <= l_len; J++)
334 *pr_sig = *pr_sig + ((double)J - *pr_pos) * ((double)J - *pr_pos) * *pl_data;
337 *pr_sig = r_sig_f * sqrt(*pr_sig/ *pr_sum);
340 *pr_pos = *pr_pos -1.0;
int go4fit_position(int l_len, double *pa_data, double *pr_pos, double *pr_sig, double *pr_sum)
void go4fit_find_peaks(void *pfData, int lType, int lFirstChan, int lPoints, int lAver, double dDeltaFact, double dDeltaMin, double *dNoise, int lPeaksMax, int *plPeaks, int *plMinima, double *pdMinima, int *plMean, int *plWidth, double *pdIntegral, int *plLeft, int *plMaximum, int *plRight, float *pfLowNoise, float *pfHighNoise)