11'''
2- Dedisperses data
2+ Dedisperses data
33'''
44# pylint: disable-msg=C0103
55import 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
0 commit comments