Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bitset>
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <string.h>
- using namespace std;
- int main(int argc, char** argv) {
- int size=100;
- int i,j,k,l,p=0,crc1=0,crc2=0,crc3=0;
- int err;
- cout<<"Please enter an error rate(1 to 100)"<<"\n";
- cin>>err;
- srand ( time(NULL) );
- ofstream myfile ("frames.txt");
- if (myfile.is_open())
- {
- for ( i = 0; i < size; i++ ) {
- myfile<< bitset<8>(rand()) <<"\n";
- }
- myfile.close();
- }
- else cout << "Unable to open file";
- //void Array::readData(string file_name) {
- bitset<8> array[size];
- bitset<9> array2[size];
- bitset<9> array3[size];
- bitset<13> array4[size];
- bitset<13> array5[size];
- bitset<13> array6[size];
- bitset<40> array8[size];
- bitset<40> array9[size];
- bitset<40> array10[size];
- bitset<15> array11[size];
- bitset<15> array12[size];
- bitset<15> array13[size];
- ifstream infile;
- //infile.open(file_name.c_str());
- infile.open("frames.txt");
- if (!infile) {
- cout << "Could not open "
- // << file_name
- << " file for reading."
- << endl;
- }
- while (infile >> array[j]) {
- j++;
- }
- infile.close();
- for(i=0;i<size;i++)
- {
- k=0;
- for(j=0;j<8;j++)
- {
- array2[i][j+1]=array[i][j];
- if(array[i][j]==1){
- k++;
- }
- // cout<<k<<endl;
- if(k%2==0)
- k=0;
- array2[i][0]=k;
- }
- //cout<<k<<endl;
- }
- // cout<<array2[5];
- //void Array::writeData(string file_name) {
- ofstream outFile;
- outFile.open("frames2.txt");
- if (!outFile) {
- cout << "Could not open "
- // << file_name
- << " file for writing."
- << endl;
- }
- for (i = 0; i < size; i++) {
- outFile << array2[i];
- outFile << endl;
- }
- outFile.close();
- //cout <<array[0]<< endl;
- int arr[size];
- for(i=0;i<err;i++)
- {
- arr[i]=rand() % size;
- for(k=0;k<i-1;k++){
- while(arr[k]==arr[i]){
- arr[i]=rand() % size;
- }
- }
- }
- //cout<<arr[0]<<arr[1]<<arr[2]<<arr[3]<<endl;
- for(i=0;i<err;i++){
- //srand ( time(NULL) );
- j = arr[i];
- array[j]=bitset<8>(rand());
- cout<<array[j]<<endl;
- cout<<array2[j]<<endl;
- cout<<j<<endl;
- }
- // cout<<array[5]<<endl;
- //cout<<array2[5]<<endl;
- for(i=0;i<size;i++)
- {
- k=0;
- for(j=0;j<8;j++)
- {
- array3[i][j+1]=array[i][j];
- if(array[i][j]==1){
- k++;
- }
- // cout<<k<<endl;
- if(k%2==0)
- k=0;
- array3[i][0]=k;
- }
- //cout<<k<<endl;
- if(array2[i][0]!=array3[i][0])
- p=p+1;
- }
- //cout<<array2[5]<<endl;
- //void Array::writeData(string file_name) {
- ofstream outFile2;
- outFile2.open("frames3.txt");
- if (!outFile2) {
- cout << "Could not open "
- // << file_name
- << " file for writing."
- << endl;
- }
- for (i = 0; i < size; i++) {
- outFile2 << array3[i];
- outFile2 << endl;
- }
- outFile2.close();
- cout<<err<<endl;
- cout<<p<<endl;
- cout<<"Error detection rate for parity check is "<<size*p/err<<" %."<<endl;
- //For CRC 1 x^5+1
- for(i=0;i<size;i++)
- {
- for(j=0;j<8;j++)
- {
- array4[i][j+5]=array[i][j];
- array5[i][j+5]=array[i][j];
- }
- // cout<<k<<endl;
- for(k=0;k<5;k++){
- array4[i][k]=0;
- array5[i][k]=0;
- }
- }
- cout<<array4[1]<<endl;
- cout<<array5[1]<<endl;
- bitset < 13 > divisor(string("1000010000000"));
- bitset < 13 > divisor2 = divisor;
- for (i = 0; i < size; i++) {
- divisor2 = divisor;
- // cout << array4[i] << endl;
- for (j = 0; j < 8; j++) {
- // array5[i][j+5]=array[i][j];
- while(array4[i][12 - j] == 0 && j<13) {
- divisor2 >>= 1;
- j=j+1;
- }
- if (j < 8) {
- array4[i] = array4[i]^divisor2;
- // cout << array4[i] << endl;
- divisor2 >>= 1;
- }
- }
- for(k=0;k<5;k++){
- array5[i][k]=array4[i][k];
- }
- //cout << array4[i] << endl;
- //cout << array[i] << endl;
- //cout << array5[i] << endl;
- //outFile3 << array5[i];
- //outFile3 << endl;
- if(i==99)
- cout<<"Son";
- }
- for(i=0;i<err;i++){
- j = arr[i];
- array[j]=bitset<8>(rand());
- }
- for(i=0;i<size;i++)
- {
- for(j=0;j<8;j++)
- {
- array4[i][j+5]=array[i][j];
- array6[i][j+5]=array[i][j];
- }
- // cout<<k<<endl;
- for(k=0;k<5;k++){
- array4[i][k]=0;
- array6[i][k]=0;
- }
- }
- for (i = 0; i < size; i++) {
- divisor2 = divisor;
- // cout << array4[i] << endl;
- for (j = 0; j < 8; j++) {
- // array5[i][j+5]=array[i][j];
- while(array4[i][12 - j] == 0 && j<13) {
- divisor2 >>= 1;
- j=j+1;
- }
- if (j < 8) {
- array4[i] = array4[i]^divisor2;
- // cout << array4[i] << endl;
- divisor2 >>= 1;
- }
- }
- for(k=0;k<5;k++){
- array6[i][k]=array4[i][k];
- }
- //cout << array4[i] << endl;
- //cout << array[i] << endl;
- //cout << array5[i] << endl;
- //outFile3 << array5[i];
- //outFile3 << endl;
- if(i==99)
- cout<<"Son"<<endl;
- }
- for(i=0;i<size;i++){
- j=0;
- l=0;
- for(k=0;k<5;k++){
- j+=array5[i][k];
- l+=array6[i][k];
- }
- if(j!=l){
- cout<<i<<" "<<array5[i]<<endl;
- cout<<i<<" "<<array6[i]<<endl;
- crc1=crc1+1;
- }
- }
- // outFile3.close();
- ofstream outFile3;
- outFile3.open("frames4.txt");
- if (!outFile3) {
- cout << "Could not open "
- // << file_name
- << " file for writing."
- << endl;
- }
- for (i = 0; i < size; i++) {
- outFile3 << array5[i];
- outFile3 << endl;
- }
- outFile3.close();
- cout<<"Error detection rate for CRC-1 is "<<crc1*size/err<<" %."<<endl;
- //For CRC 2 CRC-32
- for(i=0;i<size;i++)
- {
- for(j=0;j<8;j++)
- {
- array8[i][j+32]=array[i][j];
- array9[i][j+32]=array[i][j];
- }
- for(k=0;k<32;k++){
- array8[i][k]=0;
- array9[i][k]=0;
- }
- }
- cout<<array8[1]<<endl;
- cout<<array9[1]<<endl;
- bitset < 40 > divisor3(string("1000001001100000100011101101101110000000"));
- bitset < 40 > divisor4 = divisor3;
- for (i = 0; i < size; i++) {
- divisor4 = divisor3;
- for (j = 0; j < 8; j++) {
- while(array8[i][39 - j] == 0 && j<40) {
- divisor4 >>= 1;
- j=j+1;
- }
- if (j < 8) {
- array8[i] = array8[i]^divisor4;
- divisor4 >>= 1;
- }
- }
- for(k=0;k<32;k++){
- array9[i][k]=array8[i][k];
- }
- if(i==99)
- cout<<"Son";
- }
- for(i=0;i<err;i++){
- j = arr[i];
- array[j]=bitset<8>(rand());
- }
- for(i=0;i<size;i++)
- {
- for(j=0;j<8;j++)
- {
- array8[i][j+32]=array[i][j];
- array10[i][j+32]=array[i][j];
- }
- for(k=0;k<32;k++){
- array8[i][k]=0;
- array10[i][k]=0;
- }
- }
- for (i = 0; i < size; i++) {
- divisor4 = divisor3;
- for (j = 0; j < 8; j++) {
- while(array8[i][39 - j] == 0 && j<40) {
- divisor4 >>= 1;
- j=j+1;
- }
- if (j < 8) {
- array8[i] = array8[i]^divisor4;
- divisor4 >>= 1;
- }
- }
- for(k=0;k<32;k++){
- array10[i][k]=array8[i][k];
- }
- if(i==99)
- cout<<"Son"<<endl;
- }
- for(i=0;i<size;i++){
- j=0;
- l=0;
- for(k=0;k<32;k++){
- j+=array9[i][k];
- l+=array10[i][k];
- }
- if(j!=l){
- cout<<i<<" "<<array9[i]<<endl;
- cout<<i<<" "<<array10[i]<<endl;
- crc2=crc2+1;
- }
- }
- cout<<"Error detection rate for CRC-32 is "<<crc2*size/err<<" %."<<endl;
- //For CRC-3 x^7
- for(i=0;i<size;i++)
- {
- for(j=0;j<8;j++)
- {
- array11[i][j+7]=array[i][j];
- array12[i][j+7]=array[i][j];
- }
- for(k=0;k<7;k++){
- array11[i][k]=0;
- array12[i][k]=0;
- }
- }
- cout<<array11[1]<<endl;
- cout<<array12[1]<<endl;
- bitset < 15 > divisor5(string("100010110000000"));
- bitset < 15 > divisor6 = divisor5;
- for (i = 0; i < size; i++) {
- divisor6 = divisor5;
- for (j = 0; j < 8; j++) {
- while(array11[i][14 - j] == 0 && j<15) {
- divisor6 >>= 1;
- j=j+1;
- }
- if (j < 8) {
- array11[i] = array11[i]^divisor6;
- divisor6 >>= 1;
- }
- }
- for(k=0;k<7;k++){
- array12[i][k]=array11[i][k];
- }
- if(i==99)
- cout<<"Son";
- }
- for(i=0;i<err;i++){
- j = arr[i];
- array[j]=bitset<8>(rand());
- }
- for(i=0;i<size;i++)
- {
- for(j=0;j<8;j++)
- {
- array11[i][j+7]=array[i][j];
- array13[i][j+7]=array[i][j];
- }
- for(k=0;k<7;k++){
- array11[i][k]=0;
- array13[i][k]=0;
- }
- }
- for (i = 0; i < size; i++) {
- divisor6 = divisor5;
- for (j = 0; j < 8; j++) {
- while(array11[i][14 - j] == 0 && j<15) {
- divisor6 >>= 1;
- j=j+1;
- }
- if (j < 8) {
- array11[i] = array11[i]^divisor6;
- divisor6 >>= 1;
- }
- }
- for(k=0;k<7;k++){
- array13[i][k]=array11[i][k];
- }
- if(i==99)
- cout<<"Son"<<endl;
- }
- for(i=0;i<size;i++){
- j=0;
- for(k=0;k<7;k++){
- if(array12[i][k]!=array13[i][k])
- j=j+1;
- }
- if(j>1){
- cout<<i<<" "<<array12[i]<<endl;
- cout<<i<<" "<<array13[i]<<endl;
- crc3=crc3+1;
- }
- }
- cout<<"Error detection rate for CRC-3 is "<<crc3*size/err<<" %."<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement