Matrix2 psf2otf(Matrix& psf, Matrix& outSize) { Matrix psfSize(1, 2); psfSize(0, 0) = psf.size1(); psfSize(0, 1) = psf.size2(); Matrix padSize(1, 2); padSize = outSize - psfSize; psf = padarray(psf, padSize); psf = circshift(psf, -Matfloor(psfSize / 2)); Matrix2 otf = fft2(psf); return otf; }
inline typename enable_if2 < is_arma_type<T1>::value, Mat< std::complex<typename T1::pod_type> > >::result fft2(const T1& A, const uword n_rows, const uword n_cols) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const unwrap<T1> tmp(A); const Mat<eT>& B = tmp.M; const bool do_resize = (B.n_rows != n_rows) || (B.n_cols != n_cols); return fft2( do_resize ? resize(B,n_rows,n_cols) : B ); }
void _rolloff2(Array<complex<T> > &in, Array<complex<T> > &out, Array<T> &kernel_table, int32_t cropfilt) { /* get grid dimensionality for scaling */ int64_t gridMtx = in.dimensions(0); int64_t effMtx = out.dimensions(0); T osf = (T) gridMtx / (T) effMtx; osf *= osf; /* delta function at k0 to sample the grid kernel */ Array<T> delta_crd(2); Array<T> delta_wgt(1); delta_wgt(0) = (T) 1; Array<complex<T> > delta_dat(1); delta_dat(0) = complex<T>(1,0); /* create another grid the same size as the oversampled grid * to hold the deapodization filter. */ Array<complex<T> > rolloff(in.dimensions_vector()); _grid2(delta_dat, delta_crd, delta_wgt, rolloff, kernel_table, (T) 0., (T) 0.); fft2(rolloff, rolloff, FFTW_FORWARD); /* take magnitude of each element and divide */ for (uint64_t i=0; i<in.size(); ++i) { T mag = abs(rolloff(i)); if (mag > 0.) rolloff(i) = in(i)/mag * osf; else rolloff(i) = complex<T>(0.); } /* reduce the grid size */ out.insert(rolloff); /* apply circle filter */ if (cropfilt) crop_circle(out); }
int main(int argc, char* argv[]) { clock_t tstart, tend; double duration; /*Flag*/ bool verb, cmplx; /*I/O*/ sf_file Fsrc,Fo,Frec; /* I/O files */ sf_file left, right; /*left/right matrix*/ sf_file Fvel, Fden, Ffft; /*Model*/ sf_axis at,az,ax; /* cube axes */ /* I/O arrays*/ float *src; /*point source, distributed source*/ float **lt, **rt; float **vel, **den, **c11; /* Grid index variables */ int it,iz,im,ik,ix,i,j; int nt,nz,nx, m2, nk, nkx, nkz, nzx, nz2, nx2, nzx2, n1, n2, pad1; float cx, cz; float kx, kz, dkx, dkz, kx0, kz0; float dx, dz, dt, d1, d2; float ox, oz; sf_complex *cwavex, *cwavez, *cwavemx, *cwavemz; float **record; float **wavex, **wavez; float *curtxx, *pretxx; float *curvx, *prevx, *curvz, *prevz; /*source*/ spara sp={0}; int srcrange; float srctrunc; bool srcdecay; float slx, slz; int spx, spz; /*options*/ float gdep; int gp; tstart = clock(); sf_init(argc,argv); if(!sf_getbool("verb",&verb)) verb=false; /* verbosity */ Fvel = sf_input("vel"); Fden = sf_input("den"); /* setup I/O files */ Fsrc = sf_input ("in" ); Fo = sf_output("out"); Frec = sf_output("rec"); /*record*/ /* Read/Write axes */ at = sf_iaxa(Fsrc,1); nt = sf_n(at); dt = sf_d(at); ax = sf_iaxa(Fvel,2); nx = sf_n(ax); dx = sf_d(ax); ox=sf_o(ax); az = sf_iaxa(Fvel,1); nz = sf_n(az); dz = sf_d(az); oz=sf_o(az); sf_oaxa(Fo,az,1); sf_oaxa(Fo,ax,2); sf_oaxa(Fo,at,3); /*set for record*/ sf_oaxa(Frec, at, 1); sf_oaxa(Frec, ax, 2); if (!sf_getbool("cmplx",&cmplx)) cmplx=false; /* use complex FFT */ if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ nk = fft2_init(cmplx,pad1,nz,nx,&nz2,&nx2); nzx = nz*nx; nzx2 = nz2*nx2; /* propagator matrices */ left = sf_input("left"); right = sf_input("right"); if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx); if (!sf_histint(left,"n2",&m2)) sf_error("Need n2= in left"); if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2); if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk); lt = sf_floatalloc2(nzx,m2); rt = sf_floatalloc2(m2,nk); sf_floatread(lt[0],nzx*m2,left); sf_floatread(rt[0],m2*nk,right); /*model veloctiy & density*/ if (!sf_histint(Fvel,"n1", &n1) || n1 != nz) sf_error("Need n1=%d in vel", nz); if (!sf_histfloat(Fvel,"d1", &d1) || d1 != dz) sf_error("Need d1=%d in vel", dz); if (!sf_histint(Fvel,"n2", &n2) || n2 != nx) sf_error("Need n2=%d in vel", nx); if (!sf_histfloat(Fvel,"d2", &d2) || d2 != dx) sf_error("Need d2=%d in vel", dx); if (!sf_histint(Fden,"n1", &n1) || n1 != nz) sf_error("Need n1=%d in den", nz); if (!sf_histfloat(Fden,"d1", &d1) || d1 != dz) sf_error("Need d1=%d in den", dz); if (!sf_histint(Fden,"n2", &n2) || n2 != nx) sf_error("Need n2=%d in den", nx); if (!sf_histfloat(Fden,"d2", &d2) || d2 != dx) sf_error("Need d2=%d in den", dx); vel = sf_floatalloc2(nz, nx); den = sf_floatalloc2(nz, nx); c11 = sf_floatalloc2(nz, nx); sf_floatread(vel[0], nzx, Fvel); sf_floatread(den[0], nzx, Fden); for (ix = 0; ix < nx; ix++) { for (iz = 0; iz < nz; iz++) { c11[ix][iz] = den[ix][iz]*vel[ix][iz]*vel[ix][iz]; } } /*parameters of fft*/ Ffft = sf_input("fft"); if (!sf_histint(Ffft,"n1", &nkz)) sf_error("Need n1 in fft"); if (!sf_histint(Ffft,"n2", &nkx)) sf_error("Need n2 in fft"); if ( nkx*nkz != nk ) sf_error("Need nk=nkx*nkz, nk=%d, nkx=%d, nkz=%d", nk, nkx, nkz); if (!sf_histfloat(Ffft,"d1", &dkz)) sf_error("Need d1 in fft"); if (!sf_histfloat(Ffft,"d2", &dkx)) sf_error("Need d2 in fft"); if (!sf_histfloat(Ffft,"o1", &kz0)) sf_error("Need o1 in fft"); if (!sf_histfloat(Ffft,"o2", &kx0)) sf_error("Need o2 in fft"); /*parameters of geometry*/ if (!sf_getfloat("gdep", &gdep)) gdep = 0.0; /*depth of geophone (meter)*/ if (gdep <0.0) sf_error("gdep need to be >=0.0"); /*source and receiver location*/ if (!sf_getfloat("slx", &slx)) slx=-1.0; /*source location x */ if (!sf_getint("spx", &spx)) spx = -1; /*source location x (index)*/ if((slx<0 && spx <0) || (slx>=0 && spx >=0 )) sf_error("Need src location"); if (slx >= 0 ) spx = (int)((slx-ox)/dx+0.5); if (!sf_getfloat("slz", &slz)) slz = -1.0; /* source location z */ if (!sf_getint("spz", &spz)) spz=-1; /*source location z (index)*/ if((slz<0 && spz <0) || (slz>=0 && spz >=0 )) sf_error("Need src location"); if (slz >= 0 ) spz = (int)((slz-ox)/dz+0.5); if (!sf_getfloat("gdep", &gdep)) gdep = -1.0; /* recorder depth on grid*/ if (!sf_getint("gp", &gp)) gp=0; /* recorder depth on index*/ if ( gdep>=oz) { gp = (int)((gdep-oz)/dz+0.5);} if (gp < 0.0) sf_error("gdep need to be >=oz"); /*source and receiver location*/ if (!sf_getbool("srcdecay", &srcdecay)) srcdecay=false; /*source decay*/ if (!sf_getint("srcrange", &srcrange)) srcrange=10; /*source decay range*/ if (!sf_getfloat("srctrunc", &srctrunc)) srctrunc=100; /*trunc source after srctrunc time (s)*/ /* read wavelet & reflectivity */ src = sf_floatalloc(nt); sf_floatread(src,nt,Fsrc); curtxx = sf_floatalloc(nzx2); curvx = sf_floatalloc(nzx2); curvz = sf_floatalloc(nzx2); pretxx = sf_floatalloc(nzx); prevx = sf_floatalloc(nzx); prevz = sf_floatalloc(nzx); cwavex = sf_complexalloc(nk); cwavez = sf_complexalloc(nk); cwavemx = sf_complexalloc(nk); cwavemz = sf_complexalloc(nk); wavex = sf_floatalloc2(nzx2,m2); wavez = sf_floatalloc2(nzx2,m2); record = sf_floatalloc2(nt,nx); ifft2_allocate(cwavemx); ifft2_allocate(cwavemz); for (iz=0; iz < nzx; iz++) { pretxx[iz]=0.; prevx[iz] =0.; prevz[iz] =0.; } for (iz=0; iz < nzx2; iz++) { curtxx[iz]=0.; curvx[iz]=0.; curvz[iz]=0.; } /* Check parameters*/ if(verb) { sf_warning("======================================"); #ifdef SF_HAS_FFTW sf_warning("FFTW is defined"); #endif #ifdef SF_HAS_COMPLEX_H sf_warning("Complex is defined"); #endif sf_warning("nx=%d nz=%d nzx=%d dx=%f dz=%f", nx, nz, nzx, dx, dz); sf_warning("nkx=%d nkz=%d dkx=%f dkz=%f nk=%d", nkx, nkz, dkx, dkz, nk); sf_warning("nx2=%d nz2=%d nzx2=%d", nx2, nz2, nzx2); sf_warning("======================================"); } /*set source*/ sp.trunc=srctrunc; sp.srange=srcrange; sp.alpha=0.5; sp.decay=srcdecay?1:0; /* MAIN LOOP */ for (it=0; it<nt; it++) { if(verb) sf_warning("it=%d/%d;",it,nt-1); /*vx, vz--- matrix multiplication */ fft2(curtxx,cwavex); /* P^(k,t) */ for (im = 0; im < m2; im++) { for (ik = 0; ik < nk; ik++) { kx = kx0+dkx*(ik/nkz); kz = kz0+dkz*(ik%nkz); #ifdef SF_HAS_COMPLEX_H cwavemz[ik] = cwavex[ik]*rt[ik][im]; cwavemx[ik] = fplus(kx,dx)*cwavemz[ik]; cwavemz[ik] = fplus(kz,dz)*cwavemz[ik]; #else cwavemz[ik] = sf_crmul(cwavex[ik],rt[ik][im]); cwavemx[ik] = sf_cmul(fplus(kx,dx), cwavemz[ik]); cwavemz[ik] = sf_cmul(fplus(kz,dz), cwavemz[ik]); #endif } ifft2(wavex[im], cwavemx); /* dp/dx */ ifft2(wavez[im], cwavemz); /* dp/dz */ } for (ix = 0; ix < nx; ix++) { for (iz = 0; iz < nz; iz++) { i = iz+ix*nz; /* original grid */ j = iz+ix*nz2; /* padded grid */ cx = 0.0; cz = 0.0; for (im=0; im<m2; im++) { cx += lt[im][i]*wavex[im][j]; cz += lt[im][i]*wavez[im][j]; } curvx[j] = -1*dt/den[ix][iz]*cx + prevx[i]; /*vx(t+dt/2) = -dt/rho*dp/dx(t) + vx(t-dt/2) */ prevx[i] = curvx[j]; curvz[j] = -1*dt/den[ix][iz]*cz + prevz[i]; prevz[i] = curvz[j]; } } /*txx--- matrix multiplication */ fft2(curvx, cwavex); fft2(curvz, cwavez); for (im = 0; im < m2; im++) { for (ik = 0; ik < nk; ik++ ) { kx = kx0 + dkx*(ik/nkz); kz = kz0 + dkz*(ik%nkz); #ifdef SF_HAS_COMPLEX_H cwavemz[ik] = cwavez[ik]*rt[ik][im]; cwavemx[ik] = cwavex[ik]*rt[ik][im]; cwavemx[ik] = fminu(kx,dx)*cwavemx[ik]; cwavemz[ik] = fminu(kz,dz)*cwavemz[ik]; #else cwavemz[ik] = sf_crmul(cwavez[ik],rt[ik][im]); cwavemx[ik] = sf_crmul(cwavex[ik],rt[ik][im]); cwavemx[ik] = sf_cmul(fplus(kx,dx), cwavemx[ik]); cwavemz[ik] = sf_cmul(fplus(kz,dz), cwavemz[ik]); #endif } ifft2(wavex[im], cwavemx); /* dux/dx */ ifft2(wavez[im], cwavemz); /* duz/dz */ } for (ix = 0; ix < nx; ix++) { for (iz = 0; iz < nz; iz++) { i = iz+ix*nz; /* original grid */ j = iz+ix*nz2; /* padded grid */ cx = 0.0; cz = 0.0; for (im=0; im<m2; im++) { cx += lt[im][i]*wavex[im][j]; cz += lt[im][i]*wavez[im][j]; } curtxx[j] = -1*dt*c11[ix][iz]*(cx+cz) + pretxx[i]; } } if ((it*dt)<=sp.trunc ) { curtxx[spz+spx*nz2] += src[it]*dt; } for (ix = 0; ix < nx; ix++) { /* write wavefield to output */ sf_floatwrite(pretxx+ix*nz,nz,Fo); } /*record*/ for (ix = 0; ix < nx; ix++){ record[ix][it] = pretxx[ix*nz+gp]; } for (ix = 0; ix < nx; ix++) { for (iz = 0; iz < nz; iz++) { i = iz+ix*nz; /* original grid */ j = iz+ix*nz2; /* padded grid */ pretxx[i] = curtxx[j]; } } }/*End of MAIN LOOP*/ if(verb) sf_warning("."); for ( ix = 0; ix < nx; ix++) { sf_floatwrite(record[ix], nt, Frec); } tend = clock(); duration=(double)(tend-tstart)/CLOCKS_PER_SEC; sf_warning(">> The CPU time of sfsglr is: %f seconds << ", duration); exit (0); }
int main(int argc, char *argv[]) { /* Global variable & function declarations */ struct GModule *module; struct { struct Option *orig, *real, *imag; } opt; const char *Cellmap_real, *Cellmap_imag; const char *Cellmap_orig; int realfd, imagfd, outputfd, maskfd; /* the input and output file descriptors */ struct Cell_head realhead, imaghead; DCELL *cell_real, *cell_imag; CELL *maskbuf; int i, j; /* Loop control variables */ int rows, cols; /* number of rows & columns */ long totsize; /* Total number of data points */ double (*data)[2]; /* Data structure containing real & complex values of FFT */ G_gisinit(argv[0]); /* Set description */ module = G_define_module(); G_add_keyword(_("imagery")); G_add_keyword(_("transformation")); G_add_keyword(_("Fast Fourier Transform")); module->description = _("Inverse Fast Fourier Transform (IFFT) for image processing."); /* define options */ opt.real = G_define_standard_option(G_OPT_R_INPUT); opt.real->key = "real"; opt.real->description = _("Name of input raster map (image fft, real part)"); opt.imag = G_define_standard_option(G_OPT_R_INPUT); opt.imag->key = "imaginary"; opt.imag->description = _("Name of input raster map (image fft, imaginary part"); opt.orig = G_define_standard_option(G_OPT_R_OUTPUT); opt.orig->description = _("Name for output raster map"); /*call parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); Cellmap_real = opt.real->answer; Cellmap_imag = opt.imag->answer; Cellmap_orig = opt.orig->answer; /* get and compare the original window data */ Rast_get_cellhd(Cellmap_real, "", &realhead); Rast_get_cellhd(Cellmap_imag, "", &imaghead); if (realhead.proj != imaghead.proj || realhead.zone != imaghead.zone || realhead.north != imaghead.north || realhead.south != imaghead.south || realhead.east != imaghead.east || realhead.west != imaghead.west || realhead.ew_res != imaghead.ew_res || realhead.ns_res != imaghead.ns_res) G_fatal_error(_("The real and imaginary original windows did not match")); Rast_set_window(&realhead); /* set the window to the whole cell map */ /* open input raster map */ realfd = Rast_open_old(Cellmap_real, ""); imagfd = Rast_open_old(Cellmap_imag, ""); /* get the rows and columns in the current window */ rows = Rast_window_rows(); cols = Rast_window_cols(); totsize = rows * cols; /* Allocate appropriate memory for the structure containing the real and complex components of the FFT. DATA[0] will contain the real, and DATA[1] the complex component. */ data = G_malloc(rows * cols * 2 * sizeof(double)); /* allocate the space for one row of cell map data */ cell_real = Rast_allocate_d_buf(); cell_imag = Rast_allocate_d_buf(); #define C(i, j) ((i) * cols + (j)) /* Read in cell map values */ G_message(_("Reading raster maps...")); for (i = 0; i < rows; i++) { Rast_get_d_row(realfd, cell_real, i); Rast_get_d_row(imagfd, cell_imag, i); for (j = 0; j < cols; j++) { data[C(i, j)][0] = cell_real[j]; data[C(i, j)][1] = cell_imag[j]; } G_percent(i+1, rows, 2); } /* close input cell maps */ Rast_close(realfd); Rast_close(imagfd); /* Read in cell map values */ G_message(_("Masking raster maps...")); maskfd = Rast_maskfd(); if (maskfd >= 0) { maskbuf = Rast_allocate_c_buf(); for (i = 0; i < rows; i++) { Rast_get_c_row(maskfd, maskbuf, i); for (j = 0; j < cols; j++) { if (maskbuf[j] == 0) { data[C(i, j)][0] = 0.0; data[C(i, j)][1] = 0.0; } } G_percent(i+1, rows, 2); } Rast_close(maskfd); G_free(maskbuf); } #define SWAP1(a, b) \ do { \ double temp = (a); \ (a) = (b); \ (b) = temp; \ } while (0) #define SWAP2(a, b) \ do { \ SWAP1(data[(a)][0], data[(b)][0]); \ SWAP1(data[(a)][1], data[(b)][1]); \ } while (0) /* rotate the data array for standard display */ G_message(_("Rotating data...")); for (i = 0; i < rows; i++) for (j = 0; j < cols / 2; j++) SWAP2(C(i, j), C(i, j + cols / 2)); for (i = 0; i < rows / 2; i++) for (j = 0; j < cols; j++) SWAP2(C(i, j), C(i + rows / 2, j)); /* perform inverse FFT */ G_message(_("Starting Inverse FFT...")); fft2(1, data, totsize, cols, rows); /* open the output cell map */ outputfd = Rast_open_fp_new(Cellmap_orig); /* Write out result to a new cell map */ G_message(_("Writing raster map <%s>..."), Cellmap_orig); for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) cell_real[j] = data[C(i, j)][0]; Rast_put_d_row(outputfd, cell_real); G_percent(i+1, rows, 2); } Rast_close(outputfd); G_free(cell_real); G_free(cell_imag); fft_colors(Cellmap_orig); /* Release memory resources */ G_free(data); G_done_msg(" "); exit(EXIT_SUCCESS); }
void fftu2(unsigned int D, const long dimensions[D], unsigned long flags, const long ostrides[D], complex float* dst, const long istrides[D], const complex float* src) { fft2(D, dimensions, flags, ostrides, dst, istrides, src); fftscale2(D, dimensions, flags, ostrides, dst, ostrides, dst); }
int psp4(float **wvfld, float **wvfld0, float **dat, float **dat_v, float *vel, pspar par) /*< pseudo-spectral wave extrapolation using wavefield injection >*/ { /*survey parameters*/ int nx, nz; float dx, dz; int n_srcs; int *spx, *spz; int gpz, gpx, gpl; int gpz_v, gpx_v, gpl_v; int snap; /*fft related*/ bool cmplx; int pad1; /*absorbing boundary*/ bool abc; int nbt, nbb, nbl, nbr; float ct,cb,cl,cr; /*source parameters*/ int src; /*source type*/ int nt; float dt,*f0,*t0,*A; /*misc*/ bool verb, ps; float vref; int nx1, nz1; /*domain of interest*/ int it,iz,ik,ix,i,j; /* index variables */ int nk,nzx,nz2,nx2,nzx2,nkz,nth; int it1, it2, its; float dkx,dkz,kx0,kz0,vref2,kx,kz,k,t; float c, old; /*wave prop arrays*/ float *vv; sf_complex *cwave,*cwavem; float *wave,*curr,*prev,*lapl; /*source*/ float **rick; float freq; int fft_size; /*passing the parameters*/ nx = par->nx; nz = par->nz; dx = par->dx; dz = par->dz; n_srcs= par->n_srcs; spx = par->spx; spz = par->spz; gpz = par->gpz; gpx = par->gpx; gpl = par->gpl; gpz_v = par->gpz_v; gpx_v = par->gpx_v; gpl_v = par->gpl_v; snap = par->snap; cmplx = par->cmplx; pad1 = par->pad1; abc = par->abc; nbt = par->nbt; nbb = par->nbb; nbl = par->nbl; nbr = par->nbr; ct = par->ct; cb = par->cb; cl = par->cl; cr = par->cr; src = par->src; nt = par->nt; dt = par->dt; f0 = par->f0; t0 = par->t0; A = par->A; verb = par->verb; ps = par->ps; vref = par->vref; #ifdef _OPENMP #pragma omp parallel { nth = omp_get_num_threads(); } #else nth = 1; #endif if (verb) sf_warning(">>>> Using %d threads <<<<<", nth); nz1 = nz-nbt-nbb; nx1 = nx-nbl-nbr; nk = fft2_init(cmplx,pad1,nz,nx,&nz2,&nx2); nzx = nz*nx; nzx2 = nz2*nx2; dkz = 1./(nz2*dz); kz0 = (cmplx)? -0.5/dz:0.; dkx = 1./(nx2*dx); kx0 = -0.5/dx; nkz = (cmplx)? nz2:(nz2/2+1); if(nk!=nx2*nkz) sf_error("wavenumber dimension mismatch!"); sf_warning("dkz=%f,dkx=%f,kz0=%f,kx0=%f",dkz,dkx,kz0,kx0); sf_warning("nk=%d,nkz=%d,nz2=%d,nx2=%d",nk,nkz,nz2,nx2); if(abc) abc_init(nz,nx,nz2,nx2,nbt,nbb,nbl,nbr,ct,cb,cl,cr); /* allocate and read/initialize arrays */ vv = sf_floatalloc(nzx); lapl = sf_floatalloc(nk); wave = sf_floatalloc(nzx2); curr = sf_floatalloc(nzx2); prev = sf_floatalloc(nzx2); cwave = sf_complexalloc(nk); cwavem = sf_complexalloc(nk); if (src==0) { rick = sf_floatalloc2(nt,n_srcs); for (i=0; i<n_srcs; i++) { for (it=0; it<nt; it++) { rick[i][it] = 0.f; } rick[i][(int)(t0[i]/dt)] = A[i]; /*time delay*/ freq = f0[i]*dt; /*peak frequency*/ fft_size = 2*kiss_fft_next_fast_size((nt+1)/2); ricker_init(fft_size, freq, 0); sf_freqfilt(nt,rick[i]); ricker_close(); } } else rick = NULL; for (iz=0; iz < nzx; iz++) { vv[iz] = vel[iz]*vel[iz]*dt*dt; } vref *= dt; vref2 = vref*vref; for (iz=0; iz < nzx2; iz++) { curr[iz] = 0.; prev[iz] = 0.; } /* constructing the pseudo-analytical op */ for (ix=0; ix < nx2; ix++) { kx = kx0+ix*dkx; for (iz=0; iz < nkz; iz++) { kz = kz0+iz*dkz; k = 2*SF_PI*hypot(kx,kz); if (ps) lapl[iz+ix*nkz] = -k*k; else lapl[iz+ix*nkz] = 2.*(cos(vref*k)-1.)/vref2; } } /* modeling */ /* step forward in time */ it1 = 0; it2 = nt; its = +1; /* MAIN LOOP */ for (it=it1; it!=it2; it+=its) { if(verb) sf_warning("it=%d/%d;",it,nt); /* matrix multiplication */ fft2(curr,cwave); for (ik = 0; ik < nk; ik++) { #ifdef SF_HAS_COMPLEX_H cwavem[ik] = cwave[ik]*lapl[ik]; #else cwavem[ik] = sf_cmul(cwave[ik],lapl[ik]); #endif } ifft2(wave,cwavem); #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,iz,i,j,old,c) #endif for (ix = 0; ix < nx; ix++) { for (iz=0; iz < nz; iz++) { i = iz+ix*nz; /* original grid */ j = iz+ix*nz2; /* padded grid */ old = c = curr[j]; c += c - prev[j]; prev[j] = old; c += wave[j]*vv[i]; curr[j] = c; } } if (NULL!=wvfld0) { /* wavefield injection */ if (snap > 0 && it%snap==0) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,iz,i,j,ik) #endif for (ix=0; ix<nx1; ix++) { for (iz=0; iz<nz1; iz++) { i = iz + nz1*ix; j = iz+nbt + (ix+nbl)*nz2; /* padded grid */ ik = iz+nbt + (ix+nbl)*nz; /* padded grid */ curr[j] += vv[ik]*wvfld0[it/snap][i]; } } } } else { /* source injection */ t = it*dt; for (i=0; i<n_srcs; i++) { for(ix=-1;ix<=1;ix++) { for(iz=-1;iz<=1;iz++) { ik = spz[i]+iz+nz*(spx[i]+ix); j = spz[i]+iz+nz2*(spx[i]+ix); if (src==0) { curr[j] += vv[ik]*rick[i][it]/(abs(ix)+abs(iz)+1); } else { curr[j] += vv[ik]*Ricker(t, f0[i], t0[i], A[i])/(abs(ix)+abs(iz)+1); } } } } } /*apply abc*/ if (abc) { abc_apply(curr); abc_apply(prev); } /* record data */ if (NULL!=dat) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix) #endif for (ix = 0; ix < gpl; ix++) { dat[ix][it] = curr[gpz+(ix+gpx)*nz2]; } } if (NULL!=dat_v) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(iz) #endif for (iz = 0; iz < gpl_v; iz++) { dat_v[iz][it] = curr[gpz_v+iz+(gpx_v)*nz2]; } } /* save wavefield */ if (snap > 0 && it%snap==0) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,iz,i,j) #endif for (ix=0; ix<nx1; ix++) { for (iz=0; iz<nz1; iz++) { i = iz + nz1*ix; j = iz+nbt + (ix+nbl)*nz2; /* padded grid */ wvfld[it/snap][i] = curr[j]; } } } } if(verb) sf_warning("."); /*free up memory*/ fft2_finalize(); if (abc) abc_close(); free(vv); free(lapl); free(wave); free(curr); free(prev); free(cwave); free(cwavem); return 0; }
int psp3(float **wvfld, float **wvfld1, float **dat, float **dat1, float *img, float *vel, pspar par) /*< pseudo-spectral back propagation of two receiver wavefields >*/ { /*survey parameters*/ int nx, nz; float dx, dz; int n_srcs; int *spx, *spz; int gpz, gpx, gpl; int gpz_v, gpx_v, gpl_v; int snap; /*fft related*/ bool cmplx; int pad1; /*absorbing boundary*/ bool abc; int nbt, nbb, nbl, nbr; float ct,cb,cl,cr; /*source parameters*/ int src; /*source type*/ int nt; float dt,*f0,*t0,*A; /*misc*/ bool verb, ps; float vref; int nx1, nz1; /*domain of interest*/ int it,iz,ik,ix,i,j; /* index variables */ int nk,nzx,nz2,nx2,nzx2,nkz,nth; int it1, it2, its; float dkx,dkz,kx0,kz0,vref2,kx,kz,k; float c, old; /*wave prop arrays*/ float *vv; sf_complex *cwave,*cwavem; float *wave,*curr,*curr1,*prev,*prev1,*lapl; /*passing the parameters*/ nx = par->nx; nz = par->nz; dx = par->dx; dz = par->dz; n_srcs= par->n_srcs; spx = par->spx; spz = par->spz; gpz = par->gpz; gpx = par->gpx; gpl = par->gpl; gpz_v = par->gpz_v; gpx_v = par->gpx_v; gpl_v = par->gpl_v; snap = par->snap; cmplx = par->cmplx; pad1 = par->pad1; abc = par->abc; nbt = par->nbt; nbb = par->nbb; nbl = par->nbl; nbr = par->nbr; ct = par->ct; cb = par->cb; cl = par->cl; cr = par->cr; src = par->src; nt = par->nt; dt = par->dt; f0 = par->f0; t0 = par->t0; A = par->A; verb = par->verb; ps = par->ps; vref = par->vref; #ifdef _OPENMP #pragma omp parallel { nth = omp_get_num_threads(); } #else nth = 1; #endif if (verb) sf_warning(">>>> Using %d threads <<<<<", nth); nz1 = nz-nbt-nbb; nx1 = nx-nbl-nbr; nk = fft2_init(cmplx,pad1,nz,nx,&nz2,&nx2); nzx = nz*nx; nzx2 = nz2*nx2; dkz = 1./(nz2*dz); kz0 = (cmplx)? -0.5/dz:0.; dkx = 1./(nx2*dx); kx0 = -0.5/dx; nkz = (cmplx)? nz2:(nz2/2+1); if(nk!=nx2*nkz) sf_error("wavenumber dimension mismatch!"); sf_warning("dkz=%f,dkx=%f,kz0=%f,kx0=%f",dkz,dkx,kz0,kx0); sf_warning("nk=%d,nkz=%d,nz2=%d,nx2=%d",nk,nkz,nz2,nx2); if(abc) abc_init(nz,nx,nz2,nx2,nbt,nbb,nbl,nbr,ct,cb,cl,cr); /* allocate and read/initialize arrays */ vv = sf_floatalloc(nzx); lapl = sf_floatalloc(nk); wave = sf_floatalloc(nzx2); curr = sf_floatalloc(nzx2); curr1 = sf_floatalloc(nzx2); prev = sf_floatalloc(nzx2); prev1 = sf_floatalloc(nzx2); cwave = sf_complexalloc(nk); cwavem = sf_complexalloc(nk); for (iz=0; iz < nzx; iz++) { vv[iz] = vel[iz]*vel[iz]*dt*dt; } vref *= dt; vref2 = vref*vref; for (iz=0; iz < nzx2; iz++) { curr[iz] = 0.; curr1[iz] = 0.; prev[iz] = 0.; prev1[iz] = 0.; } for (iz=0; iz < nz1*nx1; iz++) { img[iz] = 0.; } /* constructing the pseudo-analytical op */ for (ix=0; ix < nx2; ix++) { kx = kx0+ix*dkx; for (iz=0; iz < nkz; iz++) { kz = kz0+iz*dkz; k = 2*SF_PI*hypot(kx,kz); if (ps) lapl[iz+ix*nkz] = -k*k; else lapl[iz+ix*nkz] = 2.*(cos(vref*k)-1.)/vref2; } } /* step backward in time */ it1 = nt-1; it2 = -1; its = -1; /* MAIN LOOP */ for (it=it1; it!=it2; it+=its) { if(verb) sf_warning("it=%d/%d;",it,nt); /* first receiver wavefield */ /* matrix multiplication */ fft2(curr,cwave); for (ik = 0; ik < nk; ik++) { #ifdef SF_HAS_COMPLEX_H cwavem[ik] = cwave[ik]*lapl[ik]; #else cwavem[ik] = sf_cmul(cwave[ik],lapl[ik]); #endif } ifft2(wave,cwavem); #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,iz,i,j,old,c) #endif for (ix = 0; ix < nx; ix++) { for (iz=0; iz < nz; iz++) { i = iz+ix*nz; /* original grid */ j = iz+ix*nz2; /* padded grid */ old = c = curr[j]; c += c - prev[j]; prev[j] = old; c += wave[j]*vv[i]; curr[j] = c; } } /* inject data */ if (NULL!=dat) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix) #endif for (ix = 0; ix < gpl; ix++) { curr[gpz+(ix+gpx)*nz2] += vv[gpz+(ix+gpx)*nz]*dat[ix][it]; } } /*apply abc*/ if (abc) { abc_apply(curr); abc_apply(prev); } /* second receiver wavefield */ /* matrix multiplication */ fft2(curr1,cwave); for (ik = 0; ik < nk; ik++) { #ifdef SF_HAS_COMPLEX_H cwavem[ik] = cwave[ik]*lapl[ik]; #else cwavem[ik] = sf_cmul(cwave[ik],lapl[ik]); #endif } ifft2(wave,cwavem); #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,iz,i,j,old,c) #endif for (ix = 0; ix < nx; ix++) { for (iz=0; iz < nz; iz++) { i = iz+ix*nz; /* original grid */ j = iz+ix*nz2; /* padded grid */ old = c = curr1[j]; c += c - prev1[j]; prev1[j] = old; c += wave[j]*vv[i]; curr1[j] = c; } } /* inject data */ if (NULL!=dat1) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix) #endif for (ix = 0; ix < gpl; ix++) { curr1[gpz+(ix+gpx)*nz2] += vv[gpz+(ix+gpx)*nz]*dat1[ix][it]; } } /*apply abc*/ if (abc) { abc_apply(curr1); abc_apply(prev1); } /* cross-correlation imaging condition */ if (snap > 0 && it%snap==0) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,iz,i,j) #endif for (ix = 0; ix < nx1; ix++) { for (iz = 0; iz < nz1; iz++) { i = iz + nz1*ix; j = iz+nbt + (ix+nbl)*nz2; /* padded grid */ img[i] += curr[j]*curr1[j]; //!!!IMPORTANT!!! } } } /* save wavefield */ if (snap > 0 && it%snap==0) { #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,iz,i,j) #endif for (ix=0; ix<nx1; ix++) { for (iz=0; iz<nz1; iz++) { i = iz + nz1*ix; j = iz+nbt + (ix+nbl)*nz2; /* padded grid */ wvfld[it/snap][i] = curr[j]; wvfld1[it/snap][i] = curr1[j]; } } } } if(verb) sf_warning("."); /*free up memory*/ fft2_finalize(); if (abc) abc_close(); free(vv); free(lapl); free(wave); free(curr); free(curr1); free(prev); free(prev1); free(cwave); free(cwavem); return 0; }
void Kolo::update() { if (z_osu==1 && menu==0){ if ((j + rozmiar) < tab->wielkoscListy) { for (i = 0; i < rozmiar; i++) { test[i].real(tab->pobierzElement()); test[i].imag(0); } fftPoprzednie = fftAktualne; fftAktualne = fft2(test); do { czasp = czas; czas = song.getPositionMS(); if (czasp > czas) menu=5;//ofExit(); } while (czasp == czas); } else { menu=5; //ofExit(); } j += rozmiar; (fftAktualne<fftPoprzednie)? roznica=TRUE :NULL ; if(!proba) { roznica=FALSE; } if (((fftAktualne - fftPoprzednie) > prog) /*&& (bazax.size() < MAX_KOL)*/)//dodawanie nowego kolka jeøeli rÛønica > prÛg { // if (bazax.empty() || (opoznienie <= 0))//jeøeli jest wiÍcej niø MAX_KOL to nie dodaje proba=(kolka.empty() || ((kolka.back()->promien)<=MIN_OGRANICZENIE));// && roznica==TRUE) ); //roznica porownuje fftaktualne z fftpoprzednie - zeby nie tworzyl nowej nutki, jesli dzwiek tylko wzrasta - np jakas dluzsza nuta - trzeba ocenic jak to lapie i ewentualnie wrocic do opoznienie //majac ta roznice wlasciwie nie korzystam z MAX_KOL - w spadajacych kolkach //osu jest bez zmian if (proba) { Kolo * nowe_kolo = new Kolo; nowe_kolo->xCircle = (int)fftAktualne % (1024 - 2 * PROMIEN) + PROMIEN; nowe_kolo->yCircle = (int)fftAktualne % (768 - 2 * PROMIEN) + PROMIEN; if (kolka.size() > 0) { // cout << "jestem tu 1" << endl; while (odleglosc(nowe_kolo->xCircle, nowe_kolo->yCircle, kolka.back()->xCircle, kolka.back()->yCircle) < 2 * PROMIEN) { // cout << "jestem tu 2" << endl; if (static_cast<float>(odleglosc(nowe_kolo->xCircle, 0, kolka.back()->xCircle, 0)) < 1.5 * static_cast<float>( PROMIEN)) { // cout << "jestem tu 3" << endl; if (kolka.back()->xCircle < 700) nowe_kolo->xCircle += 30; else nowe_kolo->xCircle -= 30; } else if (static_cast<float>(odleglosc(0, nowe_kolo->yCircle, 0, kolka.back()->yCircle)) < 1.5 * static_cast<float>( PROMIEN)) { // cout << "jestem tu 4" << endl; if (kolka.back()->yCircle < 500) nowe_kolo->yCircle += 30; else nowe_kolo->yCircle -= 30; } } while (odleglosc(nowe_kolo->xCircle, nowe_kolo->yCircle, kolka.back()->xCircle, kolka.back()->yCircle) > 4 * PROMIEN) { // cout << "jestem tu 20 " << endl; if (static_cast<float>(odleglosc(nowe_kolo->xCircle, 0, kolka.back()->xCircle, 0)) > 2.8 * static_cast<float>( PROMIEN)) { // cout << "jestem tu 21 " << endl; if (nowe_kolo->xCircle - kolka.back()->xCircle > 0) nowe_kolo->xCircle -= 10; else nowe_kolo->xCircle += 10; } else if (static_cast<float>(odleglosc(0, nowe_kolo->yCircle, 0, kolka.back()->yCircle)) > 2.8 * static_cast<float>( PROMIEN)) { // cout << "jestem tu 22 " << endl; if (nowe_kolo->yCircle - kolka.back()->yCircle > 0) nowe_kolo->yCircle -= 10; else nowe_kolo->yCircle += 10; } } } nowe_kolo->promien = PROMIEN; nowe_kolo->czasKolka = TIME; //if(kolka.empty()) //{ nowe_kolo->kolor.set(ofRandom(255), ofRandom(255), ofRandom(255)); /*} else { if(kolka.back()->kolor.red>230 || kolka.back()->kolor.g>230 || kolka.back()->kolor.b>230) { nowe_kolo->kolor.set(ofRandom(120,245), ofRandom(120,245), ofRandom(120,245)); } if(kolka.back()->kolor.r<90 || kolka.back()->kolor.g<90 || kolka.back()->kolor.b<90) { nowe_kolo->kolor.set(ofRandom(120,245), ofRandom(120,245), ofRandom(120,245)); } nowe_kolo->kolor=(kolka.back()->kolor+5); }*/ kolka.push_back(nowe_kolo); roznica=FALSE; } //dodawanie nowego kolka je¿eli ró¿nica > próg } for (int i = 0; i < kolka.size(); i++) { kolka[i]->czasKolka = kolka[i]->czasKolka - 1; if (kolka[i]->promien >= MIN_OGRANICZENIE) kolka[i]->promien -= ZMNIEJSZANIE; }//zmniejszanie wszystkich kol wiêkszych od MIN_OGRANICZENIE for (int i = 0; i < kolka.size(); i++) { if (kolka[i]->czasKolka <= 0) { kolka.erase(kolka.begin() + i); if(wynik) wynik--; } }//usuwanie tych które zniknely } }
cv::Mat L0Smoothing(cv::Mat Im, double lambda = 0.02, double kappa = 2.0) { cv::Mat out(Im.rows, Im.cols, CV_32FC3); Matrix SR(Im.rows, Im.cols); Matrix SG(Im.rows, Im.cols); Matrix SB(Im.rows, Im.cols); for(int j = 0; j < Im.cols; ++j) { for(int i = 0; i < Im.rows; ++i) { cv::Vec3f& v1 = Im.at<cv::Vec3f>(i, j); SR(i, j) = v1[0]; SG(i, j) = v1[1]; SB(i, j) = v1[2]; } } double betamax = 1e5; Matrix fx(1, 2); fx(0, 0) = 1; fx(0, 1) = -1; Matrix fy(2, 1); fy(0, 0) = 1; fy(1, 0) = -1; Matrix sizeI2D(1, 2); sizeI2D(0, 0) = Im.rows; sizeI2D(0, 1) = Im.cols; Matrix2 otfFx = psf2otf(fx, sizeI2D); Matrix2 otfFy = psf2otf(fy, sizeI2D); Matrix otfFx2 = MatAbsPow2(otfFx); Matrix otfFy2 = MatAbsPow2(otfFy); Matrix2 Normin1R = fft2(SR); Matrix2 Normin1G = fft2(SG); Matrix2 Normin1B = fft2(SB); Matrix Denormin2 = otfFx2 + otfFy2; float beta = 2 * lambda; int count = 1; while(beta < betamax) { float lb = lambda / beta; Matrix Denormin = beta * Denormin2; MatAdd(Denormin, 1); Matrix hR = Matdiff(SR, 2); Matrix vR = Matdiff(SR, 1); Matrix hG = Matdiff(SG, 2); Matrix vG = Matdiff(SG, 1); Matrix hB = Matdiff(SB, 2); Matrix vB = Matdiff(SB, 1); Matrix Pos2Sum = MatPow2(hR) + MatPow2(vR) + MatPow2(hG) + MatPow2(vG) + MatPow2(hB) + MatPow2(vB); for(int j = 0; j < Im.cols; ++j) { for(int i = 0; i < Im.rows; ++i) { if(Pos2Sum(i, j) < lb) { hR(i, j) = 0; vR(i, j) = 0; hG(i, j) = 0; vG(i, j) = 0; hB(i, j) = 0; vB(i, j) = 0; } } } Matrix Normin2R = Matdiffinv(hR, 2) + Matdiffinv(vR, 1); Matrix Normin2G = Matdiffinv(hG, 2) + Matdiffinv(vG, 1); Matrix Normin2B = Matdiffinv(hB, 2) + Matdiffinv(vB, 1); Matrix2 FSR = (Normin1R + fft2(Normin2R) * beta) / Denormin; Matrix2 FSG = (Normin1G + fft2(Normin2G) * beta) / Denormin; Matrix2 FSB = (Normin1B + fft2(Normin2B) * beta) / Denormin; SR = ifft2(FSR); SG = ifft2(FSG); SB = ifft2(FSB); beta = beta * kappa; printf("."); for(uint i = 0; i < out.rows; ++i) { for(uint j = 0; j < out.cols; ++j) { cv::Vec3f& v1 = out.at<cv::Vec3f>(i, j); v1[0] = SR(i, j); v1[1] = SG(i, j); v1[2] = SB(i, j); } } cv::imshow("out", out); char filename[100]; sprintf(filename, "o%02d.png", count++); cv::Mat theout; out.convertTo(theout, CV_8UC3, 255); cv::imwrite(filename, theout); cv::waitKey(1); } for(uint j = 0; j < out.cols; ++j) { for(uint i = 0; i < out.rows; ++i) { cv::Vec3f& v1 = out.at<cv::Vec3f>(i, j); v1[0] = SR(i, j); v1[1] = SG(i, j); v1[2] = SB(i, j); } } return out; }
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[]) { FILE *fp; char *s, *infile = NULL, c; double *x, *y; double *xp, *yp; void trans(double *p); int size2; int i, k; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (*(s = *++argv) == '-') { c = *++s; if ((c == 'l' || c == 'm') && (*++s == '\0')) { s = *++argv; --argc; } switch (c) { case 'l': size = atoi(s); break; case 'm': n1 = atoi(s); if (argc == 1) { n2 = n1; } else { s = *++argv; argc--; if ((*s >= '0') && (*s <= '9')) { n2 = atoi(s); } else { n2 = n1; s = *--argv; argc++; } } break; case 't': case 'c': case 'q': if ((c == 't') || (*++s == 't')) outopt = 1; if ((c == 'c') || (*s == 'c')) outopt = 2; if (c == 'q') outopt = -1; break; case 'a': case 'i': case 'p': case 'r': c -= ('a' - 'A'); case 'A': case 'P': case 'I': case 'R': out = c; break; case 'h': default: usage(); } } else infile = s; } if (n1 > size) { fprintf(stderr, "%s : Region size %d should be less than the FFT size %d!\n", cmnd, n1, size); return (1); } if (n2 > size) { fprintf(stderr, "%s : Region size %d should be less than the FFT size %d!\n", cmnd, n2, size); return (1); } if (infile) fp = getfp(infile, "rb"); else fp = stdin; size2 = size * size; x = dgetmem(2 * size2); y = x + size2; while (!feof(fp)) { if (n1) { for (xp = x, k = n2; --k >= 0; xp += size) { if (freadf(xp, sizeof(*x), n1, fp) != n1) return (-1); if (n1 < size) fillz(xp + n1, sizeof(*x), size - n1); } for (yp = y, k = n2; --k >= 0; yp += size) { if (freadf(yp, sizeof(*y), n1, fp) != n1) return (-1); if (n1 < size) fillz(yp + n1, sizeof(*x), size - n1); } } else { if ((k = freadf(x, sizeof(*x), 2 * size2, fp)) == 0) break; n2 = n1 = sqrt((double) k / 2); if (k != n1 * n1 * 2) { fprintf(stderr, "%s : Region of support is not square!\n", cmnd); return (-1); } if (n1 < size) { fillz(yp = y + size * n1, sizeof(*x), size * (size - n1)); yp -= (size - n1); xp = x + k; for (k = n1; --k >= 0; yp -= (size - n1)) { fillz(yp, sizeof(*x), size - n1); for (i = n1; --i >= 0;) *--yp = *--xp; } fillz(yp = x + size * n1, sizeof(*x), size * (size - n1)); yp -= (size - n1); for (k = n1; --k >= 0; yp -= (size - n1)) { fillz(yp, sizeof(*x), size - n1); for (i = n1; --i >= 0;) *--yp = *--xp; } } } fft2(x, y, size); if (out == 'P') for (k = 0; k < size2; k++) x[k] = x[k] * x[k] + y[k] * y[k]; else if (out == 'A') for (k = 0; k < size2; k++) x[k] = sqrt(x[k] * x[k] + y[k] * y[k]); if (out != 'I') { if (outopt) trans(x); else fwritef(x, sizeof(*x), size2, stdout); } if ((out == ' ') || (out == 'I')) { if (outopt) trans(y); else fwritef(y, sizeof(*y), size2, stdout); } } free(x); return 0; }
void main() { //* Initialize parameters (system size, grid spacing, etc.) double eps0 = 8.8542e-12; // Permittivity (C^2/(N m^2)) int N = 64; // Number of grid points on a side (square grid) double L = 1; // System size double h = L/N; // Grid spacing for periodic boundary conditions Matrix x(N), y(N); int i,j; for( i=1; i<=N; i++ ) x(i) = (i-0.5)*h; // Coordinates of grid points y = x; // Square grid cout << "System is a square of length " << L << endl; //* Set up charge density rho(i,j) Matrix rho(N,N); rho.set(0.0); // Initialize charge density to zero cout << "Enter number of line charges: "; int M; cin >> M; for( i=1; i<=M; i++ ) { cout << "For charge #" << i << endl; cout << "Enter x coordinate: "; double xc; cin >> xc; cout << "Enter y coordinate: "; double yc; cin >> yc; int ii = (int)(xc/h) + 1; // Place charge at nearest int jj = (int)(yc/h) + 1; // grid point cout << "Enter charge density: "; double q; cin >> q; rho(ii,jj) += q/(h*h); } //* Compute matrix P const double pi = 3.141592654; Matrix cx(N), cy(N); for( i=1; i<=N; i++ ) cx(i) = cos((2*pi/N)*(i-1)); cy = cx; Matrix RealP(N,N), ImagP(N,N); double numerator = -h*h/(2*eps0); double tinyNumber = 1e-20; // Avoids division by zero for( i=1; i<=N; i++ ) for( j=1; j<=N; j++ ) RealP(i,j) = numerator/(cx(i)+cy(j)-2+tinyNumber); ImagP.set(0.0); //* Compute potential using MFT method Matrix RealR(N,N), ImagR(N,N), RealF(N,N), ImagF(N,N); for( i=1; i<=N; i++ ) for( j=1; j<=N; j++ ) { RealR(i,j) = rho(i,j); ImagR(i,j) = 0.0; // Copy rho into R for input to fft2 } fft2(RealR,ImagR); // Transform rho into wavenumber domain // Compute phi in the wavenumber domain for( i=1; i<=N; i++ ) for( j=1; j<=N; j++ ) { RealF(i,j) = RealR(i,j)*RealP(i,j) - ImagR(i,j)*ImagP(i,j); ImagF(i,j) = RealR(i,j)*ImagP(i,j) + ImagR(i,j)*RealP(i,j); } Matrix phi(N,N); ifft2(RealF,ImagF); // Inv. transf. phi into the coord. domain for( i=1; i<=N; i++ ) for( j=1; j<=N; j++ ) phi(i,j) = RealF(i,j); //* Print out the plotting variables: x, y, phi ofstream xOut("x.txt"), yOut("y.txt"), phiOut("phi.txt"); for( i=1; i<=N; i++ ) { xOut << x(i) << endl; yOut << y(i) << endl; for( j=1; j<N; j++ ) phiOut << phi(i,j) << ", "; phiOut << phi(i,N) << endl; } }
//--------------------------------------------------------- void CGrid_IMCORR::cross(std::vector<double>& UNORMC , std::vector<std::vector<double> >ChipSearch, std::vector<std::vector<double> >ChipRef) { // in most cases search is bigger than ref (default = 64x64) std::vector<int>nsnew; nsnew.push_back((int)ChipSearch[0].size()); nsnew.push_back((int)ChipSearch.size()); // in most cases ref is smaller than search (default = 32x32) std::vector<int>nrnew; nrnew.push_back((int)ChipRef[0].size()); nrnew.push_back((int)ChipRef.size()); std::vector<std::vector<double> > ChipRef2; ChipRef2.resize(ChipSearch[0].size()); for(int i = 0; i < ChipSearch[0].size(); i++) ChipRef2[i].resize(ChipSearch.size(),0.0); // zero extent chipref to search chip size for(int i = 0; i<ChipRef.size(); i++) { for(int ii = 0; ii<ChipRef[0].size(); ii++) { ChipRef2[i][ii] = ChipRef[i][ii]; } } std::vector<double>ser, ref; //create arrays ser.push_back(0.0); // dummy index to allow fortran conform indexing for(int i = 0; i<ChipSearch.size(); i++) { for(int ii = 0; ii<ChipSearch[0].size(); ii++) { ser.push_back(ChipSearch[i][ii]); } } ref.push_back(0.0); // dummy index to allow fortran conform indexing for(int i = 0; i<ChipRef2.size(); i++) { for(int ii = 0; ii<ChipRef2[0].size(); ii++) { ref.push_back(ChipRef2[i][ii]); } } int lnstrt = 1; int imgptr = 1; int line = 1; int lncont = 0; // pseudo complex arrays std::vector<double>cser, cref; cser.push_back(0.0); cref.push_back(0.0); for (int i = 1; i < ser.size(); i++) { cser.push_back(ser[i]); cser.push_back(0.0); cref.push_back(ref[i]); cref.push_back(0.0); } // take fast fourier transform of search and reference data fft2(cser,nsnew,1); // from "house" format to frequency format fft2(cref,nsnew,1); // make point by multiplication of ft of search ft with conjugate of reference image for(int i = 1; i<cser.size(); i+=2) { // cser[i] = cser[i] * conjg(cref[i]) double temp_cser_real = cser[i]; cser[i] = cser[i]*cref[i] - cser[i+1]*(-cref[i+1]); cser[i+1] = temp_cser_real*(-cref[i+1]) + cref[i]*(cser[i+1]); } // take inverse fft of cser fft2(cser,nsnew,-1); // real signal format (house) again //Extract usefull valid correlation int ncol = nsnew[0] - nrnew[0] +1; int nrow = nsnew[1] - nrnew[1] +1; int denom = (int)(ChipSearch[0].size() * ChipSearch.size()); int ndxout = 1; int i =1; UNORMC.push_back(0.0); // shift for fortran compatibility; std::vector<int>WhichValues; while(i <= nrow) { int j = 1; while(j <= ncol*2) { //UNORMC.push_back((double)(cser[(j-1)*nsnew[1]+i])/(double) (denom)); UNORMC.push_back((double)(cser[(i-1)*(nsnew[1]*2)+j])/(double) (denom)); WhichValues.push_back((i-1)*(nsnew[1]*2)+j); ndxout++; j+=2; // because of pseudo complex configuration } i+=1; // I think rows are not influenced by complex padding } return; }
void CLightSet::RunLightPrep(IplImage* src,IplImage* dest) { int M,N; M=0; N=0; if (src->roi) { M = src->roi->width; N = src->roi->height; } else { M = src->width; N = src->height; } CvMat *matD; // create mat for meshgrid frequency matrices matD = cvCreateMat(M,N,CV_32FC1); CDM(M,N,matD); CvMat *matH; matH = cvCreateMat(M,N,CV_32FC1); // mat for lowpass filter float D0 = 10.0; float rH,rL,c; rH = 2.0; rL = 0.5; c = 1.0; lpfilter(matD,matH,D0,rH,rL,c); IplImage *srcshift; // shift center srcshift = cvCloneImage(src); cvShiftDFT(srcshift,srcshift); IplImage *log, *temp; log = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1); temp = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1); cvCvtScale(srcshift,temp,1.0,0); cvLog(temp,log); cvCvtScale(log,log,-1.0,0); CvMat *Fourier; Fourier = cvCreateMat( M, N, CV_32FC2 ); fft2(log,Fourier); IplImage* image_im; image_im = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1); cvSplit(Fourier,dest,image_im,0,0); cvMul(dest,matH,dest); cvMul(image_im,matH,image_im); IplImage *dst; dst = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,2); cvMerge(dest,image_im,0,0,dst); cvDFT(dst,dst,CV_DXT_INV_SCALE); cvExp(dst,dst); cvZero(dest); cvZero(image_im); cvSplit(dst,dest,image_im,0,0); //使得图像按照原来的顺序显示 cvShiftDFT(dest,dest); double max,min; // normalize cvMinMaxLoc(dest,&min,&max,NULL,NULL); cvReleaseImage(&image_im); cvReleaseImage(&srcshift); cvReleaseImage(&dst); cvReleaseImage(&log); cvReleaseImage(&temp); cvReleaseMat(&matD); cvReleaseMat(&matH); }