Diff of /HRV.src/filt.c [000000] .. [1a3ddc]

Switch to unified view

a b/HRV.src/filt.c
1
/* filt.c       Joe Mietus      Apr 1 2011 */
2
3
/* filt.c       Joe Mietus      Oct 7 2008 */
4
/* getline renamed to lineget May 18 2010 */
5
6
/*
7
filt :
8
Usage: filt filt hwin [options]
9
  Reads 1 (y) or 2 (x,y) columns from stdin and
10
  filters outliers by deleting those intervals outside
11
  of `filt' range of the average within a window
12
  `hwin' distance on either side of the
13
  current interval.
14
  options are :
15
  [-x min max] : exclude date outside min - max
16
  [-n] : print ratio of nout to nin
17
  [-p file] : print excluded points to file
18
              not including start/end hwin buffer
19
  excluded points may not be printed in time
20
  sequential order if -x opption is used
21
*/
22
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <math.h>
26
27
#define MAXSTR 256
28
#define MAXWIN 1024
29
30
char *prog, line[MAXSTR], tmp[MAXSTR];
31
int ncols;
32
static double x[MAXWIN+1], y[MAXWIN+1];
33
FILE *fp;
34
35
36
main(argc, argv)
37
int argc;
38
char * argv[];
39
{
40
    int hwin, win, i, j, k, nin, nout;
41
    int xflag, nflag, pflag;
42
    double filt, filtmin, filtmax, min, max, mtmp, sum, av;
43
44
    xflag = 0;
45
    nflag = pflag = nin = nout = 0;
46
    sum = 0.0;
47
    prog = argv[0];
48
49
    if (argc < 3) {
50
        usage();
51
    exit(1);
52
    }
53
54
    i = 1;
55
    if ((filt = atof(argv[i])) <= 0) {
56
            fprintf(stderr, "%s: filt must be greater than 0\n",
57
                    prog);
58
            exit(2);
59
    }
60
    if ((hwin = atoi(argv[++i])) <= 0) {
61
            fprintf(stderr, "%s: hwin must be integer greater than 0\n",
62
                    prog);
63
            exit(2);
64
    }
65
    if ((win = 2*hwin) > MAXWIN) {
66
            fprintf(stderr, "%s: max hwin = %d\n", prog, MAXWIN/2);
67
            exit(2);
68
    }
69
70
    for ( ; ++i < argc && *argv[i] == '-'; ) {
71
        switch(argv[i][1]) {
72
            case 'x': if (i+3 > argc) {
73
                      usage();
74
              exit(1);
75
              }
76
                  min = atof(argv[++i]);
77
                  max = atof(argv[++i]);
78
                      if (min > max) {
79
                  mtmp = min;
80
              min = max;
81
              max = mtmp;
82
                      }
83
                  xflag = 1;
84
                      break;
85
            case 'n': nflag = 1;
86
                      break;
87
            case 'p': if ((fp = fopen(argv[++i], "w")) == NULL)
88
                      fprintf(stderr, "%s : Can't open %s\n",
89
                  argv[0], argv[i]);
90
              else
91
              pflag = 1;
92
                      break;
93
            default:  usage();
94
                      exit(1);
95
        }
96
    }
97
98
    i = 0;
99
    if (fgets(line, MAXSTR, stdin) == NULL) {
100
    fprintf(stderr, "%s : no data read\n", prog);
101
    exit(2);
102
    }
103
    if (sscanf(line, "%lf %lf %s", &x[i], &y[i], tmp) == 3) {
104
    fprintf(stderr, "%s : incorrectly formatted data\n", prog);
105
    exit(2);
106
    }
107
    else if (sscanf(line, "%lf %lf", &x[i], &y[i]) == 2) {
108
        ncols = 2;
109
    }
110
    else if (sscanf(line, "%lf %s", &x[i], tmp) == 2) {
111
    fprintf(stderr, "%s : incorrectly formatted data\n", prog);
112
    exit(2);
113
    }
114
    else if (sscanf(line, "%lf", &y[i]) == 1) {
115
        ncols = 1;
116
    x[0] = 0;
117
    }
118
    else {
119
    fprintf(stderr, "%s : incorrectly formatted data\n", prog);
120
    exit(2);
121
    }
122
123
    nin++;
124
    if (pflag)
125
        fprintline(i);
126
127
    i++;
128
    while (i<=win) {
129
        if (lineget(i) == EOF) {
130
        fprintf(stderr, "%s : insufficient data\n", prog);
131
            exit(2);
132
    }
133
        nin++;
134
135
        if (xflag && (y[i]<min || y[i]>max)) {
136
        if (pflag)
137
            fprintline(i);
138
        }
139
        else {
140
            if (pflag && i<hwin)
141
            fprintline(i);
142
            i++;
143
        }
144
    }
145
146
    i = 0;
147
    j = hwin;
148
149
    for (i=0; i<=win; i++)
150
        sum += y[i];
151
    sum -= y[j];
152
    av = sum/win;
153
    sum += y[j] - y[0];
154
155
    filtmax = filtmin = filt * av;
156
157
    if (y[j] <= av+filtmax && y[j] >= av-filtmin) {
158
        printline(j);
159
        nout++;
160
    }
161
    else if (pflag)
162
        fprintline(j);
163
164
    i = 0;
165
166
    while (lineget(i) != EOF) {
167
        nin++;
168
169
    if (xflag && (y[i]<min || y[i]>max)) {
170
        if (pflag)
171
                fprintline(i);
172
    }
173
        else {
174
            if (++j > win)
175
                j = 0;
176
177
            sum += y[i] - y[j];
178
        av = sum/win;
179
            if (++i > win)
180
                i = 0;
181
        sum += y[j] - y[i];
182
183
            filtmax = filtmin = filt * av;
184
185
            if (y[j] <= av+filtmax && y[j] >= av-filtmin) {
186
            printline(j);
187
                nout++;
188
            }
189
        else {
190
            if (pflag)
191
                    fprintline(j);
192
            }
193
    }
194
    }
195
196
    if (pflag) {
197
        for (i=0; i<hwin; i++) {
198
            if (++j > win)
199
                j = 0;
200
            fprintline(j);
201
    }
202
    }
203
204
    if (nflag)
205
        fprintf(stderr, "nout : nin = %d : %d = %g\n", 
206
        nout, nin, (double)nout/nin);
207
208
    exit(0);
209
}
210
211
212
213
lineget(i)
214
int i;
215
{
216
    int j;
217
    static int n = 1;
218
219
    if (fgets(line, MAXSTR, stdin) == NULL)
220
        return(EOF);
221
222
    if (ncols == 1) {
223
        if ((j = sscanf(line, "%lf %s", &y[i], tmp)) != 1) {
224
            fprintf(stderr, "%s: incorrectly formatted data : ", prog);
225
        fprintf(stderr, "line = %d\n", n+1);
226
        exit(2);
227
        }
228
    x[i] = n;
229
    }
230
    else if (ncols == 2) {
231
        if ((j = sscanf(line, "%lf %lf %s", &x[i], &y[i], tmp)) != 2) {
232
            fprintf(stderr, "%s: incorrectly formatted data : ", prog);
233
        fprintf(stderr, "line = %d\n", n+1);
234
        exit(2);
235
        }
236
    }
237
238
    n++;
239
240
    return(j);
241
}
242
243
244
printline(i)
245
int i;
246
{
247
    if (ncols == 2)
248
        printf("%.9g ", x[i]);
249
    printf("%g\n", y[i]);
250
}
251
252
253
fprintline(i)
254
int i;
255
{
256
    if (ncols == 2)
257
        fprintf(fp, "%.9g ", x[i]);
258
    fprintf(fp, "%g\n", y[i]);
259
}
260
261
262
usage()
263
{
264
    fprintf(stderr, "Usage: %s filt hwin [options]\n", prog);
265
    fprintf(stderr, " Reads 1 (y) or 2 (x,y) columns from stdin and\n");
266
    fprintf(stderr, " filters outliers by deleting those intervals outside\n");
267
    fprintf(stderr, " of `filt' range of the average within a window\n");
268
    fprintf(stderr, " `hwin' distance on either side of the\n");
269
    fprintf(stderr, " current interval.\n\n");
270
    fprintf(stderr, " options are :\n");
271
    fprintf(stderr, " [-x min max] : exclude date outside min - max\n");
272
    fprintf(stderr, " [-n] : print ratio of nout to nin\n");
273
    fprintf(stderr, " [-p file] : print excluded points to file\n");
274
    fprintf(stderr, "             not including start/end hwin buffer\n");
275
    fprintf(stderr, " excluded points may not be printed in time\n");
276
    fprintf(stderr, " sequential order if -x opption is used\n");
277
}