int main(){ long ct_repeat=0; long ct_repeat_max=1; int ct_return=0; int i, n = 64, flag, chkerr; #ifdef XOPENME xopenme_init(1,2); #endif if (getenv("CT_REPEAT_MAIN")!=NULL) ct_repeat_max=atol(getenv("CT_REPEAT_MAIN")); /* ar */ for(i = 0; i < n; i++) ar[i] = cos(2*M_PI*i/n); #ifdef XOPENME xopenme_clock_start(0); #endif for (ct_repeat=0; ct_repeat<ct_repeat_max; ct_repeat++) { /* forward fft */ flag = 0; chkerr = fft1(n, flag); /* inverse fft */ flag = 1; chkerr = fft1(n, flag); } #ifdef XOPENME xopenme_clock_end(0); xopenme_dump_state(); xopenme_finish(); #endif (void) chkerr; /* Silence compiler about unused 'chkerr'. */ return 0; }
int main(){ int i, n = 64, flag, chkerr; /* ar */ for(i = 0; i < n; i++) ar[i] = cos(2*M_PI*i/n); /* forward fft */ flag = 0; chkerr = fft1(n, flag); /* inverse fft */ flag = 1; chkerr = fft1(n, flag); (void) chkerr; /* Silence compiler about unused 'chkerr'. */ return 0; }
void MainWindow::on_pushButton_9_clicked() { if (vec.size() == 0) return; vec_fft1.clear(); vec_fft2.clear(); vec_fft3.clear(); vec_fft0.clear(); vec_fft.clear(); vec1.clear(); wt.clear(); double omega = ui->lineEdit->text().toDouble(); double omega1 = omega - omega/vec.size(); ui->lineEdit_2->setText(QString::number(omega1)); fft1(vec, vec_fft1, vec_fft2); int N1 = vec.size() - 1; int M1 = vec.size(); for (int i = 0; i < vec.size(); i++) { double x = double(i); vec_fft.push_back(x); } for (int i = 0; i < M1; i++) { double x; if (omega != 0) x = i*omega1/M1; else x = double(i); wt.push_back(x);; } int N = vec_fft1.size(); for (int i = 0; i < N; i++) { long double x = sqrt(vec_fft1[i]*vec_fft1[i] + vec_fft2[i]*vec_fft2[i])/N; vec_fft3.push_back(x); } if (gr != NULL) delete gr; gr = new Graph(this); gr->setVecX(wt); gr->setVecY(vec_fft3); gr->setData(); gr->show(); }
VIO_Status gradient3D_volume(FILE *ifd, VIO_Volume data, int xyzv[VIO_MAX_DIMENSIONS], char *infile, char *outfile, int ndim, char *history, int curvature_flg) { float *fdata, /* floating point storage for blurred volume */ *f_ptr, /* pointer to fdata */ tmp, max_val, min_val, *dat_vector, /* temp storage of original row, col or slice vect. */ *dat_vecto2, /* storage of result of dat_vector*kern */ *kern; /* convolution kernel */ int total_voxels, vector_size_data, /* original size of row, col or slice vector */ array_size_pow2, /* actual size of vector/kernel data used in FFT */ /* routines - needs to be a power of two */ array_size; int data_offset; /* offset required to place original data (size n) */ /* into array (size m=2^n) so that data is centered*/ register int slice_limit, row,col,slice, /* counters to access original data */ vindex; /* counter to access vector and vecto2 */ int slice_size, /* size of each data step - in bytes */ row_size, col_size; char full_outfilename[256]; /* name of output file */ progress_struct progress; /* used to monitor progress of calculations */ VIO_Status status; int sizes[3], /* number of rows, cols and slices */ pos[3]; /* Input order of rows, cols, slices */ VIO_Real steps[3]; /* size of voxel step from center to center in x,y,z */ /*---------------------------------------------------------------------------------*/ /* start by setting up the raw data. */ /*---------------------------------------------------------------------------------*/ get_volume_sizes(data, sizes); /* rows,cols,slices */ get_volume_separations(data, steps); slice_size = sizes[xyzv[VIO_Y]] * sizes[xyzv[VIO_X]]; /* sizeof one slice */ col_size = sizes[xyzv[VIO_Y]]; /* sizeof one column */ row_size = sizes[xyzv[VIO_X]]; /* sizeof one row */ total_voxels = sizes[xyzv[VIO_Y]]*sizes[xyzv[VIO_X]]*sizes[xyzv[VIO_Z]]; ALLOC(fdata, total_voxels); f_ptr = fdata; /* read in data of input file. */ set_file_position(ifd,(long)0); status = io_binary_data(ifd,READ_FILE, fdata, sizeof(float), total_voxels); if (status != OK) print_error_and_line_num("problems reading binary data...\n",__FILE__, __LINE__); /*--------------------------------------------------------------------------------------*/ /* get ready to start up the transformation. */ /*--------------------------------------------------------------------------------------*/ initialize_progress_report( &progress, FALSE, sizes[xyzv[VIO_Z]] + sizes[xyzv[VIO_Y]] + sizes[xyzv[VIO_X]] + 1, "Gradient volume" ); /* note data is stored by rows (along x), then by cols (along y) then slices (along z) */ /*--------------------------------------------------------------------------------------*/ /* start with rows - i.e. the d/dx volume */ /*--------------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/ /* determine size of data structures needed */ vector_size_data = sizes[xyzv[VIO_X]]; /* array_size_pow2 will hold the size of the arrays for FFT convolution, remember that ffts require arrays 2^n in length */ array_size_pow2 = next_power_of_two(vector_size_data); array_size = 2*array_size_pow2+1; /* allocate 2*, since each point is a */ /* complex number for FFT, and the plus 1*/ /* is for the zero offset FFT routine */ ALLOC(dat_vector, array_size); ALLOC(dat_vecto2, array_size); ALLOC(kern , array_size); /* 1st calculate kern array for FT of 1st derivitive */ make_kernel_FT(kern,array_size_pow2, ABS(steps[xyzv[VIO_X]])); if (curvature_flg) /* 2nd derivative kernel */ muli_vects(kern,kern,kern,array_size_pow2); /* calculate offset for original data to be placed in vector */ data_offset = (array_size_pow2-sizes[xyzv[VIO_X]])/2; max_val = -FLT_MAX; min_val = FLT_MAX; /* 2nd now convolve this kernel with the rows of the dataset */ slice_limit = 0; switch (ndim) { case 1: slice_limit = 0; break; case 2: slice_limit = sizes[xyzv[VIO_Z]]; break; case 3: slice_limit = sizes[xyzv[VIO_Z]]; break; } for (slice = 0; slice < slice_limit; slice++) { /* for each slice */ for (row = 0; row < sizes[xyzv[VIO_Y]]; row++) { /* for each row */ f_ptr = fdata + slice*slice_size + row*sizes[xyzv[VIO_X]]; memset(dat_vector,0,(2*array_size_pow2+1)*sizeof(float)); for (col=0; col< sizes[xyzv[VIO_X]]; col++) { /* extract the row */ dat_vector[1 +2*(col+data_offset) ] = *f_ptr++; } fft1(dat_vector,array_size_pow2,1); muli_vects(dat_vecto2,dat_vector,kern,array_size_pow2); fft1(dat_vecto2,array_size_pow2,-1); f_ptr = fdata + slice*slice_size + row*sizes[xyzv[VIO_X]]; for (col=0; col< sizes[xyzv[VIO_X]]; col++) { /* put the row back */ vindex = 1 + 2*(col+data_offset); *f_ptr = dat_vecto2[vindex]/array_size_pow2; if (max_val<*f_ptr) max_val = *f_ptr; if (min_val>*f_ptr) min_val = *f_ptr; f_ptr++; } } update_progress_report( &progress, slice+1 ); } FREE(dat_vector); FREE(dat_vecto2); FREE(kern ); f_ptr = fdata; set_volume_real_range(data, min_val, max_val); printf("Making byte volume dx..." ); for(slice=0; slice<sizes[xyzv[VIO_Z]]; slice++) { pos[xyzv[VIO_Z]] = slice; for(row=0; row<sizes[xyzv[VIO_Y]]; row++) { pos[xyzv[VIO_Y]] = row; for(col=0; col<sizes[xyzv[VIO_X]]; col++) { pos[xyzv[VIO_X]] = col; tmp = CONVERT_VALUE_TO_VOXEL(data, *f_ptr); SET_VOXEL_3D( data, pos[0], pos[1], pos[2], tmp); f_ptr++; } } } if (!curvature_flg) sprintf(full_outfilename,"%s_dx.mnc",outfile); else sprintf(full_outfilename,"%s_dxx.mnc",outfile); if (debug) print ("dx: min = %f, max = %f\n",min_val, max_val); status = output_modified_volume(full_outfilename, NC_UNSPECIFIED, FALSE, min_val, max_val, data, infile, history, NULL); if (status != OK) print_error_and_line_num("problems writing dx gradient data...\n",__FILE__, __LINE__); /*--------------------------------------------------------------------------------------*/ /* now do cols - i.e. the d/dy volume */ /*--------------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/ /* determine size of data structures needed */ set_file_position(ifd,0); status = io_binary_data(ifd,READ_FILE, fdata, sizeof(float), total_voxels); if (status != OK) print_error_and_line_num("problems reading binary data...\n",__FILE__, __LINE__); f_ptr = fdata; vector_size_data = sizes[xyzv[VIO_Y]]; /* array_size_pow2 will hold the size of the arrays for FFT convolution, remember that ffts require arrays 2^n in length */ array_size_pow2 = next_power_of_two(vector_size_data); array_size = 2*array_size_pow2+1; /* allocate 2*, since each point is a */ /* complex number for FFT, and the plus 1*/ /* is for the zero offset FFT routine */ ALLOC(dat_vector, array_size); ALLOC(dat_vecto2, array_size); ALLOC(kern , array_size); /* 1st calculate kern array for FT of 1st derivitive */ make_kernel_FT(kern,array_size_pow2, ABS(steps[xyzv[VIO_Y]])); if (curvature_flg) /* 2nd derivative kernel */ muli_vects(kern,kern,kern,array_size_pow2); /* calculate offset for original data to be placed in vector */ data_offset = (array_size_pow2-sizes[xyzv[VIO_Y]])/2; /* 2nd now convolve this kernel with the rows of the dataset */ max_val = -FLT_MAX; min_val = FLT_MAX; switch (ndim) { case 1: slice_limit = 0; break; case 2: slice_limit = sizes[xyzv[VIO_Z]]; break; case 3: slice_limit = sizes[xyzv[VIO_Z]]; break; } for (slice = 0; slice < slice_limit; slice++) { /* for each slice */ for (col = 0; col < sizes[xyzv[VIO_X]]; col++) { /* for each col */ /* f_ptr = fdata + slice*slice_size + row*sizeof(float); */ f_ptr = fdata + slice*slice_size + col; memset(dat_vector,0,(2*array_size_pow2+1)*sizeof(float)); for (row=0; row< sizes[xyzv[VIO_Y]]; row++) { /* extract the col */ dat_vector[1 +2*(row+data_offset) ] = *f_ptr; f_ptr += row_size; } fft1(dat_vector,array_size_pow2,1); muli_vects(dat_vecto2,dat_vector,kern,array_size_pow2); fft1(dat_vecto2,array_size_pow2,-1); f_ptr = fdata + slice*slice_size + col; for (row=0; row< sizes[xyzv[VIO_Y]]; row++) { /* put the col back */ vindex = 1 + 2*(row+data_offset); *f_ptr = dat_vecto2[vindex]/array_size_pow2; if (max_val<*f_ptr) max_val = *f_ptr; if (min_val>*f_ptr) min_val = *f_ptr; f_ptr += row_size; } } update_progress_report( &progress, slice+sizes[xyzv[VIO_Z]]+1 ); } FREE(dat_vector); FREE(dat_vecto2); FREE(kern ); f_ptr = fdata; set_volume_real_range(data, min_val, max_val); printf("Making byte volume dy..." ); for(slice=0; slice<sizes[xyzv[VIO_Z]]; slice++) { pos[xyzv[VIO_Z]] = slice; for(row=0; row<sizes[xyzv[VIO_Y]]; row++) { pos[xyzv[VIO_Y]] = row; for(col=0; col<sizes[xyzv[VIO_X]]; col++) { pos[xyzv[VIO_X]] = col; tmp = CONVERT_VALUE_TO_VOXEL(data, *f_ptr); SET_VOXEL_3D( data, pos[0], pos[1], pos[2], tmp); f_ptr++; } } } if (!curvature_flg) sprintf(full_outfilename,"%s_dy.mnc",outfile); else sprintf(full_outfilename,"%s_dyy.mnc",outfile); if (debug) print ("dy: min = %f, max = %f\n",min_val, max_val); status = output_modified_volume(full_outfilename, NC_UNSPECIFIED, FALSE, min_val, max_val, data, infile, history, NULL); if (status != OK) print_error_and_line_num("problems writing dy gradient data...",__FILE__, __LINE__); /*--------------------------------------------------------------------------------------*/ /* now do slices - i.e. the d/dz volume */ /*--------------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/ /* determine size of data structures needed */ set_file_position(ifd,0); status = io_binary_data(ifd,READ_FILE, fdata, sizeof(float), total_voxels); if (status != OK) print_error_and_line_num("problems reading binary data...\n",__FILE__, __LINE__); f_ptr = fdata; vector_size_data = sizes[xyzv[VIO_Z]]; /* array_size_pow2 will hold the size of the arrays for FFT convolution, remember that ffts require arrays 2^n in length */ array_size_pow2 = next_power_of_two(vector_size_data); array_size = 2*array_size_pow2+1; /* allocate 2*, since each point is a */ /* complex number for FFT, and the plus 1*/ /* is for the zero offset FFT routine */ ALLOC(dat_vector, array_size); ALLOC(dat_vecto2, array_size); ALLOC(kern , array_size); if (ndim==1 || ndim==3) { /* 1st calculate kern array for FT of 1st derivitive */ make_kernel_FT(kern,array_size_pow2, ABS(steps[xyzv[VIO_Z]])); if (curvature_flg) /* 2nd derivative kernel */ muli_vects(kern,kern,kern,array_size_pow2); /* calculate offset for original data to be placed in vector */ data_offset = (array_size_pow2-sizes[xyzv[VIO_Z]])/2; /* 2nd now convolve this kernel with the slices of the dataset */ max_val = -FLT_MAX; min_val = FLT_MAX; for (col = 0; col < sizes[xyzv[VIO_X]]; col++) { /* for each column */ for (row = 0; row < sizes[xyzv[VIO_Y]]; row++) { /* for each row */ f_ptr = fdata + col*col_size + row; memset(dat_vector,0,(2*array_size_pow2+1)*sizeof(float)); for (slice=0; slice< sizes[xyzv[VIO_Z]]; slice++) { /* extract the slice vector */ dat_vector[1 +2*(slice+data_offset) ] = *f_ptr; f_ptr += slice_size; } fft1(dat_vector,array_size_pow2,1); muli_vects(dat_vecto2,dat_vector,kern,array_size_pow2); fft1(dat_vecto2,array_size_pow2,-1); f_ptr = fdata + col*col_size + row; for (slice=0; slice< sizes[xyzv[VIO_Z]]; slice++) { /* put the vector back */ vindex = 1 + 2*(slice+data_offset); *f_ptr = dat_vecto2[vindex]/array_size_pow2; if (max_val<*f_ptr) max_val = *f_ptr; if (min_val>*f_ptr) min_val = *f_ptr; f_ptr += slice_size; } } update_progress_report( &progress, col + 2*sizes[xyzv[VIO_Z]] + 1 ); } } /* if ndim */ else { max_val = 0.00001; min_val = 0.00000; for (col = 0; col < sizes[xyzv[VIO_X]]; col++) { /* for each column */ for (row = 0; row < sizes[xyzv[VIO_Y]]; row++) { /* for each row */ *f_ptr = 0.0; f_ptr++; } } } FREE(dat_vector); FREE(dat_vecto2); FREE(kern ); /* set up the correct info to copy the data back out in mnc */ f_ptr = fdata; set_volume_real_range(data, min_val, max_val); printf("Making byte volume dz..." ); for(slice=0; slice<sizes[xyzv[VIO_Z]]; slice++) { pos[xyzv[VIO_Z]] = slice; for(row=0; row<sizes[xyzv[VIO_Y]]; row++) { pos[xyzv[VIO_Y]] = row; for(col=0; col<sizes[xyzv[VIO_X]]; col++) { pos[xyzv[VIO_X]] = col; tmp = CONVERT_VALUE_TO_VOXEL(data, *f_ptr); SET_VOXEL_3D( data, pos[0], pos[1], pos[2], tmp); f_ptr++; } } } if (!curvature_flg) sprintf(full_outfilename,"%s_dz.mnc",outfile); else sprintf(full_outfilename,"%s_dzz.mnc",outfile); if (debug) print ("dz: min = %f, max = %f\n",min_val, max_val); status = output_modified_volume(full_outfilename, NC_UNSPECIFIED, FALSE, min_val, max_val, data, infile, history, NULL); if (status != OK) print_error_and_line_num("problems writing dz gradient data...",__FILE__, __LINE__); terminate_progress_report( &progress ); FREE(fdata); return(status); }
int main(void) { static double ar[8] = { 0., 0., 0., 1., 1., 0., 0., 0.}; static double ai[8] = { 0., 0., 0., 0., 0., 0., 0., 0.}; static double ar2[16] = { 0., 0., 0., 0., 0., 1., 1., 0., 0., 1., 1., 0., 0., 0., 0., 0.}; static double ai2[16] = { 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}; Complex a[8], a2[16]; int flag, i, iter, j, k, n, nmax; iter = 0; n = 8; #ifndef CPX for(i = 0; i < n; i++) a[i] = tocomplex(ar[i], ai[i]); print(ar, ai, n); #else printx(a, n); #endif /* forward FFT */ flag = 0; #ifndef CPX fft1(ar, ai, n, iter, flag); print(ar, ai, n); #else fft1x(a, n, iter, flag); printx(a, n); #endif /* reverse FFT */ flag = 1; #ifndef CPX fft1(ar, ai, n, iter, flag); print(ar, ai, n); #else fft1x(a, n, iter, flag); printx(a, n); #endif n = nmax = 4; for(i = k = 0; i < n; i++) for(j = 0; j < n; j++, k++) a2[k] = tocomplex(ar2[k], ai2[k]); #ifndef CPX print2(ar2, ai2, n, nmax); #else print2x(a2, n, nmax); #endif flag = 0; #ifndef CPX fft2(ar2, ai2, n, nmax, flag); print2(ar2, ai2, n, nmax); #else fft2x(a2, n, nmax, flag); print2x(a2, n, nmax); #endif flag = 1; #ifndef CPX fft2(ar2, ai2, n, nmax, flag); print2(ar2, ai2, n, nmax); #else fft2x(a2, n, nmax, flag); print2x(a2, n, nmax); #endif return 1; }
int main(int argc, char* argv[]) { bool verb,conj,twin,pandq,Gtot,Htot; /* OMP parameters */ #ifdef _OPENMP int ompnth; #endif float *pplus0, *pplus, *pplusinv, *pplustemp, *Pplus, *Pplus_trace, *pminus, *Pminus, *Refl, *Gp, *Gm, *G, *H; float *qplus, *qplusinv, *qplustemp, *Qplus, *Qplus_trace, *qminus, *Qminus; float *window, *taper, pi; int *tw; /* I/O files */ sf_file Fplus; sf_file FRefl; sf_file FGp; sf_file FGm; sf_file FG; sf_file FH; sf_file Ftwin; sf_file Fp; sf_file Fq; char *filename1, filename2[256], filename3[256]; /* Cube axes */ sf_axis at,af,ax; int nt,nf,ntr,mode,nshots,niter,len; int i,it,ix,ishot,iter,i0; int twc, twa, shift, n[2], rect[2], s[2]; float scale,eps,dt,df,dx,ot,of,a,b,c,d,e,f; sf_triangle tr; /*------------------------------------------------------------*/ /* Initialize RSF parameters */ /*------------------------------------------------------------*/ sf_init(argc,argv); /*------------------------------------------------------------*/ /* Initialize OMP parameters */ /*------------------------------------------------------------*/ #ifdef _OPENMP ompnth=omp_init(); #endif /*------------------------------------------------------------*/ /* Flags */ /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("conj",&conj)) conj=false; /* complex conjugation (time-reversal) flag */ if(! sf_getbool("twin",&twin)) twin=false; /* returns the timewindow as one of the outputs */ if(! sf_getbool("pandq",&pandq)) pandq=false; /* pandq=true: returns p and q */ if(! sf_getbool("Gtot",&Gtot)) Gtot=false; /* Gtot=true: returns G=Gp+Gm */ if(! sf_getbool("Htot",&Htot)) Htot=false; /* Htot=true: returns H=Gp-Gm */ if(! sf_getint("niter",&niter)) niter=1; /* number of iterations */ if(! sf_getint("nshots",&nshots)) nshots=1; /* number of shots */ if(! sf_getfloat("scale",&scale)) scale=1.0; /* scale factor */ if(! sf_getfloat("eps",&eps)) eps=1e-4; /* threshold for the timewindow */ if(! sf_getint("shift",&shift)) shift=5; /* shift in samples for the timewindow */ if (verb) { fprintf(stderr,"This program was called with \"%s\".\n",argv[0]); /*fprintf(stderr,"Nr: %d Nx: %d Nt:%d\n",nr,nx,nt);*/ if (argc > 1) { for (i = 1; i<argc; i++) { fprintf(stderr,"argv[%d] = %s\n", i, argv[i]); } } else { fprintf(stderr,"The command had no other arguments.\n"); } } /*------------------------------------------------------------*/ /* I/O files */ /*------------------------------------------------------------*/ /* "in" is the transposed version of p00plus_xxxx_xxxx.rsf Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=ntr,n2=nt Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=nt,n2=ntr */ Fplus = sf_input("in"); /* refl is REFL_000.rsf It is used to read nf, df, of Dimensions are: n1=nf,n2=ntr */ /*FRefl = (sf_file)sf_alloc(1,sizeof(sf_file));*/ FRefl = sf_input("refl"); FGp = sf_output("out"); FGm = sf_output("Gm"); if (Gtot) { FG = sf_output("G"); } if (Htot) { FH = sf_output("H"); } if (pandq) { Fp = sf_output("p"); Fq = sf_output("q"); } if (twin) { Ftwin = sf_output("window"); /* time window */ } /*------------------------------------------------------------*/ /* Axes */ /*------------------------------------------------------------*/ at = sf_iaxa(Fplus,1); sf_setlabel(at,"Time"); if(verb) sf_raxa(at); /* time */ af = sf_iaxa(FRefl,1); sf_setlabel(af,"Frequency"); if(verb) sf_raxa(af); /* frequency */ ax = sf_iaxa(Fplus,2); sf_setlabel(ax,"r"); if(verb) sf_raxa(ax); /* space */ nt = sf_n(at); dt = sf_d(at); ot = sf_o(at); nf = sf_n(af); df = sf_d(af); of = sf_o(af); ntr = sf_n(ax); dx = sf_d(ax); if (verb) fprintf(stderr,"nt: %d nf: %d ntr:%d\n",nt,nf,ntr); sf_fileclose(FRefl); /*------------------------------------------------------------*/ /* Setup output data and wavefield header */ /*------------------------------------------------------------*/ sf_oaxa(FGp,at,1); sf_oaxa(FGp,ax,2); sf_oaxa(FGm,at,1); sf_oaxa(FGm,ax,2); if (Gtot) { sf_oaxa(FG,at,1); sf_oaxa(FG,ax,2); } if (Htot) { sf_oaxa(FH,at,1); sf_oaxa(FH,ax,2); } if (pandq) { sf_oaxa(Fp,at,1); sf_oaxa(Fp,ax,2); sf_oaxa(Fq,at,1); sf_oaxa(Fq,ax,2); } if (twin) { sf_oaxa(Ftwin,at,1); sf_oaxa(Ftwin,ax,2); } /*------------------------------------------------------------*/ /* Allocate arrays */ /*------------------------------------------------------------*/ /* Downgoing wavefields - Time */ pplus0 = (float *)calloc(nt*ntr,sizeof(float)); sf_floatread(pplus0,nt*ntr,Fplus); pplus = (float *)calloc(nt*ntr,sizeof(float)); memcpy(pplus,pplus0,nt*ntr*sizeof(float)); pplustemp = (float *)calloc(nt*ntr,sizeof(float)); pplusinv = (float *)calloc(nt*ntr,sizeof(float)); qplus = (float *)calloc(nt*ntr,sizeof(float)); qplustemp = (float *)calloc(nt*ntr,sizeof(float)); /* Downgoing wavefields - Frequency */ Pplus = (float *)calloc(2*nf*ntr,sizeof(float)); Qplus = (float *)calloc(2*nf*ntr,sizeof(float)); /* The three flags of fft1 are: inv, sym, and opt */ fft1(pplus0,Pplus,Fplus,0,0,1); memcpy(Qplus,Pplus,2*nf*ntr*sizeof(float)); /* Upgoing wavefields - Time */ pminus = (float *)calloc(nt*ntr,sizeof(float)); qminus = (float *)calloc(nt*ntr,sizeof(float)); /* Downgoing wavefields - Frequency */ Pminus = (float *)calloc(2*nf*ntr,sizeof(float)); Qminus = (float *)calloc(2*nf*ntr,sizeof(float)); /* This is currently NOT used */ /* Transpose of p00plus_xxxx_xxxx */ for (ix=0; ix<ntr; ix++) { for (it=0; it<nt; it++) { pplusinv[ix*ntr+it]=pplus0[it*ntr+ix]; } } /* Single trace (in frequency) of the downgoing wavefield */ Pplus_trace = (float *)calloc(2*nf,sizeof(float)); Qplus_trace = (float *)calloc(2*nf,sizeof(float)); /* Output wavefields */ Gp = (float *)calloc(nt*ntr,sizeof(float)); Gm = (float *)calloc(nt*ntr,sizeof(float)); if (Gtot) { G = (float *)calloc(nt*ntr,sizeof(float)); } if (Htot) { H = (float *)calloc(nt*ntr,sizeof(float)); } /* Time-reversal flag */ if (conj) { mode = -1; } else { mode = +1; } /* Load the reflection response into the memory */ if (verb) fprintf(stderr,"Before loading R %d\n",2*nf*ntr); Refl = (float *)calloc(2*nf*ntr*nshots,sizeof(float)); /* Read REFL_000.rsf */ filename1 = sf_getstring("refl"); /* 000.rsf are 7 characters */ len = strlen(filename1)-7; /* copy the filename without 000.rsf */ strncpy(filename2,filename1,len); filename2[len] = '\0'; if (verb) fprintf(stderr,"filename2 is: %s and len is: %d\n",filename2,len); /*if (NULL == filename1) { fprintf(stderr,"Cannot read header file %s",filename1); }*/ for (ishot=0; ishot<nshots; ishot++) { /* write xxx.rsf in the string filename3 */ sprintf(filename3,"%03d.rsf",ishot); for (i=0; i<7; i++) filename2[len+i] = filename3[i]; filename2[len+7] = '\0'; if (verb) fprintf(stderr,"Loading %s in memory\n",filename2); FRefl = sf_input(filename2); sf_floatread(&Refl[ishot*2*nf*ntr],2*nf*ntr,FRefl); sf_fileclose(FRefl); /*if (verb) fprintf(stderr,"Iteration %d\n",ishot);*/ } /* Build time-window */ tw = (int *)calloc(ntr,sizeof(int)); window = (float *)calloc(nt*ntr,sizeof(float)); /*memset(window,0,nt*ntr*sizeof(float));*/ /* I am not sure why I set it to this value */ /*for (ix=0; ix<ntr; ix++) { tw[ix] = nt*dt+ot+0.15; }*/ if (verb) fprintf(stderr,"Build the time-window\n"); for (ix=0; ix<ntr; ix++) { for (it=0; it<nt; it++) { if (pplus0[it+ix*nt]>eps) { /*tw[ix] = it*dt+ot;*/ tw[ix] = it; /*if (verb) fprintf(stderr,"%d %d\n",ix,it);*/ break; } } } for (ix=0; ix<ntr; ix++) { twc = (int)(tw[ix]-shift); twa = (int)(-twc+shift+nt); /*if (verb) fprintf(stderr,"%d %d\n",twc,twa);*/ for (it=0; it<nt; it++) { if ((it<twc) || (it>twa)) { window[it+ix*nt] = 1.0; } } } /* Smoothing of the window */ /* Should I implement flags for rect and iter? */ /* Look at Msmooth.c to understand below */ n[0] = nt; n[1] = ntr; s[0] = 1; s[1] = nt; rect[0] = 5; rect[1] = 5; for (ix=0; ix <= 1; ix++) { if (rect[ix] <= 1) continue; tr = sf_triangle_init (rect[ix],n[ix],false); for (it=0; it < (nt*ntr/n[ix]); it++) { i0 = sf_first_index (ix,it,1+1,n,s); for (iter=0; iter < 2; iter++) { sf_smooth2 (tr,i0,s[ix],false,window); } } sf_triangle_close(tr); } /* Tapering */ pi = 4.0*atan(1.0); /*fprintf(stderr,"pi: %f\n",pi); fprintf(stderr,"ntr: %d\n",ntr);*/ taper = (float *)calloc(ntr,sizeof(float)); memset(taper,0,ntr*sizeof(float)); for (ix=0; ix<151; ix++) { taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/300))); taper[ntr-ix-1] = taper[ix]; } /*for (ix=(ntr-1); ix>(701-151-1); ix--) { taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/300))); }*/ for (ix=151; ix<(ntr-151); ix++) { taper[ix] = 1.0; } /*for (ix=0; ix<ntr; ix++) { fprintf(stderr,"taper[%d]: %f\n",ix,taper[ix]); }*/ FRefl = sf_input("refl"); /*------------------------------------------------------------*/ /* Loop over iterations */ /*------------------------------------------------------------*/ if (verb) fprintf(stderr,"Beginning of loop over iterations\n"); for (iter=0; iter<niter; iter++) { /* Set Pminus and Qminus to 0 */ memset(Pminus,0,2*nf*ntr*sizeof(float)); memset(Qminus,0,2*nf*ntr*sizeof(float)); /*------------------------------------------------------------*/ /* Loop over shot positions */ /*------------------------------------------------------------*/ if (verb) fprintf(stderr,"Beginning of loop over shot positions\n"); for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,a,b,c,d,e,f) \ shared(Pminus,Qminus,Pplus,Qplus,taper,Refl) #endif for (ix=0; ix<ntr; ix++) { /* Loop over frequencies */ #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(x + yi)(u + vi) = (xu - yv) + (xv + yu)i*/ a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot]; b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot]; c = Pplus[ishot*2*nf+it]; d = Pplus[ishot*2*nf+it+1]; e = Qplus[ishot*2*nf+it]; f = Qplus[ishot*2*nf+it+1]; Pminus[ix*2*nf+it] += a*c - mode*b*d; Pminus[ix*2*nf+it+1] += mode*a*d + b*c; Qminus[ix*2*nf+it] += a*e - mode*b*f; Qminus[ix*2*nf+it+1] += mode*a*f + b*e; } /* End of loop over frequencies */ } /* End of loop over receivers (traces) */ if (verb) if(ishot%50==0) fprintf(stderr,"Trace %d\n",ishot); } /* End of loop over shot positions */ /* Save a copy of pplus and qplus before creating their next iteration */ memcpy(pplustemp,pplus,nt*ntr*sizeof(float)); memcpy(qplustemp,qplus,nt*ntr*sizeof(float)); /* Build the next iteration of Pplus and Qplus */ fft1(Pminus,pminus,FRefl,1,0,1); fft1(Qminus,qminus,FRefl,1,0,1); if (verb) fprintf(stderr,"Build the next iteration of pplus and qplus\n"); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(pminus,qminus,pplus,qplus,pplus0,window) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { pplus[it+ix*nt] = pplus0[it+ix*nt] - scale*window[it+ix*nt]*pminus[it+ix*nt]; qplus[it+ix*nt] = pplus0[it+ix*nt] + scale*window[it+ix*nt]*qminus[it+ix*nt]; } } fft1(pplus,Pplus,Fplus,0,0,1); fft1(qplus,Qplus,Fplus,0,0,1); if (verb) fprintf(stderr,"%d %d\n",ix,it); if(iter%10==0) fprintf(stderr,"Iteration %d\n",iter); } /* End of loop over iterations */ /* Build Gp and Gm */ if (verb) fprintf(stderr,"Build Gp and Gm\n"); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(Gp,Gm,G,H,pminus,qminus,pplustemp,qplustemp,pplus0) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { Gp[it+ix*nt] = 0.5*( pplustemp[it+ix*nt] + scale*pminus[it+ix*nt] + qplustemp[it+ix*nt] - scale*qminus[it+ix*nt] ); Gm[it+ix*nt] = 0.5*( pplustemp[it+ix*nt] + scale*pminus[it+ix*nt] - qplustemp[it+ix*nt] + scale*qminus[it+ix*nt] ); if (Gtot) { G[it+ix*nt] = pplustemp[it+ix*nt] + scale*pminus[it+ix*nt]; } if (Htot) { H[it+ix*nt] = qplustemp[it+ix*nt] - scale*qminus[it+ix*nt]; } /*p[it+ix*nt] = 1.0*( pplus[it+ix*nt] + scale*pminus[it+ix*nt] ); q[it+ix*nt] = 1.0*( qplus[it+ix*nt] - scale*qminus[it+ix*nt] );*/ } } /* Write the final result */ /*FRefl = sf_input(argv[1]);*/ /*fft1(Gp,,FRefl,1,0,0); fft1(Gm,,FRefl,1,0,0);*/ sf_floatwrite(Gp,nt*ntr,FGp); sf_floatwrite(Gm,nt*ntr,FGm); if (Gtot) { sf_floatwrite(G,nt*ntr,FG); sf_fileclose(FG); free(G); } if (Htot) { sf_floatwrite(H,nt*ntr,FH); sf_fileclose(FH); free(H); } if (pandq) { sf_floatwrite(pplustemp,nt*ntr,Fp); sf_floatwrite(pminus,nt*ntr,Fq); sf_fileclose(Fp); sf_fileclose(Fq); } if (twin) { sf_floatwrite(window,nt*ntr,Ftwin); sf_fileclose(Ftwin); } sf_fileclose(Fplus); sf_fileclose(FRefl); sf_fileclose(FGp); sf_fileclose(FGm); free(Gp); free(Gm); free(pplus); free(pplusinv); free(pplustemp); free(Pplus); free(Pplus_trace); free(Pminus); free(qplus); free(qplustemp); free(Qplus); free(Qplus_trace); free(Qminus); free(Refl); free(window); free(tw); free(filename1); exit (0); }
void fft2(double ar[], double ai[], int n, int nmax, int flag) { int i, iter, j, k; double *p, *q, *wr, *wi; if(n < 2) { fprintf(stderr, "Error : Illegal parameter in fft2()\n"); return; } iter = 0; i = n; while((i /= 2) != 0) iter++; j = 1; for(i = 1; i <= iter; i++) j *= 2; if(n != j) { fprintf(stderr, "Error : n != 2 ^ k in fft2()\n"); return; } wr = (double *)malloc(n * sizeof(double)); if(wr == NULL) { fprintf(stderr, "Error : Out of memory in fft2()\n"); return; } wi = (double *)malloc(n * sizeof(double)); if(wi == NULL) { fprintf(stderr, "Error : Out of memory in fft2()\n"); free((char *)wr); return; } for(j = 0; j < n; j++, k += nmax) { for(i = 0, p = ar + j, q = ai + j; i < n; i++, p += nmax, q += nmax) { *(wr + i) = *p; *(wi + i) = *q; } fft1(wr, wi, n, iter, flag); for(i = 0, p = ar + j, q = ai + j; i < n; i++, p += nmax, q += nmax) { *p = *(wr + i); *q = *(wi + i); } } for(i = k = 0; i < n; i++, k += nmax) { for(j = 0, p = ar + k, q = ai + k; j < n; j++) { *(wr + j) = *p++; *(wi + j) = *q++; } fft1(wr, wi, n, iter, flag); for(j = 0, p = ar + k, q = ai + k; j < n; j++) { *p++ = *(wr + j); *q++ = *(wi + j); } } free((char *)wr); free((char *)wi); return; }