Exemple #1
0
int main(int argc, char **argv) {
    FILE *fp = fopen("new.dat", "r");
    int xmax, ymax;
    int **data;
    
    load_max(fp, &xmax, &ymax);
    
    data = (int**) malloc(xmax * sizeof(int*));
    for (int x = 0; x < xmax; x++) {
        data[x] = calloc(ymax, sizeof(int));
    }

    load_data(fp, xmax, ymax, data);

    int max = 0;
    for (int x = 0; x < xmax; x++) {
        #pragma omp parallel for
        for (int y = 0; y < ymax; y++) {
            // check up, down, left, right, down_diag left, down_diag right
            int values[5];
            values[0] = up(data,x,y,xmax,ymax);
            values[1] = down(data,x,y,xmax,ymax);
            values[2] = left(data,x,y,xmax,ymax);
            values[3] = d_left(data,x,y,xmax,ymax);
            values[4] = d_right(data,x,y,xmax,ymax);
            int max_tmp = max_array(values, 5);
            if (max_tmp > max) { max = max_tmp; }
        }
    }
    
    printf("%d\n", max);
    free(data);
    return 0;
}
// Note that 'arraySize =  8*((3*8*6144)+12)';
void dlsch_LLR_quant(const short *llr, const unsigned int arraySize, const short Mlevel, short *llr_quant)
{

  unsigned int i, j;
  short max_llr, min_llr;
  short llr_interval;
  float quant_step;
  float *transLevelArray;

  min_llr = min_array(llr, arraySize);
  max_llr = max_array(llr, arraySize);
  llr_interval = (max_llr - min_llr);
  quant_step = (float)llr_interval / Mlevel;


  if ((Mlevel%2) != 0) {
    printf("Mlevel should be mutiple of 2...\n");
    exit(-1);
  }

  transLevelArray = (float *)malloc((Mlevel+1)*sizeof(float));

  if (!transLevelArray) {
    printf("Cannot allocate memory for transLevelArray...!\n");
    exit(-1);
  }

  for(j=0; j < Mlevel+1; j++) {
    transLevelArray[j] = min_llr + j*quant_step;
  }

  for (i=0; i < arraySize; i++) {
    for(j=0; j < Mlevel; j++) {
      if ((transLevelArray[j] <= llr[i]) && (llr[i] <= transLevelArray[j+1])) {
        llr_quant[i] = (short)(transLevelArray[j] +  quant_step/2);  // mid-points are selected;
        break;
      }

      if (transLevelArray[j+1] <= llr[i]) { // the last term;
        llr_quant[i] = (short)(transLevelArray[j] +  quant_step/2);  // mid-points are selected;
      }
    }
  }

#ifdef TEST_DEBUG
  printf("min_llr: %d  : max_llr: %d \n", min_llr, max_llr);
  printf("llr_interval: %d  : quant_step: %f \n", llr_interval, quant_step);
  printf("transLevelArray = [");

  for(j=0; j < Mlevel+1; j++)
    printf("%f ", transLevelArray[j]);

  printf("] \n\n");
#endif // TEST_DEBUG  

}
Exemple #3
0
char current_location(int* position)
{
    // int buffer to hold blob data
    unsigned int buffer[12];
    
    // read wii camera blob data into int buffer
    m_wii_read(buffer);
    
    // points
    int x1 = (int)buffer[0];
    int y1 = (int)buffer[1];
    int x2 = (int)buffer[3];
    int y2 = (int)buffer[4];
    int x3 = (int)buffer[6];
    int y3 = (int)buffer[7];
    int x4 = (int)buffer[9];
    int y4 = (int)buffer[10];
    
    // array of points
    int x_points[4] = {x1, x2, x3, x4};
    int y_points[4] = {y1, y2, y3, y4};
    
    int i;
    for(i = 0; i<=4;i++)
    {
        if (x_points[i]==1023 && y_points[i]==1023)
        {
            x_points[i] = NaN;
            y_points[i] = NaN;
        }
    }
    
    // calculate all possible distances
    int d12 = calc_dist(x_points[0], y_points[0], x_points[1], y_points[1]);
    int d13 = calc_dist(x_points[0], y_points[0], x_points[2], y_points[2]);
    int d14 = calc_dist(x_points[0], y_points[0], x_points[3], y_points[3]);
    int d23 = calc_dist(x_points[1], y_points[1], x_points[2], y_points[2]);
    int d24 = calc_dist(x_points[1], y_points[1], x_points[3], y_points[3]);
    int d34 = calc_dist(x_points[2], y_points[2], x_points[3], y_points[3]);
    
    // store all distances in an array
    int all_distances[6] = {d12,d13,d14,d23,d24,d34};
    
    // store all pair
    int all_pairs[6][2] = {{0,1},{0,2},{0,3},{1,2},{1,3},{2,3}};
    
    // index and value of max and min distance
    int max_dat[2];
    int min_dat[2];

    // fill min and max arrays
    max_array(all_distances, 6, max_dat);
    min_array(all_distances, 6, min_dat);
    
    // index of max and min points
    int max_index = max_dat[1];
    int min_index = min_dat[1];

    // array of pair indicies
    int long_pair[2] = {all_pairs[max_index][0], all_pairs[max_index][1]};
    int short_pair[2] = {all_pairs[min_index][0], all_pairs[min_index][1]};
    
    int top_point[2];
    int bottom_point[2];
    
    // finds top and bottom points
    if (long_pair[0] == short_pair[0] || long_pair[0] == short_pair[1])
    {
        top_point[0] = x_points[long_pair[0]];
        top_point[1] = y_points[long_pair[0]];
        bottom_point[0] = x_points[long_pair[1]];
        bottom_point[1] = y_points[long_pair[1]];
    }
    else
    {
        top_point[0] =x_points[long_pair[1]];
        top_point[1] =y_points[long_pair[1]];
        bottom_point[0] =x_points[long_pair[0]];
        bottom_point[1] =y_points[long_pair[0]];

    }
    
    // calculate center of rink
    double x_center = ((double)top_point[0] + (double)bottom_point[0])/2;
    double y_center = ((double)top_point[1] + (double)bottom_point[1])/2;
    
    //double theta = acos(y_unit);
    double x_diff = (double)top_point[0] - x_center;
    double y_diff =(double)top_point[1] - y_center;
    
    // calculate position of camera relative to camera coordinate axes
    double camera_x = 512;
    double camera_y = 384;
    
    // calculate angle theta
    double theta = atan2(x_diff, y_diff);

    // rotated points
    float rotated_x = ((((camera_x-x_center)*cos(theta)) + ((camera_y-y_center)*-sin(theta))) + camera_x);
    float rotated_y = ((((camera_x-x_center)*sin(theta)) + ((camera_y-y_center)*cos(theta))) + camera_y);

    // position and angle relative to rink axes
    position[0] = (int) (10*((512 - (rotated_x)))/28);
    position[1] = (int) (10*(((rotated_y) - 384))/28);
    position[2] = (int) (theta*(180/M_PI));
    
    return 1;
    }
void build_geometry_(OperatorRegularize & op,
                     const DomainUnstructured & src, LataDeriv<Domain> & dest_domain)
{
  Journal(verb_level) << "OperatorRegularize domain " << src.id_.name_ << endl;
  if (src.elt_type_ != Domain::quadri && src.elt_type_ != Domain::hexa) {
    Journal() << "Error in OperatorRegularize::build_geometry: cannot operate on unstructured mesh with this element type" << endl;
    throw;
  }

  DomainIJK & dest = dest_domain.instancie(DomainIJK);
  dest.elt_type_ = src.elt_type_;
  const entier nsom = src.nodes_.dimension(0);
  const entier dim = src.nodes_.dimension(1);
  ArrOfInt nb_som_dir(dim);
  {
    double product_n = 1.;
    for (entier i_dim = 0; i_dim < dim; i_dim++) {
      ArrOfFloat & coord = dest.coord_.add(ArrOfFloat());
      coord.resize_array(nsom);
      entier i;
      for (i = 0; i < nsom; i++)
        coord[i] = src.nodes_(i, i_dim);
      coord.ordonne_array();
      retirer_doublons(coord, op.tolerance_);
      product_n *= coord.size_array();
      // Add extended domain layer:
      if (coord.size_array() > 1) {
        const entier n = coord.size_array();
        const entier l = op.extend_layer_;
        coord.resize_array(n + l * 2);
        double x0 = coord[n-1];
        double delta = coord[n-2] - x0;
        for (i = 1; i <= l; i++)
          coord[n + l + i] = x0 + delta * i;
        for (i = l-1; i >= 0; i--)
          coord[i + l] = coord[i];
        x0 = coord[l];
        delta = coord[l+1] - x0;
        for (i = 1; i <= l; i++)
          coord[l - i] = x0 - delta * i;
      }
      nb_som_dir[i_dim] = coord.size_array();
    }
    // Verifying that unique has deleted many points...
    // If well organised, nsom=nx*ny*nz
    // If chaos, nsom=(nx+ny+nz)/3
    // We want to verify that we are nearer to organisation than to chaos !
    if (product_n > (double) nsom * (double) nsom - 1.) {
      Journal() << "Positions do not seam regular !" << endl;
      throw;
    }
  }
  int i;
  op.renum_nodes_.resize_array(nsom);
  int nb_som_ijk = 1;
  for (i = 0; i < dim; i++) 
    nb_som_ijk *= nb_som_dir[i];
  IntTab ijk_indexes;
  ijk_indexes.resize(nsom, dim);
  for (i = 0; i < nsom; i++) {
    entier ijk_index = 0;
    for (int j = dim-1; j >= 0; j--) {
      const double x = src.nodes_(i,j);
      int index = search_in_ordered_vect(x, dest.coord_[j]);
      if (index < 0) {
        Journal() << "Error: coordinate (" << i << "," << j << ") = " << x << " not found in regularize" << endl
                  << "Try reducing regularize tolerance value (option regularize=epsilon)" << endl;
        throw;
      }
      ijk_indexes(i, j) = index;
      ijk_index += index;
      if (j)
        ijk_index *= nb_som_dir[j-1];
    }
    op.renum_nodes_[i] = ijk_index;
  }
  const int max_index = max_array(nb_som_dir);
  int nb_elems_ijk = 1;
  for (i = 0; i < dim; i++)
    nb_elems_ijk *= nb_som_dir[i] - 1;
  dest.invalid_connections_.resize_array(nb_elems_ijk);
  dest.invalid_connections_ = 1; // Everything invalid by default
  const int nelem = src.elements_.dimension(0);
  const int nb_som_elem = src.elements_.dimension(1);
  op.renum_elements_.resize_array(nelem);
  // Pour chaque element, indice dans le maillage ijk du plus sommet le plus proche de l'origine
  // (pour les faces...)
  ArrOfInt idx_elem_som;
  idx_elem_som.resize_array(nelem);
  int min_index[3];
  for (i = 0; i < nelem; i++) {
    min_index[0] = min_index[1] = min_index[2] = max_index;
    for (int j = 0; j < nb_som_elem; j++) {
      int node = src.elements_(i,j);
      for (int k = 0; k < loop_max(dim, 3); k++) {
        int idx = ijk_indexes(node, k);
        min_index[k] = (idx < min_index[k]) ? idx : min_index[k];
        break_loop(k,dim);
      }
    }
    entier idx = 0;
    entier idx_som = 0;
    if (dim == 1) {
      idx = min_index[0];
      idx_som = idx;
    } else if (dim == 2) {
      idx = min_index[1] * (nb_som_dir[0]-1) + min_index[0];
      idx_som = min_index[1] * nb_som_dir[0] + min_index[0];
    } else if (dim == 3) {
      idx = (min_index[2] * (nb_som_dir[1]-1) + min_index[1]) * (nb_som_dir[0]-1) + min_index[0];
      idx_som = (min_index[2] * nb_som_dir[1] + min_index[1]) * nb_som_dir[0] + min_index[0];
    } else
      throw;
    op.renum_elements_[i] = idx;
    dest.invalid_connections_.clearbit(idx);
    idx_elem_som[i] = idx_som;
  }
  
  if (src.faces_ok()) {
    const int nfaces = src.faces_.dimension(0);
    op.renum_faces_.resize_array(nfaces);
    op.renum_faces_ = -1;
    const int nb_elem_face = src.elem_faces_.dimension(1);
    ArrOfInt delta_dir(dim);
    delta_dir[0] = 1;
    for (i = 1; i < dim; i++)
      delta_dir[i] = delta_dir[i-1] * nb_som_dir[i-1];
    for (i = 0; i < nelem; i++) {
      // Les faces haut, gauche et arriere du cube a l'origine portent le numero 0
      // Voir DomaineIJK pour la convention sur la numerotation des faces
      for (entier j = 0; j < nb_elem_face; j++) {
        const entier i_face = src.elem_faces_(i, j);
        entier dir = j % dim;
        entier index = idx_elem_som[i];
        if (j>=dim) 
          index += delta_dir[dir];
        // Encodage du numero de la face et de la direction
        index = (index << 2) + dir;
        if (op.renum_faces_[i_face] < 0) {
          op.renum_faces_[i_face] = index;
        } else if (op.renum_faces_[i_face] != index) {
          Journal() << "Error in OperatorRegularize: faces renumbering failed" << endl;
          throw;
        }
      }
    }
  }
  op.geom_init_ = 1;
}
void ai_play_best_move(struct tic_tac_toe* t, struct ai* ai, struct disp_grid_81* disp){
	int loc = max_array(ai->grid_eval,64);
	t->grid[loc] = ai->player_id; // place move at maximum value
    disp_grid_ttc_place(disp,loc, ai->player_id); // for PSoC
}
Exemple #6
0
int ads8568_scan_ch( uint16_t *data, uint8_t *ch, uint8_t ch_count, 
        uint8_t range, uint8_t mode, uint8_t polarity )
{
    uint8_t     ch_max = 0;
    uint8_t     data_index = 0;
    uint8_t     ch_to_read[ADS8568_CH_MAX];
    uint8_t 	ch_index = 0;
    uint16_t    tmp; 
    volatile    GPIO_PinNumber_t cs;
    
    /* check parameters */ 
    if ( !ch_count || 
          ch_count > ADS8568_CH_MAX ||
          data == NULL ||
          ch == NULL )
        return -1;             

    /* start analog-to-digital conversion */
    ads8568_start_conv();

    /* find max channel */ 
    ch_max = max_array(ch, ch_count);
    if ( ch_max > ADS8568_CH_MAX )
        return -1; 

    memset((void *)ch_to_read, 0x0, sizeof(ch_to_read));    
    for ( ch_index = 0; ch_index < ch_count; ch_index++)
    {
    	if (ch[ch_index] <  ADS8568_CH_MAX)
    		ch_to_read[ch[ch_index]] = 1;
    }

    /* Wait for conversion end */
    while(GPIO_getInput(ADC_BUSY));

    /* Set CS to active*/
    GPIO_setOutput(ADC_CS2, GPIO_LOW);
    for(ch_index = 0; ch_index < ch_max; ch_index++)
    {
        if ( ch_index == 8 )
        {
            GPIO_setOutput(ADC_CS2, GPIO_HIGH);
            GPIO_setOutput(ADC_CS1, GPIO_LOW);
        }

        if( ch_to_read[ch_index] )
        {
            ads8568_read_bus(&data[data_index]); 
            data_index++; 
        }
        else 
        {
            /* dummy read */ 
            ads8568_read_bus(&tmp); 
        }
   } 

    GPIO_setOutput(ADC_CS1, GPIO_HIGH);
    GPIO_setOutput(ADC_CS2, GPIO_HIGH);

    return data_index;
}
//calibrates the camera to the projected image
//called by python
void calibrateCamera(int thresh, char *imageFile, char 
*outFile, int maxBlocks){
	//thresh is the y (intensity) value for which a pixel is considered on
	//uint8 threshold = 30;//30 works well with lights off
	int i,k,l,pixPosition;
	int j = 0;
	int dobreak = 0;//boolean
	int sum = 0;
	int ARR[4], ARR2[4], ARR3[4], ARR4[4];
	int OUTARRAY1[4];
	double OUTARRAY2[2];
	FILE * fptr;
	uint8 *Y;//stores y part of yuv data
	Y = (uint8 *)malloc(sizeof(uint8)*(camXRes)*(camYRes));
	fptr = fopen(imageFile,"rb");
	fread(Y,sizeof(uint8),(camXRes)*(camYRes),fptr);	
	fclose(fptr);
		
	//find first bright pixel (in upper left corner)
	while(j<camYRes-10){//for(j=0;j<globals.camImage.camYRes;j++)
	for(i=0;i<camXRes-20;i++){
		pixPosition = i+j* camXRes;
		if (Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(k=0;k<10;k++){//check to make sure next 10x10 are bright
				for(l=0;l<10;l++){
					sum += Y[pixPosition+l+k*( camXRes)];}}
			if (sum>=thresh*100){//if it is the top left on pixel
				dobreak = 1;
				break;}//break while loop
		}//end if

	}
	if (dobreak)
		break;j++;
	}//end i and j for loops

	ARR[0]=-1*j;//top
	ARR4[0]=-1*i;//left
	dobreak = 0;
	j =  camYRes - 2;
	
	//find last bright pixel (in lower right corner)
	while(j>10){
	for(i= camXRes-20;i>10;i--){
		pixPosition = i+j* camXRes;
		if(Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(k=0;k>-10;k--){
				for(l=0;l>-10;l--){sum+=Y[pixPosition+l+k*( camXRes)];}}
			if(sum>=thresh*100){//if it is the bottom right pixel
				dobreak = 1;
					break;}//break while loop
			}//end if

	}
			if (dobreak)
			break;
			j--;
	}//end i and j loops
	ARR3[0]=j;//bottom
	ARR2[0]=i;//right


		//find last bright pixel (in bottom left corner)
	dobreak = 0;
	j =  camYRes - 2;
	
	while(j>10){
	for(i=0;i< camXRes-20;i++){
		pixPosition = i+j* camXRes;
		if(Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(k=0;k>-10;k--){
				for(l=0;l<10;l++){sum+=Y[pixPosition+l+k*( camXRes)];}}
			if(sum>=thresh*100){//if it is the bottom right pixel
				dobreak = 1;
					break;}//break while loop
			}//end if

	}
			if (dobreak)
			break;
			j--;}//end i and j loops
	ARR3[1]=j;//bottom
	ARR4[1]=-1*i;//left
	dobreak=0;
	j=0;
	
	//top right
	while(j< camYRes-10){//for(j=0;j< camYRes;j++)
	for(i= camXRes-20;i>10;i--){
		pixPosition = i+j* camXRes;
		if (Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(k=0;k<10;k++){//check to make sure next 10x10 are bright
				for(l=0;l>-10;l--){
					sum += Y[pixPosition+l+k*( camXRes)];}}
			if (sum>=thresh*100){//if it is the top left on pixel
				dobreak = 1;
				break;}//break while loop
		}//end if

	}
			if (dobreak)
			break;j++;}//end i and j for loops
	ARR[1] = -1*j;//top
	ARR2[1] = i;//right

	//go vertical first now
	dobreak=0;
	i=0;
	//bottom left
	while(i< camXRes-20){//for(j=0;j< camYRes;j++)
	for(j= camYRes-1;j>10;j--){
		pixPosition = i+j* camXRes;
		if (Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(l=0;l<10;l++){//check to make sure next 10x10 are bright
				for(k=0;k>-10;k--){
					sum += Y[pixPosition+l+k*( camXRes)];}}
			if (sum>=thresh*100){//if it is the top left on pixel
				dobreak = 1;
				break;}//break while loop
		}//end if

	}
			if (dobreak)
			break;i++;}//end i and j for loops

	ARR3[2]=j;//bottom
	ARR4[2]=-1*i;//left

			//find last bright pixel (in top right corner)
	dobreak = 0;
	i =  camXRes - 20;
	
	while(i>10){
	for(j=0;j< camYRes-10;j++){
		pixPosition = i+j* camXRes;
		if(Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(l=0;l>-10;l--){
				for(k=0;k<10;k++){sum+=Y[pixPosition+l+k*( camXRes)];}}
			if(sum>=thresh*100){//if it is the bottom right pixel
				dobreak = 1;
					break;}//break while loop
			}//end if

	}
			if (dobreak)
			break;
			i--;}//end i and j loops
	ARR2[2]=i;//right
	ARR[2]=-1*j;//top

	dobreak=0;
	i=0;
	//find first bright pixel (in top left corner)
	while(i< camXRes-20){//for(j=0;j< camYRes;j++)
	for(j=0;j< camYRes-10;j++){
		pixPosition = i+j* camXRes;
		if (Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(l=0;l<10;l++){//check to make sure next 10x10 are bright
				for(k=0;k<10;k++){
					sum += Y[pixPosition+l+k*( camXRes)];}}
			if (sum>=thresh*100){//if it is the top left on pixel
				dobreak = 1;
				break;}//break while loop
		}//end if
	}
			if (dobreak)
			break;i++;}//end i and j for loops

	ARR[3]=-1*j;//top
	ARR4[3]=-1*i;//left
	dobreak=0;
	i= camXRes-20;
		//find last bright pixel (in lower right corner)
	while(i>10){
	for(j= camYRes-2;j>10;j--){
		pixPosition = i+j* camXRes;
		if(Y[pixPosition]>thresh){//if pixel is bright
			sum = 0;
			for(l=0;l>-10;l--){
				for(k=0;k>-10;k--){sum+=Y[pixPosition+l+k*( camXRes)];}}
			if(sum>=thresh*100){//if it is the bottom right pixel
				dobreak = 1;
					break;}//break while loop
			}//end if

	}
			if (dobreak)
			break;
			i--;}//end i and j loops
	ARR3[3]=j;//bottom
	ARR2[3]=i;//right

	//pick extremes
	OUTARRAY1[0]=-1*max_array(ARR4,4);//left offset
	OUTARRAY1[1]=max_array(ARR2,4);//right offset
	OUTARRAY1[2]=-1*max_array(ARR,4);//top offset
	OUTARRAY1[3]=max_array(ARR3,4);//bottom offset

	leftoffset = OUTARRAY1[0];
	rightoffset = OUTARRAY1[1];
	topoffset = OUTARRAY1[2];
	bottomoffset = OUTARRAY1[3];

	//set wBlock and hBlock
	OUTARRAY2[0] = (rightoffset - leftoffset)/(double)(maxBlocks+1);//block width
	OUTARRAY2[1] = (bottomoffset - topoffset)/(double)(maxBlocks+1);//block height

	fptr = fopen(outFile,"wb");
	fwrite(OUTARRAY1,sizeof(int),4,fptr);
	fwrite(OUTARRAY2,sizeof(double),2,fptr);
	fclose(fptr);

	wBlock = OUTARRAY2[0];
	hBlock = OUTARRAY2[1];

	free(Y);

}
Exemple #8
0
params* staff_segment(const image_t *img, staff_info *staff){
       int8_t range_f;
       float thrsh;
       uint16_t height;
       uint16_t width;
       uint16_t l;
       uint16_t i;
       uint16_t staffCounter,staffX,staffY;
       uint16_t length_staff_lines;
       uint16_t s_begin,s_end,fudge;
       int16_t addNum;
       projection_t* proj_onto_y;
       flex_array_t* crude_lines;
       flex_array_t* line_w;
       flex_array_t* diff_array;
       flex_array_t* minus_array_var;
       flex_array_t* compare_array;
       flex_array_t* kill_array;
       flex_array_t* less_crude_lines;
       flex_array_t* diff_lines;
       flex_array_t* test_lines;
       flex_array_t* top;
       flex_array_t* middle;
       flex_array_t* bottom;
       params* new_param;
       range_f=5;
       /*this code uses a projection to determine cuts for 
       segmenting a music page into individual lines of music*/

       height=img->height;
       width=img->width;
       /*projection on to vertical axis:*/
       proj_onto_y = project(img , 2);
       /*calculate threshold:*/
       thrsh = 0.42 * max_array(proj_onto_y);
       crude_lines = find(proj_onto_y,thrsh,greater);
       delete_flex_array(proj_onto_y);
       /*create array holding y values of all stafflines:*/
       l = crude_lines->length;
       i = 0;
       length_staff_lines=0;
 
       /*find length of staff line array*/
       while (i < l){
    
             /*next staffline must be at least two pixels away:*/
             while ( ((i+1)<l) && ((crude_lines->data[i]+1)==crude_lines->data[i+1]||((crude_lines->data[i]+2)==crude_lines->data[i+1]))){
                   i = i + 1;
             }
             length_staff_lines++;
             i = i + 1;
       }
       
       less_crude_lines=make_flex_array(length_staff_lines);

       line_w=make_flex_array(length_staff_lines);
       i=0;
       staffCounter=0;
       while (i < l){
             s_begin = crude_lines->data[i];
    
             /*next staffline must be at least two pixels away:*/
             while ( ((i+1)<l) && ((crude_lines->data[i]+1)==crude_lines->data[i+1]||((crude_lines->data[i]+2)==crude_lines->data[i+1]))){
                   i = i + 1;
             }
             s_end = crude_lines->data[i];
             /*add staffline to array:*/
             
             less_crude_lines->data[staffCounter]=(s_begin+s_end+1)/2;/*round((s_begin + s_end)/2) works the same*/
             line_w->data[staffCounter]=(s_end - s_begin+1);/*think this is necessary the +1 is new*/
             i = i + 1;
             staffCounter++;
       }
       delete_flex_array(crude_lines);
       /*search for any incorrect lines*/
       /*(check against others):*/
       diff_lines=diff(less_crude_lines);
       fudge=median(diff_lines);
       delete_flex_array(diff_lines);
       l = less_crude_lines->length;
       kill_array=make_flex_array(l);
       for (i=0;i<l;i++){
           kill_array->data[i]=0;
       }
       i = 0;
       while (i<=(l-5)){
            test_lines = sub_array(less_crude_lines,i,i+4);/*staff_lines(i:i+4);*/
            diff_array=abs_diff(test_lines);
            delete_flex_array(test_lines);
            minus_array_var=minus(diff_array,fudge);
            delete_flex_array(diff_array);
            compare_array=find(minus_array_var,fudge/5,greater);
            delete_flex_array(minus_array_var);
            if (compare_array){
               kill_array->data[i]=1;
               i = i + 1;
		       delete_flex_array(compare_array);
            }
            
            i = i + 5;
       }
       while (i<l){
             kill_array->data[i]=1;
             i++;
       }
       /*for (i=0;i<l;i++){
           if(kill_array->data[i]==0) printf("%d\n",less_crude_lines->data[i]);
       }
       system("PAUSE");*/
       /*kill bad stafflines:*/
       less_crude_lines=kill_array_indices(less_crude_lines,kill_array);
       delete_flex_array(kill_array);
       l=less_crude_lines->length;
       if(l%5){
		       fprintf(stderr,"Error, found stafflines not a multiple of 5");
		       exit(1);
       }
       staff->staff_lines=(uint16_t**)multialloc(sizeof(uint16_t),2,5,l/5);
       staff->staff_bounds=(uint16_t**)multialloc(sizeof(uint16_t),2,l/5,2);
       staff->number_staffs=l/5;
       staffX=0;
       staffY=0;
       for (i=0;i<l;i++){
           staff->staff_lines[staffX][staffY]=less_crude_lines->data[i];
           staffX++;
           if (staffX==5){
              staffX=0;
              staffY++;
           }
       }
       
       delete_flex_array(less_crude_lines);

       /*calculate a good place to cut stafflines*/
       top=get_line_at_index(staff,0);
       middle=get_line_at_index(staff,2);
       bottom=get_line_at_index(staff,4);
       diff_array=minus_array(bottom,top);
       delete_flex_array(top);
       delete_flex_array(bottom);
       new_param=malloc(sizeof(params));
       new_param->staff_h=rounded_mean(diff_array);
       delete_flex_array(diff_array);
       range_f=((new_param->staff_h)*range_f+2)/4;/*should be same as round(range_f/2*mean(staff_lines(5, :) - staff_lines(1, :))); where range_f was 2.5*/
       for (i=0;i<l/5;i++){
            addNum=middle->data[i]-range_f;
            if(addNum<0) addNum=0;
            staff->staff_bounds[i][0]=addNum;
            addNum=middle->data[i]+range_f;
            if(addNum>=height) addNum=height-1;
            staff->staff_bounds[i][1]=addNum;
       }
       delete_flex_array(middle);
       /*music parameters */
       new_param->thickness=rounded_mean(line_w);
       delete_flex_array(line_w);
       addNum=0;
       for (i=1;i<5;i++){
           top=get_line_at_index(staff,i);
           bottom=get_line_at_index(staff,i-1);
           diff_array=minus_array(top,bottom);
           delete_flex_array(top);
           delete_flex_array(bottom);
           addNum+=sum(diff_array);
           delete_flex_array(diff_array);
       }
       addNum+=2*(staff->number_staffs);
       new_param->spacing=addNum/(4*(staff->number_staffs))-new_param->thickness;/*may introduce rounding errors*/
       new_param->ks=0;
       new_param->ks_x=0;
       return new_param;
}
static gboolean update_monitors(t_global_monitor *global)
{
    char buffer[SUM+1][BUFSIZ];
    gchar caption[BUFSIZ];
    gulong net[SUM+1];
    gulong display[SUM+1], max;
    guint64 histcalculate;
    double temp;
    gint i, j;

    get_current_netload( &(global->monitor->data), &(net[IN]), &(net[OUT]), &(net[TOT]) );
    

    for (i = 0; i < SUM; i++)
    {
        /* correct value to be from 1 ... 100 */
        global->monitor->history[i][0] = net[i];

        if (global->monitor->history[i][0] < 0)
        {
            global->monitor->history[i][0] = 0;
        }

        histcalculate = 0;
        for( j = 0; j < HISTSIZE_CALCULATE; j++ )
        {
            histcalculate += global->monitor->history[i][j];
        }
        display[i] = histcalculate / HISTSIZE_CALCULATE;
        
        /* shift for next run */
        for( j = HISTSIZE_STORE - 1; j > 0; j-- )
        {
            global->monitor->history[i][j] = global->monitor->history[i][j-1];
        }
        
        /* update maximum */
        if( global->monitor->options.auto_max )
        {
            max = max_array( global->monitor->history[i], HISTSIZE_STORE );
            if( display[i] > global->monitor->net_max[i] )
            {
                global->monitor->net_max[i] = display[i];
            }
            else if( max < global->monitor->net_max[i] * SHRINK_MAX 
                    && global->monitor->net_max[i] * SHRINK_MAX >= MINIMAL_MAX )
            {
                global->monitor->net_max[i] *= SHRINK_MAX;
            }
        }

#ifdef DEBUG        
        switch( i ) 
        {
            case IN:
                fprintf( stderr, "input: Max = %lu\n", global->monitor->net_max[i] );
                break;
                
            case OUT:
                fprintf( stderr, "output: Max = %lu\n", global->monitor->net_max[i] );
                break;
                
            case TOT:
                fprintf( stderr, "total: Max = %lu\n", global->monitor->net_max[i] );
                break;
        }
#endif /* DEBUG */
        
        
        temp = (double)display[i] / global->monitor->net_max[i];
        if( temp > 1 )
        {
            temp = 1.0;
        }
        else if( temp < 0 )
        {
            temp = 0.0;
        }
        gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(global->monitor->status[i]), temp);

        format_with_thousandssep( buffer[i], BUFSIZ, display[i] / 1024.0, 2 );
    }
    
    format_with_thousandssep( buffer[TOT], BUFSIZ, (display[IN]+display[OUT])  / 1024, 2 );
    
    g_snprintf(caption, sizeof(caption), 
            _("Average of last %d measures:\n"
                "Incoming: %s kByte/s\nOutgoing: %s kByte/s\nTotal: %s kByte/s"),
                HISTSIZE_CALCULATE, buffer[IN], buffer[OUT], buffer[TOT]);
    gtk_tooltips_set_tip(tooltips, GTK_WIDGET(global->monitor->ebox), caption, NULL);

    return TRUE;
}