Пример #1
0
void read_pchk
( char *pchk_file
)
{
  FILE *f;

  f = open_file_std(pchk_file,"rb");
  if (f==NULL)
  { fprintf(stderr,"Can't open parity check file: %s\n",pchk_file);
    exit(1);
  }

  if (intio_read(f)!=('P'<<8)+0x80)
  { fprintf(stderr,"File %s doesn't contain a parity check matrix\n",pchk_file);
    exit(1);
  }

  H = mod2sparse_read(f);

  if (H==0)
  { fprintf(stderr,"Error reading parity check matrix from %s\n",pchk_file);
    exit(1);
  }

  M = mod2sparse_rows(H);
  N = mod2sparse_cols(H);

  fclose(f);
}
Пример #2
0
void handle_file_ide (gchar *filename, gint line)
{
  if (! filename || ! cur_tea_project)
     return;
 
  gchar *cmd = g_strconcat (cur_tea_project->dir_source, filename, NULL);
 
  gint i = get_n_page_by_filename (cmd);

  if (i != -1)
     {
      gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook1), i);
      if (get_page_text ())
          doc_select_line (cur_text_doc, line); 
      return;
     } 

  if (! g_file_test (cmd, G_FILE_TEST_EXISTS))
     return;
 
  else
      {
       if (! get_page_text() )
          cur_settings.selected_enc = ch_str (cur_settings.selected_enc, "UTF-8");
       else
           cur_settings.selected_enc = ch_str (cur_settings.selected_enc, cur_text_doc->encoding);

       cur_text_doc = open_file_std (cmd);
       doc_select_line (cur_text_doc, line);
      }
}
Пример #3
0
void handle_file (gchar const *filename, gint mode)
{
	if (! filename)
		return;

	gchar *cmd = NULL;

	gint i = get_n_page_by_filename (filename);

	if (i != -1)
	{
		gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook1), i);
		return;
	} 

	if (! g_file_test (filename, G_FILE_TEST_EXISTS))
		return;

	if (is_image (filename))
	{
		if (mode != 0)
		{
			cmd = g_strdup_printf (confile.ext_pic_editor, filename);
			if(system (cmd) == -1){printf("\n");}
			g_free (cmd);
			return;
		}

	if (confile.use_ext_image_viewer)
	{
		cmd = g_strdup_printf (confile.ext_pic_editor, filename);
		if(system (cmd) == -1){printf("\n");}
			g_free(cmd);
				return;
	}
	else  
		create_wnd_imgviewer (filename);

	return;
	}
  
	if (! get_page_text() )
		cur_settings.selected_enc = ch_str (cur_settings.selected_enc, confile.default_charset);
	else
		cur_settings.selected_enc = ch_str (cur_settings.selected_enc, cur_text_doc->encoding);

	open_file_std (filename);
}
Пример #4
0
void kwas_handle_file (gchar *filename, int mode)
{
	if (! filename)
		return;

	gchar *cmd = NULL;

	gint i = get_n_page_by_filename (filename);

	if (i != -1)
	{
		gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook1), i);
		return;
	} 

	if (! g_file_test (filename, G_FILE_TEST_EXISTS))
		return;

	if (is_image (filename))
	{
		if (mode != 0)
		{
			cmd = g_strdup_printf (confile.ext_pic_editor, filename);
				if(system (cmd) == -1){printf("\n");}
					g_free (cmd);
				return;
		}

	if (confile.use_ext_image_viewer)
	{
		cmd = g_strdup_printf (confile.ext_pic_editor, filename);
			if(system (cmd) == -1){printf("\n");}
				g_free(cmd);
			return;        
	}
	else  
		create_wnd_imgviewer (filename);

	return;
	}

	open_file_std (filename);
}
Пример #5
0
void handle_file_enc (gchar *filename, gchar *enc)
{
	if (! filename)
		return;

	gint i = get_n_page_by_filename (filename);

	if (i != -1)
	{
		gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook1), i);
		return;
	} 

	if (! g_file_test (filename, G_FILE_TEST_EXISTS))
		return;

	cur_settings.selected_enc = ch_str (cur_settings.selected_enc, enc);

	open_file_std (filename);
}
Пример #6
0
int main
( int argc,
  char **argv
)
{
  char *pchk_file, *rfile, *dfile, *pfile;
  char **meth;
  FILE *rf, *df, *pf;

  char *dblk, *pchk;
  double *lratio;
  double *bitpr;

  double *awn_data;		/* Places to store channel data */
  int *bsc_data;

  unsigned iters;		/* Unsigned because can be huge for enum */
  double tot_iter;		/* Double because can be huge for enum */
  double chngd, tot_changed;	/* Double because can be fraction if lratio==1*/

  int tot_valid;
  char junk;
  int valid;

  int i, j, k;

  /* Look at initial flag arguments. */

  table = 0;
  blockio_flush = 0;

  while (argc>1)
  {
    if (strcmp(argv[1],"-t")==0)
    { if (table!=0) usage();
      table = 1;
    }
    else if (strcmp(argv[1],"-T")==0)
    { if (table!=0) usage();
      table = 2;
    }
    else if (strcmp(argv[1],"-f")==0)
    { if (blockio_flush!=0) usage();
      blockio_flush = 1;
    }
    else 
    { break;
    }

    argc -= 1;
    argv += 1;
  }

  /* Look at arguments up to the decoding method specification. */

  if (!(pchk_file = argv[1])
   || !(rfile = argv[2])
   || !(dfile = argv[3]))
  { usage();
  }

  if (argv[4]==0 || argv[5]==0) usage();

  k = channel_parse(argv+4,argc-4);
  if (k<=0)
  { pfile = argv[4];
    k = channel_parse(argv+5,argc-5);
    if (k<=0) usage();
    meth = argv+5+k;
  }
  else
  { pfile = 0;
    meth = argv+4+k;
  }

  /* Look at the specification of the decoding method, which starts at meth and
     continues to the end of the command line (marked by a zero pointer). */

  if (!meth[0]) usage();

  if (strcmp(meth[0],"prprp")==0)
  { dec_method = Prprp;
    if (!meth[1] || sscanf(meth[1],"%d%c",&max_iter,&junk)!=1 || meth[2]) 
    { usage();
    }
  }
  else if (strcmp(meth[0],"enum-block")==0)
  { dec_method = Enum_block;
    if (!(gen_file = meth[1]) || meth[2]) usage();
  }
  else if (strcmp(meth[0],"enum-bit")==0)
  { dec_method = Enum_bit;
    if (!(gen_file = meth[1]) || meth[2]) usage();
  }
  else 
  { usage();
  }

  /* Check that we aren't overusing standard input or output. */

  if ((strcmp(pchk_file,"-")==0) 
    + (strcmp(rfile,"-")==0) > 1)
  { fprintf(stderr,"Can't read more than one stream from standard input\n");
    exit(1);
  }
  if ((table>0) 
    + (strcmp(dfile,"-")==0) 
    + (pfile!=0 && strcmp(pfile,"-")==0) > 1)
  { fprintf(stderr,"Can't send more than one stream to standard output\n");
    exit(1);
  }

  /* Read parity check file. */

  read_pchk(pchk_file);

  if (N<=M)
  { fprintf(stderr,
     "Number of bits (%d) should be greater than number of checks (%d)\n",N,M);
    exit(1);
  }

  /* Open file of received data. */

  rf = open_file_std(rfile,"r");
  if (rf==NULL)
  { fprintf(stderr,"Can't open file of received data: %s\n",rfile);
    exit(1);
  }

  /* Create file for decoded data. */

  df = open_file_std(dfile,"w");
  if (df==NULL)
  { fprintf(stderr,"Can't create file for decoded data: %s\n",dfile);
    exit(1);
  }

  /* Create file for bit probabilities, if specified. */

  if (pfile)
  { pf = open_file_std(pfile,"w");
    if (pf==NULL)
    { fprintf(stderr,"Can't create file for bit probabilities: %s\n",pfile);
      exit(1);
    }
  }

  /* Allocate space for data from channel. */

  switch (channel)
  { case BSC:
    { bsc_data = chk_alloc (N, sizeof *bsc_data);
      break;
    }
    case AWGN: case AWLN:
    { awn_data = chk_alloc (N, sizeof *awn_data);
      break;
    }
    default:
    { abort();
    }
  }

  /* Allocate other space. */

  dblk   = chk_alloc (N, sizeof *dblk);
  lratio = chk_alloc (N, sizeof *lratio);
  pchk   = chk_alloc (M, sizeof *pchk);
  bitpr  = chk_alloc (N, sizeof *bitpr);

  /* Print header for summary table. */

  if (table==1)
  { printf("  block iterations valid  changed\n");
  }

  /* Do the setup for the decoding method. */

  switch (dec_method)
  { case Prprp: 
    { prprp_decode_setup();
      break;
    }
    case Enum_block: case Enum_bit:
    { enum_decode_setup();
      break;
    }
    default: abort();
  }

  /* Read received blocks, decode, and write decoded blocks. */

  tot_iter = 0;
  tot_valid = 0;
  tot_changed = 0;

  for (block_no = 0; ; block_no++)
  { 
    /* Read block from received file, exit if end-of-file encountered. */

    for (i = 0; i<N; i++)
    { int c;
      switch (channel)
      { case BSC:  
        { c = fscanf(rf,"%1d",&bsc_data[i]); 
          break;
        }
        case AWGN: case AWLN:
        { c = fscanf(rf,"%lf",&awn_data[i]); 
          break;
        }
        default: abort();
      }
      if (c==EOF) 
      { if (i>0)
        { fprintf(stderr,
          "Warning: Short block (%d long) at end of received file ignored\n",i);
        }
        goto done;
      }
      if (c<1 || channel==BSC && bsc_data[i]!=0 && bsc_data[i]!=1)
      { fprintf(stderr,"File of received data is garbled\n");
        exit(1);
      }
    }

    /* Find likelihood ratio for each bit. */

    switch (channel)
    { case BSC:
      { for (i = 0; i<N; i++)
        { lratio[i] = bsc_data[i]==1 ? (1-error_prob) / error_prob
                                     : error_prob / (1-error_prob);
        }
        break;
      }
      case AWGN:
      { for (i = 0; i<N; i++)
        { lratio[i] = exp(2*awn_data[i]/(std_dev*std_dev));
        }
        break;
      }
      case AWLN:
      { for (i = 0; i<N; i++)
        { double e, d1, d0;
          e = exp(-(awn_data[i]-1)/lwidth);
          d1 = 1 / ((1+e)*(1+1/e));
          e = exp(-(awn_data[i]+1)/lwidth);
          d0 = 1 / ((1+e)*(1+1/e));
          lratio[i] = d1/d0;
        }
        break;
      }
      default: abort();
    }

    /* Try to decode using the specified method. */

    switch (dec_method)
    { case Prprp:
      { iters = prprp_decode (H, lratio, dblk, pchk, bitpr);
        break;
      }
      case Enum_block: case Enum_bit:
      { iters = enum_decode (lratio, dblk, bitpr, dec_method==Enum_block);
        break;
      }
      default: abort();
    }

    /* See if it worked, and how many bits were changed. */

    valid = check(H,dblk,pchk)==0;

    chngd = changed(lratio,dblk,N);

    tot_iter += iters;
    tot_valid += valid;
    tot_changed += chngd;

    /* Print summary table entry. */

    if (table==1)
    { printf ("%7d %10f    %d  %8.1f\n",
        block_no, (double)iters, valid, (double)chngd);
        /* iters is printed as a double to avoid problems if it's >= 2^31 */
      fflush(stdout);
    }

    /* Write decoded block. */

    blockio_write(df,dblk,N);

    /* Write bit probabilities, if asked to. */

    if (pfile)
    { for (j = 0; j<N; j++)
      { fprintf(pf," %.5f",bitpr[j]);
      }
      fprintf(pf,"\n");
    }

    /* Check for errors when writing. */

    if (ferror(df) || pfile && ferror(pf))
    { break;
    }
  }

  /* Finish up. */

done: 
  fprintf(stderr,
  "Decoded %d blocks, %d valid.  Average %.1f iterations, %.0f%% bit changes\n",
   block_no, tot_valid, (double)tot_iter/block_no, 
   100.0*(double)tot_changed/(N*block_no));

  if (ferror(df) || fclose(df)!=0)
  { fprintf(stderr,"Error writing decoded blocks to %s\n",dfile);
    exit(1);
  }

  if (pfile)
  { if (ferror(pf) || fclose(pf)!=0)
    { fprintf(stderr,"Error writing bit probabilities to %s\n",dfile);
      exit(1);
    }
  }

  exit(0);
}
Пример #7
0
void read_gen
( char *gen_file,	/* Name of generator matrix file */
  int cols_only,	/* Read only column ordering? */
  int no_pchk_file	/* No parity check file used? */
)
{
  int M2, N2;
  FILE *f;
  int i;

  f = open_file_std(gen_file,"rb");
  if (f==NULL)
  { fprintf(stderr,"Can't open generator matrix file: %s\n",gen_file);
    exit(1);
  }

  if (intio_read(f)!=('G'<<8)+0x80)
  { fprintf(stderr,"File %s doesn't contain a generator matrix\n",gen_file);
    exit(1);
  }

  if (fread (&type, 1, 1, f) != 1) goto error;

  M2 = intio_read(f);
  N2 = intio_read(f);

  if (feof(f) || ferror(f)) goto error;

  if (no_pchk_file)
  { M = M2;
    N = N2;
  }
  else 
  { if (M2!=M || N2!=N)
    { fprintf(stderr,
              "Generator matrix and parity-check matrix are incompatible\n");
      exit(1);
    }
  }

  cols = chk_alloc (N, sizeof *cols);
  rows = chk_alloc (M, sizeof *rows);

  for (i = 0; i<N; i++)
  { cols[i] = intio_read(f);
    if (feof(f) || ferror(f)) goto error;
  }

  if (!cols_only)
  {
    switch (type)
    {
      case 's':
      { 
        for (i = 0; i<M; i++)
        { rows[i] = intio_read(f);
          if (feof(f) || ferror(f)) goto error;
        }

        if ((L = mod2sparse_read(f)) == 0) goto error;
        if ((U = mod2sparse_read(f)) == 0) goto error;
  
        if (mod2sparse_rows(L)!=M || mod2sparse_cols(L)!=M) goto garbled;
        if (mod2sparse_rows(U)!=M || mod2sparse_cols(U)<M) goto garbled;
       
        break;
      }
  
      case 'd':
      {
        if ((G = mod2dense_read(f)) == 0) goto error;
  
        if (mod2dense_rows(G)!=M || mod2dense_cols(G)!=N-M) goto garbled;
  
        break;
      }
  
      case 'm':
      {
        if ((G = mod2dense_read(f)) == 0) goto error;
  
        if (mod2dense_rows(G)!=M || mod2dense_cols(G)!=M) goto garbled;
  
        break;
      }
  
      default: 
      { fprintf(stderr,
         "Unknown type of generator matrix in file %s\n",gen_file);
        exit(1);
      }
    }
  }
  
  fclose(f);

  return;

error:
  fprintf(stderr,"Error reading generator matrix from file %s\n",gen_file);
  exit(1);

garbled:
  fprintf(stderr,"Garbled generator matrix in file %s\n",gen_file);
  exit(1);
}
Пример #8
0
int main
( int argc,
  char **argv
)
{
  mod2dense *D,*Z,*G;
  mod2sparse *T;
	int *a_col,*a_row;
	int *cols2;
  char *pchk_file;
 	char fn[128];
 	char *code;
 	FILE *fp;
 	
 	int i,j,n;
 	int a,b,c;
 	
 	int codeLen,checkLen;
 	
 	pchk_file = argv[3];
	
	sscanf(argv[1],"%ld",&codeLen);
	sscanf(argv[2],"%ld",&checkLen);
	
  sprintf(fn,"data/code%dX%d.bin",codeLen,checkLen);
  
  
  D = mod2dense_allocate( checkLen, codeLen );
  
  code=malloc(codeLen*checkLen);
	
	printf(" read from %s\n",fn);
	fp=fopen(fn,"rb");
	if( fp==NULL )
	{
		printf("open file error\n");
		return -1;
	}
	fread(code,codeLen*checkLen,1,fp);
	fclose(fp);

  mod2dense_clear( D );
  for( i=0;i<checkLen;i++ )
  	for( j=0;j<codeLen;j++ )
  	{
  		if( code[i*codeLen+j] )
  			mod2dense_set( D, i, j, 1 );
  	}
  
  
  T = mod2sparse_allocate( checkLen, codeLen );
  mod2dense_to_sparse( D , T );	
  
  fp = open_file_std(pchk_file,"wb");
  if (fp==NULL) 
  { fprintf(stderr,"Can't create parity check file: %s\n",pchk_file);
    exit(1);
  }

  intio_write(fp,('P'<<8)+0x80);
  
  if (ferror(fp) || !mod2sparse_write(fp,T) || fclose(fp)!=0)
  { fprintf(stderr,"Error writing to parity check file %s\n",pchk_file);
    exit(1);
  }
  
  free(code);
  
  return 0;
}
Пример #9
0
int main
( int argc,
  char **argv
)
{
  make_method method;
  char *file, **meth;
  int seed, no4cycle;
  distrib *d;
  char junk;
  FILE *f;
  
  /* Look at initial arguments. */

  if (!(file = argv[1])
   || !argv[2] || sscanf(argv[2],"%d%c",&M,&junk)!=1 || M<=0
   || !argv[3] || sscanf(argv[3],"%d%c",&N,&junk)!=1 || N<=0
   || !argv[4] || sscanf(argv[4],"%d%c",&seed,&junk)!=1)
  { usage();
  }

  /* Look at the arguments specifying the method for producing the code. */

  meth = argv+5;

  if (!meth[0]) usage();

  no4cycle = 0;

  if (strcmp(meth[0],"evencol")==0 || strcmp(meth[0],"evenboth")==0)
  { method = strcmp(meth[0],"evencol")==0 ? Evencol : Evenboth;
    if (!meth[1])
    { usage();
    }
    d = distrib_create(meth[1]);
    if (d==0)
    { usage();
    }
    if (meth[2])
    { if (strcmp(meth[2],"no4cycle")==0)
      { no4cycle = 1;
        if (meth[3])
        { usage();
        }
      }
      else
      { usage();
      }
    }
  }
  else
  { usage();
  }

  /* Check for some problems. */

  if (distrib_max(d)>M)
  { fprintf(stderr,
      "At least one checks per bit (%d) is greater than total checks (%d)\n",
      distrib_max(d), M);
    exit(1);
  }

  if (distrib_max(d)==M && N>1 && no4cycle)
  { fprintf(stderr,
      "Can't eliminate cycles of length four with this many checks per bit\n");
    exit(1);
  } 

  /* Make the parity check matrix. */

  make_ldpc(seed,method,d,no4cycle);

  /* Write out the parity check matrix. */

  f = open_file_std(file,"wb");
  if (f==NULL) 
  { fprintf(stderr,"Can't create parity check file: %s\n",file);
    exit(1);
  }

  intio_write(f,('P'<<8)+0x80);
  
  if (ferror(f) || !mod2sparse_write(f,H) || fclose(f)!=0)
  { fprintf(stderr,"Error writing to parity check file %s\n",file);
    exit(1);
  }

  return 0;
}
Пример #10
0
int main
( int argc,
  char **argv
)
{
  char *alist_file, *pchk_file;
  FILE *af, *pf;
  int mxrw, mxcw;
  int *rw, *cw;
  int i, j, k;
  mod2entry *e;
  int trans;
  int nozeros;
  int last;

  trans = 0;
  nozeros = 0;

  for (;;)
  {
    if (argc>1 && strcmp(argv[1],"-t")==0)
    { trans = 1;
      argc -= 1;
      argv += 1;
    }
    else if (argc>1 && strcmp(argv[1],"-z")==0)
    { nozeros = 1;
      argc -= 1;
      argv += 1;
    }
    else
    { break;
    }
  }

  if (argc!=3)
  { usage();
  }

  pchk_file = argv[1];
  alist_file = argv[2];

  read_pchk(pchk_file);

  if (trans)
  { mod2sparse *HT;
    HT = H;
    H = mod2sparse_allocate(N,M);
    mod2sparse_transpose(HT,H);
    M = mod2sparse_rows(H);
    N = mod2sparse_cols(H);
  }

  af = open_file_std(alist_file,"wb");

  if (af==NULL) 
  { fprintf(stderr,"Can't create alist file: %s\n",alist_file);
    exit(1);
  }

  fprintf(af,"%d %d\n",M,N);

  rw = (int *) chk_alloc (M, sizeof *rw);
  mxrw = 0;

  for (i = 0; i<M; i++)
  { rw[i] = mod2sparse_count_row(H,i);
    if (rw[i]>mxrw)
    { mxrw = rw[i];
    }
  }

  cw = (int *) chk_alloc (N, sizeof *cw);
  mxcw = 0;

  for (j = 0; j<N; j++)
  { cw[j] = mod2sparse_count_col(H,j);
    if (cw[j]>mxcw)
    { mxcw = cw[j];
    }
  }

  fprintf(af,"%d %d\n",mxrw,mxcw);

  for (i = 0; i<M; i++)
  { fprintf(af,"%d%c",rw[i],i==M-1?'\n':' ');
  }

  for (j = 0; j<N; j++)
  { fprintf(af,"%d%c",cw[j],j==N-1?'\n':' ');
  }

  for (i = 0; i<M; i++)
  { e = mod2sparse_first_in_row(H,i);
    last = 0;
    for (k = 0; !last; k++)
    { last = nozeros ? k==rw[i]-1 : k==mxrw-1;
      fprintf (af, "%d%c", mod2sparse_at_end(e)?0:mod2sparse_col(e)+1,
                           last?'\n':' ');
      if (!mod2sparse_at_end(e)) 
      { e = mod2sparse_next_in_row(e);
      }
    }
  }

  for (j = 0; j<N; j++)
  { e = mod2sparse_first_in_col(H,j);
    last = 0;
    for (k = 0; !last; k++)
    { last = nozeros ? k==cw[j]-1 : k==mxcw-1;
      fprintf (af, "%d%c", mod2sparse_at_end(e)?0:mod2sparse_row(e)+1,
                           last?'\n':' ');
      if (!mod2sparse_at_end(e)) 
      { e = mod2sparse_next_in_col(e);
      }
    }
  }

  if (ferror(af) || fclose(af)!=0)
  { fprintf(stderr,"Error writing to alist file %s\n",alist_file);
    exit(1);
  }

  return 0;
}