void icfft2(sf_complex *out /* [n1*n2] */, sf_complex *inp /* [nkk*n2] */) /*< 2-D inverse FFT >*/ { int i1, i2; #ifdef SF_HAS_FFTW fftwf_execute(icfg); #else for (i1=0; i1 < nkk; i1++) { kiss_fft_stride(icfg2,(kiss_fft_cpx *) (inp+i1),ctrace2,nkk); for (i2=0; i2<n2; i2++) { temp[i2][i1] = ctrace2[i2]; } } for (i2=0; i2 < n2; i2++) { kiss_fft_stride(icfg1,temp[i2],(kiss_fft_cpx *) cc[i2],1); } #endif /* FFT centering and normalization*/ for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { #ifdef SF_HAS_COMPLEX_H out[i2*n1+i1] = (((i2%2==0)==(i1%2==0))? wt:-wt) * cc[i2][i1]; #else out[i2*n1+i1] = sf_crmul(cc[i2][i1],(((i2%2==0)==(i1%2==0))? wt:-wt)); #endif } } }
void ifft3(float *out /* [n1*n2*n3] */, sf_complex *inp /* [nk*n2*n3] */) /*< 3-D inverse FFT >*/ { int i1, i2, i3; #ifdef SF_HAS_FFTW fftwf_execute(icfg); #else /* IFFT over third axis */ for (i2=0; i2 < n2; i2++) { for (i1=0; i1 < nk; i1++) { kiss_fft_stride(icfg3,(kiss_fft_cpx *) (inp+i2*nk+i1),ctrace3,nk*n2); for (i3=0; i3<n3; i3++) { tmp[i3][i2][i1] = ctrace3[i3]; } } } /* IFFT over second axis */ for (i3=0; i3 < n3; i3++) { for (i1=0; i1 < nk; i1++) { kiss_fft_stride(icfg2,tmp[i3][0]+i1,ctrace2,nk); for (i2=0; i2<n2; i2++) { tmp[i3][i2][i1] = ctrace2[i2]; } } } /* IFFT over first axis */ for (i3=0; i3 < n3; i3++) { for (i2=0; i2 < n2; i2++) { if (cmplx) { kiss_fft_stride(icfg1,tmp[i3][i2],(kiss_fft_cpx *) cc[i3][i2],1); } else { kiss_fftri(icfg,tmp[i3][i2],ff[i3][i2]); } } } #endif /* FFT centering and normalization */ for (i3=0; i3<n3; i3++) { for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { if (cmplx) { out[(i3*n2+i2)*n1+i1] = ((((i3%2==0)==(i2%2==0))==(i1%2==0))? wt:-wt)*crealf(cc[i3][i2][i1]); } else { out[(i3*n2+i2)*n1+i1] = (((i3%2==0)==(i2%2==0))? wt: - wt)*ff[i3][i2][i1]; } } } } }
void cfft2(sf_complex *inp /* [n1*local_n0] */, sf_complex *out /* [nk*local_n0] */) /*< 2-D FFT >*/ { int i1, i2; int ith=0; /* FFT centering */ #pragma omp parallel for private(i2,i1) default(shared) for (i2=0; i2<local_n0; i2++) { for (i1=0; i1<n1; i1++) { #ifdef SF_HAS_COMPLEX_H cc[i2][i1] = (((i2+local_0_start)%2==0)==(i1%2==0))? inp[i2*n1+i1]:-inp[i2*n1+i1]; #else cc[i2][i1] = (((i2+local_0_start)%2==0)==(i1%2==0))? inp[i2*n1+i1]:sf_cneg(inp[i2*n1+i1]); #endif } } #ifdef _OPENMP #pragma omp parallel for private(i2,ith) default(shared) #endif for (i2=0; i2 < local_n0; i2++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif kiss_fft_stride(cfg1[ith],(kiss_fft_cpx *) cc[i2],tmp+i2*nk,1); } fftwf_execute(cfg); #ifdef _OPENMP #pragma omp parallel for private(i1,i2,ith) default(shared) #endif for (i1=0; i1 < local_n1; i1++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif kiss_fft_stride(cfg2[ith],tmp+i1*n2,ctrace2[ith],1); for (i2=0; i2<n2; i2++) { tmp[i1*n2+i2] = ctrace2[ith][i2]; } } fftwf_execute(icfg); #ifdef _OPENMP #pragma omp parallel for private(i1,i2) default(shared) #endif for (i2=0; i2<local_n0; i2++) { for (i1=0; i1<nk; i1++) { out[i2*nk+i1] = tmp2[i2*nk+i1]; } } }
void cfft2(sf_complex *inp /* [n1*n2] */, sf_complex *out /* [nk*n2] */) /*< 2-D FFT >*/ { int i1, i2; #ifdef SF_HAS_FFTW if (NULL==cfg) { cfg = fftwf_plan_dft_2d(n2,n1, (fftwf_complex *) cc[0], (fftwf_complex *) dd[0], FFTW_FORWARD, FFTW_MEASURE); if (NULL == cfg) sf_error("FFTW failure."); } #endif /* FFT centering */ for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { #ifdef SF_HAS_COMPLEX_H cc[i2][i1] = ((i2%2==0)==(i1%2==0))? inp[i2*n1+i1]:-inp[i2*n1+i1]; #else cc[i2][i1] = ((i2%2==0)==(i1%2==0))? inp[i2*n1+i1]:sf_cneg(inp[i2*n1+i1]); #endif /* #ifdef SF_HAS_COMPLEX_H cc[i2][i1] = ((i2%2==0)==(i1%2==0))? inp[i2*n1+i1]:(-1*inp[i2*n1+i1]); #else cc[i2][i1] = ((i2%2==0)==(i1%2==0))? inp[i2*n1+i1]:sf_cneg(inp[i2*n1+i1]); #endif */ } } #ifdef SF_HAS_FFTW fftwf_execute(cfg); for (i2=0; i2<n2; i2++) { for (i1=0; i1<nk; i1++) { out[i2*nk+i1]=dd[i2][i1]; } } #else for (i2=0; i2 < n2; i2++) { kiss_fft_stride(cfg1,(kiss_fft_cpx *) cc[i2],tmp[i2],1); } for (i1=0; i1 < nk; i1++) { kiss_fft_stride(cfg2,tmp[0]+i1,ctrace2,nk); for (i2=0; i2<n2; i2++) { out[i2*nk+i1] = trace2[i2]; } } #endif }
void fft2(float *inp /* [n1*n2] */, sf_complex *out /* [nk*n2] */) /*< 2-D FFT >*/ { int i1, i2; #ifdef SF_HAS_FFTW if (NULL==cfg) { cfg = cmplx? fftwf_plan_dft_2d(n2,n1, (fftwf_complex *) cc[0], (fftwf_complex *) dd, FFTW_FORWARD, FFTW_MEASURE): fftwf_plan_dft_r2c_2d(n2,n1, ff[0], (fftwf_complex *) dd, FFTW_MEASURE); if (NULL == cfg) sf_error("FFTW failure."); } #endif /* FFT centering */ for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { if (cmplx) { cc[i2][i1] = sf_cmplx(((i2%2==0)==(i1%2==0))? inp[i2*n1+i1]:-inp[i2*n1+i1],0.); } else { ff[i2][i1] = (i2%2)? -inp[i2*n1+i1]:inp[i2*n1+i1]; } } } #ifdef SF_HAS_FFTW fftwf_execute(cfg); for (i1=0; i1 < nk*n2; i1++) out[i1] = dd[i1]; #else for (i2=0; i2 < n2; i2++) { if (cmplx) { kiss_fft_stride(cfg1,(kiss_fft_cpx *) cc[i2],tmp[i2],1); } else { kiss_fftr (cfg,ff[i2],tmp[i2]); } } for (i1=0; i1 < nk; i1++) { kiss_fft_stride(cfg2,tmp[0]+i1,ctrace2,nk); for (i2=0; i2<n2; i2++) { out[i2*nk+i1] = trace2[i2]; } } #endif }
void ifft2(float *out /* [n1*n2] */, sf_complex *inp /* [nk*n2] */) /*< 2-D inverse FFT >*/ { int i1, i2; #ifdef SF_HAS_FFTW if (NULL==icfg) { icfg = cmplx? fftwf_plan_dft_2d(n2,n1, (fftwf_complex *) dd, (fftwf_complex *) cc[0], FFTW_BACKWARD, FFTW_MEASURE): fftwf_plan_dft_c2r_2d(n2,n1, (fftwf_complex *) dd, ff[0], FFTW_MEASURE); if (NULL == icfg) sf_error("FFTW failure."); } #endif #ifdef SF_HAS_FFTW for (i1=0; i1 < nk*n2; i1++) dd[i1] = inp[i1]; fftwf_execute(icfg); #else for (i1=0; i1 < nk; i1++) { kiss_fft_stride(icfg2,(kiss_fft_cpx *) (inp+i1),ctrace2,nk); for (i2=0; i2<n2; i2++) { tmp[i2][i1] = ctrace2[i2]; } } for (i2=0; i2 < n2; i2++) { if (cmplx) { kiss_fft_stride(icfg1,tmp[i2],(kiss_fft_cpx *) cc[i2],1); } else { kiss_fftri(icfg,tmp[i2],ff[i2]); } } #endif /* FFT centering and normalization */ for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { if (cmplx) { out[i2*n1+i1] = (((i2%2==0)==(i1%2==0))? wt:-wt) * crealf(cc[i2][i1]); } else { out[i2*n1+i1] = (i2%2? -wt: wt)*ff[i2][i1]; } } } }
/*------------------------------------------------------------*/ void fft2(bool inv /* inverse/forward flag */, kiss_fft_cpx **pp /* [1...n2][1...n1] */) /*< Apply 2-D FFT >*/ { int i1,i2; if (inv) { /* IFT 1 */ for(i2=0; i2 < n2; i2++) { kiss_fft(invs1,pp[i2],pp[i2]); } /* IFT 2 */ for(i1=0; i1 < n1; i1++) { kiss_fft_stride(invs2,pp[0]+i1,trace2,n1); for(i2=0; i2<n2; i2++) { pp[i2][i1] = trace2[i2]; } } /* scaling */ for (i2=0; i2<n2; i2++) { for(i1=0; i1 < n1; i1++) { pp[i2][i1] = sf_crmul(pp[i2][i1],fftscale); } } } else { /* scaling */ for (i2=0; i2<n2; i2++) { for(i1=0; i1 < n1; i1++) { pp[i2][i1] = sf_crmul(pp[i2][i1],fftscale); } } /* FFT 2 */ for(i1=0; i1 < n1; i1++) { kiss_fft_stride(forw2,pp[0]+i1,trace2,n1); for(i2=0; i2<n2; i2++) { pp[i2][i1] = trace2[i2]; } } /* FFT 1 */ for(i2=0; i2 < n2; i2++) { kiss_fft(forw1,pp[i2],pp[i2]); } } }
/*------------------------------------------------------------*/ void sf_fft3a3(bool inv /* inverse/forward flag */, kiss_fft_cpx ***pp /* [n1][n2][n3] */, sf_fft3d fft) /*< apply FFT on axis 3 >*/ { int i1, i2, i3; if (inv) { /* IFT 3 */ for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { kiss_fft_stride(fft->invs,pp[0][0]+i1+i2*fft->n1,fft->trace,fft->n1*fft->n2); for(i3=0; i3 < fft->n3; i3++) { pp[i3][i2][i1] = fft->trace[i3]; } } } /* scaling */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { pp[i3][i2][i1] = sf_crmul(pp[i3][i2][i1],fft->scale); } } } } else { /* scaling */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { pp[i3][i2][i1] = sf_crmul(pp[i3][i2][i1],fft->scale); } } } /* FFT 3 */ for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { kiss_fft_stride(fft->forw,pp[0][0]+i1+i2*fft->n1,fft->trace,fft->n1*fft->n2); for(i3=0; i3 < fft->n3; i3++) { pp[i3][i2][i1] = fft->trace[i3]; } } } } }
void freqfilt4pi_spec (const float* x /* input */, float** y /* spectrum */) /*< compute 2-D spectrum >*/ { int ik, iw; for (ik=0; ik < m2; ik++) { for (iw=0; iw < m1; iw++) { trace[iw] = x[ik*m1+iw]; } for (iw=m1; iw < nfft; iw++) { trace[iw]=0.; } kiss_fftr (tfor,trace,ctrace); for (iw=0; iw < nw; iw++) { fft[ik][iw] = ik%2? sf_cneg(ctrace[iw]): ctrace[iw]; } } for (iw=0; iw < nw; iw++) { kiss_fft_stride(xfor,fft[0]+iw,ctrace2,nw); for (ik=0; ik < m2; ik++) { y[iw][ik] = sf_cabsf(ctrace2[ik]); /* transpose */ } } }
/* This works by tackling one dimension at a time. In effect, Each stage starts out by reshaping the matrix into a DixSi 2d matrix. A Di-sized fft is taken of each column, transposing the matrix as it goes. Here's a 3-d example: Take a 2x3x4 matrix, laid out in memory as a contiguous buffer [ [ [ a b c d ] [ e f g h ] [ i j k l ] ] [ [ m n o p ] [ q r s t ] [ u v w x ] ] ] Stage 0 ( D=2): treat the buffer as a 2x12 matrix [ [a b ... k l] [m n ... w x] ] FFT each column with size 2. Transpose the matrix at the same time using kiss_fft_stride. [ [ a+m a-m ] [ b+n b-n] ... [ k+w k-w ] [ l+x l-x ] ] Note fft([x y]) == [x+y x-y] Stage 1 ( D=3) treats the buffer (the output of stage D=2) as an 3x8 matrix, [ [ a+m a-m b+n b-n c+o c-o d+p d-p ] [ e+q e-q f+r f-r g+s g-s h+t h-t ] [ i+u i-u j+v j-v k+w k-w l+x l-x ] ] And perform FFTs (size=3) on each of the columns as above, transposing the matrix as it goes. The output of stage 1 is (Legend: ap = [ a+m e+q i+u ] am = [ a-m e-q i-u ] ) [ [ sum(ap) fft(ap)[0] fft(ap)[1] ] [ sum(am) fft(am)[0] fft(am)[1] ] [ sum(bp) fft(bp)[0] fft(bp)[1] ] [ sum(bm) fft(bm)[0] fft(bm)[1] ] [ sum(cp) fft(cp)[0] fft(cp)[1] ] [ sum(cm) fft(cm)[0] fft(cm)[1] ] [ sum(dp) fft(dp)[0] fft(dp)[1] ] [ sum(dm) fft(dm)[0] fft(dm)[1] ] ] Stage 2 ( D=4) treats this buffer as a 4*6 matrix, [ [ sum(ap) fft(ap)[0] fft(ap)[1] sum(am) fft(am)[0] fft(am)[1] ] [ sum(bp) fft(bp)[0] fft(bp)[1] sum(bm) fft(bm)[0] fft(bm)[1] ] [ sum(cp) fft(cp)[0] fft(cp)[1] sum(cm) fft(cm)[0] fft(cm)[1] ] [ sum(dp) fft(dp)[0] fft(dp)[1] sum(dm) fft(dm)[0] fft(dm)[1] ] ] Then FFTs each column, transposing as it goes. The resulting matrix is the 3d FFT of the 2x3x4 input matrix. Note as a sanity check that the first element of the final stage's output (DC term) is sum( [ sum(ap) sum(bp) sum(cp) sum(dp) ] ) , i.e. the summation of all 24 input elements. */ void kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) { int i,k; const kiss_fft_cpx * bufin=fin; kiss_fft_cpx * bufout; /*arrange it so the last bufout == fout*/ if ( st->ndims & 1 ) { bufout = fout; if (fin==fout) { memcpy( st->tmpbuf, fin, sizeof(kiss_fft_cpx) * st->dimprod ); bufin = st->tmpbuf; } }else bufout = st->tmpbuf; for ( k=0; k < st->ndims; ++k) { int curdim = st->dims[k]; int stride = st->dimprod / curdim; for ( i=0 ; i<stride ; ++i ) kiss_fft_stride( st->states[k], bufin+i , bufout+i*curdim, stride ); /*toggle back and forth between the two buffers*/ if (bufout == st->tmpbuf){ bufout = fout; bufin = st->tmpbuf; }else{ bufout = st->tmpbuf; bufin = fout; } } }
void icfft2(sf_complex *out /* [n1*local_n0] */, sf_complex *inp /* [nk*local_n0] */) /*< 2-D inverse FFT >*/ { int i1, i2, ith=0; #ifdef _OPENMP #pragma omp parallel for private(i2,ith) default(shared) #endif for (i2=0; i2 < local_n0; i2++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif kiss_fft_stride(icfg1[ith],(kiss_fft_cpx *) inp+i2*nk,tmp+i2*nk,1); } fftwf_execute(cfg); #ifdef _OPENMP #pragma omp parallel for private(i1,i2,ith) default(shared) #endif for (i1=0; i1 < local_n1; i1++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif kiss_fft_stride(icfg2[ith],tmp+i1*n2,ctrace2[ith],1); for (i2=0; i2<n2; i2++) { tmp[i1*n2+i2] = ctrace2[ith][i2]; } } fftwf_execute(icfg); /* FFT centering and normalization*/ #pragma omp parallel for private(i2,i1) default(shared) for (i2=0; i2<local_n0; i2++) { for (i1=0; i1<n1; i1++) { #ifdef SF_HAS_COMPLEX_H out[i2*n1+i1] = ((((i2+local_0_start)%2==0)==(i1%2==0))? wt:-wt) * tmp2[i2*n1+i1]; #else out[i2*n1+i1] = sf_crmul(tmp2[i2*n1+i1],((((i2+local_0_start)%2==0)==(i1%2==0))? wt:-wt)); #endif } } }
void icfft2(sf_complex *out /* [n1*n2] */, sf_complex *inp /* [nk*n2] */) /*< 2-D inverse FFT >*/ { int i1, i2; #ifdef SF_HAS_FFTW #ifdef _OPENMP #pragma omp parallel for private(i2,i1) default(shared) #endif for (i2=0; i2<n2; i2++) { for (i1=0; i1<nk; i1++) { dd[i2][i1]=inp[i2*nk+i1]; } } fftwf_execute(icfg); #else for (i1=0; i1 < nk; i1++) { kiss_fft_stride(icfg2,(kiss_fft_cpx *) (inp+i1),ctrace2,nk); for (i2=0; i2<n2; i2++) { tmp[i2][i1] = ctrace2[i2]; } } for (i2=0; i2 < n2; i2++) { kiss_fft_stride(icfg1,tmp[i2],(kiss_fft_cpx *) cc[i2],1); } #endif /* FFT centering and normalization*/ #ifdef _OPENMP #pragma omp parallel for private(i2,i1) default(shared) #endif for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { #ifdef SF_HAS_COMPLEX_H out[i2*n1+i1] = (((i2%2==0)==(i1%2==0))? wt:-wt) * cc[i2][i1]; #else out[i2*n1+i1] = sf_crmul(cc[i2][i1],(((i2%2==0)==(i1%2==0))? wt:-wt)); #endif } } }
void cfft2(sf_complex *inp /* [n1*n2] */, sf_complex *out /* [nk*n2] */) /*< 2-D FFT >*/ { int i1, i2; /* FFT centering */ #ifdef _OPENMP #pragma omp parallel for private(i2,i1) default(shared) #endif for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { #ifdef SF_HAS_COMPLEX_H cc[i2][i1] = ((i2%2==0)==(i1%2==0))? inp[i2*n1+i1]:-inp[i2*n1+i1]; #else cc[i2][i1] = ((i2%2==0)==(i1%2==0))? inp[i2*n1+i1]:sf_cneg(inp[i2*n1+i1]); #endif } } #ifdef SF_HAS_FFTW fftwf_execute(cfg); #ifdef _OPENMP #pragma omp parallel for private(i2,i1) default(shared) #endif for (i2=0; i2<n2; i2++) { for (i1=0; i1<nk; i1++) { out[i2*nk+i1]=dd[i2][i1]; } } #else for (i2=0; i2 < n2; i2++) { kiss_fft_stride(cfg1,(kiss_fft_cpx *) cc[i2],tmp[i2],1); } for (i1=0; i1 < nk; i1++) { kiss_fft_stride(cfg2,tmp[0]+i1,ctrace2,nk); for (i2=0; i2<n2; i2++) { out[i2*nk+i1] = trace2[i2]; } } #endif }
void ist (int len /* data size */, float d1 /* data sampling */, int lo /* low frequency */, int hi /* high frequency */, float *result /* output [len] */, sf_complex *data /* input [len*(hi-lo+1)] */) /*< Inverse S transform >*/ { int i, i1, l2, nw; kiss_fft_cpx *d, *pp; kiss_fft_cfg itfft; nw = 2*kiss_fft_next_fast_size((len+1)/2); itfft = kiss_fft_alloc(nw,1,NULL,NULL); pp = (kiss_fft_cpx*) sf_complexalloc(nw); d = (kiss_fft_cpx*) sf_complexalloc(nw); for (i=0; i < nw; i++) { pp[i].r = 0.; pp[i].i = 0.; } for (i1=lo; i1 <= hi; i1++) { for (i=0; i < len; i++) { pp[i1-lo].r += crealf(data[(i1-lo)*len+i]); pp[i1-lo].i += cimagf(data[(i1-lo)*len+i]); } } l2 = (nw+1)/2; for (i=1; i < l2; i++) { pp[i].r /= 2.; pp[i].i /= 2.; } l2 = nw/2+1; for (i=l2; i < nw; i++) { pp[i].r = pp[nw-i].r; pp[i].i = -pp[nw-i].i; } kiss_fft_stride(itfft,pp,d,1); for (i=0; i < len; i++) { result[i] = d[i].r/len; } free(pp); free(d); }
void ifft2(float *out /* [n1*n2] */, sf_complex *inp /* [nk*n2] */) /*< 2-D inverse FFT >*/ { int i1, i2; #ifdef SF_HAS_FFTW fftwf_execute(icfg); #else for (i1=0; i1 < nk; i1++) { kiss_fft_stride(icfg2,(kiss_fft_cpx *) (inp+i1),ctrace2,nk); for (i2=0; i2<n2; i2++) { tmp[i2][i1] = ctrace2[i2]; } } for (i2=0; i2 < n2; i2++) { if (cmplx) { kiss_fft_stride(icfg1,tmp[i2],(kiss_fft_cpx *) cc[i2],1); } else { kiss_fftri(icfg,tmp[i2],ff[i2]); } } #endif /* FFT centering and normalization */ for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { if (cmplx) { out[i2*n1+i1] = (((i2%2==0)==(i1%2==0))? wt:-wt) * crealf(cc[i2][i1]); } else { out[i2*n1+i1] = (i2%2? -wt: wt)*ff[i2][i1]; } } } }
/*------------------------------------------------------------*/ void ompfft2(bool inv /* inverse/forward flag */, kiss_fft_cpx **pp /* [1...n2][1...n1] */, int ompith, fft2d fft) /*< Apply 2-D FFT >*/ { int i1,i2; if (inv) { /* IFT 1 */ for (i2=0; i2 < fft->n2; i2++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft(fft->invs1[ompith],pp[i2],pp[i2]); } /* IFT 2 */ for (i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->invs2[ompith],pp[0]+i1, fft->ctrace[ompith],fft->n1); for (i2=0; i2<fft->n2; i2++) { pp[i2][i1] = fft->ctrace[ompith][i2]; } } /* scaling */ for (i2=0; i2<fft->n2; i2++) { for (i1=0; i1<fft->n1; i1++) { pp[i2][i1] = sf_crmul(pp[i2][i1],fft->fftscale); } } } else { /* scaling */ for (i2=0; i2<fft->n2; i2++) { for (i1=0; i1<fft->n1; i1++) { pp[i2][i1] = sf_crmul(pp[i2][i1],fft->fftscale); } } /* FFT 2 */ for (i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->forw2[ompith],pp[0]+i1, fft->ctrace[ompith],fft->n1); for (i2=0; i2<fft->n2; i2++) { pp[i2][i1] = fft->ctrace[ompith][i2]; } } /* FFT 1 */ for (i2=0; i2 < fft->n2; i2++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft(fft->forw1[ompith],pp[i2],pp[i2]); } } }
void freqfilt4pi_lop (bool adj, bool add, int nx, int ny, float* x, float* y) /*< linear filtering operator >*/ { int iw, ik; kiss_fft_cpx temp; int verb; // just outputting values to check when I get zeroes sf_adjnull(adj,add,nx,ny,x,y); for (ik=0; ik < m2; ik++) { for (iw=0; iw < m1; iw++) { trace[iw] = adj? y[ik*m1+iw]: x[ik*m1+iw]; /*if(trace[iw] == 0.0){ sf_warning("trace[%d] = %f",iw,trace[iw]); }*/ } for (iw=m1; iw < nfft; iw++) { trace[iw]=0.; } kiss_fftr (tfor,trace,ctrace); for (iw=0; iw < nw; iw++) { fft[ik][iw] = ik%2? sf_cneg(ctrace[iw]): ctrace[iw]; } } for (iw=0; iw < nw; iw++) { kiss_fft_stride(xfor,fft[0]+iw,ctrace2,nw); for (ik=0; ik < m2; ik++) { //double creal( double complex z ); //transform to kiss fft cpx //creal are double complex functions - what should we //do when double complex is not supported??? if (adj){ temp.r = creal(shape[iw][ik]); temp.i = (-1.0)*cimag(shape[iw][ik]); } else { temp.r = creal(shape[iw][ik]); temp.i = cimag(shape[iw][ik]); } ctrace2[ik] = sf_cmul(ctrace2[ik],temp); } kiss_fft(xinv,ctrace2,ctrace2); for (ik=0; ik < m2; ik++) { fft[ik][iw] = ik%2? sf_cneg(ctrace2[ik]): ctrace2[ik]; } } for (ik=0; ik < m2; ik++) { kiss_fftri (tinv,fft[ik],trace); for (iw=0; iw < m1; iw++) { if (adj) { x[ik*m1+iw] += trace[iw]; } else { y[ik*m1+iw] += trace[iw]; } } } }
void fft3(float *inp /* [n1*n2*n3] */, sf_complex *out /* [nk*n2*n3] */) /*< 3-D FFT >*/ { int i1, i2, i3; float f; #ifdef SF_HAS_FFTW if (NULL==cfg) { cfg = cmplx? fftwf_plan_dft_3d(n3,n2,n1, (fftwf_complex *) cc[0][0], (fftwf_complex *) out, FFTW_FORWARD, FFTW_MEASURE): fftwf_plan_dft_r2c_3d(n3,n2,n1, ff[0][0], (fftwf_complex *) out, FFTW_MEASURE); if (NULL == cfg) sf_error("FFTW failure."); } #endif /* FFT centering */ for (i3=0; i3<n3; i3++) { for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { f = inp[(i3*n2+i2)*n1+i1]; if (cmplx) { cc[i3][i2][i1] = sf_cmplx((((i3%2==0)==(i2%2==0))==(i1%2==0))? f:-f,0.); } else { ff[i3][i2][i1] = ((i3%2==0)==(i2%2==0))? f:-f; } } } } #ifdef SF_HAS_FFTW fftwf_execute(cfg); #else /* FFT over first axis */ for (i3=0; i3 < n3; i3++) { for (i2=0; i2 < n2; i2++) { if (cmplx) { kiss_fft_stride(cfg1,(kiss_fft_cpx *) cc[i3][i2],tmp[i3][i2],1); } else { kiss_fftr (cfg,ff[i3][i2],tmp[i3][i2]); } } } /* FFT over second axis */ for (i3=0; i3 < n3; i3++) { for (i1=0; i1 < nk; i1++) { kiss_fft_stride(cfg2,tmp[i3][0]+i1,ctrace2,nk); for (i2=0; i2 < n2; i2++) { tmp[i3][i2][i1]=ctrace2[i2]; } } } /* FFT over third axis */ for (i2=0; i2 < n2; i2++) { for (i1=0; i1 < nk; i1++) { kiss_fft_stride(cfg3,tmp[0][0]+i2*nk+i1,ctrace3,nk*n2); for (i3=0; i3<n3; i3++) { out[(i3*n2+i2)*nk+i1] = trace3[i3]; } } } #endif }
/*------------------------------------------------------------*/ void wexfft(bool inv /* inverse/forward flag */, kiss_fft_cpx **pp /* [1...n2][1...n1] */, wexfft2d fft) /*< apply 2-D FFT >*/ { int i1,i2; if (inv) { /* IFT 1 */ for (i2=0; i2 < fft->n2; i2++) { kiss_fft_stride(fft->invs1, pp[i2], fft->ctmp1, 1); for (i1=0; i1<fft->n1; i1++) { pp[i2][i1] = fft->ctmp1[i1]; } } /* IFT 2 */ for (i1=0; i1 < fft->n1; i1++) { kiss_fft_stride(fft->invs2, pp[0]+i1, fft->ctmp2, fft->n1); for (i2=0; i2<fft->n2; i2++) { pp[i2][i1] = fft->ctmp2[i2]; } } /* scaling */ for (i2=0; i2<fft->n2; i2++) { for (i1=0; i1<fft->n1; i1++) { pp[i2][i1] = sf_crmul(pp[i2][i1],fft->fftscale); } } } else { /* scaling */ for (i2=0; i2<fft->n2; i2++) { for (i1=0; i1<fft->n1; i1++) { pp[i2][i1] = sf_crmul(pp[i2][i1],fft->fftscale); } } /* FFT 2 */ for (i1=0; i1 < fft->n1; i1++) { kiss_fft_stride(fft->forw2, pp[0]+i1, fft->ctmp2, fft->n1); for (i2=0; i2<fft->n2; i2++) { pp[i2][i1] = fft->ctmp2[i2]; } } /* FFT 1 */ for (i2=0; i2 < fft->n2; i2++) { kiss_fft_stride(fft->forw1, pp[i2], fft->ctmp1, 1); for (i1=0; i1<fft->n1; i1++) { pp[i2][i1] = fft->ctmp1[i1]; } } } }
/*------------------------------------------------------------*/ void sf_ompfft3a3(bool inv /* inverse/forward flag */, kiss_fft_cpx ***pp /* [n1][n2][n3] */, ompfft3d fft, int ompith) /*< apply FFT on axis 3 >*/ { int i1, i2, i3; if (inv) { /* IFT 3 */ for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->invs[ompith], pp[0][0]+i1+i2*fft->n1, fft->trace[ompith],fft->n1*fft->n2); for(i3=0; i3 < fft->n3; i3++) { pp[i3][i2][i1] = fft->trace[ompith][i3]; } } } /* scaling */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { pp[i3][i2][i1] = sf_crmul(pp[i3][i2][i1],fft->scale); } } } } else { /* scaling */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { pp[i3][i2][i1] = sf_crmul(pp[i3][i2][i1],fft->scale); } } } /* FFT 3 */ for (i2=0; i2 < fft->n2; i2++) { for(i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->forw[ompith], pp[0][0]+i1+i2*fft->n1, fft->trace[ompith],fft->n1*fft->n2); for(i3=0; i3 < fft->n3; i3++) { pp[i3][i2][i1] = fft->trace[ompith][i3]; } } } } }
void imcfft3(float *out /* [n1*n2*n3] */, sf_complex *inp /* [nk*n2*n3] */) /*< 3-D inverse FFT >*/ { int i1, i2, i3, ith=0; /* FFT over first axis */ #ifdef _OPENMP #pragma omp parallel for private(i3,i2,ith) default(shared) #endif for (i3=0; i3 < local_n0; i3++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif for (i2=0; i2 < n2; i2++) { kiss_fft_stride(icfg1[ith],(kiss_fft_cpx *) inp+(i3*n2+i2)*nk,tmp+(i3*n2+i2)*nk,1); } } /* FFT over second axis */ #ifdef _OPENMP #pragma omp parallel for private(i3,i2,i1,ith) default(shared) #endif for (i3=0; i3 < local_n0; i3++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif for (i1=0; i1 < nk; i1++) { kiss_fft_stride(icfg2[ith],tmp+i3*n2*nk+i1,ctrace2[ith],nk); for (i2=0; i2 < n2; i2++) { tmp[(i3*n2+i2)*nk+i1]=ctrace2[ith][i2]; } } } /* parallel transpose from n1*n2 * n3 to n3 * n1*n2 */ fftwf_execute(cfg); /* FFT over third axis */ #ifdef _OPENMP #pragma omp parallel for private(i3,i1,ith) default(shared) #endif for (i1=0; i1 < local_n1; i1++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif kiss_fft_stride(icfg3[ith],tmp+i1*n3,ctrace3[ith],1); for (i3=0; i3<n3; i3++) { tmp[i1*n3+i3] = ctrace3[ith][i3]; } } fftwf_execute(icfg); /* FFT centering and normalization*/ #pragma omp parallel for private(i3,i2,i1) default(shared) for (i3=0; i3<local_n0; i3++) { for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { out[(i3*n2+i2)*n1+i1] = (((((i3+local_0_start)%2==0)==(i2%2==0))==(i1%2==0))? wt:-wt)*crealf(tmp2[(i3*n2+i2)*n1+i1]); } } } }
int main(int argc, char* argv[]) { int nx, nt, nkx, nkz, ix, it, ikx, ikz, nz, iz, nbt, nbb, nbl, nbr, nxb, nzb, isx, isz; float dt, dx, dkx, kx, dz, dkz, kz, tmpdt, pi=SF_PI, o1, o2, kx0, kz0; float **nxt, **old, **cur, **ukr, **dercur, **derold, *wav; float **vx, vx2, vx0, vx02, **vz, vz2, vz0, vz02, **yi, yi0, **se, se0; float ***aa, dx2, dz2, dx4, dz4, ct, cb, cl, cr; //top, bottom, left, right float w1, w10, w2, w20, w3, w30, h1, h10, h2, h20, h3, h30; float cosg, cosg0, cosg2, cosg02, sing, sing0, sing2, sing02; float vk, vk2, tmpvk, k2, err, dt2, kx1, kz1; kiss_fft_cpx **uk, *ctracex, *ctracez; kiss_fft_cfg cfgx, cfgxi, cfgz, cfgzi; sf_file out, velx, velz, source, yita, seta; bool opt; /* optimal padding */ // #ifdef _OPENMP // int nth; // #endif sf_init(argc,argv); out = sf_output("out"); velx = sf_input("velx"); /* velocity */ velz = sf_input("velz"); /* velocity */ yita = sf_input("yita"); /* anistropic parameter*/ source = sf_input("in"); /* source wavlet*/ seta = sf_input("seta"); /* TTI angle*/ // if (SF_FLOAT != sf_gettype(inp)) sf_error("Need float input"); if (SF_FLOAT != sf_gettype(velx)) sf_error("Need float input"); if (SF_FLOAT != sf_gettype(velz)) sf_error("Need float input"); if (SF_FLOAT != sf_gettype(source)) sf_error("Need float input"); if (SF_FLOAT != sf_gettype(seta)) sf_error("Need float input"); if (!sf_histint(velx,"n1",&nx)) sf_error("No n1= in input"); if (!sf_histfloat(velx,"d1",&dx)) sf_error("No d1= in input"); if (!sf_histint(velx,"n2",&nz)) sf_error("No n2= in input"); if (!sf_histfloat(velx,"d2",&dz)) sf_error("No d2= in input"); if (!sf_histfloat(velx,"o1",&o1)) o1=0.0; if (!sf_histfloat(velx,"o2",&o2)) o2=0.0; // if (!sf_histint(inp,"n2",&nt)) sf_error("No n2= in input"); // if (!sf_histfloat(inp,"d2",&dt)) sf_error("No d2= in input"); if (!sf_getbool("opt",&opt)) opt=true; /* if y, determine optimal size for efficiency */ if (!sf_getfloat("dt",&dt)) sf_error("Need dt input"); if (!sf_getint("nt",&nt)) sf_error("Need nt input"); if (!sf_getint("isx",&isx)) sf_error("Need isx input"); if (!sf_getint("isz",&isz)) sf_error("Need isz input"); if (!sf_getfloat("err",&err)) err = 0.0001; if (!sf_getint("nbt",&nbt)) nbt=44; if (!sf_getint("nbb",&nbb)) nbb=44; if (!sf_getint("nbl",&nbl)) nbl=44; if (!sf_getint("nbr",&nbr)) nbr=44; if (!sf_getfloat("ct",&ct)) ct = 0.01; /*decaying parameter*/ if (!sf_getfloat("cb",&cb)) cb = 0.01; /*decaying parameter*/ if (!sf_getfloat("cl",&cl)) cl = 0.01; /*decaying parameter*/ if (!sf_getfloat("cr",&cr)) cr = 0.01; /*decaying parameter*/ sf_putint(out,"n1",nx); sf_putfloat(out,"d1",dx); // sf_putfloat(out,"o1",x0); sf_putint(out,"n2",nz); sf_putfloat(out,"d2",dz); sf_putint(out,"n3",nt); sf_putfloat(out,"d3",dt); sf_putfloat(out,"o1",o1); sf_putfloat(out,"o2",o2); sf_putfloat(out,"o3",0.0); nxb = nx + nbl + nbr; nzb = nz + nbt + nbb; nkx = opt? kiss_fft_next_fast_size(nxb): nxb; nkz = opt? kiss_fft_next_fast_size(nzb): nzb; if (nkx != nxb) sf_warning("nkx padded to %d",nkx); if (nkz != nzb) sf_warning("nkz padded to %d",nkz); dkx = 1./(nkx*dx); kx0 = -0.5/dx; dkz = 1./(nkz*dz); kz0 = -0.5/dz; cfgx = kiss_fft_alloc(nkx,0,NULL,NULL); cfgxi = kiss_fft_alloc(nkx,1,NULL,NULL); cfgz = kiss_fft_alloc(nkz,0,NULL,NULL); cfgzi = kiss_fft_alloc(nkz,1,NULL,NULL); uk = (kiss_fft_cpx **) sf_complexalloc2(nkx,nkz); ctracex = (kiss_fft_cpx *) sf_complexalloc(nkx); ctracez = (kiss_fft_cpx *) sf_complexalloc(nkz); wav = sf_floatalloc(nt); sf_floatread(wav,nt,source); old = sf_floatalloc2(nxb,nzb); cur = sf_floatalloc2(nxb,nzb); nxt = sf_floatalloc2(nxb,nzb); ukr = sf_floatalloc2(nxb,nzb); derold = sf_floatalloc2(nxb,nzb); dercur = sf_floatalloc2(nxb,nzb); aa = sf_floatalloc3(6,nxb,nzb); bd_init(nx,nz,nbt,nbb,nbl,nbr,ct,cb,cl,cr); vx = sf_floatalloc2(nxb,nzb); vz = sf_floatalloc2(nxb,nzb); /*input & extend velocity model*/ for (iz=nbt; iz<nz+nbt; iz++){ sf_floatread(vx[iz]+nbl,nx,velx); sf_floatread(vz[iz]+nbl,nx,velz); for (ix=0; ix<nbl; ix++){ vx[iz][ix] = vx[iz][nbl]; vz[iz][ix] = vz[iz][nbl]; } for (ix=0; ix<nbr; ix++){ vx[iz][nx+nbl+ix] = vx[iz][nx+nbl-1]; vz[iz][nx+nbl+ix] = vz[iz][nx+nbl-1]; } } for (iz=0; iz<nbt; iz++){ for (ix=0; ix<nxb; ix++){ vx[iz][ix] = vx[nbt][ix]; vz[iz][ix] = vz[nbt][ix]; } } for (iz=0; iz<nbb; iz++){ for (ix=0; ix<nxb; ix++){ vx[nz+nbt+iz][ix] = vx[nz+nbt-1][ix]; vz[nz+nbt+iz][ix] = vz[nz+nbt-1][ix]; } } vx0 =0.0; vz0 =0.0; for (iz=0; iz < nzb; iz++) { for (ix=0; ix < nxb; ix++) { vx0 += vx[iz][ix]*vx[iz][ix]; vz0 += vz[iz][ix]*vz[iz][ix]; } } vx0 = sqrtf(vx0/(nxb*nzb)); vz0 = sqrtf(vz0/(nxb*nzb)); vx02=vx0*vx0; vz02=vz0*vz0; /*input & extend anistropic model*/ yi = sf_floatalloc2(nxb,nzb); for (iz=nbt; iz<nz+nbt; iz++){ sf_floatread(yi[iz]+nbl,nx,yita); for (ix=0; ix<nbl; ix++){ yi[iz][ix] = yi[iz][nbl]; } for (ix=0; ix<nbr; ix++){ yi[iz][nx+nbl+ix] = yi[iz][nx+nbl-1]; } } for (iz=0; iz<nbt; iz++){ for (ix=0; ix<nxb; ix++){ yi[iz][ix] = yi[nbt][ix]; } } for (iz=0; iz<nbb; iz++){ for (ix=0; ix<nxb; ix++){ yi[nz+nbt+iz][ix] = yi[nz+nbt-1][ix]; } } yi0 = 0.0; for (iz=0; iz < nzb; iz++) { for (ix=0; ix < nxb; ix++) { yi0+= yi[iz][ix]*yi[iz][ix]; } } yi0 = sqrtf(yi0/(nxb*nzb)); se = sf_floatalloc2(nxb,nzb); for (iz=nbt; iz<nz+nbt; iz++){ sf_floatread(se[iz]+nbl,nx,seta); for (ix=0; ix<nbl; ix++){ se[iz][ix] = se[iz][nbl]; } for (ix=0; ix<nbr; ix++){ se[iz][nx+nbl+ix] = se[iz][nx+nbl-1]; } } for (iz=0; iz<nbt; iz++){ for (ix=0; ix<nxb; ix++){ se[iz][ix] = se[nbt][ix]; } } for (iz=0; iz<nbb; iz++){ for (ix=0; ix<nxb; ix++){ se[nz+nbt+iz][ix] = se[nz+nbt-1][ix]; } } se0 = 0.0; for (iz=0; iz < nzb; iz++) { for (ix=0; ix < nxb; ix++) { se0+= se[iz][ix]; } } se0 /= (nxb*nzb); se0 *= pi/180.0; for (iz=0; iz < nzb; iz++) { for (ix=0; ix < nxb; ix++) { se[iz][ix] *= pi/180.0; } } cosg0 = cosf(se0); cosg02 = cosg0*cosg0; sing0 = sinf(se0); sing02 = sing0*sing0; w10 = vx02*cosg02+vz02*sing02; w20 = vz02*cosg02+vx02*sing02; w30 = vx02+vz02+(vx02-vz02)*sinf(2.0*se0); h10 = sqrtf(-8.0*yi0*vx02*vz02*cosg02*sing02/(1.0+2.0*yi0)+w10*w10); h20 = sqrtf(-8.0*yi0*vx02*vz02*cosg02*sing02/(1.0+2.0*yi0)+w20*w20); h30 = sqrtf(-2.0*yi0*vx02*vz02*cosf(2.0*se0)*cosf(2.0*se0)/(1.0+2.0*yi0)+0.25*w30*w30); dt2 = dt*dt; dx2 = dx*dx; dx4 = dx2*dx2; dz2 = dz*dz; dz4 = dz2*dz2; for (iz=0; iz < nzb; iz++){ for (ix=0; ix < nxb; ix++) { vx2 = vx[iz][ix]*vx[iz][ix]; vz2 = vz[iz][ix]*vz[iz][ix]; cosg = cosf(se[iz][ix]); sing = sinf(se[iz][ix]); cosg2 = cosg*cosg; sing2 = sing*sing; w1 = vx2*cosg2+vz2*sing2; w2 = vz2*cosg2+vx2*sing2; w3 = vx2+vz2+(vx2-vz2)*sinf(2.0*se[iz][ix]); h1 = sqrtf(-8.0*yi[iz][ix]*vx2*vz2*cosg2*sing2/(1.0+2.0*yi[iz][ix])+w1*w1); h2 = sqrtf(-8.0*yi[iz][ix]*vx2*vz2*cosg2*sing2/(1.0+2.0*yi[iz][ix])+w2*w2); h3 = sqrtf(-2.0*yi[iz][ix]*vx2*vz2*cosf(2.0*se[iz][ix])*cosf(2.0*se[iz][ix])/(1.0+2.0*yi[iz][ix])+0.25*w3*w3); aa[iz][ix][4] = (w1+h1)*(dt2+(2.0*dx2-dt2*(w1+h1))/(w10+h10))/(24.0*dx4); aa[iz][ix][5] = (w2+h2)*(dt2+(2.0*dz2-dt2*(w2+h2))/(w20+h20))/(24.0*dz4); aa[iz][ix][3] = -aa[iz][ix][4]*dx2/dz2-aa[iz][ix][5]*dz2/dx2+(dt2*(w3+2.0*h3)+dx2*(w1+h1)/(w10+h10)+dz2*(w2+h2)/(w20+h20)-dt2*(w3+2.0*h3)*(w3+2.0*h3)/(w30+2.0*h30))/(12.0*dx2*dz2); aa[iz][ix][1] = -2.0*aa[iz][ix][3]-4.0*aa[iz][ix][4]-(w1+h1)/(dx2*(w10+h10)); aa[iz][ix][2] = -2.0*aa[iz][ix][3]-4.0*aa[iz][ix][5]-(w2+h2)/(dz2*(w20+h20)); aa[iz][ix][0] = -2.0*aa[iz][ix][1]-2.0*aa[iz][ix][2]-4.0*aa[iz][ix][3]-2.0*aa[iz][ix][4]-2.0*aa[iz][ix][5]; } } for (iz=0; iz < nzb; iz++) { for (ix=0; ix < nxb; ix++) { cur[iz][ix] = 0.0; } } cur[isz+nbt][isx+nbl] = wav[0]; for (iz=0; iz < nzb; iz++) { for (ix=0; ix < nxb; ix++) { old[iz][ix] = 0.0; derold[iz][ix] =cur[iz][ix]/dt; } } for (iz=nbt; iz<nz+nbt; iz++){ sf_floatwrite(cur[iz]+nbl,nx,out); } /* #ifdef _OPENMP #pragma omp parallel {nth = omp_get_num_threads();} sf_warning("using %d threads",nth); #endif */ /* propagation in time */ for (it=1; it < nt; it++) { for (iz=0; iz < nzb; iz++){ for (ix=0; ix < nxb; ix++){ nxt[iz][ix] = 0.0; uk[iz][ix].r = cur[iz][ix]; uk[iz][ix].i = 0.0; } } /* compute u(kx,kz) */ for (iz=0; iz < nzb; iz++){ /* Fourier transform x to kx */ for (ix=1; ix < nxb; ix+=2){ uk[iz][ix] = sf_cneg(uk[iz][ix]); } kiss_fft_stride(cfgx,uk[iz],ctracex,1); for (ikx=0; ikx<nkx; ikx++) uk[iz][ikx] = ctracex[ikx]; } for (ikx=0; ikx < nkx; ikx++){ /* Fourier transform z to kz */ for (ikz=1; ikz<nkz; ikz+=2){ uk[ikz][ikx] = sf_cneg(uk[ikz][ikx]); } kiss_fft_stride(cfgz,uk[0]+ikx,ctracez,nkx); for (ikz=0; ikz<nkz; ikz++) uk[ikz][ikx] = ctracez[ikz]; } /* #ifdef _OPENMP #pragma omp parallel for private(ik,ix,x,k,tmp,tmpex,tmpdt) #endif */ for (ikz=0; ikz < nkz; ikz++) { kz1 = (kz0+ikz*dkz)*2.0*pi; for (ikx=0; ikx < nkx; ikx++) { kx1 = (kx0+ikx*dkx)*2.0*pi; kx = kx1*cosg0+kz1*sing0; kz = kz1*cosg0-kx1*sing0; tmpvk = (vx02*kx*kx+vz02*kz*kz); k2 = kx1*kx1+kz1*kz1; vk2 = 0.5*tmpvk+0.5*sqrtf(tmpvk*tmpvk-8.0*yi0/(1.0+2.0*yi0)*vx02*vz02*kx*kx*kz*kz); vk = sqrtf(vk2); tmpdt = 2.0*(cosf(vk*dt)-1.0); if(!k2) tmpdt /=(k2+err); else tmpdt /= (k2); uk[ikz][ikx] = sf_crmul(uk[ikz][ikx],tmpdt); } } /* Inverse FFT*/ for (ikx=0; ikx < nkx; ikx++){ /* Inverse Fourier transform kz to z */ kiss_fft_stride(cfgzi,(kiss_fft_cpx *)uk[0]+ikx,(kiss_fft_cpx *)ctracez,nkx); for (ikz=0; ikz < nkz; ikz++) uk[ikz][ikx] = sf_crmul(ctracez[ikz],ikz%2?-1.0:1.0); } for (ikz=0; ikz < nkz; ikz++){ /* Inverse Fourier transform kx to x */ kiss_fft_stride(cfgxi,(kiss_fft_cpx *)uk[ikz],(kiss_fft_cpx *)ctracex,1); for (ikx=0; ikx < nkx; ikx++) uk[ikz][ikx] = sf_crmul(ctracex[ikx],ikx%2?-1.0:1.0); } for (iz=0; iz < nzb; iz++){ for (ix=0; ix < nxb; ix++){ ukr[iz][ix] = sf_crealf(uk[iz][ix]); ukr[iz][ix] /= (nkx*nkz); } } for (iz=2; iz < nzb-2; iz++) { for (ix=2; ix < nxb-2; ix++) { nxt[iz][ix] = ukr[iz][ix]*aa[iz][ix][0] + (ukr[iz][ix-1]+ukr[iz][ix+1])*aa[iz][ix][1] + (ukr[iz-1][ix]+ukr[iz+1][ix])*aa[iz][ix][2] + (ukr[iz-1][ix-1]+ukr[iz-1][ix+1]+ukr[iz+1][ix-1]+ukr[iz+1][ix+1])*aa[iz][ix][3] + (ukr[iz][ix-2]+ukr[iz][ix+2])*aa[iz][ix][4] + (ukr[iz-2][ix]+ukr[iz+2][ix])*aa[iz][ix][5]; } } /* nxt[0][0] = uk[0][0]*aa[0][0][0] + uk[0][1]*aa[0][0][1] + uk[1][0]*aa[0][0][2]; nxt[0][nxb-1] = uk[0][nxb-1]*aa[0][nxb-1][0] + uk[0][nxb-2]*aa[0][nxb-1][1] + uk[1][nxb-1]*aa[0][nxb-1][2]; nxt[nzb-1][0] = uk[nzb-1][0]*aa[nzb-1][0][0] + uk[nzb-1][1]*aa[nzb-1][0][1] + uk[nzb-2][0]*aa[nzb-1][0][2]; nxt[nzb-1][nxb-1] = uk[nzb-1][nxb-1]*aa[nzb-1][nxb-1][0] + uk[nzb-1][nxb-2]*aa[nzb-1][nxb-1][1] + uk[nzb-2][nxb-1]*aa[nzb-1][nxb-1][2]; for (ix=1; ix < nxb-1; ix++) { nxt[0][ix] = uk[0][ix]*aa[0][ix][0] + (uk[0][ix-1]+uk[0][ix+1])*aa[0][ix][1] + uk[1][ix]*aa[0][ix][2]; nxt[nz-1][ix] = uk[nz-1][ix]*aa[nz-1][ix][0] + (uk[nz-1][ix-1]+uk[nz-1][ix+1])*aa[nz-1][ix][1] + uk[nz-2][ix]*aa[nz-1][ix][2]; } for (iz=1; iz < nzb-1; iz++) { nxt[iz][0] = uk[iz][0]*aa[iz][0][0] + uk[iz][1]*aa[iz][0][1] + (uk[iz-1][0]+uk[iz+1][0])*aa[iz][0][2]; nxt[iz][nx-1] = uk[iz][nx-1]*aa[iz][nx-1][0] + uk[iz][nx-2]*aa[iz][nx-1][1] + (uk[iz-1][nx-1]+uk[iz+1][nx-1])*aa[iz][nx-1][2]; } */ // nxt[isz+nbt][isx+nbl] += wav[it]; for (iz=0; iz < nzb; iz++) { for (ix=0; ix < nxb; ix++) { dercur[iz][ix]= derold[iz][ix] + nxt[iz][ix]/dt; nxt[iz][ix] = cur[iz][ix] + dercur[iz][ix]*dt; // nxt[iz][ix] += 2.0*cur[iz][ix] -old[iz][ix]; } } nxt[isz+nbt][isx+nbl] += wav[it]; bd_decay(nxt); bd_decay(dercur); for (iz=0; iz < nzb; iz++) { for(ix=0; ix < nxb; ix++) { old[iz][ix] = cur[iz][ix]; cur[iz][ix] = nxt[iz][ix]; derold[iz][ix] = dercur[iz][ix]; } } for (iz=nbt; iz<nz+nbt; iz++){ sf_floatwrite(nxt[iz]+nbl,nx,out); } } bd_close(); free(**aa); free(*aa); free(aa); free(*vx); free(*vz); free(*yi); free(*se); free(*nxt); free(*cur); free(*old); free(*dercur); free(*derold); free(*uk); free(*ukr); free(vx); free(vz); free(yi); free(se); free(nxt); free(cur); free(old); free(dercur); free(derold); free(uk); free(ukr); // sf_fileclose(vel); // sf_fileclose(inp); // sf_fileclose(out); exit(0); }
/*------------------------------------------------------------*/ void ompfft3(bool inv /* inverse/forward flag */, kiss_fft_cpx ***pp /* [n1][n2][n3] */, int ompith, fft3d fft) /*< Apply 3-D FFT >*/ { int i1,i2,i3; if (inv) { /* IFT 1 */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft(fft->invs1[ompith],pp[i3][i2],pp[i3][i2]); } } /* IFT 2 */ for (i3=0; i3 < fft->n3; i3++) { for (i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->invs2[ompith],pp[i3][0]+i1, fft->ctrace2[ompith],fft->n1); for (i2=0; i2<fft->n2; i2++) { pp[i3][i2][i1] = fft->ctrace2[ompith][i2]; } } } /* IFT 3 */ for (i2=0; i2 < fft->n2; i2++) { for (i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->invs3[ompith],pp[0][i2]+i1, fft->ctrace3[ompith],fft->n1); for (i3=0; i3 < fft->n3; i3++) { pp[i3][i2][i1] = fft->ctrace3[ompith][i3]; } } } /* scaling */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { for (i1=0; i1 < fft->n1; i1++) { pp[i3][i2][i1] = sf_crmul(pp[i3][i2][i1],fft->fftscale); } } } } else { /* scaling */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { for (i1=0; i1 < fft->n1; i1++) { pp[i3][i2][i1] = sf_crmul(pp[i3][i2][i1],fft->fftscale); } } } /* FFT 3 */ for (i2=0; i2 < fft->n2; i2++) { for (i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->forw3[ompith],pp[0][i2]+i1,fft->ctrace3[ompith],fft->n1); for (i3=0; i3 < fft->n3; i3++) { pp[i3][i2][i1] = fft->ctrace3[ompith][i3]; } } } /* FFT 2 */ for (i3=0; i3 < fft->n3; i3++) { for (i1=0; i1 < fft->n1; i1++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft_stride(fft->forw2[ompith],pp[i3][0]+i1, fft->ctrace2[ompith],fft->n1); for (i2=0; i2 < fft->n2; i2++) { pp[i3][i2][i1] = fft->ctrace2[ompith][i2]; } } } /* FFT 1 */ for (i3=0; i3 < fft->n3; i3++) { for (i2=0; i2 < fft->n2; i2++) { #ifdef _OPENMP #pragma omp critical #endif kiss_fft(fft->forw1[ompith],pp[i3][i2],pp[i3][i2]); } } } }
void st (int len /* data size */, float d1 /* data sampling */, int lo /* low frequency */, int hi /* high frequency */, float *data /* input [len] */, sf_complex *result /* output [len*(hi-lo+1)] */) /*< Forward S transform >*/ { int i, i1, k, l2, nw; float s, *g; kiss_fft_cpx *d, *pp, *qq; kiss_fft_cfg tfft, itfft; nw = 2*kiss_fft_next_fast_size((len+1)/2); tfft = kiss_fft_alloc(nw,0,NULL,NULL); itfft = kiss_fft_alloc(nw,1,NULL,NULL); pp = (kiss_fft_cpx*) sf_complexalloc(nw); qq = (kiss_fft_cpx*) sf_complexalloc(nw); d = (kiss_fft_cpx*) sf_complexalloc(nw); g = sf_floatalloc(nw); s = 0.; for (i = 0; i < len; i++) { d[i].r = data[i]; d[i].i = 0.; s += data[i]; } s /= len; for (i=len; i < nw; i++) { d[i].r = 0.; d[i].i = 0.; } kiss_fft_stride (tfft,d,pp,1); l2 = (nw+1)/2; for (i=1; i < l2; i++) { pp[i].r *= 2.; pp[i].i *= 2.; } l2 = nw/2+1; for (i=l2; i < nw; i++) { pp[i].r = 0.; pp[i].i = 0.; } for (i1=lo; i1 <= hi; i1++) { if (0 == i1) { for (i=0; i < len; i++) { result[(i1-lo)*len+i] = sf_cmplx(s,0.); } } else { g[0] = gauss(i1, 0); l2 = nw/2 + 1; for (i=1; i < l2; i++) { g[i] = g[nw-i] = gauss(i1, i); } for (i=0; i < nw; i++) { s = g[i]; k = i1 + i; if (k >= nw) k -= nw; qq[i].r = pp[k].r * s; qq[i].i = pp[k].i * s; } kiss_fft_stride(itfft,qq,d,1); for (i=0; i < len; i++) { result[(i1-lo)*len+i] = sf_cmplx(d[i].r/len,d[i].i/len); } } } free(pp); free(qq); free(d); free(g); }
int main (int argc, char **argv) { int n1, nx, n3, dim, n[SF_MAX_DIM]; /* dimensions */ int i1, ix, i3, j; /* loop counters */ int nk; /* number of wavenumbers */ int npad; /* padding */ float dx; /* space sampling interval */ float dk; /* wavenumber sampling interval */ float x0; /* staring space */ float k0; /* starting wavenumber */ float wt; /* Fourier scaling */ kiss_fft_cpx **cp; /* frequency-wavenumber */ bool inv; /* forward or inverse */ bool sym; /* symmetric scaling */ bool opt; /* optimal padding */ int sign; /* transform sign */ int axis; /* transform axis */ char varname[12]; /* variable name */ char *label; /* transformed axis label */ #ifdef SF_HAS_FFTW fftwf_plan cfg; #else kiss_fft_cpx *ctrace; kiss_fft_cfg cfg; #endif sf_file in=NULL, out=NULL; sf_init(argc,argv); in = sf_input ( "in"); out = sf_output("out"); if (SF_COMPLEX != sf_gettype(in)) sf_error ("Need complex input"); if (!sf_getbool("inv",&inv)) inv = false; /* if y, perform inverse transform */ if (!sf_getbool("sym",&sym)) sym=false; /* if y, apply symmetric scaling to make the FFT operator Hermitian */ if (!sf_getint("sign",&sign)) sign = inv? 1: 0; /* transform sign (0 or 1) */ if (!sf_getbool("opt",&opt)) opt=true; /* if y, determine optimal size for efficiency */ if (!sf_getint("axis",&axis)) axis=2; /* Axis to transform */ dim = sf_filedims(in,n); n1=n3=1; for (j=0; j < dim; j++) { if (j < axis-1) n1 *= n[j]; else if (j > axis-1) n3 *= n[j]; } if (inv) { sprintf(varname,"n%d",axis); if (!sf_histint (in,varname,&nk)) sf_error("No %s= in input",varname); sprintf(varname,"d%d",axis); if (!sf_histfloat(in,varname,&dk)) sf_error("No %s= in input",varname); sprintf(varname,"fft3_n%d",axis); if (!sf_histint (in,varname,&nx)) nx=nk; sprintf(varname,"fft3_o%d",axis); if (!sf_histfloat(in,varname,&x0)) x0 = 0.; sprintf(varname,"fft3_label%d",axis); label = sf_histstring(in,varname); dx = 1./(nk*dk); sprintf(varname,"n%d",axis); sf_putint (out,varname,nx); sprintf(varname,"d%d",axis); sf_putfloat (out,varname,dx); sprintf(varname,"o%d",axis); sf_putfloat (out,varname,x0); sprintf(varname,"label%d",axis); if (NULL != label) { sf_putstring(out,varname,label); } else if (NULL != (label = sf_histstring(in,varname))) { (void) sf_fft_label(axis,label,out); } } else { sprintf(varname,"n%d",axis); if (!sf_histint (in,varname,&nx)) sf_error("No %s= in input",varname); sprintf(varname,"d%d",axis); if (!sf_histfloat(in,varname,&dx)) sf_error("No %s= in input",varname); sprintf(varname,"o%d",axis); if (!sf_histfloat(in,varname,&x0)) x0 = 0.; sprintf(varname,"label%d",axis); label = sf_histstring(in,varname); sprintf(varname,"fft3_n%d",axis); sf_putint(out,varname,nx); sprintf(varname,"fft3_o%d",axis); sf_putfloat(out,varname,x0); if (NULL != label) { sprintf(varname,"fft3_label%d",axis); sf_putstring(out,varname,label); } if (!sf_getint("pad",&npad)) npad=2; /* padding factor */ /* determine wavenumber sampling */ nk = opt? kiss_fft_next_fast_size(nx*npad): nx*npad; if (nk != nx) sf_warning("padded to %d",nk); dk = 1./(nk*dx); k0 = -0.5/dx; sprintf(varname,"n%d",axis); sf_putint (out,varname,nk); sprintf(varname,"d%d",axis); sf_putfloat (out,varname,dk); sprintf(varname,"o%d",axis); sf_putfloat (out,varname,k0); if (NULL != label && !sf_fft_label(axis,label,out)) { sprintf(varname,"label%d",axis); sf_putstring(out,varname,"Wavenumber"); } } sprintf(varname,"unit%d",axis); sf_fft_unit(axis,sf_histstring(in,varname),out); cp = (kiss_fft_cpx**) sf_complexalloc2(n1,nk); #ifdef SF_HAS_FFTW ix = nk; cfg = fftwf_plan_many_dft(1, &ix, n1, (fftwf_complex*) cp[0], NULL, n1, 1, (fftwf_complex*) cp[0], NULL, n1, 1, sign? FFTW_BACKWARD: FFTW_FORWARD, FFTW_ESTIMATE); if (NULL == cfg) sf_error("FFTW failure."); #else ctrace = (kiss_fft_cpx*) sf_complexalloc(nk); cfg = kiss_fft_alloc(nk,sign,NULL,NULL); #endif /* FFT scaling */ wt = sym? 1./sqrtf((float) nk): 1./nk; for (i3=0; i3<n3; i3++) { if (inv) { sf_floatread((float*) cp[0],n1*nk*2,in); #ifdef SF_HAS_FFTW fftwf_execute(cfg); for (ix=0; ix<nx; ix++) { for (i1=0; i1 < n1; i1++) { cp[ix][i1] = sf_crmul(cp[ix][i1],ix%2? -wt: wt); } } #else for (i1=0; i1 < n1; i1++) { /* Fourier transform k to x */ kiss_fft_stride(cfg,cp[0]+i1,ctrace,n1); for (ix=0; ix<nx; ix++) { cp[ix][i1] = sf_crmul(ctrace[ix],ix%2? -wt: wt); } } #endif sf_floatwrite((float*) cp[0],n1*nx*2,out); } else { sf_floatread((float*) cp[0],n1*nx*2,in); /* FFT centering */ for (ix=1; ix<nx; ix+=2) { for (i1=0; i1<n1; i1++) { cp[ix][i1] = sf_cneg(cp[ix][i1]); } } if (sym) { for (ix=0; ix<nx; ix++) { for (i1=0; i1 < n1; i1++) { cp[ix][i1] = sf_crmul(cp[ix][i1],wt); } } } /* pad with zeros */ for (ix=nx; ix<nk; ix++) { for (i1=0; i1<n1; i1++) { cp[ix][i1].r = 0.; cp[ix][i1].i = 0.; } } #ifdef SF_HAS_FFTW fftwf_execute(cfg); #else for (i1=0; i1 < n1; i1++) { /* Fourier transform x to k */ kiss_fft_stride(cfg,cp[0]+i1,ctrace,n1); /* Transpose */ for (ix=0; ix<nk; ix++) { cp[ix][i1] = ctrace[ix]; } } #endif sf_floatwrite((float*) cp[0],n1*nk*2,out); } } exit (0); }
void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) { kiss_fft_stride(cfg,fin,fout,1); }
void mcfft3(sf_complex *inp /* [n1*n2*n3] */, sf_complex *out /* [nk*n2*n3] */) /*< 3-D FFT >*/ { int i1, i2, i3, ith=0; /* FFT centering */ #pragma omp parallel for private(i3,i2,i1) default(shared) for (i3=0; i3<local_n0; i3++) { for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { #ifdef SF_HAS_COMPLEX_H cc[(i3*n2+i2)*n1+i1] = ((((i3+local_0_start)%2==0)==(i2%2==0))==(i1%2==0))? inp[(i3*n2+i2)*n1+i1]:-inp[(i3*n2+i2)*n1+i1]; #else cc[(i3*n2+i2)*n1+i1] = ((((i3+local_0_start)%2==0)==(i2%2==0))==(i1%2==0))? inp[(i3*n2+i2)*n1+i1]:sf_cneg(inp[(i3*n2+i2)*n1+i1]); #endif } } } /* FFT over first axis */ #ifdef _OPENMP #pragma omp parallel for private(i3,i2,ith) default(shared) #endif for (i3=0; i3 < local_n0; i3++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif for (i2=0; i2 < n2; i2++) { kiss_fft_stride(cfg1[ith],(kiss_fft_cpx *) cc+(i3*n2+i2)*nk,tmp+(i3*n2+i2)*nk,1); } } /* FFT over second axis */ #ifdef _OPENMP #pragma omp parallel for private(i3,i2,i1,ith) default(shared) #endif for (i3=0; i3 < local_n0; i3++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif for (i1=0; i1 < nk; i1++) { kiss_fft_stride(cfg2[ith],tmp+i3*n2*nk+i1,ctrace2[ith],nk); for (i2=0; i2 < n2; i2++) { tmp[(i3*n2+i2)*nk+i1]=ctrace2[ith][i2]; } } } /* parallel transpose from n1*n2 * n3 to n3 * n1*n2 */ fftwf_execute(cfg); /* FFT over third axis */ #ifdef _OPENMP #pragma omp parallel for private(i3,i1,ith) default(shared) #endif for (i1=0; i1 < local_n1; i1++) { #ifdef _OPENMP ith = omp_get_thread_num(); #endif kiss_fft_stride(cfg3[ith],tmp+i1*n3,ctrace3[ith],1); for (i3=0; i3<n3; i3++) { tmp[i1*n3+i3] = ctrace3[ith][i3]; } } fftwf_execute(icfg); #pragma omp parallel for private(i3,i2,i1) default(shared) for (i3=0; i3<local_n0; i3++) for (i2=0; i2<n2; i2++) for (i1=0; i1<nk; i1++) out[(i3*n2+i2)*n1+i1]=tmp2[(i3*n2+i2)*n1+i1]; }