// // scramble_example.c // // Data-scrambling example. Physical layer synchronization of // received waveforms relies on independent and identically // distributed underlying data symbols. If the message sequence, // however, is '00000....' and the modulation scheme is BPSK, // the synchronizer probably won't be able to recover the symbol // timing. It is imperative to increase the entropy of the data // for this to happen. The data scrambler routine attempts to // 'whiten' the data sequence with a bit mask in order to achieve // maximum entropy. This example demonstrates the interface. // #include #include #include #include #include "liquid.h" float compute_entropy(unsigned char * _x, unsigned int _n); int main() { unsigned int n=32; // number of data bytes unsigned char x[n]; // input data unsigned char y[n]; // scrambled data unsigned char z[n]; // unscrambled data unsigned int i; // generate data for (i=0; i> j) & 0x01 ]++; // b:2 for (j=0; j<8; j+=2) c2[ (_x[i] >> j) & 0x03 ]++; // b:4 for (j=0; j<8; j+=4) c4[ (_x[i] >> j) & 0x0f ]++; // b:8 c8[ _x[i] ]++; } // compute entropy float H1 = 0.0f; float H2 = 0.0f; float H4 = 0.0f; float H8 = 0.0f; float p; for (i=0; i<2; i++) { p = (float) c1[i] / (float)(8*_n); H1 -= p * log2f(p+1e-12f); } H1 /= 1.0f; for (i=0; i<4; i++) { p = (float) c2[i] / (float)(4*_n); H2 -= p * log2f(p+1e-12f); } H2 /= 2.0f; for (i=0; i<16; i++) { p = (float) c4[i] / (float)(2*_n); H4 -= p * log2f(p+1e-12f); } H4 /= 4.0f; for (i=0; i<256; i++) { p = (float) c8[i] / (float)(_n); H8 -= p * log2f(p+1e-12f); } H8 /= 8.0f; #if 0 printf("H1 : %12.8f\n", H1); printf("H2 : %12.8f\n", H2); printf("H4 : %12.8f\n", H4); printf("H8 : %12.8f\n", H8); #endif // not sure if product is truly the entropy, but // it is a fair assessment return H1 * H2 * H4 * H8; }