-
Notifications
You must be signed in to change notification settings - Fork 0
/
flagger_analysis.py
153 lines (134 loc) · 4.74 KB
/
flagger_analysis.py
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
## testing applying flagger on all spectra of a given block
## see if the noise is not too much to detect outliers
import argparse
import numpy as np
import matplotlib.pyplot as plt
import sigproc
import warnings
from pathlib import Path
import scipy.signal
from scipy import stats
import numpy.matlib as npm
import sys
dSNR = 20; # fake signal strength
threshold = 5;
filtsize = 21; # seems to work, can be increased if needed
nBeam = 0;
nBlock = 0;
#infile = '/home/user/vikram/scratch/bsfil_B0531_1.fil';
infile = '/home/user/vikram/scratch/bsfil_TEST_2.fil';
header = sigproc.read_header(infile);
nBeams = 64; #header['nbeams'];
nChans = 1024; #header['nchans'];
nInts = 4096; #header['nsamples'];
nBlockSize = nBeams * nChans * nInts;
FilSize = Path(infile).stat().st_size;
nTotBlocks = round(FilSize / nBlockSize);
fh = open(infile, 'rb');
while True:
keyword, value, idx = sigproc.read_next_header_keyword(fh);
if keyword == 'HEADER_END':
break;
nStartDataIdx = fh.tell();
nBeam = 10; #np.random.randint(nBeams);
nBlock = 10; #np.random.randint(nTotBlocks);
fh.seek(0,0);
data = np.fromfile(fh, dtype=np.uint8, count=nBlockSize, offset=nStartDataIdx + nBlock * nBlockSize);
data = np.reshape(data, (nBeams,nInts,nChans), order = 'C');
data = data[nBeam,:,128:895];
## inject fake FRB -- sort of
N = 500;
n = np.linspace(-N/2,N/2,N);
stdev = (N-1)/(6);
y = np.exp(-1/2*np.power(n/stdev,2));
y = npm.repmat(y,N,1);
y = np.multiply(y,y.T);
dataInj = np.copy(data);
dataInj[int(data.shape[0]/2)-int(N/2):int(data.shape[0]/2)+int(N/2),int(data.shape[1]/2)-int(N/2):int(data.shape[1]/2)+int(N/2)] =\
dataInj[int(data.shape[0]/2)-int(N/2):int(data.shape[0]/2)+int(N/2),int(data.shape[1]/2)-int(N/2):int(data.shape[1]/2)+int(N/2)] +\
dSNR*y;
## processing
# mask = np.zeros(np.shape(data));
# #spec = np.mean(data,axis=0);
# #specfilt = scipy.signal.medfilt(spec,kernel_size=filtsize);
# for k in range(nInts):
# spec = data[k,:];
# specfilt = scipy.signal.medfilt(spec,kernel_size=filtsize);
# speccorrec = spec - specfilt;
# sig = stats.median_abs_deviation(speccorrec);
# for kk in range(data.shape[1]):
# if speccorrec[kk]>=threshold*sig or speccorrec[kk]<=-threshold*sig:
# mask[k,kk] = 1;
# plt.figure();
# plt.subplot(2,3,1);
# plt.imshow(data,aspect='auto');
# plt.title('data : beam '+str(nBeam)+' - block '+str(nBlock));
# plt.xlabel('freq channel');
# plt.ylabel('time sample');
# plt.subplot(2,3,4);
# plt.imshow(mask,aspect='auto');
# plt.title('mask');
# plt.xlabel('freq channel');
# plt.ylabel('time sample');
mask = np.zeros(np.shape(dataInj));
data2 = np.copy(dataInj); # remove impulsive RFI
for k in range(nInts):
spec = dataInj[k,:];
specfilt = scipy.signal.medfilt(spec,kernel_size=filtsize);
speccorrec = spec - specfilt;
sig = stats.median_abs_deviation(speccorrec);
for kk in range(dataInj.shape[1]):
if speccorrec[kk]>=threshold*sig or speccorrec[kk]<=-threshold*sig:
mask[k,kk] = 1;
speccorrec[kk] = np.random.normal(0.,1.4826*sig);
data2[k,:] = speccorrec + specfilt;
## narrowband flagger
data3 = np.copy(data2); # remove narrowband RFI
spec = np.mean(data3,axis=0);
specfilt = scipy.signal.medfilt(spec,kernel_size=filtsize);
speccorrec = spec - specfilt;
sig = stats.median_abs_deviation(speccorrec);
sigTot = stats.median_abs_deviation(np.reshape(data3,-1));
for kk in range(dataInj.shape[1]):
if speccorrec[kk]>=threshold*sig or speccorrec[kk]<=-threshold*sig:
data3[:,kk] = np.random.normal(specfilt[kk],1.4826*sigTot,(nInts));
plt.figure();
plt.subplot(2,3,1);
plt.imshow(dataInj,aspect='auto');
plt.title('data : beam '+str(nBeam)+' - block '+str(nBlock)+' + FRB');
plt.xlabel('freq channel');
plt.ylabel('time sample');
plt.subplot(2,3,4);
plt.imshow(mask,aspect='auto');
plt.title('impulsive RFI mask');
plt.xlabel('freq channel');
plt.ylabel('time sample');
plt.subplot(2,3,2);
plt.imshow(data2,aspect='auto');
plt.title('impulsive RFI flagged');
plt.xlabel('freq channel');
plt.ylabel('time sample');
plt.subplot(2,3,5);
plt.imshow(data3,aspect='auto');
plt.title('impulsive + narrowband RFI flagged');
plt.xlabel('freq channel');
plt.ylabel('time sample');
plt.subplot(2,3,3);
plt.plot(np.mean(dataInj,axis=0),label='original');
plt.plot(np.mean(data2,axis=0),label='impulsive flagged');
plt.plot(np.mean(data3,axis=0),label='impulsive+narrow flagged');
plt.legend();
plt.grid();
plt.title('data profile');
plt.xlabel('freq channel');
plt.ylabel('power');
plt.subplot(2,3,6);
plt.plot(np.mean(dataInj,axis=1),label='original');
plt.plot(np.mean(data2,axis=1),label='impulsive flagged');
plt.plot(np.mean(data3,axis=1),label='impulsive+narrow flagged');
plt.legend();
plt.grid();
plt.title('data profile');
plt.xlabel('time sample');
plt.ylabel('power');
plt.show();