11'''
2- Dedisperses data
2+ Dedisperses data
33'''
44# pylint: disable-msg=C0103
55import numpy as np
66
7- def dedisperse (samples , dm ):
7+ def dedisperse (samples , highest_x = None , max_delay = 0 , disperion_measure = 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
11+ signal should be
12+ The highest_x specifies the amount of intensities that are used for estimating the minimum
13+ pulsar intensity
1014 '''
11- samples = np .asarray (samples )
12- # Distribute the DM over the amount of samples
13- delays_per_sample = np .round (np .linspace (dm , 0 , samples .shape [1 ])).astype (int )
15+
16+ # Check if parameters contain a Dispersion Measure, if not, estimate one
17+ if disperion_measure is None :
18+ # Estimates the minimum for an intensity to be considered a pulsar
19+ pulsar_intensity = find_estimation_intensity (samples , highest_x )
20+ disperion_measure = find_dm (samples , pulsar_intensity , max_delay )
21+
22+ # Distribute the Dispersion Measure over the amount of samples
23+ delays_per_sample = np .round (np .linspace (disperion_measure , 0 , samples .shape [1 ])).astype (int )
1424
1525 # Loop over the frequencies
16- for i , _ in enumerate (delays_per_sample ):
26+ for i in range (delays_per_sample . size ):
1727
1828 # Temporary array that is used to later delay the frequency
1929 temporary_samples = []
@@ -25,3 +35,80 @@ def dedisperse(samples, dm):
2535 samples [:, i ] = np .roll (temporary_samples , delays_per_sample [i ])
2636
2737 return samples
38+
39+ def find_dm (samples , pulsar_intensity , max_delay ):
40+ '''
41+ This method attempts to find a dispersion measure
42+ '''
43+
44+ # Loop through the samples to find a pulsar intensity to start calculating from
45+ for s , sample in enumerate (samples [:, 0 ]):
46+
47+ # If the sample meets the minimum intensity, attempt to find a line continuing from
48+ # this intensity
49+ if sample > pulsar_intensity :
50+ start_sample_index = s
51+
52+ # Attempt to find a line, line_coordinates contains first and last index of the pulsar
53+ line_coordinates = find_line (samples , start_sample_index , max_delay , pulsar_intensity )
54+
55+ # If a line is found, calculate and return the dispersion measure
56+ if line_coordinates is not None :
57+ disperion_measure = line_coordinates [1 ] - line_coordinates [0 ]
58+ return disperion_measure
59+
60+ return None
61+
62+
63+ def find_line (samples , start_sample_index , max_delay , pulsar_intensity ):
64+ '''
65+ This method tries to find a line starting from the sample index given in the parameters
66+ it stops if there is no intensity within the max_delay higher than the average_intensity
67+ '''
68+
69+ previous_index = start_sample_index
70+
71+ failed_to_find_line = True
72+
73+ # Loop through the frequencies
74+ for f in range (samples [1 ].size ):
75+
76+ # Loop through previous intensity until the max delay is reached
77+ for i , intensity in enumerate (samples [:, f ][previous_index :previous_index + max_delay ]):
78+
79+ # Skip the first frequency, since we start measuring from the first intensity
80+ if f == 0 :
81+ failed_to_find_line = False
82+ break
83+
84+ # If the intensity is higher than the pulsar_intensity, continue finding a signal
85+ if intensity > pulsar_intensity :
86+ previous_index = previous_index + i
87+ failed_to_find_line = False
88+ break
89+
90+ # If there is no line found, return None
91+ if failed_to_find_line :
92+ return None
93+
94+ # If all frequencies are looped, a continuous signal is found,
95+ # so we return the first and last index of the line
96+ return start_sample_index , previous_index
97+
98+ def find_estimation_intensity (samples , highest_x ):
99+ '''
100+ This method finds the average intensity for the highest x intensities
101+ The average_intensity is considered a requirement for intensities to be considered a pulsar
102+ '''
103+
104+ # Sum of all intensities
105+ sum_intensities = 0
106+
107+ # Looks for the top x highest intensities in the samples and adds them up together
108+ for sample in samples :
109+ sum_intensities += np .sum (sorted (sample , reverse = True )[:highest_x ])
110+
111+ # Calculates the average_intensity
112+ average_intensity = (sum_intensities ) / (samples .shape [0 ] * highest_x )
113+
114+ return average_intensity
0 commit comments