Skip to content

Commit 456fa35

Browse files
committed
Implemented dispersion measure guessing
1 parent 3c565ce commit 456fa35

File tree

4 files changed

+88
-97
lines changed

4 files changed

+88
-97
lines changed

dedisperse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""
22
import file for dedisperse.py
33
"""
4-
from .dedisperse import dedisperse, estimate_dm, find_initial_signal, find_initial_line
4+
from .dedisperse import dedisperse, find_dm

dedisperse/dedisperse.py

Lines changed: 59 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
'''
2-
Dedisperses data
2+
Dedisperses data
33
'''
44
# pylint: disable-msg=C0103
55
import numpy as np
66

7-
def dedisperse(samples, dm=None):
7+
def dedisperse(samples, highest_x=None, max_delay=None, dm=None):
88
'''
99
This method performs dedispersion on the filterbank data
10+
The maximum_delay specifies between the currently considered pulsar signal and the next pulsar signal should be
11+
The highest_x specifies the amount of intensities that are used for estimating the minimum pulsar intensity
1012
'''
1113

14+
# Check if parameters contain a DM, if not, estimate one
1215
if dm is None:
13-
print("Finding possible DM's")
14-
dm = estimate_dm(samples)
16+
# Estimates the minimum for an intensity to be considered a pulsar
17+
pulsar_intensity = find_estimation_intensity(samples, highest_x)
18+
dm = find_dm(samples, pulsar_intensity, max_delay)
1519

1620
# Distribute the DM over the amount of samples
1721
delays_per_sample = np.round(np.linspace(dm, 0, samples.shape[1])).astype(int)
@@ -30,106 +34,78 @@ def dedisperse(samples, dm=None):
3034

3135
return samples
3236

33-
34-
def estimate_dm(samples):
37+
def find_dm(samples, pulsar_intensity, max_delay):
3538
'''
3639
This method attempts to find a dispersion measure
3740
'''
3841

39-
# Tuple of frequency index and sample index
40-
initial_signal_point = find_initial_signal(samples)
41-
last_sample = initial_signal_point
42-
43-
for i, frequency in enumerate(samples[1]):
44-
for j, data_point in enumerate(samples[:, i]):
45-
#print(samples[:, i][previous_time_sample:].shape[0])
46-
if(j > last_sample[1]):
47-
if(data_point > 10):
48-
last_sample = i, j
49-
print("At Frequency ", i, " found on Time sample ", j, " - ", data_point)
50-
break
51-
52-
highest_difference = 0
53-
'''
54-
for s, samples in enumerate(samples):
55-
for i, data_point in enumerate(samples):
56-
if(i > highest_difference):
57-
if(data_point > 10):
58-
print(s, i, " - ", data_point)
59-
highest_difference = i
60-
break
61-
'''
62-
estimated_dm = last_sample[1] - initial_signal_point[1]
63-
print("Estimated DM is", estimated_dm)
64-
return estimated_dm
42+
# Loop through the samples to find a pulsar intensity to start calculating from
43+
for s, sample in enumerate(samples[:, 0]):
6544

45+
# If the sample meets the minimum intensity, attempt to find a line continuing from this intensity
46+
if(sample > pulsar_intensity):
47+
start_sample_index = s
6648

67-
def find_initial_signal(samples):
68-
'''
69-
This method attempts to find a viable data point to start estimating a dispersion measure from
70-
'''
49+
# Attempt to find a line, line_coordinates contains the first and last index of the pulsar
50+
line_coordinates = find_line(samples, start_sample_index, max_delay, pulsar_intensity)
51+
52+
# If a line is found, calculate and return the dispersion measure
53+
if(line_coordinates is not None):
54+
dm = line_coordinates[1] - line_coordinates[0]
55+
return dm
7156

72-
# Tuple of frequency index and sample index
73-
lowest_sample = 0, samples.shape[0]
74-
75-
for i, frequency in enumerate(samples[1]):
76-
for j, data_point in enumerate(samples[:, i]):
77-
if(j < lowest_sample[1]):
78-
if(data_point > 5):
79-
print("Initial signal found on freq, sample", i, j, data_point)
80-
return i, j
81-
'''
82-
print(lowest_sample, " ", j)
83-
lowest_sample = i, j
84-
break
85-
'''
86-
87-
print("NO INITIAL SIGNAL FOUND")
8857
return None
89-
9058

91-
def find_initial_line(samples):
59+
60+
def find_line(samples, start_sample_index, max_delay, pulsar_intensity):
9261
'''
93-
This method attempts to find a line to dedisperse
62+
This method tries to find a line starting from the sample index given in the parameters
63+
it stops if there is no intensity within the max_delay higher than the average_intensity
9464
'''
95-
96-
avg_intensity = find_avg_intensity(samples, 10)
97-
max_delay = 8
98-
99-
for s, sample in enumerate(samples[:, 1]):
100-
if(sample > avg_intensity):
101-
previous_sample_index = s
102-
print("Attempting to find line on freq,", 1, "sample", s)
103-
find_line(samples, previous_sample_index, max_delay, avg_intensity)
104-
10565

106-
print("NO INITIAL SIGNAL FOUND")
107-
return None
66+
previous_sample_index = start_sample_index
10867

68+
failed_to_find_line = True
10969

110-
def find_line(samples, previous_sample_index, max_delay, avg_intensity):
70+
# Loop through the frequencies
11171
for f, frequency in enumerate(samples[1]):
112-
for i, intensity in enumerate(samples[:, f][previous_sample_index:previous_sample_index+max_delay]):
113-
if(intensity > avg_intensity):
114-
print("Continuing to find line on freq,", f, "sample", i, intensity)
115-
previous_sample_index = i
72+
73+
# Loop through previous intensity until the max delay is reached
74+
for i, intensity in enumerate(samples[:, f][previous_sample_index:previous_sample_index + max_delay]):
75+
76+
# Skip the first frequency, since that is where the initial sample is we are measuring from
77+
if(f == 0):
78+
failed_to_find_line = False
79+
break
80+
81+
# If the intensity is higher than the pulsar_intensity, continue finding a signal
82+
if(intensity > pulsar_intensity):
83+
previous_sample_index = previous_sample_index + i
84+
failed_to_find_line = False
11685
break
117-
else:
118-
continue
119-
120-
12186

122-
def find_avg_intensity(samples, top = 10):
87+
# If there is no line found, return None
88+
if failed_to_find_line:
89+
return None
90+
91+
# If all frequencies are looped through, a line is found, so we return the start and end index of the line
92+
return start_sample_index, previous_sample_index
93+
94+
95+
def find_estimation_intensity(samples, highest_x):
12396
'''
124-
Finds average intensity for top x intensities
97+
This method finds the average intensity for the highest x intensities
98+
The average_intensity is considered a requirement for intensities to be considered a pulsar
12599
'''
126100

101+
# Sum of all intensitiesg
127102
sum_intensities = 0
128-
# Looks for the 3 highest intensities in the first 10 samples
103+
104+
# Looks for the top x highest intensities in the samples and adds them up together
129105
for sample in samples:
130-
#top_samples.append((sorted([(x,i) for (i,x) in enumerate(sample)], reverse=True)[:3] ))
131-
sum_intensities += np.sum(sorted(sample, reverse=True)[:top])
106+
sum_intensities += np.sum(sorted(sample, reverse=True)[:highest_x])
132107

133-
avg_intensity = (sum_intensities) / (samples.shape[0] * top)
108+
# Calculates the average_intensity
109+
average_intensity = (sum_intensities) / (samples.shape[0] * highest_x)
134110

135-
return (avg_intensity)
111+
return average_intensity

examples/dedisperse_samples.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@
2323

2424
frequencies, samples = special_pspm.select_data()
2525

26-
# Use this if you have your own file with a clear pulsar signal, this method assumes all signals other than the pulsar are lower than 10
27-
print_possible_dm(samples)
28-
2926
# Dispersion Measure
30-
DM = 240
27+
DM = 235
3128

3229
plt.subplot(2,1,1)
3330
data, extent = waterfall_plot(samples, frequencies)

examples/dedisperse_stream.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,33 @@
1818

1919
from clipping import clipping
2020

21-
# Read filterbank data
22-
special_pspm = fb.Filterbank(filename = "../data/my_uber_pspm.fil")
21+
# Read filterbank data,
22+
23+
# Standard file
24+
special_pspm = fb.Filterbank(filename = "../data/my_special_pspm.fil")
25+
highest_x=10
26+
max_delay=10
27+
28+
# Files with low signal to noise ratio
29+
# special_pspm = fb.Filterbank(filename = "../data/my_uber_pspm.fil")
30+
# highest_x=10
31+
# max_delay=10
32+
33+
# File with 10000 samples
34+
# special_pspm = fb.Filterbank(filename = "../data/pspm_4_2.fil")
35+
# highest_x=10
36+
# max_delay=100
37+
38+
# File with 10000 samples with low signal to noise ratio
39+
# special_pspm = fb.Filterbank(filename = "../data/pspm_4_1.fil")
40+
# highest_x=10
41+
# max_delay=100
2342

2443
special_pspm.read_filterbank()
2544

2645
frequencies, samples = special_pspm.select_data()
2746

28-
47+
# Plot the original data
2948
plt.subplot(2,1,1)
3049
data, extent = waterfall_plot(samples, frequencies)
3150

@@ -37,6 +56,8 @@
3756
extent=extent,
3857
cmap='cubehelix')
3958

59+
plt.colorbar()
60+
4061
time_series = []
4162

4263
for sample_set in samples:
@@ -47,12 +68,10 @@
4768
plt.plot(time_series)
4869
plt.show()
4970

50-
#clipped_samples = clipping(frequencies, samples)
51-
#samples = dedisperse.dedisperse(samples)
52-
#samples = dedisperse.find_lowest_pulsar(samples)
53-
#samples = dedisperse.estimate_dm(samples)
54-
samples = dedisperse.find_initial_line(samples)
71+
# Dedisperse the samples
72+
samples = dedisperse.dedisperse(samples, highest_x, max_delay)
5573

74+
# Plot the dedispersed data
5675
plt.subplot(2,1,1)
5776
data, extent = waterfall_plot(samples, frequencies)
5877

@@ -63,7 +82,6 @@
6382
interpolation='nearest',
6483
extent=extent,
6584
cmap='cubehelix')
66-
plt.colorbar()
6785

6886
time_series = []
6987

0 commit comments

Comments
 (0)