Skip to content

Commit

Permalink
update knee tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhammonds committed Feb 2, 2022
1 parent 503688d commit c548d0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion neurodsp/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
FREQS_ARR = np.array([5, 10, 15])
EXP1 = -1
EXP2 = -2
KNEE = 10**2
KNEE = 10

# Define settings for testing analyses
F_RANGE = (FREQ1-2, FREQ1+2)
Expand Down
15 changes: 9 additions & 6 deletions neurodsp/tests/sim/test_aperiodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,37 @@ def test_sim_knee():

# Build the signal and run a smoke test
sig = sim_knee(N_SECONDS, FS, EXP1, EXP2, KNEE)
check_sim_output(sig)
check_sim_output(sig, N_SECONDS, FS)

# Check against the power spectrum when you take the Fourier transform
sig_len = int(FS*N_SECONDS)
freqs = np.linspace(0, FS/2, num=sig_len//2, endpoint=True)

# Ignore the DC component to avoid division by zero in the Lorentzian
freqs = freqs[1:]
true_psd = np.array([1/(freq**-EXP1*(freq**(-EXP2-EXP1) + KNEE)) for freq in freqs])
true_psd = 1 / ((freqs ** -EXP1 * (freqs ** (-EXP2 - EXP1)+ KNEE**(-2*EXP1 - EXP2))))

# Only look at the frequencies (ignoring DC component) up to the nyquist rate
sig_hat = np.fft.fft(sig)[1:sig_len//2]
numerical_psd = np.abs(sig_hat)**2

np.allclose(true_psd, numerical_psd, atol=EPS)
scale = numerical_psd / true_psd

np.allclose(true_psd*scale, numerical_psd, atol=EPS)

# Accuracy test for a single exponent
sig = sim_knee(N_SECONDS, FS, 0, EXP2, KNEE)

freqs, powers = compute_spectrum(sig, FS, f_range=(1, 200))

def _estimate_single_knee(xs, offset, knee, exponent):
return np.zeros_like(xs) + offset - np.log10(xs**exponent + knee)
return np.zeros_like(xs) + offset - np.log10(xs**exponent + knee**exponent)

ap_params, _ = curve_fit(_estimate_single_knee, freqs, np.log10(powers))
_, _, EXP2_hat = ap_params[:]
_, KNEE_hat, EXP2_hat = ap_params[:]

assert -round(EXP2_hat) == EXP2
np.testing.assert_approx_equal(-EXP2_hat, EXP2, significant=1)
np.testing.assert_approx_equal(KNEE_hat, KNEE, significant=1)

def test_sim_random_walk():

Expand Down

0 comments on commit c548d0f

Please sign in to comment.