Пример #1
0
void calculate_trap(u8 new_data[], block* b)
{
    int i,j;
    bool binary_data_new[8],binary_data_old[8];
    bool binary_trap[8];

    void convert_binary(u8, bool []);
    bool XOR(bool, bool);
    u8 convert_char(bool []);

    if(b->index==0)
    {
        copy(b->trap_index[b->index].trap_data,new_data);
        copy(b->curr_data,new_data);
    }
    else
    {
        for(i=0; i<DATA_SIZE; i++)
        {
            convert_binary(new_data[i],binary_data_new);
            convert_binary(b->curr_data[i],binary_data_old);

            for(j=0; j<8; j++)
            {
                binary_trap[j]=XOR(binary_data_new[j],binary_data_old[j]);
            }

            b->trap_index[b->index].trap_data[i]=convert_char(binary_trap);
        }
    }

    time(&b->trap_index[b->index].time_trap);
}
Пример #2
0
//finds the LCA of powerset of set of profiles and insert if necessary
//graph holds the final graph, table[freq ID] = ID, profileID[profileID] = profile
//sums[profileID] = binary rep, k = number of profiles
//returns beginning of linked list containing all unique LCA's found
struct hashnode_s* insert_LCAs(FILE *fp,int k) {
  int count,frq,*val,bit,s=0,j,*found,size = INIT_SIZE;
  unsigned long num;
  unsigned int i;
  char *profile;
  struct hashnode_s *node = NULL,*temp = NULL;
  KEY *top;

  found = malloc(sizeof(int)*k);

  //for every subset of minimal elements...
  //checking i = 1,2 is redundant (only 1 element in bin rep), so skip
  //printf("k is %d\n",k);
  for (i = 3; i < (1<<k); i++) {
    count = 0;
    frq = 0;
    j = hashtbl_numkeys(binary);
    //printf("number of freq helices %d\n",j);
    num = (1<<j)-1;
    //printf("original num is %d with j %d\n",num,j);
    //...take their LCA...
    for (bit = 0, j = i; j > 0 && bit < k ; j >>= 1, bit++)
      if ((1 & j) == 1) {
	found[count++] = bit;
	//printf("found profile %s for bit %d\n",profileID[bit],bit);
	//sprintf(key,"%s",table[bit]);
	if (!(val = hashtbl_get(cluster,profileID[bit]))) 
	  fprintf(stderr, "No such %s in cluster with bit %d\n",profileID[bit],bit);
	frq += *val;
	num &= sums[bit];
	//printf("num is %d after & with %d from bit %d: %s\n",num,sums[bit],bit,profileID[bit]);
      }
    //printf("binary profile is %d, k is %d,frq is %d, and binary helices is %d\n",i,k,frq,num);

    //...and insert
    if (num != 0) {
      profile = malloc(sizeof(char)*ARRAYSIZE*size);
      profile = convert_binary(profile,num,&s);
      find_edges(fp,profile,found,count);

      temp = insert_LCA(profile,num,s,frq);
      if (temp) {
	//printf("LCA profile %s and i %d\n",profile,i);
	temp->next = node;
	node = temp;
      } else
	free(profile);
    }
  }
  //add final connection from root to smallest element in graph
  for (i = 0; !graph[i]; i++) ;
  for (top = hashtbl_getkeys(graph[i]); top; top = top->next)
    check_insert_edge(fp,"",top->data);

