-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhisto.c
144 lines (103 loc) · 2.27 KB
/
histo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "histo.h"
#include "dump_utils.h"
void fill_histo( uint16_t n, uint16_t pos[6], hist_arr *hist )
{
double *y = hist->y;
int size = hist->size;
int offset = hist->offset;
int i;
for(i = 0; i < 6; ++i) {
if ( pos[i] != 0 ) {
double frac = (double)pos[i] / n;
int val = round( frac * 100 );
if (0 + offset <= val && val < size + offset) {
y[val - offset]++;
hist->total++;
}
}
}
}
hist_arr alloc_hist( int size, int offset )
{
int i;
hist_arr hist;
hist.size = size;
hist.total = 0.0;
hist.offset = offset;
hist.x = malloc( size * sizeof(double) );
hist.y = malloc( size * sizeof(double) );
memset( hist.y, 0, size * sizeof(double) );
for (i = 0; i < size; ++i) {
hist.x[i] = i + offset;
}
return hist;
}
void free_hist( hist_arr hist )
{
free(hist.x); free(hist.y);
}
void ascii_hist( hist_arr *hist )
{
int i;
double *x = hist->x;
double *y = hist->y;
int size = hist->size;
double total = hist->total;
for(i = 0; i < size; ++i) {
fprintf(stdout, "%d\t%.2g\t", (int)x[i], y[i]);
double hn = (y[i] / total) * 1000;
if (hn > y[i]) hn = y[i];
int j;
for(j = 0; j < hn; ++j) {
fprintf(stdout, "#");
}
fprintf(stdout, "\n");
}
}
////* Usage and main *////
static int usage(char **argv)
{
printf("\nUsage: %s %s file.bin\n\n", argv[-1], argv[0]);
return 1;
}
int histo_main(int argc, char **argv)
{
if (argc < 2) {
return usage(argv);
}
FILE *file = NULL;
file = fopen(argv[1], "rb");
if (!file) {
printf("Can't open input file: %s\n\n", argv[1]);
return usage(argv);
}
size_t ret = 0;
dump_h dhead;
ret = read_header( &dhead, file );
float minfrac = dhead.minfrac;
hist_arr hist;
if (minfrac > 0) {
int offset = round(minfrac * 100);
int size = MAX_HIST - (2 * offset);
hist = alloc_hist( size, offset );
}
else {
hist = alloc_hist( MAX_HIST, 0);
}
while (1) {
dump_p dpos;
ret = read_pos( &dpos, file, &dhead);
if (ret == 0) break;
fill_histo(dpos.c, dpos.pos, &hist);
}
fclose(file);
ascii_hist( &hist );
free_hist(hist);
return 0;
}