/*! Find a zero (Z) in the resulting matrix. If there is no starred zero in its row or column, star Z. Repeat for each element in the matrix. Go to STEP3. */ nextstep Step2(const gsl_matrix * const M, gsl_matrix_uint * const mask, gsl_vector_uint * const rowCov, gsl_vector_uint * const colCov) { unsigned int i, j; for(i = 0; i < M->size1; i++) { for(j = 0; j < M->size2; j++) { if(gsl_vector_uint_get(rowCov, i) == COVERED) break; if(gsl_vector_uint_get(colCov, j) == UNCOVERED && gsl_matrix_get(M, i, j) == (float) 0 ) { gsl_matrix_uint_set(mask, i, j, STAR); #ifdef VERBOSE fprintf(stderr, "Covering col %d\n", j); #endif gsl_vector_uint_set(colCov, j, COVERED); #ifdef VERBOSE fprintf(stderr, "Covering row %d\n", i); #endif gsl_vector_uint_set(rowCov, i, COVERED); } } } gsl_vector_uint_set_all(rowCov, UNCOVERED); gsl_vector_uint_set_all(colCov, UNCOVERED); return STEP3; }
void MBitBer::Run() { unsigned int t,tt; gsl_matrix_uint ref = min1.GetDataObj(); gsl_matrix_uint rec = min2.GetDataObj(); stopFlag = ! (maxerrs()==0); for (int i=0;i<M();i++) { // user loop unsigned int userErrors = 0; for (int j=0;j<bpu();j++) { // bit loop tt = gsl_matrix_uint_get(&ref,i,j); t = gsl_matrix_uint_get(&rec,i,j); userErrors += (tt != t); } // bit loop gsl_vector_uint_set(bitcount,i,gsl_vector_uint_get(bitcount,i)+bpu()); gsl_vector_uint_set(errcount,i,gsl_vector_uint_get(errcount,i)+userErrors); if (gsl_vector_uint_get(errcount,i)<maxerrs()) stopFlag=false; } // user loop if (framecount == ERROR_REPORT_INTERVAL) { // report errors // dumperrs <- errocount gsl_vector_uint_memcpy(dumperrs,errcount); // dumperrs <- dumperrs - lasterrs gsl_vector_uint_sub(dumperrs,lasterrs); // lasterrs <- errcount gsl_vector_uint_memcpy(lasterrs,errcount); framecount = 0; #ifdef DUMPERRS for (int i=0;i<M();i++) { cout << gsl_vector_uint_get(dumperrs,i) << " errors for user " << i << endl; } #endif } // end update report errors framecount++; //////// production of data vout1.DeliverDataObj(*dumperrs); }
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 }
/*! Find a noncovered zero and prime it. If there is no starred zero in the row containing this primed zero, go to STEP5. Otherwise, cover this row and uncover the column containing the starred zero. Continue in this manner until there are no uncovered zeroes left. Save the smallest uncovered value and go to STEP6. */ nextstep Step4(const gsl_matrix * const M, gsl_matrix_uint * const mask, gsl_vector_uint * const rowCov, gsl_vector_uint * const colCov, int * const z0_r, int * const z0_c) { bool done = false; int row, col; nextstep next; while(done == false) { FindAZero(M, rowCov, colCov, &row, &col); if(row == -1) { #ifdef VERBOSE fprintf(stderr, "Step4: Cannot find zero.\n"); #endif done = true; next = STEP6; } else { #ifdef VERBOSE fprintf(stderr, "Ste4: Found zero at M[%d, %d]. Setting PRIME.\n", row, col); #endif gsl_matrix_uint_set(mask, row, col, PRIME); if(StarInRow(mask, row) == true) { FindStarInRow(mask, row, &col); gsl_vector_uint_set(rowCov, row, COVERED); gsl_vector_uint_set(colCov, col, UNCOVERED); } else { done = true; next = STEP5; *z0_r = row; *z0_c = col; } } } return next; }
/*! Cover each column containing a starred zero. If K columns are covered, the starred zeroes describe a complete set of unique assignments. In this case, go to DONE. Otherwise, go to STEP4. Once we have searched the entire cost matrix, we count the number of independent zeroes found. If we have found (and starred) K independent zeroes then we are done. If not we proceed to STEP4. */ nextstep Step3(const gsl_matrix * const M, const gsl_matrix_uint * const mask, gsl_vector_uint * const colCov) { unsigned int i, j; for(j = 0; j < M->size2; ++j) { if(gsl_vector_uint_get(colCov, j) == COVERED) continue; for(i = 0; i < M->size1; ++i) { if(gsl_matrix_uint_get(mask, i, j) == STAR) gsl_vector_uint_set(colCov, j, COVERED); } } for(j = 0; j < colCov->size; j++) if(gsl_vector_uint_get(colCov, j) == UNCOVERED) return STEP4; return DONE; }
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); } } }
void MAIAllocator::Run() { // fetch channel matrix gsl_matrix_complex hmm = min1.GetDataObj(); // hmm : channel coeffs matrix h(n) (M**2xN) // ij // ch matrix structure // // +- -+ // | h(0) . . . . h(n) | | // | 11 11 | | // | | | Rx1 // | h(0) . . . . h(n) | | // | 12 12 | | // | | // | h(0) . . . . h(n) | | // | 21 21 | | // | | | Rx2 // | h(0) . . . . h(n) | | // | 22 22 | | // +- -+ // // where h(n) represents the channel impulse response // ij // // at time n, from tx_i to rx_j // the matrix has MxM rows and N comumns. // The (i,j) channel is locater at row i*M+j // with i,j in the range [0,M-1] and rows counting from 0 // // // fetch error report // e(u) = errors for user u in the last ERROR_REPORT_INTERVAL (ERI) frames gsl_vector_uint temperr = vin2.GetDataObj(); // update error reports at receiver rx_m every ERI if (ericount % ERROR_REPORT_INTERVAL == 0) { // once every ERU if (temperr.size == M()) { gsl_vector_uint_memcpy(errs,&temperr); } ericount = 0; } // // every DECISION_INTERVAL frames we updates the CSI knowledge // if (framecount % DECISION_INTERVAL == 0) { for (int u=0;u<M();u++) { // user loop // extract time domain response from hmm corresponding to txn-->rxn channel gsl_vector_complex_const_view hii = gsl_matrix_complex_const_row(&hmm,u*M()+u); // copy the N-sized vector hii into u-th column of huu gsl_matrix_complex_set_col(huu,u,&hii.vector); } // user loop //cout << "maiallocator:453 - CSI update received" << endl; // huu matrix structure // // +- -+ // | h(0) . . . . h(n) | // | 11 uu | // | | // | h(n) . . . . h(n) | // | 11 uu | // +- -+ // // where h(n) represents the channel impulse response // ii // // at time n, from tx_u to rx_u // the matrix has N rows and M columns. // // ATTENTION! user_0 channel response is the first column // // Hmat(NxM) = Fourier( huu(NxM) ) // gsl_blas_zgemm(CblasNoTrans, CblasNoTrans, gsl_complex_rect(1,0), transform_mat, huu, gsl_complex_rect(0,0), Hmat); #ifdef SHOW_MATRIX cout << "Hmat(freq,user) (frame:" << framecount << ") = " << endl; gsl_matrix_complex_show(Hmat); #endif // // *********************************************************** // CARRIER ALLOCATION STRATEGIES // *********************************************************** // switch (Mode()) { case 0: // FIXED_ALLOCATION break; case 1: // GIVE_BEST_CARR // // SORT CARRIERS OF EACH USERS // // uses Hmat: the frequency responses of channel tx_n --> rx_n // // starting from user u ... // find the best (in u ranking) unused carrier and assign it to u // next user until no more available carriers for(int u=0; u<M(); u++) { // cycle through users gsl_vector_complex_const_view huser = gsl_matrix_complex_const_column(Hmat,u); gsl_vector_uint_view sortindu = gsl_matrix_uint_column(Hperm,u); for (int j=0; j<N(); j++) { double currpower = gsl_complex_abs2(gsl_vector_complex_get(&huser.vector,j)); gsl_vector_set(huserabs,j,currpower); } // sort over c using abs(h(u,c)) gsl_sort_vector_index(p,huserabs); for (int j=0; j<N(); j++) { uint currindex = p->data[j]; gsl_vector_uint_set(&sortindu.vector,j,currindex); } } // // FIND INITIAL USER RANDOMLY // curruser = gsl_rng_uniform_int(ran,M()); // // ASSIGN FREQUENCIES // gsl_vector_uint_set_all(nextcarr,0); gsl_vector_uint_set_all(usedcarr,0); for (int j=0; j<J(); j++) { for (int uu=0; uu<M(); uu++) { int u = (uu+curruser) % M(); int isassigned = 0; while (! isassigned) { int tag = gsl_vector_uint_get(nextcarr,u); gsl_vector_uint_set(nextcarr,u,++tag); int carrier = gsl_matrix_uint_get(Hperm,N()-tag,u); if (! gsl_vector_uint_get(usedcarr,carrier)) { isassigned = 1; gsl_vector_uint_set(usedcarr,carrier,isassigned); gsl_matrix_uint_set(signature_frequencies,u,j,carrier); } else if (tag==N()) { cerr << "Block: " << BlockName << " allocation problem." << endl; exit(1); } } } } // // show channels and permutations // // gsl_matrix_complex_show(Hmat); //gsl_matrix_uint_show(Hperm); //gsl_matrix_uint_show(signature_frequencies); break; case 2: // SWAP_BAD_GOOD // // SWAP_BAD_GOOD // // sort carriers for each user // choose randomly a starting user u // for each user starting with u // swap worst carrier used by u with best carrier if used by others // sort carriers for(int u=0; u<M(); u++) { gsl_vector_complex_const_view huser = gsl_matrix_complex_const_column(Hmat,u); gsl_vector_uint_view sortindu = gsl_matrix_uint_column(Hperm,u); gsl_vector_view huserabs = gsl_matrix_column(habs,u); for (int j=0; j<N(); j++) { double currpower = gsl_complex_abs2(gsl_vector_complex_get(&huser.vector,j)); gsl_vector_set(&huserabs.vector,j,currpower); } // // sort channels for user <u> // gsl_sort_vector_index(p,&huserabs.vector); for (int j=0; j<N(); j++) { uint currindex = p->data[j]; gsl_vector_uint_set(&sortindu.vector,j,currindex); } } // // Hperm(N,USERS) contains sorted channels index for each users // habs(N,USERS) contains channel energy per each user // // // FIND INITIAL USER RANDOMLY for fairness // curruser = gsl_rng_uniform_int(ran,M()); // // ASSIGN FREQUENCIES // // // for each user ... // for (int uu=0; uu<M(); uu++) { int u = (uu+curruser) % M(); // // worst allocated channel for user u // double worstvalue=GSL_POSINF; unsigned int worstjindex; for (int j=0; j<J(); j++) { unsigned int chind = gsl_matrix_uint_get(signature_frequencies,u,j); double currh = gsl_matrix_get(habs,chind,u); if (currh < worstvalue) { worstvalue = currh; worstjindex = j; } } // // find best channel allocated by other users // // double bestvalue=0; unsigned int bestuser, bestjindex; for (int uuu=0; uuu<M()-1; uuu++) { unsigned int otheru = (uuu+u) % M(); for (int j=0; j<J(); j++) { unsigned int chind = gsl_matrix_uint_get(signature_frequencies,otheru,j); double currh = gsl_matrix_get(habs,chind,otheru); if (currh > bestvalue) { bestvalue = currh; bestjindex = j; bestuser = otheru; } } } // // finally the swap ! // unsigned int chind = gsl_matrix_uint_get(signature_frequencies,u,worstjindex); gsl_matrix_uint_set(signature_frequencies,u,worstjindex, gsl_matrix_uint_get(signature_frequencies, bestuser,bestjindex)); gsl_matrix_uint_set(signature_frequencies,bestuser,bestjindex,chind); // cout << "\n\nProcessing user " << u << endl // << "\tSwapped " << u << "." << worstjindex // << " <-> " << bestuser << "." << bestjindex << endl; } break; case 3: // BEST_OVERLAP // // SORT CARRIERS OF EACH USERS // gsl_matrix_uint_memcpy(signature_frequencies, signature_frequencies_init); for(int u=0; u<M(); u++) { gsl_vector_complex_const_view huser = gsl_matrix_complex_const_column(Hmat,u); gsl_vector_uint_view sortindu = gsl_matrix_uint_column(Hperm,u); for (int j=0; j<N(); j++) { double currpower = gsl_complex_abs2(gsl_vector_complex_get(&huser.vector, j)); gsl_vector_set(huserabs,j,currpower); } gsl_sort_vector_index(p,huserabs); for (int j=0; j<N(); j++) { uint currindex = p->data[j]; gsl_vector_uint_set(&sortindu.vector,j,currindex); } } // // each user take his best carriers allowing carrier overlap // for (int u=0; u<M(); u++) { for (int j=0; j<J(); j++) { int carrier = gsl_matrix_uint_get(Hperm,N()-j-1,u); gsl_matrix_uint_set(signature_frequencies,u,j,carrier); } } // // show channels and permutations // //gsl_matrix_complex_show(Hmat); //gsl_matrix_uint_show(Hperm); //gsl_matrix_uint_show(signature_frequencies); break; case 4: // SOAR_AI // // SOAR // // agent crai5 // bases the decisions on the frequency response tx_m --> rx_m in Hmat(N,M) // for each user it proposes a swap between carriers if the instantaneous impulse channel response // is better // // agent crai6 // for each user it proposes a swap of allocated carriers with one other users // error report is the metric for correct decisions (RL) #ifdef PAUSED // keypress cout << "pause maillocator: before decision loop ... (press ENTER key)" << endl; cin.ignore(); #endif // Every DECISION_INTERVAL we increase the input-time and allow decisions if (framecount % DECISION_INTERVAL == 0) { pAgent->Update(inputTime,++input_time); pAgent->Commit(); } // run agent till output noDecisions = 0; numberCommands=0; while (! (noDecisions) ) { // main decisional loop // // INPUT LINK Update // UpdateInputLink(); //pAgent->RunSelf(1); pAgent->RunSelfTilOutput(); numberCommands = pAgent->GetNumberCommands() ; #ifdef PAUSED // keypress cout << "pause maillocator: after RunSelfTilOutput() ... (press ENTER key)" << endl; cin.ignore(); #endif // loop through received commands for (int cmd = 0 ; cmd < numberCommands ; cmd++) { Identifier* pCommand = pAgent->GetCommand(cmd) ; string name = pCommand->GetCommandName() ; if (name == "assign-free") { std::string sUid = pCommand->GetParameterValue("uid"); std::string sDeassign = pCommand->GetParameterValue("deassign"); std::string sAssign = pCommand->GetParameterValue("assign"); #ifdef SHOW_SOAR cout << "assign-free command received [ u:" << sUid << " , -" << sDeassign << " , +" << sAssign << " ]" << endl; #endif AssignFree(sUid,sDeassign,sAssign); pCommand->AddStatusComplete(); } else if (name == "swap-carriers") { std::string sU1 = pCommand->GetParameterValue("u1"); std::string sC1 = pCommand->GetParameterValue("c1"); std::string sU2 = pCommand->GetParameterValue("u2"); std::string sC2 = pCommand->GetParameterValue("c2"); #ifdef SHOW_SOAR cout << "swap-carriers command received [ u1:" << sU1 << " , c1:" << sC1 << " , u2:" << sU2 << " , c2:" << sC2 << " ]" << endl; #endif SwapCarriers(sU1,sC1,sU2,sC2); pCommand->AddStatusComplete(); } else if (name == "increase-power") { std::string sUid = pCommand->GetParameterValue("uid"); std::string sCid = pCommand->GetParameterValue("cid"); #ifdef SHOW_SOAR cout << "increase-power command received [ u:" << sUid << " , c:" << sCid << " ]" << endl; #endif IncreasePower(sUid,sCid); pCommand->AddStatusComplete(); break; } else if (name == "no-choices") { #ifdef SHOW_SOAR cout << "no-choices command received" << endl; #endif noDecisions = 1; pCommand->AddStatusComplete(); break; } else { #ifdef SHOW_SOAR cout << "ignoring unknown output command from SOAR" << endl; #endif break; } // cout << "framecount = " << framecount << endl; } // end command loop } // while (! (noDecisions) ) break; } // switch (Mode()) } // if DECISION_INTERVAL % 0 // // every 10s dump frame count // time(&nowtime); if (difftime(nowtime,reporttime) > TIMEDELTA) { reporttime = nowtime; cout << "frame:" << framecount << "\r"; cout.flush(); } //////// production of data framecount++; ericount++; mout1.DeliverDataObj( *signature_frequencies ); mout2.DeliverDataObj( *signature_powers ); #ifdef SHOW_MATRIX cout << "signature frequencies (frame:" << framecount-1 << ") = " << endl; gsl_matrix_uint_show(signature_frequencies); #endif }