My Project
histogram.c
Go to the documentation of this file.
1
11#include "util.h"
12
13#include <stdio.h>
14#include <math.h>
15#include <stdlib.h>
16
17#ifndef RCD
18#define RCD 0.5
19#endif
20
21void histogram_init(unsigned long long h[129][65])
22{
23 int i, j;
24
25 for (i = 0; i < 129; ++i)
26 for (j = 0; j < 65; ++j)
27 h[i][j] = 0;
28}
29
30void histogram_print(unsigned long long h[129][65])
31{
32 int i, j;
33 unsigned long long n, N;
34 double x, y, s, s2;
35 double mean[65], variance[65], median[65];
36
37 for (j = 0; j < 65; ++j) printf("; %d", j * 2 - 64);
38 printf("; total; mean; error; bias; precision");
39 for (i = 0; i < 129; ++i) {
40 printf("\n%d", i - 64);
41 n = s = s2 = 0;
42 x = (i - 64);
43 for (j = 0; j < 65; ++j) {
44 y = 2 * j - 64;
45 printf("; %lld", h[i][j]);
46 n += h[i][j];
47 s += y * h[i][j];
48 s2 += (x - y) * (x - y) * h[i][j];
49 }
50 s /= n;
51 s2 /= (n - 1);
52 if (n > 5) printf("; %lld; %.2f; %.2f; %.2f; %.2f", n, s, sqrt(s2), x - s, sqrt(s2 - (x - s) * (x - s)));
53 else if (n > 0) printf("; %lld; %.2f; %.2f", n, s, sqrt(s2));
54 else printf("; %lld", n);
55 }
56
57 printf("\n");
58 N = 0;
59 printf("total");
60 for (j = 0; j < 65; ++j) {
61 n = s = s2 = 0;
62 y = 2 * j - 64;
63 for (i = 0; i < 129; ++i) {
64 x = (i - 64);
65 n += h[i][j];
66 s += x * h[i][j];
67 s2 += (x - y) * (x - y) * h[i][j];
68 }
69 N += n;
70 mean[j] = s / n;
71 variance[j] = s2 / (n - 1) ;
72 s = 0;
73 if ((n & 1) == 1) {
74 for (i = 0; i < 129; ++i) {
75 s += h[i][j];
76 if (s >= (n + 1) / 2) {
77 median[j] = i - 64;
78 break;
79 }
80 }
81 } else {
82 for (i = 0; i < 129; ++i) {
83 s += h[i][j];
84 if (s == n / 2) {
85 median[j] = i - 63.5;
86 break;
87 } else if (s > n / 2) {
88 median[j] = i - 64;
89 break;
90 }
91 }
92 }
93 printf("; %lld", n);
94 }
95 printf("; %lld\n", N);
96 printf("mean"); for (j = 0; j < 65; ++j) printf("; %.2f", mean[j]); printf("\n");
97 printf("median"); for (j = 0; j < 65; ++j) printf("; %.2f", median[j]); printf("\n");
98 printf("error"); for (j = 0; j < 65; ++j) printf("; %.2f", sqrt(variance[j])); printf("\n");
99 printf("bias"); for (j = 0; j < 65; ++j) printf("; %.2f", (2*j - 64) - mean[j]); printf("\n");
100 printf("precision"); for (j = 0; j < 65; ++j) {x = (2*j - 64) - mean[j]; printf("; %.2f", sqrt(variance[j] - x * x));} printf("\n");
101}
102
103void histogram_stats(unsigned long long h[129][65])
104{
105 double y[65], x[129];
106 double m_x, m_y, s_x, s_y, s_xy;
107 double a, b, r;
108 double n;
109 int i, j;
110
111 for (j = 0; j < 65; ++j) y[j] = j * 2 - 64;
112 for (j = 0; j < 129; ++j) x[j] = j - 64;
113
114 m_x = m_y = s_x = s_y = s_xy = 0.0;
115 n = 0.0;
116
117 for (i = 0; i < 129; ++i)
118 for (j = 0; j < 65; ++j) {
119 n += h[i][j];
120 m_x += x[i] * h[i][j];
121 m_y += y[j] * h[i][j];
122 s_x += x[i] * x[i] * h[i][j];
123 s_y += y[j] * y[j] * h[i][j];
124 s_xy += x[i] * y[j] * h[i][j];
125 }
126
127 m_x /= n;
128 m_y /= n;
129
130 s_x = sqrt(s_x / n - m_x * m_x);
131 s_y = sqrt(s_y / n - m_y * m_y);
132 s_xy = s_xy / n - m_x * m_y;
133
134 r = s_xy / (s_x * s_y);
135
136
137 printf("statistics summary\n");
138 printf("n = %.0f\n", n);
139 printf("m_eval = %.6f; m_score = %.6f\n", m_x, m_y);
140 printf("s_eval = %.6f; s_score = %.6f; cov = %.6f\n", s_x, s_y, s_xy);
141 a = sqrt(s_xy) / s_x; // regression
142 b = m_y - a * m_x;
143 printf("score = %.6f * eval + %.6f; r = %.6f; r2 = %.6f (regression)\n", a, b, r, r*r);
144 a = s_y / s_x; // correlation
145 b = m_y - a * m_x;
146 printf("score = %.6f * eval + %.6f;(correlation)\n", a, b);
147 a = sqrt(s_xy) / s_y; // correlation
148 b = m_x - a * m_y;
149 printf("eval = %.6f * score + %.6f;(regression)\n", a, b);
150 a = s_x / s_y; // correlation
151 b = m_x - a * m_y;
152 printf("eval = %.6f * score + %.6f;(correlation)\n", a, b);
153 printf("\n");
154}
155
156void histogram_to_ppm(const char *file, unsigned long long histogram[129][65])
157{
158 FILE *f;
159 int i, j, i_k, j_k, v;
160 unsigned long long max;
161 double w;
162 unsigned char r[256], g[256], b[256];
163
164 // open the file
165 f = fopen(file, "w");
166 if (f == NULL) {
167 warn("cannot open ppm file %s\n", file);
168 return;
169 }
170
171 // compute max_value
172 max = 0;
173 for (i = 0; i < 129; ++i)
174 for (j = 0; j < 65; ++j) {
175 if (histogram[i][j] > max) max = histogram[i][j];
176 }
177 w = 255.0/max;
178
179 // palette
180 r[0] = g[0] = b[0] = 255;
181 for (i = 1; i < 64; i++) { r[i + 0] = 255 - i * 4; g[i + 0] = 0; b[i + 0] = 255;}
182 for (i = 0; i < 64; i++) { r[i + 64] = 0; g[i + 64] = i * 4; b[i + 64] = 255 - i * 4;}
183 for (i = 0; i < 64; i++) { r[i + 128] = i * 4; g[i + 128] = 255; b[i + 128] = 0;}
184 for (i = 0; i < 64; i++) { r[i + 192] = 255; g[i + 192] = 255 - i * 4; b[i + 192] = 0;}
185
186 // header;
187 fprintf(f, "P3\n%d %d\n%d\n", 516, 520, 255);
188
189 // data
190 for (i = 64; i >= 0; --i) {
191 for (i_k = 0; i_k < 8; ++i_k) {
192 for (j = 0; j < 129; ++j) {
193 v = floor(histogram[j][i] * w + RCD); if (v == 0 && histogram[j][i] > 0) v = 1;
194 for (j_k = 0; j_k < 4; ++j_k) fprintf(f, "%d %d %d ", r[v], g[v], b[v]);
195 }
196 fputc('\n', f);
197 }
198 }
199
200 // end
201 fclose(f);
202}
203
void histogram_to_ppm(const char *file, unsigned long long histogram[129][65])
Definition histogram.c:156
void histogram_init(unsigned long long h[129][65])
Definition histogram.c:21
void histogram_stats(unsigned long long h[129][65])
Definition histogram.c:103
#define RCD
Definition histogram.c:18
void histogram_print(unsigned long long h[129][65])
Definition histogram.c:30
Miscellaneous utilities header.
#define warn(...)
Display a warning message as "WARNING : ... ".
Definition util.h:373