TextureData* TextureManager::createRenderTarget(int w, int h, const TextureParameters& parameters) { int wrap = 0; switch (parameters.wrap) { case eClamp: wrap = GTEXTURE_CLAMP; break; case eRepeat: wrap = GTEXTURE_REPEAT; break; } int filter = 0; switch (parameters.filter) { case eNearest: filter = GTEXTURE_NEAREST; break; case eLinear: filter = GTEXTURE_LINEAR; break; } float scalex = application_->getLogicalScaleX(); float scaley = application_->getLogicalScaleY(); int baseWidth = w; int baseHeight = h; int width = (int)floor(baseWidth * scalex + 0.5); int height = (int)floor(baseHeight * scaley + 0.5); int exwidth = nextpow2(width); int exheight = nextpow2(height); g_id gid = gtexture_RenderTargetCreate(exwidth, exheight, wrap, filter); TextureData *data = new TextureData; data->gid = gid; data->parameters = parameters; data->width = width; data->height = height; data->exwidth = exwidth; data->exheight = exheight; data->baseWidth = baseWidth; data->baseHeight = baseHeight; return data; }
static void scan_dim(Param &out, const Param &in, int dim) { uint threads_y = std::min(THREADS_Y, nextpow2(out.info.dims[dim])); uint threads_x = THREADS_X; uint groups_all[] = {divup((uint)out.info.dims[0], threads_x), (uint)out.info.dims[1], (uint)out.info.dims[2], (uint)out.info.dims[3]}; groups_all[dim] = divup(out.info.dims[dim], threads_y * REPEAT); if (groups_all[dim] == 1) { scan_dim_launcher<Ti, To, op, inclusive_scan>(out, out, in, dim, true, threads_y, groups_all); } else { Param tmp = out; tmp.info.dims[dim] = groups_all[dim]; tmp.info.strides[0] = 1; for (int k = 1; k < 4; k++) { tmp.info.strides[k] = tmp.info.strides[k - 1] * tmp.info.dims[k - 1]; } int tmp_elements = tmp.info.strides[3] * tmp.info.dims[3]; // FIXME: Do I need to free this ? tmp.data = bufferAlloc(tmp_elements * sizeof(To)); scan_dim_launcher<Ti, To, op, inclusive_scan>(out, tmp, in, dim, false, threads_y, groups_all); int gdim = groups_all[dim]; groups_all[dim] = 1; if (op == af_notzero_t) { scan_dim_launcher<To, To, af_add_t, true>(tmp, tmp, tmp, dim, true, threads_y, groups_all); } else { scan_dim_launcher<To, To, op, true>(tmp, tmp, tmp, dim, true, threads_y, groups_all); } groups_all[dim] = gdim; bcast_dim_launcher<To, To, op, inclusive_scan>(out, tmp, dim, true, threads_y, groups_all); bufferFree(tmp.data); } }
void scan_dim_by_key(Param<To> out, CParam<Ti> in, CParam<Tk> key, int dim, bool inclusive_scan) { uint threads_y = std::min(THREADS_Y, nextpow2(out.dims[dim])); uint threads_x = THREADS_X; uint blocks_all[] = {divup(out.dims[0], threads_x), out.dims[1], out.dims[2], out.dims[3]}; blocks_all[dim] = divup(out.dims[dim], threads_y * REPEAT); if (blocks_all[dim] == 1) { scan_dim_final_launcher<Ti, Tk, To, op>(out, in, key, dim, threads_y, blocks_all, true, inclusive_scan); } else { Param<To> tmp = out; Param<char> tmpflg; Param<int> tmpid; tmp.dims[dim] = blocks_all[dim]; tmp.strides[0] = 1; for (int k = 1; k < 4; k++) tmp.strides[k] = tmp.strides[k - 1] * tmp.dims[k - 1]; for (int k = 0; k < 4; k++) { tmpflg.strides[k] = tmp.strides[k]; tmpid.strides[k] = tmp.strides[k]; tmpflg.dims[k] = tmp.dims[k]; tmpid.dims[k] = tmp.dims[k]; } int tmp_elements = tmp.strides[3] * tmp.dims[3]; tmp.ptr = memAlloc<To>(tmp_elements); tmpflg.ptr = memAlloc<char>(tmp_elements); tmpid.ptr = memAlloc<int>(tmp_elements); scan_dim_nonfinal_launcher<Ti, Tk, To, op>(out, tmp, tmpflg, tmpid, in, key, dim, threads_y, blocks_all, inclusive_scan); int bdim = blocks_all[dim]; blocks_all[dim] = 1; scan_dim_final_launcher<To, char, To, op>(tmp, tmp, tmpflg, dim, threads_y, blocks_all, false, true); blocks_all[dim] = bdim; bcast_dim_launcher<To, op>(out, tmp, tmpid, dim, threads_y, blocks_all); memFree(tmp.ptr); memFree(tmpflg.ptr); memFree(tmpid.ptr); } }
struct queue * queue_create( uint32_t size ) { uint32_t npower = 0; struct queue * self = NULL; size = size ? size : 8; size = nextpow2( size ); npower = getpower( size ); self = (struct queue *)malloc( sizeof(struct queue) ); if ( self ) { self->entries = (struct task *)calloc( size, sizeof(struct task) ); if ( self->entries ) { self->size = size; self->head = self->tail = 0; } else { free( self ); self = NULL; } } return self; }
void reduce_first(Param<To> out, CParam<Ti> in, bool change_nan, double nanval) { uint threads_x = nextpow2(std::max(32u, (uint)in.dims[0])); threads_x = std::min(threads_x, THREADS_PER_BLOCK); uint threads_y = THREADS_PER_BLOCK / threads_x; uint blocks_x = divup(in.dims[0], threads_x * REPEAT); uint blocks_y = divup(in.dims[1], threads_y); Param<To> tmp = out; uptr<To> tmp_alloc; if (blocks_x > 1) { tmp_alloc = memAlloc<To>(blocks_x * in.dims[1] * in.dims[2] * in.dims[3]); tmp.ptr = tmp_alloc.get(); tmp.dims[0] = blocks_x; for (int k = 1; k < 4; k++) tmp.strides[k] *= blocks_x; } reduce_first_launcher<Ti, To, op>(tmp, in, blocks_x, blocks_y, threads_x, change_nan, nanval); if (blocks_x > 1) { // FIXME: Is there an alternative to the if condition? if (op == af_notzero_t) { reduce_first_launcher<To, To, af_add_t>( out, tmp, 1, blocks_y, threads_x, change_nan, nanval); } else { reduce_first_launcher<To, To, op>(out, tmp, 1, blocks_y, threads_x, change_nan, nanval); } } }
size_t MorletWaveFFT::init(size_t width, double freq, size_t win_size, double sample_freq) { double dt = 1.0 / sample_freq; double sf = freq / width; double st = 1.0 / (2.0*M_PI*sf); double a = 1 / sqrt(st*sqrt(M_PI)); double omega = 2.0 * M_PI * freq; nt = size_t(7.0*st/dt)+1; len0 = win_size + nt - 1; len = nextpow2(len0); fft = (fftw_complex*)fftw_malloc(len*sizeof(fftw_complex)); fftw_complex* cur_wave = (fftw_complex*)fftw_malloc(len*sizeof(fftw_complex)); double t = -3.5*st; double scale = 2.0*st*st; for(size_t i=0; i<nt; ++i) { double c = a * exp(-t*t/scale); double omega_t = omega * t; cur_wave[i][0] = c * cos(omega_t); cur_wave[i][1] = c * sin(omega_t); t += dt; } for(size_t i=nt; i<len;++i) cur_wave[i][0] = cur_wave[i][1] = 0.0; fftw_plan plan = fftw_plan_dft_1d(len, cur_wave, fft, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute(plan); fftw_destroy_plan(plan); fftw_free(cur_wave); return len; }
void mean_dim(Param out, Param in, Param inWeight, int dim) { uint threads_y = std::min(THREADS_Y, nextpow2(in.info.dims[dim])); uint threads_x = THREADS_X; uint groups_all[] = {(uint)divup(in.info.dims[0], threads_x), (uint)in.info.dims[1], (uint)in.info.dims[2], (uint)in.info.dims[3]}; groups_all[dim] = divup(in.info.dims[dim], threads_y * REPEAT); if (groups_all[dim] > 1) { dim4 d(4, out.info.dims); d[dim] = groups_all[dim]; Array<To> tmpOut = createEmptyArray<To>(d); Array<Tw> tmpWeight = createEmptyArray<Tw>(d); mean_dim_launcher<Ti, Tw, To>(tmpOut, tmpWeight, in, inWeight, dim, threads_y, groups_all); Param owt; groups_all[dim] = 1; mean_dim_launcher<Ti, Tw, To>(out, owt, tmpOut, tmpWeight, dim, threads_y, groups_all); } else { Param tmpWeight; mean_dim_launcher<Ti, Tw, To>(out, tmpWeight, in, inWeight, dim, threads_y, groups_all); } }
void StringTable::_init(size_t size) { size = nextpow2((size_t)(size / loadFactor)); if (size < 32) size = 32; table = (StringEntry *)mem.xcalloc(size, sizeof(table[0])); tabledim = size; pools = NULL; npools = nfill = 0; count = 0; }
static void scan_dim(Param &out, const Param &in) { uint threads_y = std::min(THREADS_Y, nextpow2(out.info.dims[dim])); uint threads_x = THREADS_X; uint groups_all[] = {divup((uint)out.info.dims[0], threads_x), (uint)out.info.dims[1], (uint)out.info.dims[2], (uint)out.info.dims[3]}; groups_all[dim] = divup(out.info.dims[dim], threads_y * REPEAT); if (groups_all[dim] == 1) { scan_dim_fn<Ti, To, op, dim, true>(out, out, in, threads_y, groups_all); } else { Param tmp = out; tmp.info.dims[dim] = groups_all[dim]; tmp.info.strides[0] = 1; for (int k = 1; k < 4; k++) { tmp.info.strides[k] = tmp.info.strides[k - 1] * tmp.info.dims[k - 1]; } dim_type tmp_elements = tmp.info.strides[3] * tmp.info.dims[3]; // FIXME: Do I need to free this ? tmp.data = cl::Buffer(getContext(), CL_MEM_READ_WRITE, tmp_elements * sizeof(To)); scan_dim_fn<Ti, To, op, dim, false>(out, tmp, in, threads_y, groups_all); int gdim = groups_all[dim]; groups_all[dim] = 1; if (op == af_notzero_t) { scan_dim_fn<To, To, af_add_t, dim, true>(tmp, tmp, tmp, threads_y, groups_all); } else { scan_dim_fn<To, To, op, dim, true>(tmp, tmp, tmp, threads_y, groups_all); } groups_all[dim] = gdim; bcast_dim_fn<To, To, op, dim, true>(out, tmp, threads_y, groups_all); } }
void mean_first(Param out, Param in, Param inWeight) { uint threads_x = nextpow2(std::max(32u, (uint)in.info.dims[0])); threads_x = std::min(threads_x, THREADS_PER_GROUP); uint threads_y = THREADS_PER_GROUP / threads_x; uint groups_x = divup(in.info.dims[0], threads_x * REPEAT); uint groups_y = divup(in.info.dims[1], threads_y); Param tmpOut = out; Param noWeight; noWeight.info.offset = 0; for (int k = 0; k < 4; ++k) { noWeight.info.dims[k] = 0; noWeight.info.strides[k] = 0; } // Does not matter what the value is it will not be used. Just needs to be valid. noWeight.data = inWeight.data; Param tmpWeight = noWeight; if (groups_x > 1) { tmpOut.data = bufferAlloc(groups_x * in.info.dims[1] * in.info.dims[2] * in.info.dims[3] * sizeof(To)); tmpWeight.data = bufferAlloc(groups_x * in.info.dims[1] * in.info.dims[2] * in.info.dims[3] * sizeof(Tw)); tmpOut.info.dims[0] = groups_x; for (int k = 1; k < 4; k++) tmpOut.info.strides[k] *= groups_x; tmpWeight.info = tmpOut.info; } mean_first_launcher<Ti, Tw, To>(tmpOut, tmpWeight, in, inWeight, threads_x, groups_x, groups_y); if (groups_x > 1) { // No Weight is needed when writing out the output. mean_first_launcher<Ti, Tw, To>(out, noWeight, tmpOut, tmpWeight, threads_x, 1, groups_y); bufferFree(tmpOut.data); bufferFree(tmpWeight.data); } }
static void scan_dim(Param<To> out, CParam<Ti> in) { uint threads_y = std::min(THREADS_Y, nextpow2(out.dims[dim])); uint threads_x = THREADS_X; uint blocks_all[] = {divup(out.dims[0], threads_x), out.dims[1], out.dims[2], out.dims[3]}; blocks_all[dim] = divup(out.dims[dim], threads_y * REPEAT); if (blocks_all[dim] == 1) { scan_dim_launcher<Ti, To, op, dim, true>(out, out, in, threads_y, blocks_all); } else { Param<To> tmp = out; tmp.dims[dim] = blocks_all[dim]; tmp.strides[0] = 1; for (int k = 1; k < 4; k++) tmp.strides[k] = tmp.strides[k - 1] * tmp.dims[k - 1]; int tmp_elements = tmp.strides[3] * tmp.dims[3]; tmp.ptr = memAlloc<To>(tmp_elements); scan_dim_launcher<Ti, To, op, dim, false>(out, tmp, in, threads_y, blocks_all); int bdim = blocks_all[dim]; blocks_all[dim] = 1; //FIXME: Is there an alternative to the if condition ? if (op == af_notzero_t) { scan_dim_launcher<To, To, af_add_t, dim, true>(tmp, tmp, tmp, threads_y, blocks_all); } else { scan_dim_launcher<To, To, op, dim, true>(tmp, tmp, tmp, threads_y, blocks_all); } blocks_all[dim] = bdim; bcast_dim_launcher<To, op, dim>(out, tmp, threads_y, blocks_all); memFree(tmp.ptr); } }
/* {{{ hash_si_init */ int hash_si_init(struct hash_si *h, size_t size) { size = nextpow2(size); h->size = size; h->used = 0; h->data = (struct hash_si_pair *) malloc(sizeof(struct hash_si_pair) * size); if (h->data == NULL) { return 1; } memset(h->data, 0, sizeof(struct hash_si_pair) * size); return 0; }
/********************* * * FUNCTION: *zeropad * * DESCRIPTION: * Pad the input array until its size is a power of 2. * ***********************/ FLOAT *zeropad(INT16 *in, INT32 *in_Nelements) { FLOAT *temp; INT32 cpt,cpt1; cpt = nextpow2(*in_Nelements); temp = (FLOAT *) malloc((cpt+2)*sizeof(FLOAT)); for (cpt1=0;cpt1<*in_Nelements;cpt1++) temp[cpt1]=in[cpt1]; for (cpt1=*in_Nelements;cpt1<cpt;cpt1++) temp[cpt1]=0; *in_Nelements = cpt; return temp; }
int dconvolve(const double *data, int ndata, const double *syn, int nsyn, double *conv) { int nextpow2(); void cfft(); int ncorr,n_max,i; dcomplex *cdata,*csyn,*ccorr; if (ndata <=0 || nsyn <=0) { fprintf(stderr,"No data or syn is read in ...\n"); return -1; } ncorr = ndata+nsyn-1; // length of correlation time series n_max = nextpow2(ncorr); // closest 2 power // dynamic allocate array to avoid defining upper limit of the length cdata = (dcomplex *) malloc(n_max * sizeof(dcomplex)); csyn = (dcomplex *) malloc(n_max * sizeof(dcomplex)); ccorr = (dcomplex *) malloc(n_max * sizeof(dcomplex)); // set complex data and syn array for (i=0; i<ndata; i++) {cdata[i].re = data[i]; cdata[i].im = 0.;} for (i=ndata; i<n_max; i++) cdata[i].re = cdata[i].im = 0.; for (i=0; i<nsyn; i++) {csyn[i].re = syn[i]; csyn[i].im = 0.;} for (i=nsyn; i<n_max; i++) csyn[i].re = csyn[i].im = 0.; // fft both complex data and syn array cfft(cdata,n_max,1); cfft(csyn,n_max,1); // in frequency domain, calculate the fft of correlation for (i=0;i<n_max;i++) ccorr[i] = dcmult(cdata[i],csyn[i]); // fft back to get the correlation time series cfft(ccorr,n_max,-1); for (i=0;i<ncorr;i++) conv[i] = ccorr[i].re/n_max; free(cdata); free(csyn); free(ccorr); return ncorr; }
/** Convert PSD into time series.*/ dmat* psd2time(const dmat *psdin, rand_t *rstat, double dt, int nstepin){ if(!psdin){ error("psdin cannot be null\n"); } long nstep=nextpow2(nstepin); double df=1./(dt*nstep); dmat *fs=dlinspace(0, df, nstep); dmat *psd=NULL; if(psdin->ny==1){//[alpha, beta, fmin, fmax] discribes power law with cut on/off freq. psd=dnew(nstep, 1); double alpha=psdin->p[0]; double beta=psdin->p[1]; long i0=1, imax=nstep; if(psdin->nx>2){ i0=(long)round(psdin->p[2]/df); if(i0<1) i0=1; } if(psdin->nx>3){ imax=(long)round(psdin->p[3]/df); } info("fmin=%g, fmax=%g, df=%g, i0=%ld, imax=%ld\n", psdin->p[2], psdin->p[3], df, i0, imax); for(long i=i0; i<imax; i++){ psd->p[i]=beta*pow(i*df, alpha); } }else if(psdin->ny==2){ if(psdin->nx<2){ error("Invalid PSD\n"); } psd=dinterp1(psdin, 0, fs, 1e-40); psd->p[0]=0;/*disable pistion. */ }else{ error("psdin is invalid format.\n"); } cmat *wshat=cnew(nstep, 1); //cfft2plan(wshat, -1); for(long i=0; i<nstep; i++){ wshat->p[i]=sqrt(psd->p[i]*df)*COMPLEX(randn(rstat), randn(rstat)); } cfft2(wshat, -1); dmat *out=NULL; creal2d(&out, 0, wshat, 1); cfree(wshat); dfree(psd); dfree(fs); dresize(out, nstepin, 1); return out; }
void unwrap_col(Param<T> out, CParam<T> in, const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy, const dim_t px, const dim_t py, const dim_t nx) { dim_t TX = std::min(THREADS_PER_BLOCK, nextpow2(out.dims[0])); dim3 threads(TX, THREADS_PER_BLOCK / TX); dim3 blocks(divup(out.dims[1], threads.y), out.dims[2] * out.dims[3]); dim_t reps = divup((wx * wy), threads.x); // is > 1 only when TX == 256 && wx * wy > 256 CUDA_LAUNCH((unwrap_kernel<T, true>), blocks, threads, out, in, wx, wy, sx, sy, px, py, nx, reps); POST_LAUNCH_CHECK(); }
static void scan_first(Param<To> out, CParam<Ti> in) { uint threads_x = nextpow2(std::max(32u, (uint)out.dims[0])); threads_x = std::min(threads_x, THREADS_PER_BLOCK); uint threads_y = THREADS_PER_BLOCK / threads_x; uint blocks_x = divup(out.dims[0], threads_x * REPEAT); uint blocks_y = divup(out.dims[1], threads_y); if (blocks_x == 1) { scan_first_launcher<Ti, To, op, true>(out, out, in, blocks_x, blocks_y, threads_x); } else { Param<To> tmp = out; tmp.dims[0] = blocks_x; tmp.strides[0] = 1; for (int k = 1; k < 4; k++) tmp.strides[k] = tmp.strides[k - 1] * tmp.dims[k - 1]; int tmp_elements = tmp.strides[3] * tmp.dims[3]; tmp.ptr = memAlloc<To>(tmp_elements); scan_first_launcher<Ti, To, op, false>(out, tmp, in, blocks_x, blocks_y, threads_x); //FIXME: Is there an alternative to the if condition ? if (op == af_notzero_t) { scan_first_launcher<To, To, af_add_t, true>(tmp, tmp, tmp, 1, blocks_y, threads_x); } else { scan_first_launcher<To, To, op, true>(tmp, tmp, tmp, 1, blocks_y, threads_x); } bcast_first_launcher<To, op>(out, tmp, blocks_x, blocks_y, threads_x); memFree(tmp.ptr); } }
fftLite::fftLite(size_t N_) : cosLUT(nullptr), sinLUT(nullptr), cosLUT_blu(nullptr), sinLUT_blu(nullptr), revBits(nullptr), temps(nullptr), N(0), log2N(0) { if (N_ == 0) return; // prepare for radix-2 Cooley-Tukey if (isPowOf2(N_)) { M = 0; N = N_; } else { // prepare for Bluestein M = N_; N = nextpow2(2 * N_ + 1); cosLUT_blu = new double[M]; sinLUT_blu = new double[M]; for (int i = 0; i < M; ++i) { double temp = M_PI * (((uint64_t)i * i) % (2*M)) / M; //double temp = M_PI * i * i / M; // cos(temp), with large temp cosLUT_blu[i] = cos(temp); sinLUT_blu[i] = sin(temp); } temps = new double[4 * N]; } // radix twiddle factors cosLUT = new double[N / 2]; sinLUT = new double[N / 2]; for (int i = 0; i < N / 2; ++i) { cosLUT[i] = cos(2 * M_PI * i / N); sinLUT[i] = sin(2 * M_PI * i / N); } // pre-compute bit reversal of position index log2N = log2uint(N); revBits = new int[N]; for (int i = 0; i < N; ++i) revBits[i] = reverseBits(i, log2N); }
uintptr_t fa_autocorr_fast_init(int n) { int level; fa_autocorr_fast_t *f = (fa_autocorr_fast_t *)malloc(sizeof(fa_autocorr_fast_t)); /*level = nextpow2(2*n-1);*/ level = nextpow2(2*n); /*printf("level = %d\n", level);*/ f->fft_len = (1<<level); f->h_fft = fa_fft_init(f->fft_len); f->fft_buf1 = (float *)malloc(sizeof(float)*f->fft_len*2); f->fft_buf2 = (float *)malloc(sizeof(float)*f->fft_len*2); memset(f->fft_buf1, 0, sizeof(float)*f->fft_len*2); memset(f->fft_buf2, 0, sizeof(float)*f->fft_len*2); return (uintptr_t)f; }
void* allocate(std::size_t inRequestedSize) { std::size_t actual_size = nextpow2(inRequestedSize); std::size_t index = log2(inRequestedSize); if (mSegmentPools.size() <= index) { mSegmentPools.resize(index + 1); } auto& pool = mSegmentPools[index]; if (!pool) { auto np = new SegmentPool(actual_size); pool.reset(np); } return pool->allocate(); }
void *kmalloc(uint32_t size) { /* local vars */ linknode *ptr; uint32_t base, i; /* Get free hole: */ base = get_hole(log2(nextpow2(size))); /* Allocate physical pages: */ for (i = base & 0xFFFFF000; i < base+size; i+=PAGE_SIZE) arch_vmpage_map(NULL, i, 0); /* Store info about this allocated space... */ ptr = (linknode *) get_hole(log2(16)); arch_vmpage_map(NULL, (uint32_t) ptr, 0); linkedlist_add(&usedlist, ptr); ptr->datum[0] = base; ptr->datum[1] = size; return (void *) base; }
void reduce_dim(Param<To> out, CParam<Ti> in, bool change_nan, double nanval) { uint threads_y = std::min(THREADS_Y, nextpow2(in.dims[dim])); uint threads_x = THREADS_X; dim_t blocks_dim[] = {divup(in.dims[0], threads_x), in.dims[1], in.dims[2], in.dims[3]}; blocks_dim[dim] = divup(in.dims[dim], threads_y * REPEAT); Param<To> tmp = out; uptr<To> tmp_alloc; if (blocks_dim[dim] > 1) { int tmp_elements = 1; tmp.dims[dim] = blocks_dim[dim]; for (int k = 0; k < 4; k++) tmp_elements *= tmp.dims[k]; tmp_alloc = memAlloc<To>(tmp_elements); tmp.ptr = tmp_alloc.get(); for (int k = dim + 1; k < 4; k++) tmp.strides[k] *= blocks_dim[dim]; } reduce_dim_launcher<Ti, To, op, dim>(tmp, in, threads_y, blocks_dim, change_nan, nanval); if (blocks_dim[dim] > 1) { blocks_dim[dim] = 1; if (op == af_notzero_t) { reduce_dim_launcher<To, To, af_add_t, dim>( out, tmp, threads_y, blocks_dim, change_nan, nanval); } else { reduce_dim_launcher<To, To, op, dim>( out, tmp, threads_y, blocks_dim, change_nan, nanval); } } }
struct session_manager * session_manager_create( uint8_t index, uint32_t size ) { struct session_manager * self = NULL; self = calloc( 1, sizeof(struct session_manager)+sizeof(struct hashtable) ); if ( self == NULL ) { return NULL; } size = nextpow2( size ); self->autoseq = 0; self->index = index; self->table = (struct hashtable *)( self + 1 ); if ( _init_table(self->table, size) != 0 ) { free( self ); self = NULL; } return self; }
To mean_all(Param in) { int in_elements = in.info.dims[0] * in.info.dims[1] * in.info.dims[2] * in.info.dims[3]; // FIXME: Use better heuristics to get to the optimum number if (in_elements > 4096) { bool is_linear = (in.info.strides[0] == 1); for (int k = 1; k < 4; k++) { is_linear &= (in.info.strides[k] == (in.info.strides[k - 1] * in.info.dims[k - 1])); } if (is_linear) { in.info.dims[0] = in_elements; for (int k = 1; k < 4; k++) { in.info.dims[k] = 1; in.info.strides[k] = in_elements; } } uint threads_x = nextpow2(std::max(32u, (uint)in.info.dims[0])); threads_x = std::min(threads_x, THREADS_PER_GROUP); uint threads_y = THREADS_PER_GROUP / threads_x; uint groups_x = divup(in.info.dims[0], threads_x * REPEAT); uint groups_y = divup(in.info.dims[1], threads_y); Array<To> tmpOut = createEmptyArray<To>(groups_x); Array<Tw> tmpCt = createEmptyArray<Tw>(groups_x); Param iWt; mean_first_launcher<Ti, Tw, To>(tmpOut, tmpCt, in, iWt, threads_x, groups_x, groups_y); vector<To> h_ptr(tmpOut.elements()); vector<Tw> h_cptr(tmpOut.elements()); getQueue().enqueueReadBuffer(*tmpOut.get(), CL_TRUE, 0, sizeof(To) * tmpOut.elements(), h_ptr.data()); getQueue().enqueueReadBuffer(*tmpCt.get(), CL_TRUE, 0, sizeof(Tw) * tmpCt.elements(), h_cptr.data()); MeanOp<To, Tw> Op(h_ptr[0], h_cptr[0]); for (int i = 1; i < (int)h_ptr.size(); i++) { Op(h_ptr[i], h_cptr[i]); } return Op.runningMean; } else { vector<Ti> h_ptr(in_elements); getQueue().enqueueReadBuffer(*in.data, CL_TRUE, sizeof(Ti) * in.info.offset, sizeof(Ti) * in_elements, h_ptr.data()); //TODO : MeanOp with (Tw)1 Transform<Ti, To, af_add_t> transform; Transform<uint, Tw, af_add_t> transform_weight; MeanOp<To, Tw> Op(transform(h_ptr[0]), transform_weight(1)); for (int i = 1; i < (int)in_elements; i++) { Op(transform(h_ptr[i]), transform_weight(1)); } return Op.runningMean; } }
T mean_all_weighted(Param in, Param inWeight) { int in_elements = in.info.dims[0] * in.info.dims[1] * in.info.dims[2] * in.info.dims[3]; // FIXME: Use better heuristics to get to the optimum number if (in_elements > 4096) { bool in_is_linear = (in.info.strides[0] == 1); bool wt_is_linear = (in.info.strides[0] == 1); for (int k = 1; k < 4; k++) { in_is_linear &= ( in.info.strides[k] == ( in.info.strides[k - 1] * in.info.dims[k - 1])); wt_is_linear &= (inWeight.info.strides[k] == (inWeight.info.strides[k - 1] * inWeight.info.dims[k - 1])); } if (in_is_linear && wt_is_linear) { in.info.dims[0] = in_elements; for (int k = 1; k < 4; k++) { in.info.dims[k] = 1; in.info.strides[k] = in_elements; } inWeight.info = in.info; } uint threads_x = nextpow2(std::max(32u, (uint)in.info.dims[0])); threads_x = std::min(threads_x, THREADS_PER_GROUP); uint threads_y = THREADS_PER_GROUP / threads_x; uint groups_x = divup(in.info.dims[0], threads_x * REPEAT); uint groups_y = divup(in.info.dims[1], threads_y); Array<T> tmpOut = createEmptyArray<T>(groups_x); Array<Tw> tmpWeight = createEmptyArray<Tw>(groups_x); mean_first_launcher<T, Tw, T>(tmpOut, tmpWeight, in, inWeight, threads_x, groups_x, groups_y); vector<T> h_ptr(tmpOut.elements()); vector<Tw> h_wptr(tmpWeight.elements()); getQueue().enqueueReadBuffer(*tmpOut.get(), CL_TRUE, 0, sizeof(T) * tmpOut.elements(), h_ptr.data()); getQueue().enqueueReadBuffer(*tmpWeight.get(), CL_TRUE, 0, sizeof(Tw) * tmpWeight.elements(), h_wptr.data()); MeanOp<T, Tw> Op(h_ptr[0], h_wptr[0]); for (int i = 1; i < (int)tmpOut.elements(); i++) { Op(h_ptr[i], h_wptr[i]); } return Op.runningMean; } else { vector<T> h_ptr(in_elements); vector<Tw> h_wptr(in_elements); getQueue().enqueueReadBuffer(*in.data, CL_TRUE, sizeof(T) * in.info.offset, sizeof(T) * in_elements, h_ptr.data()); getQueue().enqueueReadBuffer(*inWeight.data, CL_TRUE, sizeof(Tw) * inWeight.info.offset, sizeof(Tw) * in_elements, h_wptr.data()); MeanOp<T, Tw> Op(h_ptr[0], h_wptr[0]); for (int i = 1; i < (int)in_elements; i++) { Op(h_ptr[i], h_wptr[i]); } return Op.runningMean; } }
void nearest_neighbour(Param idx, Param dist, Param query, Param train, const dim_t dist_dim, const unsigned n_dist) { try { const unsigned feat_len = query.info.dims[dist_dim]; const To max_dist = maxval<To>(); // Determine maximum feat_len capable of using shared memory (faster) cl_ulong avail_lmem = getDevice().getInfo<CL_DEVICE_LOCAL_MEM_SIZE>(); size_t lmem_predef = 2 * THREADS * sizeof(unsigned) + feat_len * sizeof(T); size_t ltrain_sz = THREADS * feat_len * sizeof(T); bool use_lmem = (avail_lmem >= (lmem_predef + ltrain_sz)) ? true : false; size_t lmem_sz = (use_lmem) ? lmem_predef + ltrain_sz : lmem_predef; unsigned unroll_len = nextpow2(feat_len); if (unroll_len != feat_len) unroll_len = 0; std::string ref_name = std::string("knn_") + std::to_string(dist_type) + std::string("_") + std::to_string(use_lmem) + std::string("_") + std::string(dtype_traits<T>::getName()) + std::string("_") + std::to_string(unroll_len); int device = getActiveDeviceId(); kc_t::iterator cache_idx = kernelCaches[device].find(ref_name); kc_entry_t entry; if (cache_idx == kernelCaches[device].end()) { std::ostringstream options; options << " -D T=" << dtype_traits<T>::getName() << " -D To=" << dtype_traits<To>::getName() << " -D THREADS=" << THREADS << " -D FEAT_LEN=" << unroll_len; switch(dist_type) { case AF_SAD: options <<" -D DISTOP=_sad_"; break; case AF_SSD: options <<" -D DISTOP=_ssd_"; break; case AF_SHD: options <<" -D DISTOP=_shd_ -D __SHD__"; break; default: break; } if (std::is_same<T, double>::value || std::is_same<T, cdouble>::value) { options << " -D USE_DOUBLE"; } if (use_lmem) options << " -D USE_LOCAL_MEM"; cl::Program prog; buildProgram(prog, nearest_neighbour_cl, nearest_neighbour_cl_len, options.str()); entry.prog = new Program(prog); entry.ker = new Kernel[3]; entry.ker[0] = Kernel(*entry.prog, "nearest_neighbour_unroll"); entry.ker[1] = Kernel(*entry.prog, "nearest_neighbour"); entry.ker[2] = Kernel(*entry.prog, "select_matches"); kernelCaches[device][ref_name] = entry; } else { entry = cache_idx->second; } const dim_t sample_dim = (dist_dim == 0) ? 1 : 0; const unsigned nquery = query.info.dims[sample_dim]; const unsigned ntrain = train.info.dims[sample_dim]; unsigned nblk = divup(ntrain, THREADS); const NDRange local(THREADS, 1); const NDRange global(nblk * THREADS, 1); cl::Buffer *d_blk_idx = bufferAlloc(nblk * nquery * sizeof(unsigned)); cl::Buffer *d_blk_dist = bufferAlloc(nblk * nquery * sizeof(To)); // For each query vector, find training vector with smallest Hamming // distance per CUDA block if (unroll_len > 0) { auto huOp = KernelFunctor<Buffer, Buffer, Buffer, KParam, Buffer, KParam, const To, LocalSpaceArg> (entry.ker[0]); huOp(EnqueueArgs(getQueue(), global, local), *d_blk_idx, *d_blk_dist, *query.data, query.info, *train.data, train.info, max_dist, cl::Local(lmem_sz)); } else { auto hmOp = KernelFunctor<Buffer, Buffer, Buffer, KParam, Buffer, KParam, const To, const unsigned, LocalSpaceArg> (entry.ker[1]); hmOp(EnqueueArgs(getQueue(), global, local), *d_blk_idx, *d_blk_dist, *query.data, query.info, *train.data, train.info, max_dist, feat_len, cl::Local(lmem_sz)); } CL_DEBUG_FINISH(getQueue()); const NDRange local_sm(32, 8); const NDRange global_sm(divup(nquery, 32) * 32, 8); // Reduce all smallest Hamming distances from each block and store final // best match auto smOp = KernelFunctor<Buffer, Buffer, Buffer, Buffer, const unsigned, const unsigned, const To> (entry.ker[2]); smOp(EnqueueArgs(getQueue(), global_sm, local_sm), *idx.data, *dist.data, *d_blk_idx, *d_blk_dist, nquery, nblk, max_dist); CL_DEBUG_FINISH(getQueue()); bufferFree(d_blk_idx); bufferFree(d_blk_dist); } catch (cl::Error err) { CL_TO_AF_ERROR(err); throw; } }
void unwrap(Param out, const Param in, const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy, const dim_t px, const dim_t py, const dim_t nx, const bool is_column) { std::string ref_name = std::string("unwrap_") + std::string(dtype_traits<T>::getName()) + std::string("_") + std::to_string(is_column); int device = getActiveDeviceId(); kc_entry_t entry = kernelCache(device, ref_name); if (entry.prog == 0 && entry.ker == 0) { ToNumStr<T> toNumStr; std::ostringstream options; options << " -D is_column=" << is_column << " -D ZERO=" << toNumStr(scalar<T>(0)) << " -D T=" << dtype_traits<T>::getName(); if (std::is_same<T, double>::value || std::is_same<T, cdouble>::value) { options << " -D USE_DOUBLE"; } Program prog; buildProgram(prog, unwrap_cl, unwrap_cl_len, options.str()); entry.prog = new Program(prog); entry.ker = new Kernel(*entry.prog, "unwrap_kernel"); addKernelToCache(device, ref_name, entry); } dim_t TX = 1, TY = 1; dim_t BX = 1; const dim_t BY = out.info.dims[2] * out.info.dims[3]; dim_t reps = 1; if (is_column) { TX = std::min(THREADS_PER_GROUP, nextpow2(out.info.dims[0])); TY = THREADS_PER_GROUP / TX; BX = divup(out.info.dims[1], TY); reps = divup((wx * wy), TX); } else { TX = THREADS_X; TY = THREADS_Y; BX = divup(out.info.dims[0], TX); reps = divup((wx * wy), TY); } NDRange local(TX, TY); NDRange global(local[0] * BX, local[1] * BY); auto unwrapOp = KernelFunctor<Buffer, const KParam, const Buffer, const KParam, const int, const int, const int, const int, const int, const int, const int, const int>(*entry.ker); unwrapOp(EnqueueArgs(getQueue(), global, local), *out.data, out.info, *in.data, in.info, wx, wy, sx, sy, px, py, nx, reps); CL_DEBUG_FINISH(getQueue()); }
OGL2DTexture::OGL2DTexture(OGLDevice& ogldevice,const base::String& identifier, const ion_uint32 width,const ion_uint32 height,const ion_uint32 levels,const ion_uint32 flags,const video::Pixelformat format, const video::Memorypool pool):video::Texture2D(format,identifier,flags),m_rOGLDevice(ogldevice),m_IsValid(true), m_IsDataOK(true),m_IsMapped(false),m_GLHandle(0),m_Framebuffer(0),m_DepthRenderbuffer(0), m_StencilRenderbuffer(0),m_LevelWidth(0),m_LevelHeight(0),m_MappedLevel(0),m_CurrentLevel(0xffffffff)/*, m_Picbuffer(width,height,ogl2dtexformat(format))*/ { if (!video::isDepthformat(format)) { glGenTextures(1,&m_GLHandle); } m_pSurfaces=0; m_OGLTexture.target(GL_TEXTURE_2D); m_OGLTexture.handle(m_GLHandle); ion_uint32 actualwidth=width,actualheight=height; if (!ISPOW2(actualwidth)) actualwidth=nextpow2(actualwidth); if (!ISPOW2(actualheight)) actualheight=nextpow2(actualheight); if (actualwidth!=actualheight) { ion_uint32 i=(actualwidth>actualheight) ? actualwidth : actualheight; actualwidth=actualheight=i; } m_pPicbuffer= ::new video::SimplePicbuffer(actualwidth,actualheight,ogl2dtexformat(format)); m_Width=actualwidth; m_Height=actualheight; m_Pixelformat=m_pPicbuffer->pixelformat(); m_NumMipmaplevels=levels; m_Internalformat=oglpixelformat(format); if (ogldevice.extensionSupported("GL_EXT_framebuffer_object")) { if (video::isDepthformat(format)) { if (ogldevice.extensionSupported("GL_ARB_depth_texture")) { GLenum depthformat=GL_DEPTH_COMPONENT24_ARB; switch (format) { case video::Pixelformat_D15S1: case video::Pixelformat_D16: depthformat=GL_DEPTH_COMPONENT16_ARB; break; case video::Pixelformat_D24: case video::Pixelformat_D24S4: case video::Pixelformat_D24S8: case video::Pixelformat_FD24S8: depthformat=GL_DEPTH_COMPONENT24_ARB; break; case video::Pixelformat_D32: depthformat=GL_DEPTH_COMPONENT32_ARB; break; default:break; } // TODO: depthstencil formats glGenRenderbuffersEXT(1,&m_DepthRenderbuffer); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,m_DepthRenderbuffer); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,GL_DEPTH_COMPONENT24_ARB,width,height); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,0); m_NumMipmaplevels=1; m_pSurfaces=new OGL2DSurface[1]; m_pSurfaces[0].m_pOGL2DTexture=this; m_pSurfaces[0].m_pTexture=this; m_pSurfaces[0].m_Level=0; m_pSurfaces[0].m_GLHandle=0; m_pSurfaces[0].m_Width=actualwidth; m_pSurfaces[0].m_Height=actualheight; m_pSurfaces[0].m_DepthRenderbuffer=m_DepthRenderbuffer; m_pSurfaces[0].m_Framebuffer=0; m_pSurfaces[0].m_StencilRenderbuffer=0; } else m_IsValid=false; } else if (flags&video::Textureflag_IsRendertaget) { glGenFramebuffersEXT(1,&m_Framebuffer); glBindTexture(GL_TEXTURE_2D,m_GLHandle); // TODO: Save bound textures and restore them afterwards GLenum format=oglrgbaformat(video::rgbalayoutFromPixelformat(m_Pixelformat)); GLenum type=GL_UNSIGNED_BYTE; // TODO m_NumMipmaplevels=1; // TODO: Support mipmaps in rendertargets m_pSurfaces=new OGL2DSurface[1]; m_pSurfaces[0].m_pOGL2DTexture=this; m_pSurfaces[0].m_pTexture=this; m_pSurfaces[0].m_Level=0; m_pSurfaces[0].m_GLHandle=m_GLHandle; m_pSurfaces[0].m_Width=actualwidth; m_pSurfaces[0].m_Height=actualheight; m_pSurfaces[0].m_DepthRenderbuffer=0; m_pSurfaces[0].m_Framebuffer=m_Framebuffer; m_pSurfaces[0].m_StencilRenderbuffer=0; glTexImage2D(GL_TEXTURE_2D,0,m_Internalformat,width,height,0,format,type,0); glBindTexture(GL_TEXTURE_2D,0); } } else if (video::isDepthformat(format) || (flags&video::Textureflag_IsRendertaget)) { m_IsValid=false; } if (m_GLHandle!=0) { glbind(); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,(levels==1) ? GL_LINEAR : GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if (levels>1) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL,levels-1); } if (m_NumMipmaplevels==0) { ion_uint32 length=(actualwidth<actualheight) ? actualwidth : actualheight; if (length<=1) m_NumMipmaplevels=1; else { m_NumMipmaplevels=((ion_uint32)( logf((float)length)/logf(2.0f) )) + 1; } } m_OGLTexture.numLevels(m_NumMipmaplevels); m_pDataSubmitted=new bool[m_NumMipmaplevels]; for (ion_uint32 lvl=0;lvl<m_NumMipmaplevels;++lvl) m_pDataSubmitted[lvl]=false; currentLevel(0); }
int main(int argc, char *argv[]){ FILE *xdatc_file; char xdatc_name[256]; int indx, yndx, google; int P=1; double *x; double alpha=0.01; static int help_flag=0, fine_flag=0; double fpo=0; //initial frequency double dt=0; int exc=8192; // 2^13 int nav=16384; // 2^14 while (1) { static struct option long_options[] = { {"help", no_argument, &help_flag, 1}, {"infile", required_argument, 0, 'r'}, // triggers file {"dt value", required_argument, 0, 'd'}, // dt value {"fpo value", required_argument, 0, 'v'}, // fpo value {"P value", required_argument, 0, 'p'}, // Zerro padding {"alpha value", required_argument, 0, 'a'}, // alpha value (fals alarm probability) {"exc value", required_argument, 0, 'e'}, // {"nav value", required_argument, 0, 'n'}, // size of the block of bins of the spectrum {"fine", no_argument, &fine_flag, 1}, // perform fine calculation {0, 0, 0, 0} }; if(help_flag) { printf("*** Searching monochromatic signals in data sequence ***\n"); printf("Usage: ./pspm2fx2 [switch1] <value1> [switch2] <value2> ...\n") ; printf("Switches are:\n\n"); printf("-r input file\n"); printf("--dt (or -d) dt value\n"); printf("--fpo (or -v) fpo (starting frequency) value\n"); printf("--P (or -p) zero padding value\n"); printf("--nav (or -n) nav (default value %d)\n", nav); printf("--alpha (or -a) false alarm probability (default value %2.2f)\n", alpha); printf("--exp (or -e) exc (default value %d)\n", exc); printf("--fine perform fine frequency value calculation\n\n"); printf("Also:\n\n"); printf("--help This help\n"); exit (0); } int option_index = 0; int cc = getopt_long (argc, argv, "r:d:v:p:n:a:e:", long_options, &option_index); if (cc == -1) break; switch (cc) { case 'r': strcpy (xdatc_name, optarg); break; case 'v': fpo = atof(optarg); break; case 'a': alpha = atof(optarg); break; case 'd': dt = atof(optarg); break; case 'e': exc = atoi(optarg); break; case 'n': nav = atoi(optarg); break; case 'p': P = atoi(optarg); break; case '?': abort(); default: break; } } if ((dt == 0) || (fpo == 0)) {perror("The \"dt\" or \"fpo\" is not set\n"); abort();} xdatc_file = fopen(xdatc_name,"r"); fseek(xdatc_file, 0, SEEK_END); // seek to end of file unsigned int file_length = ftell(xdatc_file) / sizeof(double); // get size od the file rewind(xdatc_file); // go bet to begining of the file unsigned int lenX =(P+1)*pow(2, nextpow2(file_length)); //set to power of 2 and zero padding x = (double*)malloc(lenX*sizeof(double)); google = 0; // read xdat file while (fread(&x[google], sizeof(double), 1, xdatc_file)){ if(feof(xdatc_file)) break; else{ google += 1; } } fclose(xdatc_file); //Remove the mean double m = mean(google, x); for (indx=0; indx < google; indx++){ x[indx] -= m; } // Fulfill the reast of the array by zeros for(indx = google; indx < lenX; indx++){x[indx]=0;} /**** FFT ****/ fftw_complex *fft_in, *fft_out; fftw_plan p; fft_in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * lenX); for (indx=0; indx < lenX; indx++){fft_in[indx][0] = x[indx]; fft_in[indx][1] = 0; } fft_out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * lenX); p = fftw_plan_dft_1d(lenX, fft_in, fft_out, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute(p); //Take only positive frequencies unsigned int lenxp=lenX / 2; double * xp = (double*)malloc(sizeof(double)*lenxp); for (indx=0; indx < lenxp; indx++){ xp[indx] = fft_out[indx][0]*fft_out[indx][0] + fft_out[indx][1]*fft_out[indx][1];} fftw_destroy_plan(p); fftw_free(fft_in); if (exc != 0){ for (indx=0; indx < exc; indx++) { xp[indx] = 0; xp[lenxp - indx - 1] = 0;} } // Calculate threshold corresponding to the false alarm probability alpha double tr = treso(alpha, file_length); // Calculate mean values unsigned int part=(lenxp - 2*exc) / nav; double * mean_ar=(double*)malloc(sizeof(double)*part); double g; for (indx=0; indx < part; indx++){ g=0; for( yndx=0; yndx < nav; yndx++){ g += xp[exc + indx*nav + yndx]; } mean_ar[indx]=g/nav; } // Remove mean for (indx=0; indx < part; indx++){ for (yndx=0; yndx < nav; yndx++){ xp[exc + indx*nav + yndx] /= mean_ar[indx]; } } // Find and count local maxima google=0; for(indx=1; indx < lenxp-1; indx++){if ((xp[indx] > xp[indx-1]) && (xp[indx] > xp[indx+1]) && (xp[indx] > tr)){google++;};} // Create the array of local maxima int maxarsize=google; struct line *maxar=malloc(sizeof(struct line)*maxarsize); double bin, am; google=0; if(maxarsize == 0){ perror("NO SIGNALS FOUND !"); }else{ if (fine_flag){ /* Fine search */ for(indx=1; indx < lenxp-1; indx++){ if ((xp[indx] > xp[indx-1]) && (xp[indx] > xp[indx+1]) && (xp[indx] > tr)){ /* Parabolic interpolation for frequency */ bin=indx + (xp[indx-1] - xp[indx+1]) / (2*(xp[indx-1] - 2*xp[indx] + xp[indx+1])); maxar[google].freq = fpo + bin/(lenX*dt); /* Liniar interpolation for the phase of signal */ if( bin > indx){ maxar[google].phase = atan(fft_out[indx][1]/fft_out[indx][0]) + (atan(fft_out[indx+1][1]/fft_out[indx+1][0]) - atan(fft_out[indx][1]/fft_out[indx][0])) * (bin - indx); }else{ maxar[google].phase = atan(fft_out[indx][1]/fft_out[indx][0]) + (atan(fft_out[indx][1]/fft_out[indx][0]) - atan(fft_out[indx-1][1]/fft_out[indx-1][0])) * (indx - bin);} /* ------ Amplitude of a signal ----- */ am = xp[indx] - (xp[indx+1] - xp[indx-1])*(xp[indx+1] - xp[indx-1])/(8*(xp[indx+1] - 2*xp[indx] + xp[indx-1])); // linear interpolation maxar[google].ampl =(2/(double)nav)*sqrt(am*mean_ar[(maxarsize-2*exc)/nav]); /* ----- Signal to nois ratio of a signal ------ */ maxar[google].snr = sqrt(2*(am - 1)); google++; }; }; }else{ /* Coarse calculation of monochromatic signales */ for( indx=1; indx<lenxp; indx++){ if ((xp[indx] > xp[indx-1]) && (xp[indx] > xp[indx+1]) && (xp[indx] > tr)){ maxar[google].freq = fpo + indx/(lenX*dt); /*----- Phase of signal ----*/ maxar[google].phase = atan(fft_out[indx][1]/fft_out[indx][0]); am = xp[indx]; /* ------ Amplitude of a signal ----- */ maxar[google].ampl = (2/(double)nav)*sqrt(am*mean_ar[(maxarsize-2*exc)/nav]); /* ----- Signal to nois ratio of a signal ------ */ maxar[google].snr = sqrt(2*(am - 1)); google++; } } } // Print summary printf("%% Summary for the file %s\n", xdatc_name); if (fine_flag){printf("%% Fine search\n");} else {printf("%% Coars search\n");} printf("%% fpo = %f\n", fpo); printf("%% dt = %f\n", dt); printf("%% alpha = %5.4f\n", alpha); printf("%% nav = %d\n", nav); printf("%% exc = %d \n", exc); printf("%% P = %d\n", P); printf("%% Nr. Frequency Amplitude h SNR Phase\n"); for(indx=0; indx < google; indx++){printf("%5d %15.6f %12.5f %12.5f %12.5f\n", indx+1, maxar[indx].freq, maxar[indx].ampl, maxar[indx].snr, maxar[indx].phase);} } fftw_free(fft_out); return 0; }
//----------------------------------------------------------------------------- // name: ensurepow2() // desc: ... //----------------------------------------------------------------------------- unsigned long ensurepow2( unsigned long n ) { return nextpow2( n-1 ); }