-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakdur.c
executable file
·187 lines (152 loc) · 3.76 KB
/
breakdur.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
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NPOINTS (64)
// buffer size PER CHANNEL
#define BUFSIZE_CHAN (32768)
#define LINELENGTH (80)
typedef struct breakpoint {
double time;
double value;
} BREAKPOINT;
void print_points(const BREAKPOINT* points, unsigned long npoints)
{
unsigned int i;
printf("Printing all points:\n");
for (i = 0; i < npoints; i++) {
printf("%f: %f\n", points[i].time, points[i].value);
}
printf("\n");
}
void scale_values(BREAKPOINT* points, unsigned long npoints, float factor)
{
unsigned int i;
printf("Scaling values with factor of %f:\n", factor);
for (i = 0; i < npoints; i++) {
points[i].value *= factor;
printf("%f: %f\n", points[i].time, points[i].value);
}
printf("\n");
}
void scale_times(BREAKPOINT* points, unsigned long npoints, float factor)
{
unsigned int i;
printf("Scaling times with factor of %f:\n", factor);
for (i = 0; i < npoints; i++) {
points[i].time *= factor;
printf("%f: %f\n", points[i].time, points[i].value);
}
printf("\n");
}
// npoints size of BREAKPOINT array
BREAKPOINT maxpoint(const BREAKPOINT* points, unsigned long npoints)
{
unsigned int i;
BREAKPOINT point;
point.time = 0.0;
point.value = 0.0;
if (npoints < 2) {
printf("maxpoint requires more than one breakpoint\n");
return point;
}
for (i = 1; i < npoints; i++) {
if (point.value < points[i].value){
point.value = points[i].value;
point.time = points[i].time;
}
}
return point;
}
BREAKPOINT* get_breakpoints(FILE* fp, unsigned long* psize)
{
int got;
unsigned long npoints = 0, size = NPOINTS;
double lasttime = 0.0;
BREAKPOINT* points = NULL;
char line[LINELENGTH];
if (fp==NULL) return NULL;
points = (BREAKPOINT*) malloc(sizeof(BREAKPOINT) * size);
if (points==NULL) return NULL;
while (fgets(line, 80, fp)) {
if ((got = sscanf(line, "%lf%lf", &points[npoints].time, &points[npoints].value)) < 0) {
continue;
}
if (got == 0) {
printf("Line %lu has non-numeric data\n", npoints + 1);
break;
}
if (got == 1) {
printf("Incomplete breakpoint found at point %lu\n", npoints + 1);
break;
}
if (points[npoints].time < lasttime) {
printf("error in breakpoint data at point %lu: time not increasing\n", npoints + 1);
break;
}
lasttime = points[npoints].time;
if (++npoints == size) {
BREAKPOINT* tmp;
size += NPOINTS;
tmp = (BREAKPOINT*) realloc(points, sizeof(BREAKPOINT) * size);
if (tmp == NULL) {
// release the memory; return NULL to caller
npoints = 0;
free(points);
points = NULL;
break;
}
points = tmp;
}
}
if (npoints) *psize = npoints;
return points;
}
int main(int argc, char* argv[])
{
unsigned long size;
double dur;
BREAKPOINT point, *points;
FILE* fp;
printf("breakdur: find duration of breakpoint file\n");
if (argc < 2) {
printf("usage: breakdur infile.txt \n");
return 0;
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
return 0;
}
size = 0;
points = get_breakpoints(fp, &size);
if (points==NULL) {
printf("No breakpoints read.\n");
fclose(fp);
return 1;
}
if (size < 2) {
printf("Error: at least two breakpoints required\n");
free(points);
fclose(fp);
return 1;
}
// breakpoints start from 0
if (points[0].time != 0.0) {
printf("Error in breakpoint data: first time must be 0.0\n");
free(points);
fclose(fp);
return 1;
}
printf("read %lu breakpoints\n", size);
dur = points[size - 1].time;
printf("duration: %f seconds\n", dur);
point = maxpoint(points, size);
printf("maximum value: %f at %f secs\n", point.value, point.time);
print_points(points, size);
// TODO: allow passing in normalize and scale factors
scale_values(points, size, 0.85);
scale_times(points, size, 2.0);
free(points);
fclose(fp);
return 0;
}