/* * Low-autocorrelation problem of binary sequences * 'Brute-force' method * Rick van der Zwet, Oct 2009 * Licence: BSD */ #include #include #include #include int main(int argc, char *argv[]) { const int SIZE = 20; int * signal = malloc(SIZE * sizeof(int)); int e_total, e_sub; // Temp counters int i,k; long int n; float f_y; long int best_number = 0; float best_value = 0; void print_array(long int number, float result) { for(i = 0; i < SIZE; i++) { if (number & (1 << i)) { printf("+"); } else { printf("-"); } } printf(" = %f\n", result); } // All values to be determined for (n = 0; n < pow(2,SIZE); n++) { // Make array ready for (i = 0; i < SIZE; i++) { signal[i] = (n & (1 << i)) ? 1 : -1; } // Calculate E(y) e_total = 0; for(k = 1; k < SIZE; k++) { e_sub = 0; for(i = 0; i < SIZE - k; i++) { e_sub += signal[i] * signal[i+k]; //printf("[k:%i i:%i] %i\n", k, i, signal[i] * signal[i+k]); } // Power of two e_total += pow(e_sub,2); } f_y = pow((float)SIZE,2)/(2 * e_total); // Save best value if (f_y >= best_value) { print_array(n,f_y); best_value = f_y; best_number = n; } } // Clean up mess free(signal); return EX_OK; }