⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hamming_wd.c

📁 error correction code
💻 C
字号:
// ------------------------------------------------------------------------
// File: hamming_wd.c
// Weight distribution of the Hamming code of length 2^m-1
//
// Based on recurrence satisfied by coefficients of W.D.
// in MacWilliams and Slone
// ------------------------------------------------------------------------
// This program is complementary material for the book:
//
// R.H. Morelos-Zaragoza, The Art of Error Correcting Coding, Wiley, 2002.
//
// ISBN 0471 49581 6
//
// This and other programs are available at http://the-art-of-ecc.com
//
// You may use this program for academic and personal purposes only. 
// If this program is used to perform simulations whose results are 
// published in a journal or book, please refer to the book above.
//
// The use of this program in a commercial product requires explicit
// written permission from the author. The author is not responsible or 
// liable for damage or loss that may be caused by the use of this program. 
//
// Copyright (c) 2002. Robert H. Morelos-Zaragoza. All rights reserved.
// ------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>

main(int argc, char **argv)
{
  int m;
  double A[3];  // previous, current and next coefficient of
                     // weight distribution A(X)
  double n, n1;
  char name[40];
  double counter;
  double comb(double a, double b);
  FILE *fp;

  printf("Computation of the weight distribution of a Hamming code\n");

  if (argc != 3)
  {
    printf("Usage: %s m file_wd\n", argv[0]);
    exit(1);
  }
  sscanf(argv[1], "%d", &m);
  sscanf(argv[2], "%s", name);
  fp = fopen(name,"w");

  n = (double) pow(2.0,(double)m)-1.0;
  n1 = n - 1.0;
  A[0] = 1.0;
  A[1] = 0.0;

for (counter=1.0; counter<=n; counter+=1.0)
printf("comb(%lf,%lf) = %lf\n", n, counter, comb(n,counter));

  printf("\t0 \t1\n");
  fprintf(fp, "\t0 \t1\n");

  counter = 2.0;
  do 
    {
      A[2] = (comb(n,counter-1.0) - (n-counter+2.0)*A[0] - A[1]) / counter;
      if ((A[2]=fabs(A[2])) > 0.01)  // it's dangerous to compare with 0.0
      {
        printf("\t%.0lf\t%.0lf\n", counter, A[2]);
        fprintf(fp, "\t%.0lf\t%.0lf\n", counter, A[2]);
      }
      A[0] = A[1];
      A[1] = A[2];
      counter += 1.0;
    }
  while (counter <= n);
}


long double comb(double n, double k)
{
  long double i, res, z;
  
  res = 1.0;
  if (n<=k) 
    return(1.0);
  z = n-k;
  if (z > k)
    for (i=1.0; i<(k+0.001); i+=1.0)
      res *= ((n-i+1.0)/i);
  else
    for (i=1.0; i<(z+0.001); i+=1.0)
      res *= ((n-i+1.0)/i);
  return(res);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -