Skip to content

Conversation

@pavelkomarov
Copy link
Collaborator

extended the non-legacy smooth_finite_difference methods to have an axis parameter and support multidimensional data

…xis parameter and support multidimensional data
[(1, 0), (2, 2), (1, 0), (2, 2)],
[(1, 0), (3, 3), (1, 0), (3, 3)]],
spectraldiff: [[(-15, -15), (-14, -15), (0, -1), (0, 0)],
spectraldiff: [[(-15, -15), (-14, -14), (0, -1), (0, 0)],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spectraldiff uses convolution, which is now based on scipy's convolve1d rather than numpy's convolve, which changes its answers ever so slightly.

for i in range(num_iterations):
x_padded = np.pad(x_hat, pad_width, mode='symmetric') # pad with repetition of the edges
x_hat = np.convolve(x_padded, kernel, 'valid')[:len(x)] # 'valid' slices out only full-overlap spots
x_hat = convolve1d(x_hat, kernel, axis=axis, mode='reflect') # 'reflect' pads the signal with repeats
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is great. No more need to pad things ourselves nor carefully slice out the answer to ensure same-length.


# included code
from pynumdiff.finite_difference import second_order as finite_difference
from pynumdiff.finite_difference import finitediff
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why I wasn't doing this before I'm not sure.

x_hat = x
for _ in range(num_iterations):
x_hat = scipy.signal.medfilt(x_hat, window_size)
x_hat = scipy.signal.medfilt(x_hat, s)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medfilt has a slightly different mechanism for working with multidimensional data; you have to give a multidimensional kernel. Where that kernel has dimensional length 1, no filtering occurs.

```

where `x` is data, `dt` is a step size, and various keyword arguments control the behavior. Some methods support variable step size, in which case the second parameter is renamed `dt_or_t` and can receive either a constant step size or an array of values to denote sample locations.
where `x` is data, `dt` is a step size, and various keyword arguments control the behavior. Some methods support variable step size, in which case the second parameter is renamed `dt_or_t` and can receive either a constant step size or an array of values to denote sample locations. Some methods support multidimensional data, in which case there is an `axis` argument to control the dimension differentiated along.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling attention to the fact we support multidimensional data.

@pavelkomarov pavelkomarov merged commit 4cf2728 into master Dec 5, 2025
2 checks passed
@pavelkomarov pavelkomarov deleted the more-multi branch December 5, 2025 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants