-
Notifications
You must be signed in to change notification settings - Fork 37
/
rffind.c
238 lines (197 loc) · 4.5 KB
/
rffind.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <getopt.h>
#include "rftime.h"
#include "rfio.h"
#define LIM 128
#define NMAX 64
void filter(struct spectrogram s,int site_id,float sigma,char *filename,int graves)
{
int i,j,k,l;
float s1,s2,avg,std,dz;
FILE *file;
double f;
int *mask;
float *sig;
mask=(int *) malloc(sizeof(int)*s.nchan);
sig=(float *) malloc(sizeof(float)*s.nchan);
// Open file
file=fopen(filename,"a");
// Loop over subints
for (i=0;i<s.nsub;i++) {
// Set mask
for (j=0;j<s.nchan;j++)
mask[j]=1;
// Iterate to remove outliers
for (k=0;k<10;k++) {
// Find average
for (j=0,s1=s2=0.0;j<s.nchan;j++) {
if (mask[j]==1) {
s1+=s.z[i+s.nsub*j];
s2+=1.0;
}
}
avg=s1/s2;
// Find standard deviation
for (j=0,s1=s2=0.0;j<s.nchan;j++) {
if (mask[j]==1) {
dz=s.z[i+s.nsub*j]-avg;
s1+=dz*dz;
s2+=1.0;
}
}
std=sqrt(s1/s2);
// Update mask
for (j=0,l=0;j<s.nchan;j++) {
if (fabs(s.z[i+s.nsub*j]-avg)>sigma*std) {
mask[j]=0;
l++;
}
}
}
// Reset mask
for (j=0;j<s.nchan;j++) {
sig[j]=(s.z[i+s.nsub*j]-avg)/std;
if (sig[j]>sigma)
mask[j]=1;
else
mask[j]=0;
}
// Find maximum when points are adjacent
for (j=0;j<s.nchan-1;j++) {
if (mask[j]==1 && mask[j+1]==1) {
if (s.z[i+s.nsub*j]<s.z[i+s.nsub*(j+1)])
mask[j]=0;
}
}
for (j=s.nchan-2;j>=0;j--) {
if (mask[j]==1 && mask[j-1]==1) {
if (s.z[i+s.nsub*j]<s.z[i+s.nsub*(j-1)])
mask[j]=0;
}
}
// Mark points
for (j=0;j<s.nchan;j++) {
if (mask[j]==1) {
f=s.freq-0.5*s.samp_rate+(double) j*s.samp_rate/(double) s.nchan;
if (s.mjd[i]>1.0) {
if (graves==0)
fprintf(file,"%lf %lf %f %d\n",s.mjd[i],f,sig[j],site_id);
else
fprintf(file,"%lf %lf %f %d 9999\n",s.mjd[i],f,sig[j],site_id);
}
}
}
}
fclose(file);
free(mask);
free(sig);
return;
}
void usage(void)
{
printf("rffind: Find signals RF observations\n\n");
printf("-p <path> Input path to file /a/b/c_??????.bin\n");
printf("-s <start> Number of starting subintegration [0]\n");
printf("-l <length> Number of subintegrations to plot [3600]\n");
printf("-f <freq> Frequency to zoom into (Hz)\n");
printf("-w <bw> Bandwidth to zoom into (Hz)\n");
printf("-o <offset> Frequency offset to apply\n");
printf("-C <site> Site ID\n");
printf("-g GRAVES data\n");
printf("-S Sigma limit [default: 5.0]\n");
printf("-h This help\n");
}
int main(int argc,char *argv[])
{
int i,j,k,l,j0,j1,m=2,n;
struct spectrogram s;
char path[128];
int isub=0,nsub=0;
char *env;
int site_id=0,graves=0;
float avg,std;
int arg=0;
float sigma=5.0;
FILE *file;
double f,f0=0.0,df0=0.0;
char filename[128]="find.dat";
// Get site
env=getenv("ST_COSPAR");
if (env!=NULL) {
site_id=atoi(env);
} else {
printf("ST_COSPAR environment variable not found.\n");
}
// Read arguments
if (argc>1) {
while ((arg=getopt(argc,argv,"p:f:w:s:l:hC:o:S:g"))!=-1) {
switch (arg) {
case 'p':
strcpy(path,optarg);
break;
case 's':
isub=atoi(optarg);
break;
case 'C':
site_id=atoi(optarg);
break;
case 'l':
nsub=atoi(optarg);
break;
case 'o':
strcpy(filename,optarg);
break;
case 'f':
f0=(double) atof(optarg);
break;
case 'g':
graves=1;
break;
case 'S':
sigma=atof(optarg);
break;
case 'w':
df0=(double) atof(optarg);
break;
case 'h':
usage();
return 0;
default:
usage();
return 0;
}
}
} else {
usage();
return 0;
}
if (nsub==0) {
// Read data
for (i=isub;;i++) {
s=read_spectrogram(path,i,nsub,f0,df0,1,0.0);
// Exit on emtpy file
if (s.nsub==0)
break;
printf("Read spectrogram\n%d channels, %d subints\nFrequency: %g MHz\nBandwidth: %g MHz\n",s.nchan,s.nsub,s.freq*1e-6,s.samp_rate*1e-6);
// Filter
filter(s,site_id,sigma,filename,graves);
// Free
free_spectrogram(s);
}
} else {
// Read data
s=read_spectrogram(path,isub,nsub,f0,df0,1,0.0);
// Exit on emtpy file
if (s.nsub>0) {
printf("Read spectrogram\n%d channels, %d subints\nFrequency: %g MHz\nBandwidth: %g MHz\n",s.nchan,s.nsub,s.freq*1e-6,s.samp_rate*1e-6);
// Filter
filter(s,site_id,sigma,filename,graves);
}
// Free
free_spectrogram(s);
}
return 0;
}