-
Notifications
You must be signed in to change notification settings - Fork 37
/
rffft.c
446 lines (377 loc) · 11 KB
/
rffft.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <fftw3.h>
#include <getopt.h>
#include <time.h>
#include <sys/time.h>
#include "rftime.h"
#include <sox.h>
#include "rffft_internal.h"
void usage(void)
{
printf("rffft: FFT RF observations\n\n");
printf("-i <file> Input file (can be fifo) [stdin]\n");
printf("-p <path> Output path, directory into which to puth the files\n");
printf("-o <output> Output filename [default: YYYY-MM-DDTHH:MM:SS.sss_XXXXXX.bin]\n");
printf("-f <frequency> Center frequency (Hz)\n");
printf("-s <samprate> Sample rate (Hz)\n");
printf("-c <chansize> Channel size [100Hz]\n");
printf("-t <tint> Integration time [1s]\n");
printf("-n <nsub> Number of integrations per file [60]\n");
printf("-m <use> Use every mth integration [1]\n");
printf("-F <format> Input format char, int, float, wav [int]\n");
printf("-T <start time> YYYY-MM-DDTHH:MM:SSS.sss\n");
printf("-R <fmin,fmax> Frequency range to store (Hz)\n");
printf("-S <index> Starting index [int]\n");
printf("-I Invert frequencies\n");
printf("-b Digitize output to bytes [off]\n");
printf("-q Quiet mode, no output [off]\n");
printf("-P Parse frequency, samplerate, format and start time from filename\n");
printf("-h This help\n");
return;
}
int main(int argc,char *argv[])
{
int i,j,k,l,nchan,m=0,nint=1,arg=0,nbytes,nsub=60,flag,nuse=1,realtime=1,quiet=0,imin,imax,partial=0,useoutput=0;
fftwf_complex *c,*d;
fftwf_plan fft;
FILE *infile,*outfile;
char infname[128]="",outfname[128]="",path[64]=".",prefix[32]="",output[128]="";
char informat='i',outformat='f';
int16_t *ibuf;
char *cbuf;
float *fbuf;
int32_t *wbuf;
float *z,length,fchan=100.0,tint=1.0,zavg,zstd,*zw;
char *cz;
double freq,samp_rate,mjd,freqmin=-1,freqmax=-1;
struct timeval start,end;
char tbuf[30],nfd[32],header[256]="";
int sign=1;
int parse_params_from_filename = 0;
sox_format_t * wav_reader = NULL;
// Read arguments
if (argc>1) {
while ((arg=getopt(argc,argv,"i:f:s:c:t:p:n:hm:F:T:bqR:o:IS:P"))!=-1) {
switch(arg) {
case 'i':
strcpy(infname,optarg);
break;
case 'p':
strcpy(path,optarg);
break;
case 'o':
strcpy(output,optarg);
useoutput=1;
break;
case 'f':
freq=(double) atof(optarg);
break;
case 's':
samp_rate=(double) atof(optarg);
break;
case 'c':
fchan=atof(optarg);
break;
case 'F':
if (strcmp(optarg,"char")==0)
informat='c';
else if (strcmp(optarg,"int")==0)
informat='i';
else if (strcmp(optarg,"float")==0)
informat='f';
else if (strcmp(optarg, "wav") == 0)
informat='w';
break;
case 'R':
sscanf(optarg,"%lf,%lf",&freqmin,&freqmax);
break;
case 'b':
outformat='c';
break;
case 'n':
nsub=atoi(optarg);
break;
case 'S':
m=atoi(optarg);
break;
case 'q':
quiet=1;
break;
case 'm':
nuse=atoi(optarg);
break;
case 't':
tint=atof(optarg);
break;
case 'T':
strcpy(nfd,optarg);
realtime=0;
break;
case 'I':
sign=-1;
break;
case 'P':
parse_params_from_filename = 1;
realtime=0;
break;
case 'h':
usage();
return 0;
default:
usage();
return 0;
}
}
} else {
usage();
return 0;
}
if (parse_params_from_filename != 0) {
if (rffft_params_from_filename(infname, &samp_rate, &freq, &informat, nfd) != 0) {
fprintf(stderr, "Error parsing parameters from filename\n");
exit(-1);
};
}
if (informat == 'w') {
if (sox_init() != SOX_SUCCESS) {
fprintf(stderr, "Error initalizing sox");
exit(-1);
}
if (sox_format_init() != SOX_SUCCESS) {
fprintf(stderr, "Error initalizing sox");
exit(-1);
}
wav_reader = sox_open_read(infname, NULL, NULL, NULL);
if (wav_reader == NULL) {
fprintf(stderr, "Error opening file %s", infname);
exit(-1);
}
if (wav_reader->signal.channels != 2) {
fprintf(stderr, "Error: Only wav files with 2 channels supported.");
exit(-1);
}
samp_rate = wav_reader->signal.rate;
}
// Ensure integer number of spectra per subintegration
tint=ceil(fchan*tint)/fchan;
// Number of channels
nchan=(int) (samp_rate/fchan);
// Number of integrations
nint=(int) (tint*(float) samp_rate/(float) nchan);
// Get channel range
if (freqmin>0.0 && freqmax>0.0) {
imin=(int) ((freqmin-freq+0.5*samp_rate)/fchan);
imax=(int) ((freqmax-freq+0.5*samp_rate)/fchan);
if (imin<0 || imin>=nchan || imax<0 || imax>=nchan || imax<=imin) {
fprintf(stderr,"Output frequency range (%.3lf MHz -> %.3lf MHz) incompatible with\ninput settings (%.3lf MHz center frequency, %.3lf MHz sample rate)!\n",freqmin*1e-6,freqmax*1e-6,freq*1e-6,samp_rate*1e-6);
return -1;
}
partial=1;
}
// Dump statistics
printf("Filename: %s\n", (strlen(infname) ? infname : "stdin"));
printf("Frequency: %f MHz\n",freq*1e-6);
printf("Bandwidth: %f MHz\n",samp_rate*1e-6);
printf("Sampling time: %f us\n",1e6/samp_rate);
printf("Number of channels: %d\n",nchan);
printf("Channel size: %f Hz\n",samp_rate/(float) nchan);
printf("Integration time: %f s\n",tint);
printf("Number of averaged spectra: %d\n",nint);
printf("Number of subints per file: %d\n",nsub);
printf("Starting index: %d\n",m);
// Allocate
c=fftwf_malloc(sizeof(fftwf_complex)*nchan);
d=fftwf_malloc(sizeof(fftwf_complex)*nchan);
ibuf=(int16_t *) malloc(sizeof(int16_t)*2*nchan);
cbuf=(char *) malloc(sizeof(char)*2*nchan);
fbuf=(float *) malloc(sizeof(float)*2*nchan);
wbuf = (int32_t *)malloc(sizeof(int32_t) * 2 * nchan);
z=(float *) malloc(sizeof(float)*nchan);
cz=(char *) malloc(sizeof(char)*nchan);
zw=(float *) malloc(sizeof(float)*nchan);
// Compute window
for (i=0;i<nchan;i++)
zw[i]=0.54-0.46*cos(2.0*M_PI*i/(nchan-1));
// Plan
fft=fftwf_plan_dft_1d(nchan,c,d,FFTW_FORWARD,FFTW_ESTIMATE);
// Create prefix
if (realtime==1) {
gettimeofday(&start,0);
strftime(prefix,30,"%Y-%m-%dT%T",gmtime(&start.tv_sec));
} else {
sprintf(prefix,"%.19s",nfd);
mjd=nfd2mjd(nfd);
}
// Open file
if (informat != 'w') {
if (strlen(infname)) {
infile = fopen(infname, "r");
} else {
infile = stdin;
}
}
// Forever loop
for (;;m++) {
// File name
if (useoutput==0) {
sprintf(outfname,"%s/%s_%06d.bin",path,prefix,m);
} else {
sprintf(outfname,"%s/%s_%06d.bin",path,output,m);
}
outfile=fopen(outfname,"w");
// Loop over subints to dump
for (k=0;k<nsub;k++) {
// Initialize
for (i=0;i<nchan;i++)
z[i]=0.0;
// Log start time
gettimeofday(&start,0);
// Integrate
for (j=0;j<nint;j++) {
// Read buffer
if (informat=='i')
nbytes=fread(ibuf,sizeof(int16_t),2*nchan,infile);
else if (informat=='c')
nbytes=fread(cbuf,sizeof(char),2*nchan,infile);
else if (informat=='f')
nbytes=fread(fbuf,sizeof(float),2*nchan,infile);
else if (informat == 'w')
nbytes = sox_read(wav_reader, wbuf, 2 * nchan);
// End on empty buffer
if (nbytes==0)
break;
// Skip buffer
if (j%nuse!=0)
continue;
// Unpack
if (informat=='i') {
for (i=0;i<nchan;i++) {
c[i][0]=(float) ibuf[2*i]/32768.0*zw[i];
c[i][1]=(float) ibuf[2*i+1]/32768.0*zw[i]*sign;
}
} else if (informat=='c') {
for (i=0;i<nchan;i++) {
c[i][0]=(float) cbuf[2*i]/256.0*zw[i];
c[i][1]=(float) cbuf[2*i+1]/256.0*zw[i]*sign;
}
} else if (informat=='f') {
for (i=0;i<nchan;i++) {
c[i][0]=(float) fbuf[2*i]*zw[i];
c[i][1]=(float) fbuf[2*i+1]*zw[i]*sign;
}
} else if (informat == 'w') {
for (i = 0; i < nchan; i++) {
c[i][0] = (float) wbuf[2*i] / 2147483648 * zw[i];
c[i][1] = (float) wbuf[2*i+1] / 2147483648 * zw[i] * sign;
}
}
// Execute
fftwf_execute(fft);
// Add
for (i=0;i<nchan;i++) {
if (i<nchan/2)
l=i+nchan/2;
else
l=i-nchan/2;
//z[l]+=sqrt(d[i][0]*d[i][0]+d[i][1]*d[i][1]);
z[l]+=d[i][0]*d[i][0]+d[i][1]*d[i][1];
}
}
// Log end time
gettimeofday(&end,0);
// Time stats
length=(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec)*1e-6;
// Scale
for (i=0;i<nchan;i++)
z[i]*=(float) nuse/(float) nchan;
// Scale to bytes
if (outformat=='c') {
// Compute average
for (i=0,zavg=0.0;i<nchan;i++)
zavg+=z[i];
zavg/=(float) nchan;
// Compute standard deviation
for (i=0,zstd=0.0;i<nchan;i++)
zstd+=pow(z[i]-zavg,2);
zstd=sqrt(zstd/(float) nchan);
// Convert
for (i=0;i<nchan;i++) {
z[i]=256.0/6.0*(z[i]-zavg)/zstd;
if (z[i]<-128.0)
z[i]=-128.0;
if (z[i]>127.0)
z[i]=127.0;
cz[i]=(char) z[i];
}
}
// Format start time
if (realtime==1) {
strftime(tbuf,30,"%Y-%m-%dT%T",gmtime(&start.tv_sec));
sprintf(nfd,"%s.%03ld",tbuf,start.tv_usec/1000);
} else {
mjd2nfd(mjd+(m*nsub+k)*tint/86400.0,nfd);
length=tint;
}
// Header
if (partial==0) {
if (outformat=='f')
sprintf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\nEND\n",nfd,freq,samp_rate,length,nchan,nsub);
else if (outformat=='c')
sprintf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\nNBITS 8\nMEAN %e\nRMS %e\nEND\n",nfd,freq,samp_rate,length,nchan,nsub,zavg,zstd);
} else if (partial==1) {
if (outformat=='f')
sprintf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\nEND\n",nfd,0.5*(freqmax+freqmin),freqmax-freqmin,length,imax-imin,nsub);
else if (outformat=='c')
sprintf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\nNBITS 8\nMEAN %e\nRMS %e\nEND\n",nfd,0.5*(freqmax+freqmin),freqmax-freqmin,length,imax-imin,nsub,zavg,zstd);
}
// Limit output
if (!quiet)
printf("%s %s %f %d\n",outfname,nfd,length,j);
// Dump file
fwrite(header,sizeof(char),256,outfile);
if (partial==0) {
if (outformat=='f')
fwrite(z,sizeof(float),nchan,outfile);
else if (outformat=='c')
fwrite(cz,sizeof(char),nchan,outfile);
} else if (partial==1) {
if (outformat=='f')
fwrite(&z[imin],sizeof(float),imax-imin,outfile);
else if (outformat=='c')
fwrite(&cz[imin],sizeof(char),imax-imin,outfile);
}
// Break;
if (nbytes==0)
break;
}
// Break;
if (nbytes==0)
break;
// Close file
fclose(outfile);
}
if (informat != 'w') {
fclose(infile);
}
if (informat == 'w') {
sox_close(wav_reader);
sox_format_quit();
sox_quit();
}
// Destroy plan
fftwf_destroy_plan(fft);
// Deallocate
free(ibuf);
free(cbuf);
free(fbuf);
free(wbuf);
fftwf_free(c);
fftwf_free(d);
free(z);
free(cz);
free(zw);
return 0;
}