52 int f_position(
int l_len,
double *pa_data,
double *pr_pos,
double *pr_sig,
double *pr_sum)
62 double *pl_data,d_max ;
87 for(J = 1; J <= l_len; J++)
89 *pr_sum = *pr_sum + *pl_data;
90 d_sum_prod = d_sum_prod + J * *pl_data;
96 *pr_pos = (d_sum_prod/ *pr_sum + 0.5);
98 for(J = 1; J <= l_len; J++)
100 *pr_sig = *pr_sig + ((double)J - *pr_pos) * ((double)J - *pr_pos) * *pl_data;
103 *pr_sig = r_sig_f * sqrt(*pr_sig/ *pr_sum);
106 *pr_pos = *pr_pos -1.0;
130 int lMaxIndex,lMinIndex,lIndexMax,lDirection,lIndex,lLastUp,lLastDown,ii,i,
131 *plMinimaL,*plMaximaL,*plMinimaR,*plMaximaR,lPrint,*pl;
133 double dValue,dLow,dHigh,dDelta,dAver,*pdData,*pdNoise,dMax,dMin,dPos,dSig,dSum,*pd;
137 pdData = (
double *)malloc(lPoints*8);
138 pdNoise = (
double *)malloc(lPoints*8);
139 memset(pdData,0,lPoints*8);
140 memset(pdNoise,0,lPoints*8);
144 pw = ((
short *)pfData) + lFirstChan;
145 pl = ((
int *)pfData) + lFirstChan;
146 pf = ((
float *)pfData) + lFirstChan;
147 pd = ((
double *)pfData) + lFirstChan;
148 if(lAver == 0)lAver=1;
149 lPoints=lPoints/lAver;
150 dAver = (double) lAver;
155 { *(pdData+ii) = (
double)*(pw+lAver*ii);
156 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pw+lAver*ii+i);
157 *(pdData+ii) = *(pdData+ii)/dAver;
158 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
159 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
160 case TYPE__INT:
for(ii=0;ii<lPoints;ii++)
161 { *(pdData+ii) = (
double)*(pl+lAver*ii);
162 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pl+lAver*ii+i);
163 *(pdData+ii) = *(pdData+ii)/dAver;
164 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
165 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
167 { *(pdData+ii) = (
double)*(pf+lAver*ii);
168 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pf+lAver*ii+i);
169 *(pdData+ii) = *(pdData+ii)/dAver;
170 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
171 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
173 { *(pdData+ii) = (
double)*(pd+lAver*ii);
174 for(i=1;i<lAver;i++)*(pdData+ii) += (double)*(pd+lAver*ii+i);
175 *(pdData+ii) = *(pdData+ii)/dAver;
176 if(*(pdData+ii) > dMax) dMax=*(pdData+ii);
177 if(*(pdData+ii) < dMin) dMin=*(pdData+ii);}
break;
178 default: printf(
"Data type %d not supported!\n",lType);
179 printf(
"Type 0: short, 1: int, 2: float, 3: double\n");
186 for(ii=0;ii<lPoints;ii++)
188 if(*(pdData+ii) == 0.0) *(pdNoise+ii) = dDeltaFact;
189 else *(pdNoise+ii) = sqrt(*(pdData+ii)) * dDeltaFact;
190 if(dDeltaMin > *(pdNoise+ii)) *(pdNoise+ii)=dDeltaMin;
193 for(ii=0;ii<lPoints;ii++)
194 { *(pdNoise+ii) = *(dNoise+lFirstChan+lAver*ii);
195 for(i=1;i<lAver;i++)*(pdNoise+ii) += *(dNoise+lFirstChan+lAver*ii+i);
196 *(pdNoise+ii) = *(pdNoise+ii)/dAver;
202 plMinimaL = (
int *)malloc(lPeaksMax*16);
203 plMaximaL = (
int *)(plMinimaL+lPeaksMax);
204 plMinimaR = (
int *)(plMinimaL+2*lPeaksMax);
205 plMaximaR = (
int *)(plMinimaL+3*lPeaksMax);
206 memset(plMinimaL,0,lPeaksMax*16);
209 dHigh = *pdData + dDelta;
210 dLow = *pdData - dDelta;
217 while ((lIndex <= lIndexMax) & (lMaxIndex < lPeaksMax-2))
219 dValue = *(pdData+lIndex);
222 if (lDirection !=
UP)
224 if(lPrint)printf(
"%5d - %5d - %5d d %8.1f\n",
229 *(plMinimaR+lMinIndex)=lIndex;
230 *(plMinimaL+lMinIndex)=lLastDown;
238 if (lDirection ==
UP)
240 if(lPrint)printf(
"%5d + %5d - %5d d %8.1f\n",
245 *(plMaximaR+lMaxIndex)=lIndex;
246 *(plMaximaL+lMaxIndex)=lLastUp;
253 if((dValue < dLow)|(dValue > dHigh))
255 dDelta= *(pdNoise+lIndex);
256 dHigh = dValue + dDelta;
257 dLow = dValue - dDelta;
261 if(lDirection ==
DOWN)
263 *(plMinimaL+lMinIndex)=lLastDown;
264 *(plMinimaR+lMinIndex)=lIndexMax;
265 if(lPrint)printf(
"%5d - %5d - %5d\n",
272 if(lPrint)printf(
"-------------------------\n");
273 if(lPrint)printf(
"Minima %d Maxima %d \n",lMinIndex,lMaxIndex);
275 if(lMinIndex != (lMaxIndex+1))
280 printf(
"Error, wrong minima\n");
285 if(lPrint)printf(
" [ Left Right ] \n");
286 *plMinima = (*plMinimaL + *plMinimaR)/2;
287 *plMinima = (int)((
double)(*plMinima)*dAver + dAver/2.)+lFirstChan;
289 for(ii = *plMinimaL;ii <= *plMinimaR;ii++) *pdMinima += *(pdData+ii);
290 *pdMinima = *pdMinima/(double)(*plMinimaR - *plMinimaL + 1);
291 if(lPrint)printf(
"- [%6d %6d] %6d %6.1f\n",
296 for(lIndex=0;lIndex < lMaxIndex;lIndex++)
299 *(plMaxima+lIndex)=(*(plMaximaL+lIndex)+*(plMaximaR+lIndex))/2;
300 if(lPrint)printf(
"+ [%6d %6d] %6d\n",
305 *(plMinima+lIndex+1) = (*(plMinimaL+lIndex+1) + *(plMinimaR+lIndex+1))/2;
306 *(plMinima+lIndex+1) = (
int)((double)(*(plMinima+lIndex+1))*dAver + dAver/2.)+lFirstChan;
307 *(pdMinima+lIndex+1)=0.;
308 for(ii = *(plMinimaL+lIndex+1);ii <= *(plMinimaR+lIndex+1);ii++)
309 *(pdMinima+lIndex+1) += *(pdData+ii);
310 *(pdMinima+lIndex+1) = *(pdMinima+lIndex+1)/
311 (double)(*(plMinimaR+lIndex+1) - *(plMinimaL+lIndex+1)+1);
312 if(lPrint)printf(
"- [%6d %6d] %6d %6.1f\n",
313 *(plMinimaL+lIndex+1),
314 *(plMinimaR+lIndex+1),
315 *(plMinima+lIndex+1),
316 *(pdMinima+lIndex+1));
318 ii = *(plMinimaL+lIndex+1) - *(plMinimaR+lIndex) + 1;
321 f_position(ii,pdData + *(plMinimaR+lIndex),&dPos,&dSig,&dSum);
323 *(plMaxima+*plPeaks)=(
int)((dPos+(double)(*(plMinimaR+lIndex)))*dAver)+lFirstChan;
324 *(plWidth+*plPeaks) =(
int)(dSig*dAver + 0.5);
325 if(*(pdMinima+lIndex) < *(pdMinima+lIndex+1))
326 dSum=dSum-((double)ii * *(pdMinima+lIndex)+ (double)ii * (*(pdMinima+lIndex+1)-*(pdMinima+lIndex))/2.);
328 dSum=dSum-((double)ii * *(pdMinima+lIndex)- (double)ii * (*(pdMinima+lIndex)-*(pdMinima+lIndex+1))/2.);
329 *(pdIntegral+*plPeaks) = dSum*dAver;
337 printf(
"Error, negative interval\n");
341 if(lPrint)printf(
"peaks found %d\n",*plPeaks);
int f_position(int l_len, double *pa_data, double *pr_pos, double *pr_sig, double *pr_sum)
void f_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 *plMaxima, int *plWidth, double *pdIntegral)