Skip to content

Commit f3f74d8

Browse files
committed
add jacobian funcs
1 parent e89aa35 commit f3f74d8

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

fooof/core/jacobians.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
""""Functions for computing Jacobian matrices to be used during fitting."""
2+
3+
import numpy as np
4+
5+
###################################################################################################
6+
###################################################################################################
7+
8+
## Periodic fit functions
9+
10+
def jacobian_gauss(xs, *params):
11+
"""Create the Jacobian matrix for the Guassian function."""
12+
13+
jacobians = []
14+
for a, b, c in zip(*[iter(params)] * 3):
15+
16+
sub = b * np.exp((-(((-a + xs)**2) / (2 * c**2))))
17+
18+
jacobian = np.hstack([
19+
(sub * (-a + xs) / c**2).reshape(-1, 1),
20+
np.exp(-(-a + xs)**2 / (2 * c**2)).reshape(-1, 1),
21+
(sub * (-a + xs)**2 / c**3).reshape(-1, 1),
22+
])
23+
jacobians.append(jacobian)
24+
25+
return np.hstack(jacobians)
26+
27+
28+
## Aperiodic fit functions
29+
30+
def jacobian_expo(xs, *params):
31+
"""Create the Jacobian matrix for the exponential function."""
32+
33+
a, b, c = params
34+
jacobian = np.hstack([
35+
np.ones([len(xs), 1]),
36+
- (1 / (b + xs**c)).reshape(-1, 1),
37+
-((xs**c * np.log10(xs)) / (b + xs**c)).reshape(-1, 1),
38+
])
39+
40+
return jacobian
41+
42+
43+
def jacobian_expo_nk(xs, *params):
44+
"""Create the Jacobian matrix for the exponential no-knee function."""
45+
46+
jacobian = np.hstack([
47+
np.ones([len(xs), 1]),
48+
(-np.log10(xs) / np.log10(10)).reshape(-1, 1),
49+
])
50+
51+
return jacobian

fooof/tests/core/test_jacobians.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Tests for fooof.core.jacobians."""
2+
3+
4+
from fooof.core.jacobians import *
5+
6+
###################################################################################################
7+
###################################################################################################
8+
9+
def test_jacobian_gauss():
10+
11+
xs = np.arange(1, 100)
12+
ctr, hgt, wid = 50, 5, 10
13+
14+
jacobian = jacobian_gauss(xs, ctr, hgt, wid)
15+
assert isinstance(jacobian, np.ndarray)
16+
assert jacobian.shape == (len(xs), 3)
17+
18+
def test_jacobian_expo():
19+
20+
xs = np.arange(1, 100)
21+
off, knee, exp = 10, 5, 2
22+
23+
jacobian = jacobian_expo(xs, off, knee, exp)
24+
assert isinstance(jacobian, np.ndarray)
25+
assert jacobian.shape == (len(xs), 3)
26+
27+
def test_jacobian_expo_nk():
28+
29+
xs = np.arange(1, 100)
30+
off, exp = 10, 2
31+
32+
jacobian = jacobian_expo_nk(xs, off, exp)
33+
assert isinstance(jacobian, np.ndarray)
34+
assert jacobian.shape == (len(xs), 2)

0 commit comments

Comments
 (0)