dstrlist* dsplit_on_cs(const dstring* text, const char* on, size_t max) { dstrlist* result = dlist_new(); int slen = strlen(on); unsigned int i, j, k = 0; if (max == 1) { dlist_add(result, text); return result; } else if (max <= 0) { max = text->len; } j = 0; i = dposcs(text, on, 0); while(i != -1) { dlist_push(result, dsub(text, j, i - j)); k++; j = i + slen; if (k + 1 == max) { dlist_push(result, dsub(text, j, text->len - j)); return result; } i = dposcs(text, on, j); } dlist_push(result, dsub(text, j, text->len - j)); return result; }
void dpolynorm( LWDVector v1, LWDVector v2, LWDVector vlast, LWDVector norm ) { LWDVector a, b; dsub( v2, v1, a ); dsub( vlast, v1, b ); dcross( a, b, norm ); dnormalize( norm ); }
double ddist( LWDVector a, LWDVector b ) { LWDVector c; dsub( a, b, c ); return dlen( c ); }
/** Add two PSDs that doesn't have the same frequency. the first column of each dmat is the frequency nu, and the second column is PSD. Bug discovered on 2013-03-24:only psd2 was added to to psd.*/ static dmat *add_psd_nomatch(const dmat *psd1,const dmat *psd2){ dmat *nu1=dsub(psd1,0,psd1->nx,0,1); dmat *p2ynew=dinterp1(psd2, 0, nu1, 1e-40); dmat *psd=dnew(nu1->nx,2); double *py=psd->p+psd->nx; const double *p1y=psd1->p+psd1->nx; for(long i=0; i<psd->nx; i++){ psd->p[i]=nu1->p[i]; py[i]=p1y[i]+p2ynew->p[i]; } dfree(nu1); dfree(p2ynew); return psd; }
/*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; }
/* Add a PSD scaled by scale to another. The first column of each dmat is the frequency nu, and the second column is PSD. */ void add_psd2(dmat **pout, const dmat *in, double scale){ if(!*pout){ *pout=ddup(in); }else{ dmat *out=*pout; double *p1=PCOL(out,1); dmat *p2new=0; const long nx=out->nx; const double *p2=0; if(check_psd_match(out, in)){ p2=PCOL(in, 1); }else{ dmat *nu1=dsub(out,0, nx,0,1); p2new=dinterp1(in, 0, nu1, 1e-40); p2=PCOL(p2new,0); dfree(nu1); } for(long i=0; i<nx; i++){ p1[i]+=p2[i]*scale; } dfree(p2new); } }
//除法计算 void divid(const char a[],const char b[],char result[]) { bool isNegative = false; char *op1,*pa,*pb,*pr; int up,alen,blen,adotp,bdotp,i,k,dotp,t,t1,j,quo_size; /////////////判定符号/////////////// //如果为异号 if((a[0] == '-'||b[0] == '-')&&a[0] != b[0]) result[0] = '-',isNegative = true; //去除负号 if(a[0] == '-')a++; if(b[0] == '-')b++; /////////////////////////////////// alen = strlen(a)-1; //减去一位小数点 blen = strlen(b)-1; ///////获取被除数小数点移位后的位置////////// adotp = strchr(a,'.')-a; bdotp = strchr(b,'.')-b; //计算商小数点位置 dotp = adotp+blen-bdotp; if(isNegative)dotp++; //////////准备数据///////////// op1 = (char *)calloc(alen+blen+1,sizeof(char)); pa = (char *)calloc(alen+blen+1,sizeof(char)); pb = (char *)calloc(blen+1,sizeof(char)); pr = (char *)calloc(alen+blen+1,sizeof(char)); for(i = 0,t=0; i<=alen; i++) { if(a[i]!='.') pa[t++] = a[i]; } for(;t<dotp-1;t++) { pa[t] = '0'; } for(;t<blen;t++) { pa[t] = '0'; } pa[t] = '\0'; for(i = 0,t=0; i<=blen; i++) { if(b[i]!='.') pb[t++] = b[i]; } pb[t] = '\0'; clz(pa); clz(pb); ////////取得被除数的高位数op1,且op1大于被除数b////////// strncpy(op1,pa,strlen(pb)); if(strcmp(op1,pb)<0) { strncpy(op1,pa,strlen(pb)+1); } /////计算////// j = k = strlen(op1); t1=0; quo_size = strlen(pa)+1-k; //获取商的长度 while(t1<quo_size) { up = 0; t = cmp(op1,pb); while(t>=0) { dsub(op1,pb); t = cmp(op1,pb); up++; } pr[t1++] = up+'0'; op1[strlen(op1)]=pa[j++]; clz(op1); } quo_size+=50;//加50精度 while(t1<quo_size&&(cmp(op1,(char *)"0")>0)) { up = 0; op1[strlen(op1)]='0'; t = cmp(op1,pb); while(t>=0) { dsub(op1,pb); t = cmp(op1,pb); up++; } pr[t1++] = up+'0'; } ////////////////////////////// if(isNegative)t=1; else t=0; for(i=0;i<=t1;i++)//复制结果并给商加上小数点 { if(t==dotp) result[t++] = '.'; result[t++]=pr[i]; } clz(result); clDot(result); free(op1); free(pa); free(pb); }
/* Compute cxx on atm to compare against L2, invpsd, fractal. */ static void test_cxx(){ rand_t rstat; int seed=4; double r0=0.2; double dx=1./4; long N=16; long nx=N; long ny=N; long nframe=40960; seed_rand(&rstat, seed); { dmat *cxx=dnew(N*N,N*N); map_t *atm=mapnew(nx+1, ny+1, dx, dx,NULL); for(long i=0; i<nframe; i++){ info("%ld of %ld\n", i, nframe); for(long j=0; j<(nx+1)*(ny+1); j++){ atm->p[j]=randn(&rstat); } fractal_do((dmat*)atm, dx, r0, L0, ninit); dmat *sec=dsub((dmat*)atm, 0, nx, 0, ny); dmat *atmvec=dref_reshape(sec, nx*ny, 1); dmm(&cxx,1, atmvec,atmvec,"nt",1); dfree(atmvec); dfree(sec); } dscale(cxx, 1./nframe); writebin(cxx, "cxx_fractal"); dfree(cxx); mapfree(atm); } { dmat *cxx=dnew(N*N,N*N); dmat *spect=turbpsd(nx, ny, dx, r0, 100, 0, 0.5); spect->p[0]=spect->p[1]; cmat *atm=cnew(nx, ny); //cfft2plan(atm, -1); dmat *atmr=dnew(nx*ny,1); dmat *atmi=dnew(nx*ny,1); for(long ii=0; ii<nframe; ii+=2){ info("%ld of %ld\n", ii, nframe); for(long i=0; i<atm->nx*atm->ny; i++){ atm->p[i]=COMPLEX(randn(&rstat), randn(&rstat))*spect->p[i]; } cfft2(atm, -1); for(long i=0; i<atm->nx*atm->ny; i++){ atmr->p[i]=creal(atm->p[i]); atmi->p[i]=cimag(atm->p[i]); } dmm(&cxx,1, atmr,atmr,"nt",1); dmm(&cxx,1, atmi,atmi,"nt",1); } dscale(cxx, 1./nframe); writebin(cxx, "cxx_fft"); dfree(cxx); dfree(atmr); dfree(atmi); cfree(atm); } loc_t *loc=mksqloc_auto(16,16,1./4,1./4); locwrite(loc,"loc"); dmat *B=stfun_kolmogorov(loc, r0); writebin(B, "B_theory"); }
int main(int argc, char* argv[]) { dstring *s1, *s2, *s3, *s4, *s5, *s6; /* dstring* dnew(); */ test_start("dnew()"); s1 = dnew(); test_assert(s1 != NULL); test_assert(s1->data != NULL); test_assert(s1->size > 0); test_assert(s1->len == 0); test_assert(s1->data[0] == 0); test_end(); /* dstring* dnewcopy(const dstring* str); */ test_start("dnewcopy()"); s2 = dnewcopy(s1); test_assert(s2 != NULL); test_assert(s2->data != NULL); test_assert(s2->size > 0); test_assert(s2->len == 0); test_assert(s2->data[0] == 0); s1->data[0] = 'A'; s1->data[1] = 0; s1->len = 1; dfree(s2); s2 = dnewcopy(s1); test_assert(s2->len == 1); test_assert(strcmp(s1->data, s2->data) == 0); test_end(); /* void dfree(dstring* str); */ test_start("dfree()"); dfree(s1); dfree(s2); test_assert(1); test_end(); /* dstring* dfromc(int chr); */ test_start("dfromc()"); s1 = dfromc('X'); test_assert(s1 != NULL); test_assert(s1->len == 1); test_assert(s1->data != NULL); test_assert(strcmp(s1->data, "X") == 0); dfree(s1); test_end(); /* dstring* dfromcs(const char* str); */ test_start("dfromcs()"); s1 = dfromcs("Hello World!"); test_assert(s1 != NULL); test_assert(s1->len == 12); test_assert(s1->data != NULL); test_assert(strcmp(s1->data, "Hello World!") == 0); dfree(s1); test_end(); /* dstring* dfrommem(const void* mem, size_t size); */ test_start("dfrommem()"); s1 = dfrommem("Hello World!", 12); test_assert(s1 != NULL); test_assert(s1->len == 12); test_assert(s1->data != NULL); test_assert(strcmp(s1->data, "Hello World!") == 0); dfree(s1); test_end(); /* dstring* dcpy(dstring* dst, const dstring* src); */ test_start("dcpy()"); s1 = dfromcs("Hello "); s2 = dfromcs("World!!!"); s3 = dcpy(s1, s2); test_assert(s1 == s3); test_assert(s1->len == 8); test_assert(s1->data != NULL); test_assert(strcmp(s1->data, "World!!!") == 0); dfree(s1); dfree(s2); test_end(); /* dstring* dcpyc(dstring* dst, int src); */ test_start("dcpyc()"); s1 = dfromcs("Hello"); s2 = dcpyc(s1, '!'); test_assert(s2 == s1); test_assert(s1->len == 1); test_assert(s1->data != NULL); test_assert(strcmp(s1->data, "!") == 0); dfree(s1); test_end(); /* dstring* dcpycs(dstring* dst, const char* src); */ test_start("dcpycs"); s1 = dfromcs("Hello"); s2 = dcpycs(s1, "Hola!"); test_assert(s2 == s1); test_assert(s1->len == 5); test_assert(s1->data != NULL); test_assert(strcmp(s1->data, "Hola!") == 0); dfree(s1); test_end(); /* dstring* dcpymem(dstring* dst, const void* mem, size_t size); */ test_start("dcpymem"); s1 = dfromcs("Hello"); s2 = dcpymem(s1, "Foo!", 4); test_assert(s2 == s1); test_assert(s1->len == 4); test_assert(s1->data != NULL); test_assert(strcmp(s1->data, "Foo!") == 0); dfree(s1); test_end(); /* dstring* dncpy(dstring* dst, const dstring* src, dstrlen_t n); */ test_start("dncpy"); s1 = dfromcs("Hello"); s2 = dfromcs("World!"); s1 = dncpy(s1, s2, 5); test_assert(s1->len == 5); test_assert(strcmp(s1->data, "World") == 0); s1 = dncpy(s1, s2, 20); test_assert(s1->len == 6); test_assert(strcmp(s1->data, "World!") == 0); dfree(s1); dfree(s2); test_end(); /* dstring* dncpycs(dstring* dst, const char* src, dstrlen_t n); */ test_start("dncpycs"); s1 = dfromcs("Hello"); s1 = dncpycs(s1, "World!", 5); test_assert(s1->len == 5); test_assert(strcmp(s1->data, "World") == 0); s1 = dncpycs(s1, "World!", 20); test_assert(s1->len == 6); test_assert(strcmp(s1->data, "World!") == 0); dfree(s1); test_end(); /* dstring* dcat(dstring* dst, const dstring* src); */ test_start("dcat"); s1 = dfromcs("Hello "); s2 = dfromcs("World!"); s3 = dcat(s1, s2); test_assert(s1 == s3); test_assert(s1->len == 12); test_assert(strcmp(s1->data, "Hello World!") == 0); dfree(s1); dfree(s2); test_end(); /* dstring* dcatc(dstring* dst, int src); */ test_start("dcatc"); s1 = dfromcs("Hello"); s3 = dcatc(s1, '!'); test_assert(s1 == s3); test_assert(s1->len == 6); test_assert(strcmp(s1->data, "Hello!") == 0); dfree(s1); test_end(); /* dstring* dcatcs(dstring* dst, const char* src); */ test_start("dcatcs"); s1 = dfromcs("Hello "); s3 = dcatcs(s1, "World!"); test_assert(s1 == s3); test_assert(s1->len == 12); test_assert(strcmp(s1->data, "Hello World!") == 0); dfree(s1); test_end(); /* dstring* dcatmem(dstring* dst, const void* src, size_t size); */ test_start("dcatmem"); s1 = dfromcs("Hello "); s3 = dcatmem(s1, "Guys!", 5); test_assert(s1 == s3); test_assert(s1->len == 11); test_assert(strcmp(s1->data, "Hello Guys!") == 0); dfree(s1); test_end(); /* dstring* dncat(dstring* dst, const dstring* src, dstrlen_t n); */ test_start("dncat"); s2 = dfromcs("Guys!"); s1 = dfromcs("Hello "); s3 = dncat(s1, s2, 4); test_assert(s1 == s3); test_assert(s1->len == 10); test_assert(strcmp(s1->data, "Hello Guys") == 0); dfree(s1); s1 = dfromcs("Hello "); s3 = dncat(s1, s2, 8); test_assert(s1 == s3); test_assert(s1->len == 11); test_assert(strcmp(s1->data, "Hello Guys!") == 0); dfree(s1); dfree(s2); test_end(); /* dstring* dncatcs(dstring* dst, const char* src, dstrlen_t n); */ test_start("dncatcs"); s1 = dfromcs("Hello "); s3 = dncatcs(s1, "Guys!", 4); test_assert(s1 == s3); test_assert(s1->len == 10); test_assert(strcmp(s1->data, "Hello Guys") == 0); dfree(s1); s1 = dfromcs("Hello "); s3 = dncatcs(s1, "Guys!", 8); test_assert(s1 == s3); test_assert(s1->len == 11); test_assert(strcmp(s1->data, "Hello Guys!") == 0); dfree(s1); test_end(); /* int dcmp(const dstring* a, const dstring* b); */ test_start("dcmp"); s1 = dfromcs("ddd"); s2 = dfromcs("aaa"); s3 = dfromcs("zzz"); s4 = dfromcs("bo"); s5 = dfromcs("aaaa"); s6 = dfromcs("ddd"); test_assert(dcmp(s1, s2) != 0); test_assert(dcmp(s1, s3) != 0); test_assert(dcmp(s1, s4) != 0); test_assert(dcmp(s1, s5) != 0); test_assert(dcmp(s1, s6) == 0); dfree(s1); dfree(s2); dfree(s3); dfree(s4); dfree(s5); dfree(s6); test_end(); /* int dcmpc(const dstring* a, int b); */ test_start("dcmpc"); s1 = dfromc('X'); test_assert(dcmpc(s1, 'a') != 0); test_assert(dcmpc(s1, 'x') != 0); test_assert(dcmpc(s1, 'X') == 0); dfree(s1); test_end(); /* int dcmpcs(const dstring* a, const char* b); */ test_start("dcmpcs"); s1 = dfromcs("Foo"); test_assert(dcmpcs(s1, "FO") != 0); test_assert(dcmpcs(s1, "barr") != 0); test_assert(dcmpcs(s1, "Foo") == 0); dfree(s1); test_end(); /* int dcmpmem(const dstring* a, const void* b, size_t size); */ test_start("dcmpmem"); s1 = dfromcs("Foo"); test_assert(dcmpmem(s1, "FO", 2) != 0); test_assert(dcmpmem(s1, "barr", 4) != 0); test_assert(dcmpmem(s1, "Foo", 3) == 0); dfree(s1); test_end(); /* dstring* dsub(const dstring* a, dstrlen_t start, dstrlen_t length); */ test_start("dsub"); s1 = dfromcs("hello world!"); s2 = dsub(s1, 0, 5); s3 = dsub(s1, 6, 6); s4 = dsub(s1, 6, 12); s5 = dsub(s1, 20, 10); test_assert(dcmpcs(s2, "hello") == 0); test_assert(dcmpcs(s3, "world!") == 0); test_assert(dcmpcs(s4, "world!") == 0); test_assert(dcmpcs(s5, "") == 0); dfree(s1); dfree(s2); dfree(s3); dfree(s4); dfree(s5); test_end(); /* int dstartswith(const dstring* a, const dstring* b); */ test_start("dstartswith"); s1 = dfromcs("Is this ok?"); s2 = dfromcs("Is"); s3 = dfromcs("bla"); test_assert(dstartswith(s1, s2)); test_assert(!dstartswith(s1, s3)); dfree(s1); dfree(s2); dfree(s3); test_end(); /* int dstartswithc(const dstring* a, int b); */ test_start("dstartswithc"); s1 = dfromcs("Is this ok?"); test_assert(dstartswithc(s1, 'I')); test_assert(!dstartswithc(s1, 'O')); dfree(s1); test_end(); /* int dstartswithcs(const dstring* a, const char* b); */ test_start("dstartswithcs"); s1 = dfromcs("Is this ok?"); test_assert(dstartswithcs(s1, "Is")); test_assert(!dstartswithcs(s1, "is")); dfree(s1); test_end(); /* int dendswith(const dstring* a, const dstring* b); */ test_start("dendswith"); s1 = dfromcs("Is this ok?"); s2 = dfromcs("ok?"); s3 = dfromcs("Ok?"); test_assert(dendswith(s1, s2)); test_assert(!dendswith(s1, s3)); dfree(s1); dfree(s2); dfree(s3); test_end(); /* int dendswithc(const dstring* a, int b); */ test_start("dendswithc"); s1 = dfromcs("Is this ok?"); test_assert(dendswithc(s1, '?')); test_assert(!dendswithc(s1, '!')); dfree(s1); test_end(); /* int dendswithcs(const dstring* a, const char* b); */ test_start("dendswithcs"); s1 = dfromcs("Is this ok?"); test_assert(dendswithcs(s1, "ok?")); test_assert(!dendswithcs(s1, "ok!")); dfree(s1); test_end(); /* int dpos(const dstring* a, const dstring* b, dstrlen_t start); */ test_start("dpos"); s1 = dfromcs("this is a good place"); s2 = dfromcs("is"); s3 = dfromcs("bla"); test_assert(dpos(s1, s2, 0) == 2); test_assert(dpos(s1, s2, 4) == 5); test_assert(dpos(s1, s2, 7) == -1); test_assert(dpos(s1, s3, 44) == -1); dfree(s1); dfree(s2); dfree(s3); test_end(); /* int dposc(const dstring* a, int b, dstrlen_t start); */ test_start("dposc"); s1 = dfromcs("this is a good place"); test_assert(dposc(s1, 'i', 0) == 2); test_assert(dposc(s1, 'i', 4) == 5); test_assert(dposc(s1, 'j', 7) == -1); test_assert(dposc(s1, 'x', 44) == -1); dfree(s1); test_end(); /* int dposcs(const dstring* a, const char* b, dstrlen_t start); */ test_start("dposcs"); s1 = dfromcs("this is a good place"); test_assert(dposcs(s1, "is", 0) == 2); test_assert(dposcs(s1, "is", 4) == 5); test_assert(dposcs(s1, "is", 7) == -1); test_assert(dposcs(s1, "bla", 44) == -1); dfree(s1); test_end(); return 0; }
/** Find vibration peaks in the PSD by comparing the PSD against a LPF version plus noise. */ dmat *psd_vibid(const dmat *psdin){ double *f=psdin->p; double *psd=psdin->p+psdin->nx; dmat *y=dsub(psdin, 0, 0, 1, 1); const double gain=0.1; const double gain2=0.05; int inpeak=0; double ylpf0=y->p[1]; double dylpf0=fabs(y->p[1]-y->p[0]); double ylpf=0, dylpf=0; int nmaxp=100; dmat *res=dnew(4, nmaxp); double thres=25e-18;/*threshold: 5 nm*/ double sumxy=0, sumy=0, sum=0; int count=0; for(long i=1; i<psdin->nx-1; i++){ if(!inpeak){ //second order LPF ylpf0=(1.-gain)*ylpf0+y->p[i]*gain; ylpf=(1.-gain)*ylpf+ylpf0*gain; double diff=y->p[i]-y->p[i-1]; if(diff>0){ dylpf0=(1.-gain2)*dylpf0+diff*gain2; dylpf=(1.-gain2)*dylpf+dylpf0*gain2; } if(y->p[i+1]>ylpf+dylpf*5 && f[i]>1){//beginning of peak inpeak=1; if(count>0 && f[i] < f[(int)IND(res,3,count-1)] + 0.1){ //combine with last peak if within 1 Hz. count--; }else{ IND(res,2,count)=i; sumxy=f[i]*psd[i];//for CoG sumy=psd[i];//for CoG sum=0;//integration } } }else{ //continuation of peak sumxy+=f[i]*psd[i]; sumy+=psd[i]; sum+=(f[i]-f[i-1])*(psd[i]+psd[i-1]); if(y->p[i]<ylpf+dylpf && y->p[i+1]<ylpf+dylpf){//end of peak inpeak=0; if(sum*0.5>thres){ IND(res,0,count)=sumxy/sumy; IND(res,1,count)=sum*0.5; IND(res,3,count)=i; count++; if(count==nmaxp){ nmaxp*=2; dresize(res, 4, nmaxp); } } } } } dfree(y); dresize(res, 4, count); return res; }