#include #include #include #include #include "nrutil.h" void dfpmin(double p[], int n, double gtol, int *iter, double *fret, double (*func)(double []), void (*dfunc)(double [], double [])); int strcount(char *line, char c); char *paulgetline(char *line, int nchars, FILE *fp); double spectrumError( double orientation[] ); void errorGradient( double orientationIn[], double gradientOut[] ); double *spectrum; double **localstd; int *squash; int nnnn, tflag=1; #define MIN(A,B) ((A > B) ? (B) : (A)) #define PI 3.141592653589793 #define GTOL 1.0e-09 double theta[] = {PI/4, 0.05, 1.52, 0.05, PI/4, 1.52, 0.05, 0.5, 1.0, 1.52}; double phi[] = {0.05, 0.5, 0.5, 1.0, PI/4, 1.0, 1.52, 1.52, 1.52, 1.52}; int main(int argc, char *argv[]) { FILE *fp1, *fp2; int q, i, j, ni, n1, n2, nstd=0, *arglist; int nflag=0, iflag=0, oflag=0, no, o=0, hl=2, iter, nxcl=0, yflag=0; double *x, *xstd, x1=1200.0, x2=2200.0, hold, lowxcl[20], highxcl[20]; double **spectra, **stds, **stdinterp, **p, pp[4], *thickness, *fret, frettemp, intlow=3000.0, inthigh=3750.0; char file1[256], sfile[256], ofile[256], yfile[256], *line, *linestart; double *w, **v, **a, *y, grate[3], **synthetics; int *yes; if (argc == 1) { printf("Usage: pauldeconvolve [-i] [-t] [-n] [-s stdfilename] [-o outputfilename]\n"); printf(" [-hl headerlines] [-1 lowwavenumber] [-2 highwavenumber]\n"); printf(" [-x low1 high1 [-x low2 high2 ] ... ] [-y synthfile] [-int low high] [infile] \n"); printf("-i puts you in interactive mode, giving you a chance to set all other parameters as prompted.\n"); printf("-t indicates fixed thickness, otherwise thickness is varied during the fitting.\n"); printf("-n indicates spectra are normalized and must be denormalized before fitting.\n"); printf("-s specifies the standards file [no header lines!], (wavenumber, a, b, c) in csv format at 1 mm thick.\n"); printf("-o specifies the name of the output file. By default .decon is appended to the input name.\n"); printf("-hl specfies how many header lines in input file; these are copied to output file.\nThe line after the headerlines should give the thicknesses in mm.\n"); printf("The spectrum is fitted between lowwavenumber and highwavenumber (defaults 1200-2200).\n"); printf("-x indicates a region between low and high limits to be ignored in the fitting.\n"); printf("-y gives the name of the synthesized (a,b,c) output file and turns on synth-ing.\n"); printf("-int gives the wavenumber range to be integrated in the synthesized spectra (default 3000-3750).\n"); exit(0); } arglist = (int *) malloc((unsigned) 300*sizeof(int)); line = (char *) malloc((unsigned) 5000*sizeof(char)); for (i=1;i.decon): "); scanf("%s", ofile); if (strcmp(ofile, "x")) oflag = 1; else oflag = 0; printf("Step 2a. Disallow thickness ratio variations? (0 for fixed thickness): "); scanf("%d", &tflag); printf("Step 2b. Are unknown spectra already normalized? (0 for as-measured, 1 for normalized): "); scanf("%d", &nflag); printf("Step 3a. Enter low wavenumber limit of fitted range (enter 0 for default: %f): ", x1); scanf("%lf", &hold); if (hold != 0.0) x1 = hold; printf("Step 3b. Enter low wavenumber limit of fitted range (enter 0 for default: %f): ", x2); scanf("%lf", &hold); if (hold != 0.0) x2 = hold; printf("Step 4. How many regions to exclude (now there are %d; 0 for no more): ", nxcl); scanf("%d", &q); for (i=nxcl;i= 0) { linestart = line; sscanf(line, "%lf", &x[i]); for (j=0;j= x[n1+i]) squash[i] = 1; } /* First, interpolate standards to sample x points */ for (i=n1;i test) test=temp; } if (test < TOLX) { FREEALL return; } for (i=1;i<=n;i++) dg[i]=g[i]; (*dfunc)(p,g); test=0.0; den=FMAX(*fret,1.0); for (i=1;i<=n;i++) { temp=fabs(g[i])*FMAX(fabs(p[i]),1.0)/den; if (temp > test) test=temp; } if (test < gtol) { FREEALL return; } for (i=1;i<=n;i++) dg[i]=g[i]-dg[i]; for (i=1;i<=n;i++) { hdg[i]=0.0; for (j=1;j<=n;j++) hdg[i] += hessin[i][j]*dg[j]; } fac=fae=sumdg=sumxi=0.0; for (i=1;i<=n;i++) { fac += dg[i]*xi[i]; fae += dg[i]*hdg[i]; sumdg += SQR(dg[i]); sumxi += SQR(xi[i]); } if (fac*fac > EPS*sumdg*sumxi) { fac=1.0/fac; fad=1.0/fae; for (i=1;i<=n;i++) dg[i]=fac*xi[i]-fad*hdg[i]; for (i=1;i<=n;i++) { for (j=1;j<=n;j++) { hessin[i][j] += fac*xi[i]*xi[j] -fad*hdg[i]*hdg[j]+fae*dg[i]*dg[j]; } } } for (i=1;i<=n;i++) { xi[i]=0.0; for (j=1;j<=n;j++) xi[i] -= hessin[i][j]*g[j]; } } nrerror("too many iterations in dfpmin"); FREEALL } #undef ITMAX #undef EPS #undef TOLX #undef STPMX #undef FREEALL #define ALF 1.0e-4 #define TOLX 1.0e-7 void lnsrch(int n, double xold[], double fold, double g[], double p[], double x[], double *f, double stpmax, int *check, double (*func)(double [])) { int i; double a,alam,alam2,alamin,b,disc,f2,fold2,rhs1,rhs2,slope,sum,temp, test,tmplam; *check=0; for (sum=0.0,i=1;i<=n;i++) sum += p[i]*p[i]; sum=sqrt(sum); if (sum > stpmax) for (i=1;i<=n;i++) p[i] *= stpmax/sum; for (slope=0.0,i=1;i<=n;i++) slope += g[i]*p[i]; test=0.0; for (i=1;i<=n;i++) { temp=fabs(p[i])/FMAX(fabs(xold[i]),1.0); if (temp > test) test=temp; } alamin=TOLX/test; alam=1.0; for (;;) { for (i=1;i<=n;i++) x[i]=xold[i]+alam*p[i]; *f=(*func)(x); if (alam < alamin) { for (i=1;i<=n;i++) x[i]=xold[i]; *check=1; return; } else if (*f <= fold+ALF*alam*slope) return; else { if (alam == 1.0) tmplam = -slope/(2.0*(*f-fold-slope)); else { rhs1 = *f-fold-alam*slope; rhs2=f2-fold2-alam2*slope; a=(rhs1/(alam*alam)-rhs2/(alam2*alam2))/(alam-alam2); b=(-alam2*rhs1/(alam*alam)+alam*rhs2/(alam2*alam2))/(alam-alam2); if (a == 0.0) tmplam = -slope/(2.0*b); else { disc=b*b-3.0*a*slope; /*if (disc<0.0) nrerror("Roundoff problem in lnsrch."); else */tmplam=(-b+sqrt(disc))/(3.0*a); } if (tmplam>0.5*alam) tmplam=0.5*alam; } } alam2=alam; f2 = *f; fold2=fold; alam=FMAX(tmplam,0.1*alam); } } #undef ALF #undef TOLX int strcount(char *line, char c) { int count=0; char *marker = line; while ((marker = strchr(marker, c)) != '\0') {count++; marker++;} return count; } char *paulgetline(char *line, int nchars, FILE *fp) { char c, *startline=line; int n=0; while ((c = fgetc(fp)) != EOF && c != 10 && c !=13 && n++ <= nchars) *line++ = c; if (c == 13) { if ((c = fgetc(fp)) != EOF && c != 10) { ungetc(c, fp); } } *line = '\0'; if (line == startline) return 0x0; else { line = startline; return line; } } double spectrumError( double orientation[] ) { double theta = orientation[1]; double phi = orientation[2]; double tratio = orientation[3]; double *test, errorVal, errorInc, a, b, c; int i; test = (double *) malloc((unsigned) nnnn*sizeof(double)); /* generate test spectrum */ a = cos(theta)*sin(phi); a *= a; b = sin(theta)*sin(phi); b *= b; c = cos(phi); c *= c; for (i=0;i