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

Switch to unified view

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