CRC译码算法

来源:互联网 发布:不以 乎 句式 编辑:程序博客网 时间:2024/05/22 15:39





利用CRC算法计算误码率和误比特率,研究此算法的性能曲线。

CRC.h

#pragma once#include<time.h>#include<cstdlib>class CRC16 {public:/* data_in = the data to be encoded,[len] = number of bits to be encoded,data_out = the encoded output*/static void crc_enc(int* data_in, int* data_out, int len);static bool error_detect(int* det_in, int& error_dec);static int polynomial_division(int* data_in, int stop);static bool code_random(int* data, double p);};

CRC.cpp

#include"CRC16.h"//多项式除法 int CRC16::polynomial_division(int* data_in, int stop) {int first_position = 0;  for (first_position = 0; first_position < 1040; first_position++) {if (data_in[first_position] == 1) {break;}}while (first_position <= stop) {int generate[2000] = { 0 };generate[first_position] = 1;generate[first_position + 1] = 1;generate[first_position + 14] = 1;generate[first_position + 16] = 1;for (int j = 0; j < 1040; j++) {if (data_in[j] + generate[j] == 1) {data_in[j] = 1;}else {data_in[j] = 0;}}for (first_position = 0; first_position < 1040; first_position++) {if (data_in[first_position] == 1) {break;}}}return first_position;}//编码过程 void CRC16::crc_enc(int* data_in, int* data_out, int len) {for (int i = 0; i < 16; i++) {data_in[len + i] = 0;}int stop = 1023;polynomial_division(data_in, stop);for (int k = 0; k < 16; k++) {data_out[len + k] = data_in[len + k];}}//差错检验 bool CRC16::error_detect(int* det_in, int& error_dec) {int* data = new int[2000];for (int i = 0; i < 1040; i++) {data[i] = det_in[i];}int stop = 1023;int first_position = polynomial_division(data, stop);if (first_position != 1040) { error_dec++;return 1;}return 0;}//以概率p出错(过BSC信道) bool CRC16::code_random(int* data, double p) {srand((unsigned)time(NULL));int flag = 0;for (int i = 0; i < 1040; i++) {int num = rand() % (int)(1 / p);if (num < 1) {data[i] = 1 - data[i];flag = 1;}}if (flag == 1)return 1;return 0;}
main.cpp

#include<iostream> #include<cstdio>#include<time.h>#include<cstdlib>#include "CRC16.h"using namespace std;int main() {//生成1024位的源码 cout << "generated 1024-bit source code, the source code is as follows: " << endl;int* data_in = new int[2000];int* data_out = new int[2000];int len = 0;srand((unsigned)time(NULL));for (len = 0; len < 1024; len++) {data_in[len] = rand() % 2;data_out[len] = data_in[len];  //将源码复制一份到data_outcout << data_in[len];}//源码进行CRC编码生成校验码,源码+校验码存储于data_outCRC16::crc_enc(data_in, data_out, len);cout << endl << "The generated checksum is as follows: " << endl;for (int k = 1024; k < 1040; k++) {cout << data_out[k];}cout << endl;//进行测试double p[5] = { 0.1, 0.01, 0.001, 0.0001 };  //出错概率for (int k = 0; k < 4; k++) {int test = 10000;  //测试样例的数量double temp = (double)test;int error = 0; //数据出错的真实数量 int error_dec = 0; //数据出错的测试出的数量int m = 0; //漏检的数量 cout << endl << "Now the source code + check code through the probability of " << p[k] << " Of the BSC channel" << endl;cout << "The number of tests is " << test << endl;while (test--) {int* data = new int[2000];for (int i = 0; i < 1040; i++) {data[i] = data_out[i];}//过BSC信道if (CRC16::code_random(data, p[k]))error++;//差错检验 if (!CRC16::error_detect(data, error_dec)) {int i = 0;for (i = 0; i < 1040; i++) {if (data_out[i] != data[i])break;}//漏检检验 if (i != 1040) {cout << i << endl;m++;}}}cout << "There are a number of examples of real errors " << error << endl;cout << "There are a total of samples for detecting errors " << error_dec << endl;cout << "There are a total of missing samples " << m << endl;cout << "The error rate is " << (double)(error / temp) << endl;if (error != 0) {cout << "The error detection rate is " << (double)(error_dec / error) << endl;cout << "Missed rate " << (double)(m / error) << endl;}}system("pause");return 0;}
实验结果如下:


0 0
原创粉丝点击