int main(int argc, char *argv[]) { const char *filename_wav, *filename_png; kiss_fftr_cfg fft; int err = 0; for (;;) { const int c = getopt(argc, argv, "h"); if (0 > c) break; switch (c) { case '?': err = EINVAL; /*@fallthrough@*/ case 'h': usage(); return err; } } if (argc < 3 || argc != (optind + 2)) { usage(); return -EINVAL; } filename_wav = argv[optind++]; filename_png = argv[optind++]; fft = kiss_fftr_alloc(NUM_FFT, 0, 0, 0); if (!fft) { err = ENOMEM; goto out; } err = read_wav(fft, filename_wav); if (err) goto out; err = plot_spectrum(filename_png); if (err) goto out; out: if (fft) kiss_fftr_free(fft); tmr_debug(); mem_debug(); return err; }
static void process_bird(double basebin, int harm, double *lofreq, double *hifreq) { int ii, plotnumpts = 1000, not_done_yet = 1, plotoffset; int lodatabin, firstcorrbin, numgoodpts, replot = 1; char inchar; float med, xx[2], yy[2], inx, iny; float powargr, powargi, pwr, maxpow = 0.0, maxbin = 0.0; double truebin, pred_freq, average; double firstbin = 0.0, lastbin = 0.0, numbins = 0.0; fcomplex *data, *result; /* 'bin' means normal resolution FFT amplitude */ /* 'pt' means an interpolated FFT amplitude */ /* 'pts'=='bins' only if NUMBETWEEN==1 */ *lofreq = *hifreq = 0.0; truebin = basebin * harm; pred_freq = truebin / T; xx[0] = xx[1] = pred_freq; data = get_rawbins(fftfile, truebin, BINSTOGET, &med, &lodatabin); if (lodatabin <= 0) { data[abs(lodatabin)].r = 1.0; data[abs(lodatabin)].i = 1.0; } firstcorrbin = (int) truebin - MAXBINSTOSHOW / 2; average = med / -log(0.5); result = gen_cvect(FFTLEN); numgoodpts = corr_complex(data, BINSTOGET, RAW, kernel, FFTLEN, FFT, result, MAXPTSTOSHOW, firstcorrbin - lodatabin, NUMBETWEEN, khw, CORR); for (ii = 0; ii < numgoodpts; ii++) { pwr = POWER(result[ii].r, result[ii].i) / average; if (pwr > maxpow) { maxpow = pwr; maxbin = firstcorrbin + dr * ii; } } printf("\nHarmonic %d of %.15g Hz (%.15g Hz, bin = %.15g)\n", harm, basebin / T, pred_freq, truebin); printf(" Max power = %.2f at %.15g Hz (bin = %.15g)\n", maxpow, maxbin / T, maxbin); do { cpgsci(1); if (replot) { plotoffset = MAXPTSTOSHOW / 2 - plotnumpts / 2; firstbin = firstcorrbin + dr * plotoffset; numbins = dr * plotnumpts; lastbin = firstbin + numbins; plot_spectrum(result + plotoffset, plotnumpts, firstbin, dr, T, average); cpgswin(0.0, 1.0, 0.0, 1.0); xx[0] = xx[1] = (truebin - firstbin) / numbins; yy[0] = 0.0; yy[1] = 1.0; cpgsci(2); /* red */ cpgline(2, xx, yy); /* Predicted freq */ cpgsci(7); /* yellow */ if (*lofreq) { xx[0] = xx[1] = ((*lofreq * T) - firstbin) / numbins; cpgline(2, xx, yy); /* Boundary */ } if (*hifreq) { xx[0] = xx[1] = ((*hifreq * T) - firstbin) / numbins; cpgline(2, xx, yy); /* Boundary */ } } replot = 1; cpgsci(7); /* yellow */ cpgcurs(&inx, &iny, &inchar); switch (inchar) { case ' ': case 'A': case 'a': xx[0] = xx[1] = inx; cpgline(2, xx, yy); /* New boundary */ if (*lofreq == 0.0) { *lofreq = (inx * numbins + firstbin) / T; printf(" Added 1st boundary at %.12g Hz\n", *lofreq); } else { *hifreq = (inx * numbins + firstbin) / T; printf(" Added 2nd boundary at %.12g Hz\n", *hifreq); } replot = 0; break; case 'I': /* Zoom in */ case 'i': plotnumpts /= 2; if (plotnumpts <= 8) plotnumpts = 8; printf(" Zooming in...\n"); break; case 'O': /* Zoom out */ case 'o': plotnumpts *= 2; if (plotnumpts > MAXPTSTOSHOW) plotnumpts = MAXPTSTOSHOW; printf(" Zooming out...\n"); break; case 'C': /* Clear/Delete the points */ case 'c': case 'D': case 'd': case 'X': case 'x': *lofreq = *hifreq = 0.0; printf(" Clearing boundaries.\n"); break; case 'Q': /* Quit/Next birdie */ case 'q': case 'N': case 'n': *lofreq = *hifreq = 0.0; free(data); vect_free(result); printf(" Skipping to next harmonic.\n"); return; default: printf(" Unrecognized option '%c'.\n", inchar); break; } if (*lofreq && *hifreq) not_done_yet = 0; } while (not_done_yet); if (*hifreq < *lofreq) { double tmpfreq; tmpfreq = *lofreq; *lofreq = *hifreq; *hifreq = tmpfreq; } free(data); vect_free(result); }
void spectrum(bool bVerbose, char *trj,char *shifts,bool bAbInitio, char *corrfn,char *noefn, int maxframes,bool bFour,bool bFit,int nrestart, int npair,t_pair pair[],int nat,real chem_shifts[], real taum,real maxdist, real w_rls[],rvec xp[],t_idef *idef) { FILE *fp; int i,j,m,ii,jj,natoms,status,nframes; rvec *x,dx; matrix box; real t0,t1,t,dt; real r2,r6,r_3,r_6,tauc; rvec **corr; real **Corr; t_sij *spec; snew(spec,npair); fprintf(stderr,"There is no kill like overkill! Going to malloc %d bytes\n", npair*maxframes*sizeof(corr[0][0])); snew(corr,npair); for(i=0; (i<npair); i++) snew(corr[i],maxframes); nframes = 0; natoms = read_first_x(&status,trj,&t0,&x,box); if (natoms > nat) gmx_fatal(FARGS,"Not enough atoms in trajectory"); do { if (nframes >= maxframes) { fprintf(stderr,"\nThere are more than the %d frames you told me!", maxframes); break; } t1 = t; if (bVerbose) fprintf(stderr,"\rframe: %d",nframes); rm_pbc(idef,natoms,box,x,x); if (bFit) do_fit(natoms,w_rls,xp,x); for(i=0; (i<npair); i++) { ii = pair[i].ai; jj = pair[i].aj; rvec_sub(x[ii],x[jj],dx); copy_rvec(dx,corr[i][nframes]); r2 = iprod(dx,dx); r6 = r2*r2*r2; r_3 = invsqrt(r6); r_6 = r_3*r_3; spec[i].rij_3 += r_3; spec[i].rij_6 += r_6; for(m=0; (m<5); m++) { spec[i].Ylm[m] = c_add(spec[i].Ylm[m], calc_ylm(m-2,dx,r2,r_3,r_6)); } } nframes++; } while (read_next_x(status,&t,natoms,x,box)); close_trj(status); if (bVerbose) fprintf(stderr,"\n"); fp=ffopen("ylm.out","w"); calc_aver(fp,nframes,npair,pair,spec,maxdist); fclose(fp); /* Select out the pairs that have to be correlated */ snew(Corr,npair); for(i=j=0; (i<npair); i++) { if (spec[i].bNOE) { Corr[j] = &(corr[i][0][0]); j++; } } fprintf(stderr,"There are %d NOEs in your simulation\n",j); if (nframes > 1) dt = (t1-t0)/(nframes-1); else dt = 1; do_autocorr(corrfn,"Correlation Function for Interproton Vectors", nframes,j,Corr,dt,eacP2,nrestart,FALSE,FALSE,bFour,TRUE); calc_tauc(bVerbose,npair,pair,dt,nframes/2,spec,(real **)corr); plot_spectrum(noefn,npair,pair,spec,taum); }