Skip to content

Commit 35b594c

Browse files
pavelkomarovclaude
andcommitted
Fix spectraldiff multidim: padding bug, dead code, docstring cleanup
- Fix bug where xpad was built but x0 was never reassigned, so pad_to_zero_dxdt had no effect on the signal fed to FFT - Remove leftover 1D padding code (pre/post/hstack on original x) - Add comment explaining filt reshape is for broadcasting - Fix docstring blank line in pad_to_zero_dxdt param description - Fix rbfdiff docstring :math: continuation indentation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1797c96 commit 35b594c

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

pynumdiff/basis_fit.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def spectraldiff(x, dt, params=None, options=None, high_freq_cutoff=None,
1818
and 1. Frequencies below this threshold will be kept, and above will be zeroed.
1919
:param bool even_extension: if True, extend the data with an even extension so signal starts and ends at the same value.
2020
:param bool pad_to_zero_dxdt: if True, extend the data with extra regions that smoothly force the derivative to
21-
2221
zero before taking FFT.
2322
2423
:return: - **x_hat** (np.array) -- estimated (smoothed) x
@@ -42,17 +41,9 @@ def spectraldiff(x, dt, params=None, options=None, high_freq_cutoff=None,
4241
# Make derivative go to zero at the ends (optional):
4342
if pad_to_zero_dxdt:
4443
padding = 100
45-
pre = x[0] * np.ones(padding)
46-
post = x[-1] * np.ones(padding)
47-
x = np.hstack((pre, x, post)) # extend the edges
48-
49-
# Pad first and last values x100
50-
first = x0[0:1]
51-
last = x0[-1:]
52-
pre = np.repeat(first, padding, axis=0)
53-
post = np.repeat(last, padding, axis=0)
54-
55-
xpad = np.concatenate((pre, x0, post), axis=0) # concatenate along axis 0
44+
pre = np.repeat(x0[0:1], padding, axis=0)
45+
post = np.repeat(x0[-1:], padding, axis=0)
46+
x0 = np.concatenate((pre, x0, post), axis=0) # np.concatenate works along any axis, unlike hstack/vstack
5647
else:
5748
padding = 0
5849

@@ -70,7 +61,7 @@ def spectraldiff(x, dt, params=None, options=None, high_freq_cutoff=None,
7061

7162
filt = np.ones(k.shape) # start with all frequencies passing
7263
filt[discrete_cutoff:-discrete_cutoff] = 0 # zero out high-frequency components
73-
filt = filt.reshape((N,) + (1,)*(x0.ndim-1))
64+
filt = filt.reshape((N,) + (1,)*(x0.ndim-1)) # reshape for broadcasting over extra dimensions
7465

7566
# Smoothed signal
7667
X = np.fft.fft(x0, axis=0)
@@ -98,7 +89,7 @@ def rbfdiff(x, dt_or_t, sigma=1, lmbd=0.01):
9889
9990
:param np.array[float] x: data to differentiate
10091
:param float or array[float] dt_or_t: This function supports variable step size. This parameter is either the constant
101-
:math:`\\Delta t` if given as a single float, or data locations if given as an array of same length as :code:`x`.
92+
:math:`\\Delta t` if given as a single float, or data locations if given as an array of same length as :code:`x`.
10293
:param float sigma: controls width of radial basis functions
10394
:param float lmbd: controls smoothness
10495

0 commit comments

Comments
 (0)