Skip to content

Commit

Permalink
Phase lag fixes, code cleanup and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
pupperemeritus committed Sep 6, 2023
1 parent 3b15707 commit bc23d38
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
10 changes: 5 additions & 5 deletions stingray/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,17 +2058,17 @@ def lsft_slow(
ft_res : numpy.ndarray
An array of Fourier transformed data.
"""
y_ = copy.deepcopy(y) - np.mean(y)
freqs = freqs[freqs >= 0]
y_ = y - np.mean(y)
freqs = np.asarray(freqs[np.asarray(freqs) >= 0])

ft_real = np.zeros_like(freqs)
ft_imag = np.zeros_like(freqs)
ft_res = np.zeros_like(freqs, dtype=np.complex128)

num_y = len(y_)
num_freqs = len(freqs)
num_y = y_.shape[0]
num_freqs = freqs.shape[0]
sum_y = np.sum(y_)
const1 = np.sqrt(0.5) * np.sqrt(num_y)
const1 = np.sqrt(0.5 * num_y)
const2 = const1 * np.sign(sign)
ft_real = ft_imag = np.zeros(num_freqs)
ft_res = np.zeros(num_freqs, dtype=np.complex128)
Expand Down
12 changes: 2 additions & 10 deletions stingray/lombscargle.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,17 +476,9 @@ def _initialize_empty(self):
self.variance2 = None
return

def phase_lag(self):
"""Not applicable for unevenly sampled data"""
raise AttributeError(
"Object has no attribute named 'phase_lag' ! Not applicable for unevenly sampled data"
)

def time_lag(self):
"""Not applicable for unevenly sampled data"""
raise AttributeError(
"Object has no attribute named 'time_lag' ! Not applicable for unevenly sampled data"
)
super().__doc__
return self.phase_lag() / (2 * np.pi * self.freq)

def classical_significances(self):
"""Not applicable for unevenly sampled data"""
Expand Down
8 changes: 5 additions & 3 deletions stingray/tests/test_fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,11 @@ def test_unnormalize_poisson_noise(self, norm, power_type):

@pytest.mark.parametrize("phlag", [0.05, 0.1, 0.2, 0.4])
def test_lag(phlag):
freq=1.1123232252
freq = 1.1123232252

def func(time, phase=0):
return 2 + np.sin(2 * np.pi * (time * freq - phase))

time = np.sort(np.random.uniform(0, 100, 3000))
ft0 = lsft_slow(func(time, 0), time, np.array([freq]))
ft1 = lsft_slow(func(time, phlag), time, np.array([freq]))
Expand All @@ -556,9 +558,9 @@ def func(time, phase=0):
measured_lag -= 0.5
while measured_lag <= -0.5:
measured_lag += 0.5

print(measured_lag)
assert np.isclose((np.angle(ft1) - np.angle(ft0)) / 2 / np.pi, phlag, atol=0.01)
assert np.isclose((np.angle(ft1) - np.angle(ft0)) / 2 / np.pi, phlag, atol=0.02, rtol=0.02)


def test_lsft_slow_fast():
Expand Down
2 changes: 0 additions & 2 deletions stingray/tests/test_lombscargle.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ def test_valid_method(self, method):
@pytest.mark.parametrize(
"func_name",
[
"phase_lag",
"time_lag",
"classical_significances",
"from_time_array",
"from_events",
Expand Down

0 comments on commit bc23d38

Please sign in to comment.