Skip to content

Commit 5aa0837

Browse files
committed
optimize gaussian jacobian
1 parent 438d73a commit 5aa0837

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

fooof/core/jacobians.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ def jacobian_gauss(xs, *params):
3030
Jacobian matrix, with shape [len(xs), n_params].
3131
"""
3232

33-
jacobians = []
34-
for a, b, c in zip(*[iter(params)] * 3):
33+
jacobians = np.zeros((len(xs), len(params)))
3534

36-
sub = b * np.exp((-(((-a + xs)**2) / (2 * c**2))))
35+
for i, (a, b, c) in enumerate(zip(*[iter(params)] * 3)):
3736

38-
jacobian = np.hstack([
39-
(sub * (-a + xs) / c**2).reshape(-1, 1),
40-
np.exp(-(-a + xs)**2 / (2 * c**2)).reshape(-1, 1),
41-
(sub * (-a + xs)**2 / c**3).reshape(-1, 1),
42-
])
43-
jacobians.append(jacobian)
37+
ax = -a + xs
38+
ax2 = ax**2
4439

45-
return np.hstack(jacobians)
40+
c2 = c**2
41+
c3 = c**3
42+
43+
exp = np.exp(-ax2 / (2 * c2))
44+
exp_b = exp * b
45+
46+
ii = i * 3
47+
jacobians[:, ii] = (exp_b * ax) / c2
48+
jacobians[:, ii+1] = exp
49+
jacobians[:, ii+2] = (exp_b * ax2) / c3
50+
51+
return jacobians
4652

4753

4854
## Aperiodic fit functions

0 commit comments

Comments
 (0)