source: liacs/NC2010/low-correlation/brute-force-fitness.c@ 227

Last change on this file since 227 was 29, checked in by Rick van der Zwet, 15 years ago

Crap files in wrong directory

File size: 1.4 KB
Line 
1/*
2 * Low-autocorrelation problem of binary sequences
3 * 'Brute-force' method
4 * Rick van der Zwet, Oct 2009
5 * Licence: BSD
6 */
7#include <sysexits.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
11
12int main(int argc, char *argv[]) {
13
14const int SIZE = 20;
15int * signal = malloc(SIZE * sizeof(int));
16int e_total, e_sub;
17// Temp counters
18int i,k;
19long int n;
20float f_y;
21
22long int best_number = 0;
23float best_value = 0;
24
25void print_array(long int number, float result) {
26 for(i = 0; i < SIZE; i++) {
27 if (number & (1 << i)) {
28 printf("+");
29 } else {
30 printf("-");
31 }
32 }
33 printf(" = %f\n", result);
34}
35
36// All values to be determined
37for (n = 0; n < pow(2,SIZE); n++) {
38 // Make array ready
39 for (i = 0; i < SIZE; i++) {
40 signal[i] = (n & (1 << i)) ? 1 : -1;
41 }
42
43 // Calculate E(y)
44 e_total = 0;
45 for(k = 1; k < SIZE; k++) {
46 e_sub = 0;
47 for(i = 0; i < SIZE - k; i++) {
48 e_sub += signal[i] * signal[i+k];
49 //printf("[k:%i i:%i] %i\n", k, i, signal[i] * signal[i+k]);
50 }
51 // Power of two
52 e_total += pow(e_sub,2);
53 }
54 f_y = pow((float)SIZE,2)/(2 * e_total);
55
56 // Save best value
57 if (f_y >= best_value) {
58 print_array(n,f_y);
59 best_value = f_y;
60 best_number = n;
61 }
62
63}
64
65// Clean up mess
66free(signal);
67return EX_OK;
68}
Note: See TracBrowser for help on using the repository browser.