Last change
on this file since 17 was 2, checked in by Rick van der Zwet, 15 years ago |
Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)
|
File size:
1.4 KB
|
Rev | Line | |
---|
[2] | 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 | int main(int argc, char *argv[]) {
|
---|
| 12 |
|
---|
| 13 | const int SIZE = 20;
|
---|
| 14 | int * signal = malloc(SIZE * sizeof(int));
|
---|
| 15 | int e_total, e_sub;
|
---|
| 16 | // Temp counters
|
---|
| 17 | int i,k;
|
---|
| 18 | long int n;
|
---|
| 19 | float f_y;
|
---|
| 20 |
|
---|
| 21 | long int best_number = 0;
|
---|
| 22 | float best_value = 0;
|
---|
| 23 |
|
---|
| 24 | void print_array(long int number, float result) {
|
---|
| 25 | for(i = 0; i < SIZE; i++) {
|
---|
| 26 | if (number & (1 << i)) {
|
---|
| 27 | printf("+");
|
---|
| 28 | } else {
|
---|
| 29 | printf("-");
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 | printf(" = %f\n", result);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | // All values to be determined
|
---|
| 36 | for (n = 0; n < pow(2,SIZE); n++) {
|
---|
| 37 | // Make array ready
|
---|
| 38 | for (i = 0; i < SIZE; i++) {
|
---|
| 39 | signal[i] = (n & (1 << i)) ? 1 : -1;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | // Calculate E(y)
|
---|
| 43 | e_total = 0;
|
---|
| 44 | for(k = 1; k < SIZE; k++) {
|
---|
| 45 | e_sub = 0;
|
---|
| 46 | for(i = 0; i < SIZE - k; i++) {
|
---|
| 47 | e_sub += signal[i] * signal[i+k];
|
---|
| 48 | //printf("[k:%i i:%i] %i\n", k, i, signal[i] * signal[i+k]);
|
---|
| 49 | }
|
---|
| 50 | // Power of two
|
---|
| 51 | e_total += pow(e_sub,2);
|
---|
| 52 | }
|
---|
| 53 | f_y = pow((float)SIZE,2)/(2 * e_total);
|
---|
| 54 |
|
---|
| 55 | // Save best value
|
---|
| 56 | if (f_y >= best_value) {
|
---|
| 57 | print_array(n,f_y);
|
---|
| 58 | best_value = f_y;
|
---|
| 59 | best_number = n;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | // Clean up mess
|
---|
| 65 | free(signal);
|
---|
| 66 | return EX_OK;
|
---|
| 67 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.