void event_list::verify_and_link (midipulse slength) { clear_links(); for (event_list::iterator on = m_events.begin(); on != m_events.end(); on++) { event & eon = dref(on); if (eon.is_note_on()) /* Note On, find its Note Off */ { event_list::iterator off = on; /* get next possible Note Off */ off++; bool endfound = false; while (off != m_events.end()) { event & eoff = dref(off); if /* Off, == notes, not marked */ ( eoff.is_note_off() && eoff.get_note() == eon.get_note() && ! eoff.is_marked() ) { eon.link(&eoff); /* link + mark */ eoff.link(&eon); eon.mark(); eoff.mark(); endfound = true; break; } off++; } if (! endfound) { off = m_events.begin(); while (off != on) { event & eoff = dref(off); if ( eoff.is_note_off() && eoff.get_note() == eon.get_note() && ! eoff.is_marked() ) { eon.link(&eoff); /* link + mark */ eoff.link(&eon); eon.mark(); eoff.mark(); endfound = true; break; } off++; } } } } unmark_all(); mark_out_of_range(slength); }
void editable_events::clear_links () { for (Events::iterator i = m_events.begin(); i != m_events.end(); ++i) { event & e = dref(i); e.clear_link(); e.unmark(); } }
int dsh_prompt( char** input ) { int buffer_size = 256; int num_read = 0; char *curr = NULL; char *line = NULL; line = (char*) calloc( buffer_size , sizeof(char) ); if( line == NULL ) return -1; //Display prompt printf( "dsh> " ); //Read first 256 characters or until newline reached scanf( "%256[^\n]%n" , line , &num_read ); if( num_read == 0 ) { free(line); return 1; } //While 256 characters are read, allocate 256 more bytes and keep reading while( num_read == 256 ) { buffer_size += 256; //Reallocate space line = realloc( line , buffer_size ); if( line == NULL ) return -1; //Set pointer to end of last read curr = line + buffer_size - 256; scanf( "%256[^\n]%n" , curr , &num_read ); } //CHANGEME! //This will temporarily set return to 1 if user enters exit //Needs to be moved to function that handles commands if( !strncmp( line , "exit" , 4 ) ) { free( line ); return 2; } //Return input by reference dref(input) = line; return 0; }
void event_list::mark_out_of_range (midipulse slength) { for (Events::iterator i = m_events.begin(); i != m_events.end(); ++i) { event & e = dref(i); if (e.get_timestamp() > slength) { e.mark(); /* we have to prune it */ if (e.is_linked()) e.get_linked()->mark(); } } }
/** Second harmonic oscillator. Filter a time series for testing. */ dmat *sho_filter( const dmat *x,/**<Input time series*/ double dt, /**<Input time series sampling*/ double f0, /**<Resonance frequency*/ double zeta /**<Damping*/ ){ SHO_T *sho=sho_new(f0, zeta); dmat *xi=dref(x); if(xi->nx==1){ xi->nx=xi->ny; xi->ny=1; } dmat *yo=dnew(xi->nx, xi->ny); for(long iy=0; iy<xi->ny; iy++){ sho_reset(sho); for(long ix=0; ix<xi->nx; ix++){ IND(yo, ix, iy)=sho_step(sho, IND(xi, ix, iy), dt); } } dfree(xi); free(sho); return yo; }
/*Interpolate psd onto new f. We interpolate in log space which is more linear.*/ dmat *psdinterp1(const dmat *psdin, const dmat *fnew, int uselog){ dmat *f1=drefcols(psdin, 0, 1); dmat *psd1=dsub(psdin, 0, 0, 1, 1);//copy dmat *f2=dref(fnew); double t1=dtrapz(f1, psd1); double ydefault=1e-40; if(uselog){ dcwlog(psd1); ydefault=log(ydefault); } dmat *psd2=dinterp1(f1, psd1, f2, ydefault); if(uselog){ dcwexp(psd2,1); } double t2=dtrapz(f2, psd2); if(fabs(t1-t2)>fabs(0.5*(t1+t2)*2)){ warning("psd interpolation failed. int_orig=%g, int_interp=%g\n", t1, t2); } //Don't scale psd2 as it may have overlapping frequency regions dfree(f1); dfree(f2); dfree(psd1); return psd2; }
/** Setup the detector transfer functions. See maos/setup_powfs.c */ static void setup_powfs_dtf(POWFS_S *powfs, const PARMS_S* parms){ const int npowfs=parms->maos.npowfs; for(int ipowfs=0; ipowfs<npowfs; ipowfs++){ const int ncomp=parms->maos.ncomp[ipowfs]; const int ncomp2=ncomp>>1; const int embfac=parms->maos.embfac[ipowfs]; const int pixpsa=parms->skyc.pixpsa[ipowfs]; const double pixtheta=parms->skyc.pixtheta[ipowfs]; const double blur=parms->skyc.pixblur[ipowfs]*pixtheta; const double e0=exp(-2*M_PI*M_PI*blur*blur); const double dxsa=parms->maos.dxsa[ipowfs]; const double pixoffx=parms->skyc.pixoffx[ipowfs]; const double pixoffy=parms->skyc.pixoffy[ipowfs]; const double pxo=-(pixpsa/2-0.5+pixoffx)*pixtheta; const double pyo=-(pixpsa/2-0.5+pixoffy)*pixtheta; loc_t *loc_ccd=mksqloc(pixpsa, pixpsa, pixtheta, pixtheta, pxo, pyo); powfs[ipowfs].dtf=mycalloc(parms->maos.nwvl,DTF_S); for(int iwvl=0; iwvl<parms->maos.nwvl; iwvl++){ const double wvl=parms->maos.wvl[iwvl]; const double dtheta=wvl/(dxsa*embfac); const double pdtheta=pixtheta*pixtheta/(dtheta*dtheta); const double du=1./(dtheta*ncomp); const double du2=du*du; const double dupth=du*pixtheta; cmat *nominal=cnew(ncomp,ncomp); //cfft2plan(nominal,-1); //cfft2plan(nominal,1); cmat* pn=nominal; const double theta=0; const double ct=cos(theta); const double st=sin(theta); for(int iy=0; iy<ncomp; iy++){ int jy=iy-ncomp2; for(int ix=0; ix<ncomp; ix++){ int jx=ix-ncomp2; double ir=ct*jx+st*jy; double ia=-st*jx+ct*jy; IND(pn,ix,iy)=sinc(ir*dupth)*sinc(ia*dupth) *pow(e0,ir*ir*du2)*pow(e0,ia*ia*du2) *pdtheta; } } if(parms->skyc.fnpsf1[ipowfs]){ warning("powfs %d has additional otf to be multiplied\n", ipowfs); dcell *psf1c=dcellread("%s", parms->skyc.fnpsf1[ipowfs]); dmat *psf1=NULL; if(psf1c->nx == 1){ psf1=dref(psf1c->p[0]); }else if(psf1c->nx==parms->maos.nwvl){ psf1=dref(psf1c->p[iwvl]); }else{ error("skyc.fnpsf1 has wrong dimension\n"); } dcellfree(psf1c); if(psf1->ny!=2){ error("skyc.fnpsf1 has wrong dimension\n"); } dmat *psf1x=dnew_ref(psf1->nx, 1, psf1->p); dmat *psf1y=dnew_ref(psf1->nx, 1, psf1->p+psf1->nx); dmat *psf2x=dnew(ncomp*ncomp, 1); for(int iy=0; iy<ncomp; iy++){ int jy=iy-ncomp2; for(int ix=0; ix<ncomp; ix++){ int jx=ix-ncomp2; psf2x->p[ix+iy*ncomp]=sqrt(jx*jx+jy*jy)*dtheta; } } info("powfs %d, iwvl=%d, dtheta=%g\n", ipowfs, iwvl, dtheta*206265000); writebin(psf2x, "powfs%d_psf2x_%d", ipowfs,iwvl); dmat *psf2=dinterp1(psf1x, psf1y, psf2x, 0); normalize_sum(psf2->p, psf2->nx*psf2->ny, 1); psf2->nx=ncomp; psf2->ny=ncomp; writebin(psf2, "powfs%d_psf2_%d", ipowfs,iwvl); cmat *otf2=cnew(ncomp, ncomp); //cfft2plan(otf2, -1); ccpd(&otf2, psf2);//peak in center cfftshift(otf2);//peak in corner cfft2(otf2, -1); cfftshift(otf2);//peak in center writebin(otf2, "powfs%d_otf2_%d", ipowfs, iwvl); writebin(nominal, "powfs%d_dtf%d_nominal_0",ipowfs,iwvl); for(int i=0; i<ncomp*ncomp; i++){ nominal->p[i]*=otf2->p[i]; } writebin(nominal, "powfs%d_dtf%d_nominal_1",ipowfs,iwvl); dfree(psf1x); dfree(psf1y); dfree(psf2x); dfree(psf1); cfree(otf2); dfree(psf2); } cfftshift(nominal);//peak in corner cfft2(nominal,-1); cfftshift(nominal);//peak in center cfft2i(nominal,1); warning_once("double check nominal for off centered skyc.fnpsf1\n"); /*This nominal will multiply to OTF with peak in corner. But after inverse fft, peak will be in center*/ ccp(&powfs[ipowfs].dtf[iwvl].nominal, nominal); cfree(nominal); loc_t *loc_psf=mksqloc(ncomp, ncomp, dtheta, dtheta, -ncomp2*dtheta, -ncomp2*dtheta); powfs[ipowfs].dtf[iwvl].si=mkh(loc_psf,loc_ccd,0,0,1); locfree(loc_psf); if(parms->skyc.dbg){ writebin(powfs[ipowfs].dtf[iwvl].nominal, "%s/powfs%d_dtf%d_nominal",dirsetup,ipowfs,iwvl); writebin(powfs[ipowfs].dtf[iwvl].si, "%s/powfs%d_dtf%d_si",dirsetup,ipowfs,iwvl); } powfs[ipowfs].dtf[iwvl].U=cnew(ncomp,1); dcomplex *U=powfs[ipowfs].dtf[iwvl].U->p; for(int ix=0; ix<ncomp; ix++){ int jx=ix<ncomp2?ix:(ix-ncomp); U[ix]=COMPLEX(0, -2.*M_PI*jx*du); } }/*iwvl */ locfree(loc_ccd); }/*ipowfs */ }
/** Generate stars for nsky star fields from star catalog. \return a cell array of nskyx1, each cell contains (2+nwvl) x nstar array of location, and magnitudes. */ dcell *genstars(long nsky, /**<number of star fields wanted*/ double lat, /**<galactic latitude.*/ double lon, /**<galactic longitude*/ double catscl, /**<Scale the catlog star count.*/ double fov, /**<diameter of the patrol field of view in arcsec.*/ int nwvl, /**<number of wavelength*/ double *wvls, /**<wavelength vector*/ rand_t *rstat /**<random stream*/ ){ char fn[80]; double cat_fov=0;/*catalogue fov */ int Jind=-1; if(nwvl==2 && fabs(wvls[0]-1.25e-6)<1.e-10 && fabs(wvls[1]-1.65e-6)<1.e-10){ snprintf(fn,80,"besancon/JH_5sqdeg_lat%g_lon%g_besancon.bin", lat, lon); cat_fov=5.0;/*5 arc-degree squared. */ Jind=0; }else if(nwvl==3 && fabs(wvls[0]-1.25e-6)<1.e-10 && fabs(wvls[1]-1.65e-6)<1.e-10 && fabs(wvls[2]-2.2e-6)<1.e-10){ snprintf(fn,80,"besancon/JHK_5sqdeg_lat%g_lon%g_besancon.bin", lat, lon); cat_fov=5.0;/*5 arc-degree squared. */ Jind=0; }else{ Jind=-1; error("We only have stars for J+H and J+H+K band. Please fill this part\n"); } info("Loading star catalogue from %s\n",fn); dmat *catalog=dread("%s",fn); if(catalog->ny!=nwvl){ error("Catalogue and wanted doesn't match\n"); } long ntot=catalog->nx; long nsky0=0; dcell *res=dcellnew(nsky,1); dmat* pcatalog=catalog; double fov22=pow(fov/2/206265,2); double navg0=M_PI*pow(fov/2./3600.,2)/cat_fov * ntot; if(catscl>0){//regular sky coverage sim double navg=navg0*catscl; info("Average number of stars: %g, after scaled by %g\n", navg, catscl); /*generate nstart && magnitude according to distribution.*/ for(long isky=0; isky<nsky; isky++){ long nstar=randp(rstat, navg); if(nstar==0) continue; res->p[isky]=dnew(nwvl+2, nstar); dmat* pres=res->p[isky]; for(long istar=0; istar<nstar; istar++){ long ind=round(ntot*randu(rstat));/*randomly draw a star index in the catlog */ for(int iwvl=0; iwvl<nwvl; iwvl++){ P(pres,2+iwvl,istar)=P(pcatalog,ind,iwvl); } } } }else{ /*instead of doing draws on nb of stars, we scan all possibilities and assemble the curve in postprocessing. catscl is negative, with absolute value indicating the max number of J<=19 stars to consider*/ long nmax=round(-catscl); nsky0=nsky/nmax; if(nsky0*nmax!=nsky){ error("nsky=%ld, has to be dividable by max # of stars=%ld", nsky, nmax); } int counti[nmax];//record count in each bin memset(counti, 0, sizeof(int)*nmax); int count=0; while(count<nsky){ long nstar=randp(rstat, navg0); if(nstar==0) continue; dmat *tmp=dnew(nwvl+2, nstar); dmat* pres=tmp; int J19c=0; for(long istar=0; istar<nstar; istar++){ long ind=round((ntot-1)*randu(rstat)); for(int iwvl=0; iwvl<nwvl; iwvl++){ P(pres,2+iwvl,istar)=P(pcatalog,ind,iwvl); } if(P(pres,2+Jind,istar)<=19){ J19c++; } } //J19c=0 is ok. Do not skip. if(J19c<nmax && counti[J19c]<nsky0){ int isky=counti[J19c]+(J19c)*nsky0; res->p[isky]=dref(tmp); count++; counti[J19c]++; } dfree(tmp); } } /*Fill in the coordinate*/ for(long isky=0; isky<nsky; isky++){ if(!res->p[isky]) continue; long nstar=res->p[isky]->ny; dmat* pres=res->p[isky]; for(long istar=0; istar<nstar; istar++){ /*randomly draw the star location. */ double r=sqrt(fov22*randu(rstat)); double th=2*M_PI*randu(rstat); P(pres,0,istar)=r*cos(th); P(pres,1,istar)=r*sin(th); } } dfree(catalog); return res; }
void event_list::unmark_all () { for (Events::iterator i = m_events.begin(); i != m_events.end(); ++i) dref(i).unmark(); }
int parse_input( int * argc , char* input, char*** argv ) { dref(argc) = 0; //Count of arguments int count = 0; //Counter to store arguments int done = 0; int before = 0; int found = 0; int quotes = 0; int status = 0; char** args = NULL; char* argin = input; char* iptin = input; //Tokenize and count number of arguments in input while( !done ) { before = found; switch( dref(iptin) ) { case 0: done = 1; break; case '\"': quotes++; found = 0; break; case ' ': if( is_even(quotes) ) { found = 0; break; } case '\t': default: found = 1; if( !before ) dref(argc)++; } if( !done && !found ) dref(iptin) = 0; iptin++; } //Check if there are an odd number of "" if( is_odd(quotes) ) { printf( "Parse error: Unable to match \"\" delimiters.\n" ); return -1; } //Allocate Exactly Enough Memory if( dref(argc) > 0 ) args = (char**) malloc( (dref(argc)+1)*sizeof(char*) ); else return 0; //Store Arguments found = 0; argin = input; while( argin < iptin ) { if( dref(argin) != 0 ) { if( !found ) { //printf( "Argument %d: %s\n" , count , argin ); args[count++] = argin; } found = 1; } else found = 0; argin++; } args[dref(argc)] = NULL; //For testing purposes if( dref(argc) > 0 && argv != NULL ) { dref(argv) = args; } else free(args); return 0; }