annotate Meerwald/frid2_common.c @ 8:f83ef905a63d

fixing many warnings increase capacity for coordinates in bruyn fix some uninit. variables
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 22 Apr 2008 13:36:05 +0200
parents be303a3f5ea8
children bd669312f068
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "frid2_common.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "signature.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 extern char *progname;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 void embed_low_freq(double **dcts, int cols, int rows, double alpha, int verbose) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 int n;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 int row, col, dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 n = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 row = col = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 dir = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 while (n < nbit_signature) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 double d, x;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 int embed;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 int out;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 col -= dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 row += dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 if (col < 0) { dir = -1; col = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 if (col >= cols) { dir = 1; col = cols - 1; row += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 if (row < 0) { dir = 1; row = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 if (row >= rows) { dir = -1; row = rows - 1; col += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 d = dcts[row][col];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 if (fabs(d) <= 1.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 if (verbose > 3)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 fprintf(stderr, "%s: bit #%d - skipped (%d/%d)\n", progname, n, col, row);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 continue;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 embed = 2 * get_signature_bit(n) - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 x = (d > 0.0) ? 1.0 : -1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 out = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 while (fabs(x) < fabs(d)) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 x *= FORWARD_STEP(alpha);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 out =- out;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 if (out != embed) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 if (fabs(d - x) < fabs(d - x * BACKWARD_STEP(alpha)))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 x *= FORWARD_STEP(alpha);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 x *= BACKWARD_STEP(alpha);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 d = (x + x * BACKWARD_STEP(alpha)) / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 if (verbose > 3)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 fprintf(stderr, "%s: embedding bit #%d (= %d) at (%d/%d): %f -> %f\n", progname, n, get_signature_bit(n), col, row, dcts[row][col], d);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 dcts[row][col] = d;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 n++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 void embed_med_freq(double **dcts, int cols, int rows, double gamma, int seed, int verbose) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 // select mid-frequency (30%) coefficients
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 int start = (int) (0.35 * rows * cols + 0.5);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 int end = (int) (0.65 * rows * cols + 0.5);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 double *vector;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 int x = 0, y = 0, dir = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 vector = malloc((end - start) * sizeof(double));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 for (i = 0; i < (end - start); i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 vector[i] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 // create pseudo-random vector
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 srandom(seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 for (i = 0; i < nbit_signature; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 if (get_signature_bit(i))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 random();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 for (j = 0; j < (end - start); j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 vector[j] += (double) (random() & RAND_MAX) / (double) RAND_MAX - 0.5;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 if (!get_signature_bit(i))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 random();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 for (i = 0; i < (end - start); i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 vector[i] /= sqrt(nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 for (i = 0; i < end; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 // zig-zag scan
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 x -= dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 y += dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 if (x < 0) { dir = -1; x = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 if (x >= cols) { dir = 1; x = cols - 1; y += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 if (y < 0) { dir = 1; y = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 if (y >= rows) { dir = -1; y = rows - 1; x += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 // embed vector
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 if ((i - start) >= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 // fprintf(stderr, "%d/%d: %f -> %f\n", x, y, dcts[y][x], dcts[y][x] + gamma * vector[i - start]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 dcts[y][x] += gamma * vector[i - start];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 free(vector);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 double detect_low_freq(double **dcts, int cols, int rows, double alpha, double beta, int verbose) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 int n;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 int row, col, dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 double sum1, sum2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 n = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 row = col = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 dir = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 sum1 = sum2 = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 while (n < nbit_signature1) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 double d, x;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 int detect;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 int out;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 col -= dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 row += dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 if (col < 0) { dir = -1; col = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 if (col >= cols) { dir = 1; col = cols - 1; row += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 if (row < 0) { dir = 1; row = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 if (row >= rows) { dir = -1; row = rows - 1; col += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 d = dcts[row][col];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 if (fabs(d) <= 1.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 if (verbose > 3)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 fprintf(stderr, "%s: bit #%d - skipped (%d/%d)\n", progname, n, col, row);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 continue;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 detect = 2 * get_signature1_bit(n) - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 x = (d > 0.0) ? 1.0 : -1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 out = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 while (fabs(x) < fabs(d)) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 x *= FORWARD_STEP(alpha);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 out =- out;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 if (verbose > 3)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 fprintf(stderr, "%s: detected bit #%d (= %d) at (%d/%d): %f\n", progname, n, out > 0 ? 1 : 0, col, row, d);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 set_signature2_bit(n, out > 0 ? 1 : 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 sum1 += pow(fabs(d), beta) * out * detect;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 sum2 += pow(fabs(d), beta);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 n++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 return sum1 / sum2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 double detect_med_freq(double **dcts, int cols, int rows, int seed, int verbose) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 int i, j, k;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 int start= (int) (0.35 * rows * cols + 0.5);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 int end = (int) (.65 * rows * cols + 0.5);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 int sum, sum1, sum2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 int x = 0, y = 0, dir = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 double *vector;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 int startx, starty, startdir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 double corr[2];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 double correlation;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 // locate start positions
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 for (i = 0; i < start; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 x -= dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 y += dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 if (x < 0) { dir = -1; x = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 if (x >= cols) { dir = 1; x = cols - 1; y += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 if (y < 0) { dir = 1; y = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 if (y >= rows) { dir = -1; y = rows - 1; x += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 // save start positions
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 startx = x;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 starty = y;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 startdir = dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 srandom(seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 vector = malloc((end - start) * sizeof(double));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 for (i = 0; i < nbit_signature1; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 for (j = 0; j <= (end - start); j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 vector[j] = (double) (random() & RAND_MAX) / (double) RAND_MAX - 0.5;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 for (j = 0; j <= 1; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 x = startx;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 y = starty;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194 dir = startdir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 corr[j] = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 for (k = 0; start + k < end; k++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198 x -= dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 y += dir;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 if (x < 0) { dir = -1; x = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 if (x >= cols) { dir = 1; x = cols - 1; y += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 if (y < 0) { dir = 1; y = 0; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 if (y >= rows) { dir = -1; y = rows - 1; x += 2; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 corr[j] += dcts[y][x] * vector[k + j];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 set_signature2_bit(i, (corr[0] >= corr[1]) ? 0 : 1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 sum = 0; sum1 = 0; sum2 = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 for (i = 0; i < nbit_signature1; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 sum += get_signature1_bit(i) * get_signature2_bit(i);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214 sum1 += get_signature1_bit(i) * get_signature1_bit(i);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 sum2 += get_signature2_bit(i) * get_signature2_bit(i);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 correlation = (double) sum / (sqrt(sum1) * sqrt(sum2));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 return correlation;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.