home *** CD-ROM | disk | FTP | other *** search
- /*
- Find peaks:
- */
-
- #include <stdio.h>
- #include <spec.h>
- #define TMPFILE "global.def"
- #define MAXPEAKS 30
-
- float *spc,*err,*tim;
- int tri=0,
- range = 1024;
-
- help()
- {
- printf("finding prompt peaks in file (first approximation)\n");
- printf("fprompt file [options]\n");
- printf("print out prompt peak positions\n");
- printf("options:\n");
- printf(" -tri [1/2] triangular spectrum beginning with minimum: 1\n");
- printf(" peak: 2\n");
- printf(" Normal Spectrum brginning with minimum : 0\n");
- printf(" peak: 3\n");
- printf(" When specifying -tri, the phases will be recalculated !\n");
- printf(" -get file get old spectra definition file (for reading\n");
- printf(" the spectrum phase and parity)\n");
- printf(" -def file specifies another definition file for output\n");
- printf(" -v print peak positions to stdout\n\n");
- printf("the default output file is %s\n",TMPFILE);
- printf("This file is read by the RVT routine and is organized as follows:\n");
- printf("Each line consists of 4 integers.\n");
- printf("The first specifies the Time 0 position.\n");
- printf("The second can be +1 -1 +2 or -2 and specifies, if it is\n");
- printf(" to be mirrored (-) and if it is a 0 degree (1)\n");
- printf(" or 90 degree (2) spectrum\n");
- printf("The last two numbers are used to specify a range for\n");
- printf("the RVT routine and the exponential fit.\n");
- printf("\n(C) Rainer Kowallik\n");
- exit(0);
- }
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int xx,yy,n,m,i,spcmax,peakptr;
- int peaks[MAXPEAKS], phase[MAXPEAKS], xend[MAXPEAKS];
- char z[80],defnam[80];
- float x,y;
- FILE *fp;
-
- checkopt(argc,argv,"-dummy",z);
-
- spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
- err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
- tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
-
- strcpy(defnam,TMPFILE);
-
- guessphase(phase,tri);
- getphase(defnam,phase,&tri,&range);
-
- if(checkopt(argc,argv,"-tri",z)) {
- tri=atoi(z);
- if(tri>3) {
- printf("don't fool me , or I will fool you too !\n");
- exit(0);
- }
- guessphase(phase,tri);
- }
- if(checkopt(argc,argv,"-def",z)) strcpy(defnam,z);
-
- if(checkopt(argc,argv,"-get",z)) getphase(z,phase,&tri,&range);
-
- spcmax=readspec(argv[1],spc,err,tim,z);
-
- peakptr = find_peaks(spc,peaks,MAXPEAKS,10,spcmax);
- if(checkopt(argc,argv,"-v",z)) {
- for(i = 0; i < peakptr; i++) {
- n = peaks[i];
- printf("peak at %5d height %f\n",n,spc[n]);
- }
- }
- peakptr = calc_phases(spc,peaks,phase,peakptr,spcmax); /* !!!!!!!!!! */
-
- /* ---------------------------------------
- finding end of usable data for spectra
- --------------------------------------- */
-
- findusable(peaks,range,tri,phase,xend,peakptr);
- n=xend[0];
- for(i=0;i<peakptr;i++) {
- if(xend[i]<n) n=xend[i];
- }
- xend[peakptr-1] = n; /* minimum must be the last entry */
-
-
- /* ---------------------------------------------------
- writing data
- ------------------------------------------------------ */
-
- fp=fopen(defnam,"w");
- for(i=0;i<peakptr;i++) {
- fprintf(fp,"%d %2d %d %d\n",peaks[i],phase[i],5,xend[i]);
- }
- fclose(fp);
- printf("\ndata written to %s\n now start editor on this file !\n",TMPFILE);
- free(spc); free(err); free(tim);
- exit(0);
- }
-
- iabs(x)
- int x;
- {
- if(x>=0) return(x);
- return(-x);
- }
-
-
- /* -----------------------------------------------
- reading spectra definition file
- ----------------------------------------------- */
- getphase(name,phase,tri,range)
- char name[];
- int phase[], *tri, *range;
- {
- int i,n,m,p[30];
- FILE *fp;
-
- fp=fopen(name,"r");
- if(fp==NULL) {
- return(0);
- }
- i=0; *tri=0;
- while(!feof(fp)) {
- fscanf(fp,"%d %d %d %d\n",&p[i],&phase[i],&n,&n);
- i=i+1;
- }
- fclose(fp);
- for(n=0;n<i;n++) if(phase[i]<0) *tri=2; /* triangular spectrum ? */
- if(phase[0]<0) *tri=1; /* triangular, beginning with minimum */
- n=p[0];
- if(p[1]>(n+5)) {
- n = iabs(n - p[1]);
- } else {
- n = iabs(n - p[2]);
- }
- i=1;
- while(i<n) i= i << 1;
- m = i >> 1;
- if(iabs(n-m) < iabs(n-i)) {
- n = m;
- } else {
- n = i;
- }
- *range = n;
- }
-
- guessphase(phase,tri)
- int phase[], tri;
- {
- int i,n,m;
-
- n=1;
- for(i=0;i<30;i++) {
- n= -1 * n;
- switch(tri) {
- case 0:
- if(n>0) phase[i]=1;
- if(n<0) phase[i]=2;
- break;
- case 1:
- if(n>0) {
- phase[i++] = -1;
- phase[i]=1;
- }
- if(n<0) {
- phase[i++]= -2;
- phase[i]=2;
- }
- break;
- case 2:
- if(n>0) {
- phase[i++]=1;
- phase[i]= -1;
- }
- if(n<0) {
- phase[i++]=2;
- phase[i]= -2;
- }
- break;
- }
- }
- }
-
- /* ------------------------------------------------
- finding usable data area
- ------------------------------------------------ */
- findusable(peaks,range,tri,phase,xend,peakptr)
- int peaks[],phase[],xend[],
- range,tri,peakptr;
- {
- int i,n,m,start,end;
-
- start = range / 2;
- end = range;
- if(tri>0) {
- start = range / 4;
- end = range / 2;
- }
- end = end - 3;
- for(n=0;n<peakptr;n++) { /* scanning spectra */
- xend[n] = end;
- if(phase[n] > 0) { /* positive time axis */
- m = peaks[n] + start;
- for(i=start;i<end;i++) {
- m = m + 1;
- if(!outrange(spc[m],err[m],spc[m+1],err[m+1])) continue;
- if(!outrange(spc[m],err[m],spc[m+2],err[m+2])) continue;
- if(!outrange(spc[m-1],err[m-1],spc[m+1],err[m+1])) continue;
- xend[n] = i - 2 ;
- }
- } else {
- m = peaks[n] - start;
- for(i=start;i<end;i++) {
- m = m - 1;
- if(!outrange(spc[m],err[m],spc[m-1],err[m-1])) continue;
- if(!outrange(spc[m],err[m],spc[m-2],err[m-2])) continue;
- if(!outrange(spc[m+1],err[m+1],spc[m-1],err[m-1])) continue;
- xend[n] = i - 2 ;
- }
- }
- }
- }
-
- outrange(x1,e1,x2,e2)
- float x1,e1,x2,e2;
- {
- float a,b,c;
-
- a = (float) fabs((double)(x1-x2));
- b = 3 * (e1 + e2);
- if(a>b) return(TRUE);
- return(FALSE);
- }
-
- /* ------------------------------------------------------------------------
- Here starts the Peak finder routine:
- Algorithm:
- 1. calculate arithmetic mean of spectrum.
- 2. copy spectrum to work area.
- 3. Find Maximum of Work area.
- 4. find points left and right, which are below the arithmetic mean.
- 5. Set area between these points to 0
- 6. goto 3 until no points are above the mean value.
- ------------------------------------------------------------------------ */
-
- find_peaks(spc,peaks,maxpeaks,part,spcmax)
- float spc[];
- int peaks[];
- int maxpeaks;
- int part;
- int spcmax;
- {
- int peakptr, n, m, i;
- float *buffer, mean, low;
-
- mean = 0.0;
-
- buffer = (float *) calloc(spcmax+2,sizeof(float));
- for(i = 0; i < spcmax; i++) {
- mean += spc[i];
- buffer[i] = spc[i];
- }
- mean = mean / spcmax;
-
- for(peakptr = 0; peakptr < maxpeaks; peakptr++) {
- n = eliminate_max(buffer,mean,spcmax);
- if(n < 0) break;
- if(peakptr == 0) low = spc[n] / part;
- if(spc[n] < low) {
- peakptr--;
- continue;
- }
- peaks[peakptr] = n;
- }
- sort(peaks,peakptr);
- free(buffer);
- return(peakptr);
- }
-
- eliminate_max(spc,mean,spcmax)
- float spc[];
- float mean;
- int spcmax;
- {
- int i,n,m, x_max, lmin, rmin;
- float y_max;
-
- y_max = mean; x_max = -1; /* find maximum */
- for(i = 0; i < spcmax; i++) {
- if(spc[i] > y_max) {
- y_max = spc[i];
- x_max = i;
- }
- }
- if(x_max < 0) return(x_max);
- lmin = x_max; /* find left minimum */
- while(spc[lmin] > mean) lmin--;
- rmin = x_max; /* find right minimum */
- while(spc[rmin] > mean) rmin++;
- for(i = lmin; i<rmin; i++) spc[i] = 0.0; /* eliminate peak */
-
- return(x_max);
- }
-
- sort(buffer,p)
- int buffer[];
- int p;
- {
- int i,n,m,z;
-
- for(n = 1; n < (p - 1); n++) {
- i = 1;
- for(m = 0; m < (p - n); m++) {
- if(buffer[m] > buffer[m+1]) {
- z = buffer[m];
- buffer[m] = buffer[m+1];
- buffer[m+1] = z; i = 0;
- }
- }
- if(i == 1) break;
- }
- }
-
- calc_phases(spc,peaks,phase,peakptr,spcmax)
- float *spc;
- int *peaks;
- int *phase;
- int peakptr;
- int spcmax;
- {
- int i,n,m;
- float mean;
- int x1,x2;
- float y1,y2;
-
- mean = 0.0;
- for(n = 0; n < spcmax; n++) mean += spc[n];
- mean = mean / spcmax;
-
- for(n = 0; n < peakptr; n++) {
- x1 = peaks[n] - 50;
- x2 = peaks[n] + 50;
- y1 = spc[x1];
- y2 = spc[x2];
- if((y1 > mean) && (y2 > mean)) { /* triangular spectrum */
- for(i = peakptr; i >= n; i--) peaks[i+1] = peaks[i];
- peakptr = peakptr + 1;
- phase[n] = -1;
- n = n + 1;
- phase[n] = 1;
- }
- if((y1 > mean) && (y2 < mean)) { /* normal inverted spectrum */
- phase[n] = -1;
- }
- if((y1 < mean) && (y2 > mean)) { /* normal, NOT inverted spectrum */
- phase[n] = 1;
- }
- }
- return(peakptr);
- }
-
-