forked from SixByNine/sigproc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbandpass.c
149 lines (143 loc) · 3.21 KB
/
bandpass.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "sigproc.h"
#include "header.h"
FILE *input;
main(int argc, char *argv[])
{
char string[80];
int increment=0,i,j,k,l,ndumps,ic,x,y,z,a,cube,dumpcount;
double elapsed_time;
unsigned char c;
unsigned short s;
float *f,*g,temp,fchan;
double secsperdump;
/* default is to read from standard input and to sum all dumps */
input=stdin;
ndumps=cube=dumpcount=0;
secsperdump=elapsed_time=0.0;
/* check command-line parameters */
i=1;
while (i<argc) {
print_version(argv[0],argv[1]);
if (strings_equal(argv[i],"-d")) {
i++;
ndumps=atoi(argv[i]);
} else if (strings_equal(argv[i],"-t")) {
i++;
secsperdump=atof(argv[i]);
} else if (help_required(argv[i])) {
bandpass_help();
exit(0);
} else if (strings_equal(argv[i],"-cube")) {
cube=1;
} else if (file_exists(argv[i])) {
input=open_file(argv[i],"rb");
} else {
bandpass_help();
sprintf(string,"unknown argument (%s) passed to bandpass",argv[i]);
error_message(string);
}
i++;
}
if (!read_header(input))
error_message("could not read filterbank header...");
/* set number of dumps to average over if user has supplied seconds */
if (secsperdump > 0.0)
ndumps = (int) rint(secsperdump/tsamp);
/* initialize buffer for storing bandpass */
ic=nchans*nifs;
f = (float *) malloc(sizeof(float)*ic);
g = (float *) malloc(sizeof(float)*ic);
for (i=0; i<ic; i++) f[i]=g[i]=0.0;
k=l=0;
while (!feof(input)) {
switch (nbits) {
case 1:
fread(&c,1,1,input);
for (j=0;j<8;j++){
f[l] = c&1;
if(j<7)
l++;
c>>=1;
}
break;
case 2:
fread(&c,1,1,input);
char2fourints(c,&x,&y,&z,&a);
f[l]=(float) x;
l++;
f[l]=(float) y;
l++;
f[l]=(float) z;
l++;
f[l]=(float) a;
break;
case 4:
fread(&c,1,1,input);
char2ints(c,&x,&y);
f[l]=(float) x;
l++;
f[l]=(float) y;
break;
case 8:
fread(&c,nbits/8,1,input);
f[l]=(float) c;
break;
case 16:
fread(&s,nbits/8,1,input);
f[l]=(float) s;
break;
case 32:
fread(&temp,nbits/8,1,input);
f[l]=temp;
break;
default:
sprintf(string,"cannot read %d bits per sample...\n",nbits);
error_message(string);
break;
}
l++;
if (l==ic) {
for (i=0;i<ic;i++) g[i]+=f[i];
k++;
if (k==ndumps) {
dumpcount++;
if (!cube) puts("#START");
for (i=0; i<nchans; i++) {
fchan=fch1+(foff*(double) i);
if (cube) printf("%f ",elapsed_time);
printf("%f ",fchan);
for (j=0; j<nifs; j++) {
l=j*nchans+i;
printf("%f ",g[l]/(float)ndumps);
g[l]=f[l]=0.0;
}
printf("\n");
}
if(cube) printf("\n");
if (!cube) puts("#STOP");
k=0;
}
l=0;
if (cube) {
elapsed_time+=tsamp;
if (dumpcount==nchans) exit(0);
}
}
}
/* write out final dump if necessary */
if (!ndumps) {
ndumps=k;
for (i=0; i<nchans; i++) {
fchan=fch1+(foff*(double) i);
printf("%f ",fchan);
for (j=0; j<nifs; j++) {
l=j*nchans+i;
printf("%f ",g[l]/(float)ndumps);
}
printf("\n");
}
}
}