-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsigstat.c
174 lines (145 loc) · 2.51 KB
/
sigstat.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* get stats about numbers in a column
* ignore the first line
* cc sigstat.c getsig.c -lm -o sigstat
* ./sigstat DATA_FILE
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include "getsig.h"
#define USAGE ("usage: %s DATA_FILE\n")
struct stats {
size_t n;
int16_t min;
int16_t max;
int16_t median;
int16_t mode;
double delta;
double mean;
double m2;
double var;
double sd;
};
static inline void updatestatsn(struct stats *st)
{
st->n ++;
}
static inline void updatestatsmin(int16_t x, struct stats *st)
{
if (x < st->min)
st->min = x;
}
static inline void updatestatsmax(int16_t x, struct stats *st)
{
if (x > st->max)
st->max = x;
}
static inline void updatestatsdelta(int16_t x, struct stats *st)
{
st->delta = x - st->mean;
}
static inline void updatestatsmean(int16_t x, struct stats *st)
{
updatestatsdelta(x, st);
st->mean += st->delta / st->n;
}
static inline void updatestatsm2(int16_t x, struct stats *st)
{
st->m2 += st->delta * (x - st->mean);
}
static inline void updatestatsvar(int16_t x, struct stats *st)
{
updatestatsm2(x, st);
st->var = st->m2 / st->n;
}
static inline void updatestatssd(struct stats *st)
{
st->sd = sqrt(st->var);
}
void updatestats(int16_t x, struct stats *st)
{
updatestatsn(st);
updatestatsmin(x, st);
updatestatsmax(x, st);
updatestatsmean(x, st);
updatestatsvar(x, st);
}
void updatestatsend(struct stats *st)
{
updatestatssd(st);
}
void initstats(struct stats *st)
{
st->n = 0;
st->min = INT16_MAX;
st->max = INT16_MIN;
st->delta = 0;
st->mean = 0;
st->m2 = 0;
st->var = 0;
st->sd = 0;
}
void printstats(const struct stats *st)
{
printf("n: %zu\n"
"min: %d\n"
"max: %d\n"
"mean: %f\n"
"var: %f\n"
"sd: %f\n",
st->n,
st->min,
st->max,
st->mean,
st->var,
st->sd);
}
int sigstat(FILE *fp)
{
int16_t sig;
int ret;
struct stats sigstats;
size_t n;
char *line;
n = MAX_WIDTH_LINE;
line = malloc(n);
if (!line)
return 1;
initstats(&sigstats);
while ((ret = getnextsig(fp, line, n, &sig)) == 0)
updatestats(sig, &sigstats);
free(line);
if (ret != 1)
return 1;
updatestatsend(&sigstats);
printstats(&sigstats);
return 0;
}
int main(int argc, char **argv)
{
FILE *fp;
int ret;
char *line;
size_t n;
if (argc != 2) {
printf(USAGE, argv[0]);
return 1;
}
fp = fopen(argv[1], "r");
if (!fp) {
perror(NULL);
return 1;
}
skipline(fp);
ret = sigstat(fp);
if (ret)
perror("sigstat failed");
ret = fclose(fp);
if (ret == EOF) {
perror(NULL);
return 1;
}
return 0;
}