-
Notifications
You must be signed in to change notification settings - Fork 1
/
psr_detector_wav.c
357 lines (316 loc) · 6.37 KB
/
psr_detector_wav.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#define DEFAULT_BINS 250
int
main (int argc, char **argv)
{
double thebuffer[DEFAULT_BINS*5];
double theotherbuffer[DEFAULT_BINS*5];
int bincnts[DEFAULT_BINS*5];
int numbins = DEFAULT_BINS;
double prate =0.0;
double sdt = 0.0;
double tpb = 0.0;
double srate = 0.0;
double thresh;
short sample;
double minv = 0.0;
double maxv = 0.0;
int indx = 0;
double segt = 0.0;
long long totsamps = 0LL;
long long skipped = 0LL;
int i;
unsigned long offset;
unsigned long duration;
double add;
int opt;
char *fn;
FILE *fp;
double aval = -5e9;
double alpha = 0.05;
double beta = 1.0-alpha;
int avcnt = 0;
double lp = 0.0;
int logmode = 0;
if (argc < 2)
{
fprintf (stderr, "Usage: do_psr -p <pulserate> -s <samprate> -o <offset> -d <duration> -n <numbins> <infile>\n");
exit (1);
}
/*
* Set defaults
*/
prate = -1;
srate = -1;
offset = 0L;
duration = 3600L;
numbins = 250;
add = 0.0;
thresh = 5.0;
while ((opt = getopt(argc, argv, "p:n:d:s:o:a:t:l")) != -1)
{
switch(opt)
{
case 'a':
add = atof(optarg);
break;
case 'p':
prate = atof(optarg);
break;
case 'n':
numbins = atoi(optarg);
if (numbins > (5*DEFAULT_BINS))
{
fprintf (stderr, "Numbins exceeds the maximum of %d\n", (5*DEFAULT_BINS));
exit (1);
}
break;
case 'd':
duration = atol(optarg);
break;
case 's':
srate = atof(optarg);
break;
case 'o':
offset = atol(optarg);
break;
case 't':
thresh = atof(optarg);
break;
case 'l':
logmode = 1;
break;
default:
fprintf (stderr, "Unknown option: '%c'\n", opt);
exit (1);
}
}
/*
* Make sure that -p and -s have both been specified, and are valid
*/
if (prate < 0.0)
{
fprintf (stderr, "-p must be specified, and must be > 0\n");
exit (1);
}
if (prate > 50.0)
{
fprintf (stderr, "-p specifies an unusually-high expected pulse rate\n");
}
if (srate < 0.0)
{
fprintf (stderr, "-s must be specified, and must be > 0\n");
exit (1);
}
if (srate < 100.0)
{
fprintf (stderr, "Warning: -s specifies an unusually-low sample rate\n");
}
fprintf (stderr, "Running with: prate %f srate %f bins %d\n", prate, srate, numbins);
/*
* Open input file
*/
fn = argv[optind];
fp = fopen (fn, "r");
if (fp == NULL)
{
perror ("Cannot open input file");
exit (1);
}
/*
* Offset/duration are given in seconds--convert to samples
*/
offset *= srate*sizeof(sample);
duration *= srate*sizeof(sample);
/*
* Calculate folding values from input parameters
*/
/*
* Time-per-bin
*/
tpb = (1.0/prate)/(double)numbins;
fprintf (stderr, "Time-per-bin: %f\n", tpb);
/*
* Dt produced by sample-rate
*/
sdt = 1.0/srate;
fprintf (stderr, "Dt: %f\n", sdt);
/*
* Zero-out out buffers
*/
memset ((void *)thebuffer, 0, sizeof(thebuffer));
memset ((void *)bincnts, 0, sizeof(bincnts));
/*
* Get to desired offset (+16 to skip WAV header)
*/
if (fseek (fp, offset+16, SEEK_SET) == -1)
{
perror ("Seek failed");
}
/*
* We read one 16-bit sample at a time, and process accordingly.
* This is reasonably efficient, since stdio takes care of buffering
* for us.
*/
while (fread(&sample, sizeof(sample), 1, fp) > 0)
{
double ds;
/*
* Housekeeping on number of samples
*/
totsamps ++;
/*
* We're done, even though we haven't reached EOF
*/
if (totsamps > duration)
{
break;
}
/*
* Scale sample into something "reasonable" for a double
*/
ds = (double)sample / (double)16384.5;
{
/*
* Implement high-pass filter
*/
alpha = 1.0 / (srate*30.0);
lp = (alpha * ds) + ((1.0-alpha) *lp);
ds -= lp;
ds += add;
}
/*
* Add into accumulator buffer
*
* We are "free and easy" for the first couple of minutes, and then we've established a baseline
*/
if (totsamps < (srate * 600))
{
thebuffer[indx] += ds;
bincnts[indx] += 1;
if (aval < -1e9)
{
aval = ds;
fprintf (stderr, "Initializing aval\n");
avcnt++;
}
else
{
aval += ds;
avcnt++;
}
}
/*
* Once a baseline is established, we clip based on this baseline
*/
else if (thresh > 0.0)
{
if (avcnt > 0)
{
aval /= (double) avcnt;
avcnt = -1;
}
/*
* If the deviation from the local-average isn't too large, add this sample
*/
if (fabs(ds) < (fabs(aval) * thresh))
{
thebuffer[indx] += ds;
/*
* Bump number of samples in this position
*/
bincnts[indx] += 1;
aval = (alpha*ds) + (beta*aval);
}
else
{
skipped++;
}
}
else
{
thebuffer[indx] += ds;
bincnts[indx] += 1;
}
/*
* Bump up our "segment" timer by the time-per-sample
*/
segt += sdt;
/*
* One "segment" is one binwidth (pulsar-period / 256)
* If our segt after incrementing is >= binwidth, handle
* bumping to next bin, and dealing with residual on
* segment time.
*/
if (segt >= tpb)
{
/*
* Segt becomes whatever was left over from subtracting-out tpb
*/
segt = segt - tpb;
/*
* Bump to the next bin
*/
indx ++;
/*
* Handle wrap-around
*/
if (indx >= numbins)
{
indx = 0;
}
}
}
fprintf (stderr, "Processed %d seconds of data\n", (int)(totsamps/(long long int)srate));
fprintf (stderr, "Skipped %d seconds of data\n", (int)(skipped/(long long int)srate));
{
int maxloc = 0;
int start = 0;
/*
* Dump the folded buffer
*/
minv = 9e9;
maxv = 1e-9;
for (i = 0; i < numbins; i++)
{
if ((thebuffer[i]/bincnts[i]) < minv)
{
minv = thebuffer[i]/bincnts[i];
}
if ((thebuffer[i]/bincnts[i]) > maxv)
{
maxv = (thebuffer[i]/bincnts[i]);
maxloc = i;
}
}
start = numbins/2;
for (i = 0; i < numbins; i++)
{
theotherbuffer[start] = thebuffer[maxloc];
start++;
maxloc++;
if (start >= numbins)
{
start = 0;
}
if (maxloc >= numbins)
{
maxloc = 0;
}
}
}
for (i = 0; i < numbins; i++)
{
double lg;
double linear;
linear = theotherbuffer[i]/bincnts[i];
lg = 10.0*log10(theotherbuffer[i]/bincnts[i]);
fprintf (stdout, "%f %12.9f\n", (double)i*tpb, logmode ? lg : linear);
}
exit (0);
}