📄 simulation313.c
字号:
// ------------------------------------------------------------------------
// File: simulation313.c
//
// Simulation of a repetition code over a binary symmetric channel
//
// ------------------------------------------------------------------------
// 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>
#include <limits.h>
#define MAXRAND LONG_MAX // Maximum value of random()
#define N 3 // Code length
#define TWOTTN 3 // Equal to 2^{N-1}-1
main(int argc, char *argv[])
{
long seed;
double init_p, final_p, inc_p;
double sim, num_sim;
double p;
int codeword[N], received[N], decoded[N];
double error;
int i, info, estimate, onescount;
FILE *fp;
char name[80];
if (argc != 7)
{
printf("Usage: %s seed init_p final_p inc_p num_sim output_file\n",
argv[0]);
exit(0);
}
sscanf(argv[1],"%ld", &seed);
sscanf(argv[2],"%lf", &init_p);
sscanf(argv[3],"%lf", &final_p);
sscanf(argv[4],"%lf", &inc_p);
sscanf(argv[5],"%lf", &num_sim);
sscanf(argv[6],"%s", &name);
fp = fopen(name,"w");
srandom(seed);
p = init_p;
while (p < final_p)
{
error = 0.0;
sim = 0.0;
while (sim < num_sim)
{
// SOURCE: Generate random symbols
info = (random()>>10) % 2;
// ENCODER: Encode information
if (info)
{
for (i=0; i<N; i++)
codeword[i] = 1;
}
else
{
for (i=0; i<N; i++)
codeword[i] = 0;
}
// CHANNEL: Pass through BSC with parameter p
for (i=0; i<N; i++)
received[i] = codeword[i] ^ bsc(p);
// DECODER: Majority logic decoding is easiest
onescount = 0;
for (i=0; i<N; i++)
if (received[i]) onescount++;
if (onescount > N/2)
{
estimate = 1;
for (i=0; i<N; i++)
decoded[i] = 1;
}
else
{
estimate = 0;
for (i=0; i<N; i++)
decoded[i] = 0;
}
if (estimate != info) error += 1.0;
sim += 1.0;
}
fprintf(fp, "%e %e\n", p, (double)error/num_sim);
printf("%e %e\n", p, (double)error/num_sim);
fflush(stdout); fflush(fp);
p += inc_p;
}
}
int bsc(double p)
{
if ( ((double)(random())/MAXRAND) <= p )
return(1);
else
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -