void SoftDemapper::Setup() { //////// initialization of dynamic data structures // number of symbols Ns=(1 << Nb()); /// vector and matrix allocation gray_encoding = gsl_vector_uint_alloc(Ns); symbol_arg = 2.0*double(M_PI/Ns); ///////// Gray Decoder SetUp for (unsigned int i=0; i<Ns; i++) { unsigned int tmp=0; for (unsigned int k=0; k<Nb(); k++) { unsigned int t=(1<<k), tt=2*t; tmp += t * (((t+i)/tt) % 2); } gsl_vector_uint_set(gray_encoding,tmp,i); } //////// rate declaration for ports }
/** * Constructor for a delayed vector field. * \param[in] fields_ Vector of vector fields for each delay. * \param[in] delays_ Delays associated with each vector field. */ vectorFieldDelay::vectorFieldDelay(std::vector<vectorField *> *fields_, const gsl_vector_uint *delays_) : dim(fields_->at(0)->getDim()), nDelays(delays_->size), delayMax(gsl_vector_uint_get(delays_, nDelays - 1)), fields(fields_) { // Copy delays delays = gsl_vector_uint_alloc(nDelays); gsl_vector_uint_memcpy(delays, delays_); // Allocate workspace work = gsl_vector_alloc(dim); }
void ViterbiDecoder::Run() { int DataLength, CodeLength, i, j, index; int *g_encoder; int nn, KK, mm, max_states, code_type, dec_type; double elm; float *input_c_float; int *output_u_int; int *out0, *out1, *state0, *state1; /////////////////////////////////////////////// /////////////////////////////////////////////// /////////////////////////////////////////////// /* first input is the data word */ gsl_vector_class inputobj = vin1.GetDataObj(); CodeLength = inputobj.vec->size; /* number of data bits */ /* convert the input into float */ input_c_float = (float *)calloc( CodeLength, sizeof(float) ); for (i=0;i<CodeLength;i++) input_c_float[i] = gsl_vector_get(inputobj.vec,i); /* default values */ code_type = CType(); nn = gp_mat->size1; KK = gp_mat->size2; mm = KK - 1; max_states = 1 << mm; /* 2^mm */ /* determine the DataLength */ DataLength = (CodeLength/nn)-mm; /* Convert code polynomial to binary */ g_encoder = (int*)calloc(nn, sizeof(int) ); for (i = 0;i<nn;i++) { for (j=0;j<KK;j++) { elm = gsl_matrix_get(gp_mat,i,j); if (elm != 0) { g_encoder[i] = g_encoder[i] + (int) pow(2,(KK-j-1)); } } } /* create appropriate transition matrices */ out0 = (int *)calloc( max_states, sizeof(int) ); out1 = (int *)calloc( max_states, sizeof(int) ); state0 = (int *)calloc( max_states, sizeof(int) ); state1 = (int *)calloc( max_states, sizeof(int) ); if ( code_type ) { nsc_transit( out0, state0, 0, g_encoder, KK, nn ); nsc_transit( out1, state1, 1, g_encoder, KK, nn ); } else { rsc_transit( out0, state0, 0, g_encoder, KK, nn ); rsc_transit( out1, state1, 1, g_encoder, KK, nn ); } gsl_vector_uint *output = gsl_vector_uint_alloc(DataLength); output_u_int = (int *)calloc( DataLength, sizeof(int) ); /* Run the Viterib algorithm */ Viterbi( output_u_int, out0, state0, out1, state1, input_c_float, KK, nn, DataLength ); /* cast to outputs */ for (j=0;j<DataLength;j++) { gsl_vector_uint_set(output,j,output_u_int[j]); } gsl_vector_uint_class outobj(output); // outobj.show(); vout1.DeliverDataObj(outobj); /////////////////////////////////////////////// /////////////////////////////////////////////// /////////////////////////////////////////////// /* Clean up memory */ free( out0 ); free( out1 ); free( state0 ); free( state1 ); free( g_encoder ); free( input_c_float ); free( output_u_int ); gsl_vector_uint_free(output); }
// // // BlockUser // // void MBlockUser::Setup() { //////// initialization of dynamic data structures // number of symbols Ns=(1 << Nb()); /// vector and matrix allocation gray_encoding = gsl_vector_uint_alloc(Ns); symbol_arg = 2.0*double(PI/Ns); count=0; coding_mat = gsl_matrix_complex_calloc(J(),K()); selection_mat = gsl_matrix_complex_calloc(N(),J()); transform_mat = gsl_matrix_complex_calloc(N(),N()); outmat = gsl_matrix_complex_calloc(N(),M()); // outmat(i,j) = symbol at time i from user j tmp = gsl_vector_complex_calloc(K()); tmp1 = gsl_vector_complex_calloc(J()); tmp2 = gsl_vector_complex_calloc(N()); // tmpout = gsl_vector_complex_calloc(N()); // tmpout is a view from outmat // // // IFFT MATRIX UNITARY // // double ifftarg=2.0*double(M_PI/N()); double ifftamp=1.0/sqrt(double(N())); for (int i=0; i<N(); i++) for (int j=0; j<N(); j++) gsl_matrix_complex_set(transform_mat, i, j, gsl_complex_polar(ifftamp,ifftarg*i*j) ); //////// rate declaration for ports // in1.SetRate( Nb()*K()*M() ); // M users K symbols Nb bits ///////// Gray Encoder SetUp for (unsigned int i=0; i<Ns; i++) { gsl_vector_uint_set(gray_encoding,i,0); for (unsigned int k=0; k<Nb(); k++) { unsigned int t=(1<<k); unsigned int tt=2*t; unsigned int ttt= gsl_vector_uint_get(gray_encoding,i) + t * (((t+i)/tt) % 2); gsl_vector_uint_set(gray_encoding,i,ttt); } } }
/*! Computes assignment matrix for M. */ int Hungarian(const gsl_matrix * const M, const bool convToMinAsg, gsl_matrix * const Assignment) { int res, z0_r, z0_c; bool done = false; unsigned int next = STEP1; gsl_vector_uint *rowCov, *colCov; gsl_matrix_uint *mask; gsl_matrix_int *path; gsl_matrix *MCopy; MCopy = gsl_matrix_alloc(M->size1, M->size2); gsl_matrix_memcpy(MCopy, M); if(convToMinAsg == true) { res = ConvertToMinAsg(MCopy); if(res != GSL_SUCCESS) return res; } // Allocate memory rowCov = gsl_vector_uint_alloc(M->size1); colCov = gsl_vector_uint_alloc(M->size2); mask = gsl_matrix_uint_alloc(M->size1, M->size2); path = gsl_matrix_int_calloc(ceil(((float)(mask->size1*mask->size2))/2), 2 ); // Initialize gsl_vector_uint_set_all(rowCov, UNCOVERED); gsl_vector_uint_set_all(colCov, UNCOVERED); gsl_matrix_uint_set_all(mask, UNMASKED); while(done == false) { switch(next) { case STEP1: next = Step1(MCopy); #ifdef VERBOSE Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 1"); PrintCover( rowCov, colCov, "Post Step 1 Cover"); #endif break; case STEP2: next = Step2(MCopy, mask, rowCov, colCov); #ifdef VERBOSE Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 2"); PrintCover( rowCov, colCov, "Post Step 2 Cover"); #endif break; case STEP3: next = Step3(MCopy, mask, colCov); #ifdef VERBOSE Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 3"); PrintCover( rowCov, colCov, "Post Step 3 Cover"); #endif break; case STEP4: next = Step4(MCopy, mask, rowCov, colCov, &z0_r, &z0_c); #ifdef VERBOSE Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 4"); PrintCover( rowCov, colCov, "Post Step 4 Cover"); #endif break; case STEP5: next = Step5(mask, path, rowCov, colCov, z0_r, z0_c); #ifdef VERBOSE Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 5"); PrintCover( rowCov, colCov, "Post Step 5 Cover"); #endif break; case STEP6: next = Step6(MCopy, rowCov, colCov); #ifdef VERBOSE Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 6"); PrintCover( rowCov, colCov, "Post Step 6 Cover"); #endif break; case DONE: #ifdef VERBOSE Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "DONE"); #endif UpdateAssignment(mask, Assignment); done = true; break; default: done = true; fprintf(stderr, "Error!\n"); } } // Release memory gsl_matrix_free(MCopy); gsl_vector_uint_free(rowCov); gsl_vector_uint_free(colCov); gsl_matrix_uint_free(mask); gsl_matrix_int_free(path); return GSL_SUCCESS; }
void MAIAllocator::Setup() { //////// initialization of dynamic data structures Hmat = gsl_matrix_complex_alloc(N(),M()); Hchan = gsl_vector_complex_alloc(N()); Hperm = gsl_matrix_uint_alloc(N(),M()); p = gsl_permutation_alloc(N()); huserabs = gsl_vector_alloc(N()); nextcarr = gsl_vector_uint_alloc(M()); usedcarr = gsl_vector_uint_alloc(N()); errs = gsl_vector_uint_alloc(M()); habs = gsl_matrix_alloc(N(),M()); huu = gsl_matrix_complex_alloc(N(),M()); framecount = 0; ericount = 0; csicount = 0; noDecisions = 0; ostringstream cmd; // // time // time(&reporttime); // // Random Generator // ran = gsl_rng_alloc( gsl_rng_default ); // SIGNATURE FREQUENCIES INITIAL SETUP signature_frequencies = gsl_matrix_uint_alloc(M(),J()); signature_frequencies_init = gsl_matrix_uint_alloc(M(),J()); signature_powers = gsl_matrix_alloc(M(),J()); for (int i=0; i<M(); i++) for (int j=0; j<J(); j++) gsl_matrix_uint_set(signature_frequencies_init,i,j,(j*M()+i) % N()); // // INITIAL ALLOCATION // gsl_matrix_uint_memcpy(signature_frequencies, signature_frequencies_init); // maximum initial powers for all carriers gsl_matrix_set_all(signature_powers,INIT_CARR_POWER); gsl_vector_uint_set_zero(errs); // // // FFT Transform Matrix // // transform_mat = gsl_matrix_complex_calloc(N(),N()); double fftarg=-2.0*double(M_PI/N()); double fftamp=1.0/sqrt(double(N())); for (int i=0; i<N(); i++) for (int j=0; j<N(); j++) gsl_matrix_complex_set(transform_mat,i,j, gsl_complex_polar(fftamp,fftarg*i*j) ); switch (Mode()) { case 0: cout << BlockName << " - Allocator type FIXED_ALLOCATION selected" << endl; break; case 1: cout << BlockName << " - Allocator type GIVE_BEST_CARR selected" << endl; break; case 2: cout << BlockName << " - Allocator type SWAP_BAD_GOOD selected" << endl; break; case 3: cout << BlockName << " - Allocator type BEST_OVERLAP selected" << endl; break; case 4: cout << BlockName << " - Allocator type SOAR_AI selected" << endl; // // SOAR INITIALIZATION // max_errors = MAX_ERROR_RATE * ERROR_REPORT_INTERVAL * Nb() * K(); cout << BlockName << " - Max errors tuned to " << max_errors << " errors/frame." << endl; // // first we initialize the vectors and matrices // in the order of appearance in the header file. // umapUserVec = vector < Identifier * > (M()); umapUserUidVec = vector < IntElement * > (M()); umapUserErrsVec = vector < IntElement * > (M()); umapUserPowerVec = vector < FloatElement * > (M()); umapUserCarrMat = vector < Identifier * > (M()*J()); umapUserCarrCidMat = vector < IntElement * > (M()*J()); umapUserCarrPowerMat = vector < FloatElement * > (M()*J()); chansCoeffMat = vector < Identifier * > (M()*N()); chansCoeffUserMat = vector < IntElement * > (M()*N()); chansCoeffCarrMat = vector < IntElement * > (M()*N()); chansCoeffValueMat = vector < FloatElement * > (M()*N()); carmapCarrVec = vector < Identifier * > (N()); carmapCarrCidVec = vector < IntElement * > (N()); // // then we create an instance of the Soar kernel in our process // pKernel = Kernel::CreateKernelInNewThread() ; //pKernel = Kernel::CreateRemoteConnection() ; // Check that nothing went wrong. We will always get back a kernel object // even if something went wrong and we have to abort. if (pKernel->HadError()) { cerr << BlockName << ".SOAR - " << pKernel->GetLastErrorDescription() << endl ; exit(1); } // We check if an agent has been prevoiusly created, otherwise we create it // NOTE: We don't delete the agent pointer. It's owned by the kernel pAgent = pKernel->GetAgent("AIAllocator") ; if (! pKernel->IsAgentValid(pAgent)) { pAgent = pKernel->CreateAgent("AIAllocator") ; } // Check that nothing went wrong // NOTE: No agent gets created if there's a problem, so we have to check for // errors through the kernel object. if (pKernel->HadError()) { cerr << BlockName << ".SOAR - " << pKernel->GetLastErrorDescription() << endl ; exit(1); } // // load productions // pAgent->LoadProductions(SoarFn()); // spawn debugger #ifdef SPAWN_DEBUGGER pAgent->SpawnDebugger(); #endif // Check that nothing went wrong // NOTE: No agent gets created if there's a problem, so we have to check for // errors through the kernel object. if (pKernel->HadError()) { cerr << BlockName << ".SOAR - " << pKernel->GetLastErrorDescription() << endl ; exit(1); } // keypress //cout << "pause maillocator:203 ... (press ENTER key)" << endl; //cin.ignore(); // // we can now generate initial input link structure // // NO MORE adjust max-nil-output-cycle //cmd << "max-nil-output-cycles " << 120; //pAgent->ExecuteCommandLine(cmd.str().c_str()); // the input-link pInputLink = pAgent->GetInputLink(); // input-time input_time = 0; inputTime = pAgent->CreateIntWME(pInputLink,"input-time",input_time); // the usrmap structure (common wmes) umap = pAgent->CreateIdWME(pInputLink,"usrmap"); // BITS_PER_REPORT = ERROR_REPORT_INTERVAL * Nb() * K() // MAX_ERRORS = MAX_ERROR_RATE * BITS_PER_REPORT umapMaxerr = pAgent->CreateIntWME(umap,"maxerr",max_errors); umapPstep = pAgent->CreateFloatWME(umap,"pstep",POWER_STEP); umapPmax = pAgent->CreateFloatWME(umap,"pmax",MAX_POWER); // the channels chans = pAgent->CreateIdWME(pInputLink,"channels"); // the carmap carmap = pAgent->CreateIdWME(pInputLink,"carmap"); // the usrmap structure (users substructure) for (int i=0;i<M();i++) { // user loop umapUserVec[i] = pAgent->CreateIdWME(umap,"user"); umapUserUidVec[i] = pAgent->CreateIntWME(umapUserVec[i],"uid",i); umapUserErrsVec[i] = pAgent->CreateIntWME(umapUserVec[i],"errs",int(0)); umapUserPowerVec[i] = pAgent->CreateFloatWME(umapUserVec[i],"power",J()); // update the current allocation for (int j=0;j<J();j++) { // allocated carriers loop unsigned int usedcarr = gsl_matrix_uint_get(signature_frequencies,i,j); double usedpow = gsl_matrix_get(signature_powers,i,j); umapUserCarrMat[i*J()+j] = pAgent->CreateIdWME(umapUserVec[i],"carr"); umapUserCarrCidMat[i*J()+j] = pAgent->CreateIntWME(umapUserCarrMat[i*J()+j],"cid",usedcarr); umapUserCarrPowerMat[i*J()+j] = pAgent->CreateFloatWME(umapUserCarrMat[i*J()+j],"power",usedpow); } // allocated carriers loop // the channels for (int j=0;j<N();j++) { // all channels loop chansCoeffMat[i*N()+j] = pAgent->CreateIdWME(chans,"coeff"); chansCoeffUserMat[i*N()+j] = pAgent->CreateIntWME(chansCoeffMat[i*N()+j],"user",i); chansCoeffCarrMat[i*N()+j] = pAgent->CreateIntWME(chansCoeffMat[i*N()+j],"carr",j); chansCoeffValueMat[i*N()+j] = pAgent->CreateFloatWME(chansCoeffMat[i*N()+j],"value",0.0); } // all channels loop } // user loop // the carmap structure for (int j=0;j<N();j++) { // all carriers loop carmapCarrVec[j] = pAgent->CreateIdWME(carmap,"carr"); carmapCarrCidVec[j] = pAgent->CreateIntWME(carmapCarrVec[j],"cid",j); } // all carriers loop // // END OF SOAR INITIALIZAZION // break; default: cerr << BlockName << " - Unhandled allocator type !" << endl; exit(1); } //////// rate declaration for ports }
void pvalue(gsl_rng * r, gsl_vector_uint *x, gsl_vector_uint *sums, double *_pvalue, double *_alpha, double *_alpha_score, double *_beta_score, PvalueConfig *config) { fprintf(stderr,"pvalue\n"); assert(x->size == sums->size); size_t i; size_t dim=x->size; unsigned int runs=config->runs; unsigned int N=0; unsigned int NN=0; for(i=0;i<dim;i++) { N+=ELT(x,i); NN+=ELT(sums,i); } printf("N=%i\n",N); double *p=(double*)malloc(sizeof(double)*dim); for(i=0;i<dim;i++) { p[i]=ELT(sums,i)*1.0/NN; } double cutoff = logRV(dim,x,sums,NN); fprintf(stderr,"cutoff = %f\n",cutoff); double rv; unsigned int positives=0; //unsigned int *n=(unsigned int*)malloc(sizeof(unsigned int)*dim); gsl_vector_uint *n=gsl_vector_uint_alloc(dim); for (i = 0; i < runs; i++) { gsl_ran_multinomial (r, dim, N, p, n->data); rv=logRV(dim,n,sums,NN); if (rv <= cutoff) { positives++; /*fprintf(multi,"%i %i %i\t%i\n",n->data[0],n->data[1],n->data[2], n->data[0]+n->data[1]+n->data[2]); */ } } double pvalue=positives*1.0/runs; fprintf(stderr,"%i %i %f",positives,runs,pvalue); *_pvalue=pvalue; pvalue_alpha_beta(N,4,1,_alpha,_beta_score); if (N!=0) { *_alpha=0.07/sqrt(N); } *_alpha_score=1.0-pvalue/(*_alpha); fprintf (stderr,"\n"); free(p); //free(n); gsl_vector_uint_free(n); }
void pvalue_all_3d(gsl_rng * r, gsl_vector_uint *x, gsl_vector_uint *sums) { assert(x->size == sums->size); size_t i,j,k; size_t dim=x->size; unsigned int N=0; unsigned int NN=0; for(i=0;i<dim;i++) { N+=ELT(x,i); NN+=ELT(sums,i); } double *p=(double*)malloc(sizeof(double)*dim); for(i=0;i<dim;i++) { p[i]=((double)ELT(sums,i))/NN; } double cutoff = logRV(dim,x,sums,NN); fprintf(stderr,"cutoff = %f\n",cutoff); double rv; unsigned int positives=0; gsl_vector_uint *n=gsl_vector_uint_alloc(dim); FILE *graph_pos,*graph_neg; char buff[256]; sprintf(buff,"pvalue_graph_pos-%i-%i-%i.dat",x->data[0],x->data[1],x->data[2]); if ( (graph_pos=fopen(buff,"w+"))==NULL) { fprintf(stderr,"ERROR: Can't open pvalue_graph_pos.dat"); exit(-1); } sprintf(buff,"pvalue_graph_neg-%i-%i-%i.dat",x->data[0],x->data[1],x->data[2]); if ( (graph_neg=fopen(buff,"w+"))==NULL) { fprintf(stderr,"ERROR: Can't open pvalue_graph_neg.dat"); exit(-1); } double pr; double pv=0; unsigned int positivos=0; unsigned int total=0; pr=gsl_ran_multinomial_pdf(dim,p,x->data); printf("pr(x) = %f\n",pr); for (i=0; i <= N;i+=1) { SET_ELT(n,0,i); for(j=0; i+j<= N;j+=1) { SET_ELT(n,1,j); SET_ELT(n,2,N-(i+j)); rv=logRV(dim,n,sums,NN); pr=gsl_ran_multinomial_pdf(dim,p,n->data); //printf("%f %f\n",rv,cutoff); if (rv <= cutoff) { pv+=pr; positives++; fprintf(graph_pos,"%u %u %f\n",i,j,pr); } else { fprintf(graph_neg,"%u %u %f\n",i,j,pr); } total++; } } printf("pos = %u total = %u: ratio = %f\n",positives,total,((double)positives)/total); printf("pvalue2 = %f\n",pv); fclose(graph_pos); fclose(graph_neg); /*for (i = 0; i < runs; i++) { gsl_ran_multinomial (r, dim, N, p, n->data); rv=logRV(dim,n,sums,NN); if (rv <= cutoff) positives++; //fprintf(stderr,"%i: %i %i\t%f\t%i\n",i,n[0],n[1],rv,); }*/ free(p); //free(n); gsl_vector_uint_free(n); }