  free(found);
  return node;
}
int main(){
     int result;
     int vetor[9] = {0, 1, 0, 0, 
                      0, 0, 1, 0, 
                      0};
nn
     result = convert_binary(vetor);   
     assert(result == 5);

}
Пример #4
0
//find all edges originating from generated LCA vertices
//node is beginning of LCA list, k is number of original profiles
void find_LCA_edges(FILE *fp,struct hashnode_s *node,int k) {
  int LCA,size = INIT_SIZE,i,count=0;
  char *profile;
  struct hashnode_s *last = NULL,*temp = NULL; 

  if (!node) return;
  profile = malloc(sizeof(char)*ARRAYSIZE*size);
  for (; node; node = node->next) {
    if (last) {
        free(last->data);
        free(last->key);
        free(last);
    }
    last = node;
    //printf("considering LCA %s with bin %d\n",(char*)node->key,*((int*)node->data));
    //check LCA against original profiles for edges
    for (i = 0; i < k; i++) {
      LCA = *((int*)node->data) & sums[i];
      if (LCA == 0) continue;
      profile = convert_binary(profile,LCA,&count);
      if (LCA != *((int*)node->data))
	check_insert_edge(fp,profile,node->key);
    }
    //    count = 0;
    if (!node->next) continue;
    //check LCA against other LCA's
    for (temp = node->next; temp; temp = temp->next) {
      //printf("considering %s and %s\n",node->key,temp->key);
      LCA = *((int*)node->data) & *((int*)temp->data);
      if (LCA == 0) continue;
      profile = convert_binary(profile,LCA,&count);
      if (LCA != *((int*)node->data)) 
	check_insert_edge(fp,profile,node->key);
      else if (LCA != *((int*)temp->data)) 
	check_insert_edge(fp,profile,temp->key);
    }
  }
  free(last->data);
  free(last->key);
  free(last);
  free(profile);
}
Пример #5
0
void forward_recovery(block *b)
{
    int i,j,k;
    bool binary_data_old[8], binary_data_new[8];
    bool binary_data_data[8];
    int index_new;
    u8 reqd_data[DATA_SIZE];

    void convert_binary(u8, bool []);
    bool XOR(bool, bool);
    u8 convert_char(bool []);
    void get_time(time_t *);

    time_t t;

    copy(reqd_data,b->Main_Data);
    reqd_data[DATA_SIZE]='\0';

    get_time(&t);
    index_new=search_trap(t,b);

    for(i=b->Main_Data_Index+1; i<=index_new; i++)
    {
        for(j=0; j<DATA_SIZE; j++)
        {
            convert_binary(reqd_data[j],binary_data_old);
            convert_binary(b->trap_index[i].trap_data[j],binary_data_new);

            for(k=0; k<8; k++)
            {
                binary_data_data[k]=XOR(binary_data_old[k],binary_data_new[k]);
            }

            reqd_data[j]=convert_char(binary_data_data);
        }

        reqd_data[DATA_SIZE]='\0';
    }

}
Пример #6
0
int main(int argc, char *argv[]){
  boost_po::options_description options("Allowed options");

  std::string trainfname;
  std::string testfname;
  int get_train_err = 0;
  
  options.add_options()
    ("train", boost_po::value<std::string>(&trainfname)->required(), "train")
    ("test", boost_po::value<std::string>(&testfname)->required(), "test")
    ("terr", boost_po::value<int>(&get_train_err), "get train err");
  boost_po::variables_map options_map;
  boost_po::store(boost_po::parse_command_line(argc, argv, options), options_map);
  boost_po::notify(options_map);

  arma::mat X;
  arma::icolvec Y;
  
  clock_t t;
  std::cout << "start loading " << trainfname << std::endl;
  START_TIMER(t);
  X.load(trainfname, arma::csv_ascii);
  END_TIMER(t, t);

  std::cout << "data matrix loaded, size: " << X.n_rows << ", " << X.n_cols << " time = " << TO_SEC(t) << " sec" << std::endl;

  Y = arma::conv_to<arma::icolvec>::from(X.col(X.n_cols - 1));
  X.shed_col(X.n_cols - 1);

  convert_binary(Y);

  const int32_t num_samples = X.n_rows;
  const int32_t num_features = X.n_cols;

  arma::colvec mu(num_features); //gaussian mean
  arma::mat sigma(num_features, num_features); //gaussian covariance  
  double xi = 2;
  double delta = 1;

  //initialize gaussian parameters
  mu.zeros();
  sigma = sigma.eye()*COV_FAC;

  std::cout << "start inverting sigma" << std::endl;
  
  START_TIMER(t);
  arma::mat sigma_inv = sigma.i();
  END_TIMER(t, t);
  std::cout << "finished inverting, time = " << TO_SEC(t) << " secs" << std::endl;

  clock_t ts;
  std::cout << "starts training" << std::endl;
  START_TIMER(ts);
  
  int iter_cnt = 0;
  while(std::abs(delta) > CONVERGE_THRE){
    
    arma::mat post_sigma_inv;
    arma::mat post_sigma;
    arma::colvec post_mu;
    double xi_sqr;
    double post_xi;
    int32_t i;

    for(i = 0; i < num_samples; i++){
      std::cout << "sample = i = " << i << std::endl;
      arma::colvec x = X.row(i).t();
      std::cout << "x.print()" << std::endl;
      x.print();

      post_sigma_inv = get_post_sigma_inv(xi, x, sigma_inv);
  
      post_sigma = post_sigma_inv.i();
      
      post_mu = get_post_mu(x, sigma_inv, post_sigma, mu, Y(i));
      
      xi_sqr = get_xi_sqr(x, post_sigma, post_mu);
      post_xi = sqrt(xi_sqr);
      
      sigma = post_sigma;
      sigma_inv = post_sigma_inv;
      mu = post_mu;

      delta = post_xi - xi;
      xi = post_xi;
      END_TIMER(t, ts);
      std::cout << "xi = " << xi << " delta = " << delta << " time = " << TO_SEC(t) << " sec" << std::endl;
    }
    ++iter_cnt;
    std::cout << "iteration " << iter_cnt << " done. xi = " << xi << " delta = " << delta << " std::abs(delta) = " << std::abs(delta) << " time = " << TO_SEC(t) << " sec" << std::endl;
  }
  
  END_TIMER(t, ts);
  std::cout << "time = " << TO_SEC(t) << " sec" << std::endl;
  std::cout << " training done" << std::endl;
  std::cout << "xi = " << xi << std::endl;

  arma::mat X_test;
  arma::icolvec Y_test;

  std::cout << "start loading test matrix " << testfname << std::endl;
  START_TIMER(t);
  X_test.load(testfname, arma::csv_ascii);
  END_TIMER(t, t);

  std::cout << "test data matrix loaded, size: " << X_test.n_rows << ", " << X_test.n_cols << " time = " << TO_SEC(t) << " sec" << std::endl;

  Y_test = arma::conv_to<arma::icolvec>::from(X_test.col(X_test.n_cols - 1));
  X_test.shed_col(X_test.n_cols - 1);

  double test_err = classification_error(xi, sigma, sigma_inv, mu, X_test, Y_test);
  std::cout << "test error = " << test_err << std::endl;

  if(get_train_err){
    double train_err = classification_error(xi, sigma, sigma_inv, mu, X, Y);
    std::cout << "train error = " << train_err << std::endl;
  }

  sigma.save(trainfname + ".sigma.out", arma::csv_ascii);
  mu.save(trainfname + ".mu.out", arma::csv_ascii);
  X.save(trainfname + ".out", arma::csv_ascii);
  
}
Пример #7
0
static PyObject *Decoder_put(PyObject *self, PyObject *args)
{
	GSList *l;
	PyObject *py_data, *py_res;
	struct srd_decoder_inst *di, *next_di;
	struct srd_pd_output *pdo;
	struct srd_proto_data *pdata;
	uint64_t start_sample, end_sample;
	int output_id;
	struct srd_pd_callback *cb;

	if (!(di = srd_inst_find_by_obj(NULL, self))) {
		/* Shouldn't happen. */
		srd_dbg("put(): self instance not found.");
		return NULL;
	}

	if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
		&output_id, &py_data)) {
		/*
		 * This throws an exception, but by returning NULL here we let
		 * Python raise it. This results in a much better trace in
		 * controller.c on the decode() method call.
		 */
		return NULL;
	}

	if (!(l = g_slist_nth(di->pd_output, output_id))) {
		srd_err("Protocol decoder %s submitted invalid output ID %d.",
			di->decoder->name, output_id);
		return NULL;
	}
	pdo = l->data;

	srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
		 di->inst_id, start_sample, end_sample,
		 OUTPUT_TYPES[pdo->output_type], output_id);

	if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) {
		srd_err("Failed to g_malloc() struct srd_proto_data.");
		return NULL;
	}
	pdata->start_sample = start_sample;
	pdata->end_sample = end_sample;
	pdata->pdo = pdo;

	switch (pdo->output_type) {
	case SRD_OUTPUT_ANN:
		/* Annotations are only fed to callbacks. */
		if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
			/* Convert from PyDict to srd_proto_data_annotation. */
			if (convert_annotation(di, py_data, pdata) != SRD_OK) {
				/* An error was already logged. */
				break;
			}
			cb->cb(pdata, cb->cb_data);
		}
		break;
	case SRD_OUTPUT_PYTHON:
		for (l = di->next_di; l; l = l->next) {
			next_di = l->data;
			srd_spew("Sending %d-%d to instance %s",
				 start_sample, end_sample, next_di->inst_id);
			if (!(py_res = PyObject_CallMethod(
				next_di->py_inst, "decode", "KKO", start_sample,
				end_sample, py_data))) {
				srd_exception_catch("Calling %s decode(): ",
							next_di->inst_id);
			}
			Py_XDECREF(py_res);
		}
		if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
			/* Frontends aren't really supposed to get Python
			 * callbacks, but it's useful for testing. */
			pdata->data = py_data;
			cb->cb(pdata, cb->cb_data);
		}
		break;
	case SRD_OUTPUT_BINARY:
		if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
			/* Convert from PyDict to srd_proto_data_binary. */
			if (convert_binary(di, py_data, pdata) != SRD_OK) {
				/* An error was already logged. */
				break;
			}
			cb->cb(pdata, cb->cb_data);
		}
		break;
	case SRD_OUTPUT_META:
		if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
			/* Annotations need converting from PyObject. */
			if (convert_meta(pdata, py_data) != SRD_OK) {
				/* An exception was already set up. */
				break;
			}
			cb->cb(pdata, cb->cb_data);
		}
		break;
	default:
		srd_err("Protocol decoder %s submitted invalid output type %d.",
			di->decoder->name, pdo->output_type);
		break;
	}

	g_free(pdata);

	Py_RETURN_NONE;
}
Пример #8
0
int main(void)
{
    int data = 0, d0, d1, d2, d3, parity, n;
    int facility, card;

    /* Skip the first 25 bits of data (make sure they are zeros) */
    for (n = 0; n < 25; n++)
    {
        data = read_data();
        if (data != 0) {
            /* Error */
            printf("leading zeros expected\n");
        }
    }

    /* Read in groups of 5-bits */
    int start = 1;
    while (1) 
    {
        /* The bits are sent LSB first */
        d0 = read_data();
        if (d0 == -1) {
            /* End of data */
            break;
        }

        /* Read the rest of the data */
        d1 = read_data();
        d2 = read_data();
        d3 = read_data();
        parity = read_data();

        if (d1 == -1 || d2 == -1 || d3 == -1 || parity == -1) {
            printf("premature end of data\n");
            break;
        }

        /* Verify the parity bit (odd parity) */
        if (EVENP(d0,d1,d2,d3,parity)) {
            printf("parity failure on 5-bit chunk\n");
        }

        if (start) {
            /* The first segment should be 0xB == 1011B */
            if (! (d0 & d1 & !d2 & d3) ) {
                /* Invalid starting segment */
                printf("invalid start segment %d %d %d %d\n", d0, d1, d2, d3);
            }
        } else {
            /* Check for the ending segment 0xF == 1111B */
            if (d0 & d1 & d2 & d3) {
                /* End of data sequence, the LRC follows */
                d0 = read_data();
                d1 = read_data();
                d2 = read_data();
                d3 = read_data();
                parity = read_data();
                if (EVENP(d0,d1,d2,d3,parity)) {
                    printf("LRC parity failure\n");
                }
                /* The parity for LRC checks out, verify the actual LRC */

                /* Consume the remaining zeros */
                while (1) {
                    data = read_data();
                    if (data == -1) {
                        break;
                    }
                    if (data != 0) {
                        printf("trailing data must be zeros\n");
                    }
                }
                break;
            }

            if (d3 != 0) {
                printf("data segments must have zero padding\n");
            }

            /* Otherwise this is a chunk of card data. Store it in a circular
             * buffer of 26 bits, so when we are finished iterating we are
             * left with the last 26 bits of input data (rest is discarded) */
            printf("%d%d%d\n", d2, d1, d0);
            store_data(d2);
            store_data(d1);
            store_data(d0);
        }
        start = 0;
    }

    /* Check the data parity (first and last bit */

    /* Dump the data */
    for (n = 1; n < BUFLEN-1; n++) {
        printf("%d", buffer[(bufferPos+n) % BUFLEN]);
    }
    printf("\n");

    /* Extract the facility ID */
    facility = convert_binary(1, 8);
    printf("%d\n", facility);

    card = convert_binary(9, 16);
    printf("%d\n", card);

}