Monotonic Interpolation in 2D - python

I would like to use scipy.interpolate.interp2d or scipy.interpolate.RectBivariateSpline to interpolate a 2D image, but would like for the interpolation to be monotonic.
Is there an equivalent function that assures monotonicity or a way to force interp2d or RectBivariateSpline to return monotonic interpolations?
I believe I am looking for something similar to PchipInterpolator, but for 2D (or n-dimensional).

The first question is: How do you define monotonicity in 2D (and in higher dimensions)? It seems to be not unique. I searched and found this paper: Piecewise polynomial monotonic interpolation of 2D gridded data (https://hal.inria.fr/hal-01059532/document). Maybe this helps.
The function RectBivariateSpline in scipy is wrapped around fitpack (as I remember, but maybe not). If that is the case, then it wouldn't be monotonic, it is b-spline-based.

Related

How to apply sgolay function(of matlab) in python?

I have a 1-D numpy array of positions(basically an eye-gaze data).
I want to apply sgolay function of matlab in python.
Though i have already seen scipy's savgol_filter and savgol_coeffs,i am not able to understand how to implement it as i am not well versed in it's mathematics.
I have also seen that there is a confusion about whether savgol_filter or savgol_coeffs of python is equivalent to matlab's sgolay function.
I want to differentiate the numpy array once to get velocity and then from velocity array differentiate it again to get acceleration.
The whole procedure is attached in this image

Apodization Mask for Fast Fourier Transforms in Python

I need to do a Fourier transform of a map in Python. Fast Fourier Transforms expect periodic boundary conditions, but the input map is not periodic. So I need to apply an input filter/weight slowly tapering the map toward zero at the edges. Are there libraries for doing this in python?
My favorite function to apodize a map is the generalized Gaussian (also called 'Super-Gaussian' which is a Gaussian whose exponent is raised to a power P. By setting P to, say, 4 or 6 you get a flat-top pulse which falls off smoothly, which is good for FFT applications where sharp edges always create ripples in conjugate space.
The generalized Gaussian is available on Scipy. Here is a minimal code (Python 3) to apodize a 2D array with a generalized Gaussian. As noted in previous comments, there are dozens of functions which would work just as well.
import numpy as np
from scipy.signal import general_gaussian
# A 128x128 array
array = np.random.rand(128,128)
# Define a general Gaussian in 2D as outer product of the function with itself
window = np.outer(general_gaussian(128,6,50),general_gaussian(128,6,50))
# Multiply
ap_array = window*array
Such tapering is often referred to as a "window".
Scipy has many window functions.
You can use numpy.expand_dims to create the 2D window you want.
Regarding Stefan's comment, apparently the numpy team thinks that including more than arrays was a mistake. I would stick to using scipy for signal processing. Watch out, as they moved quite a bit of functions around in their 1.0 release so older documentation is, well, quite old.
As a final note: a "filter" is typically reserved for multiplications you apply in the Frequency domain, not spatial domain.

Create 3D- polynomial via numpy etc. from given coordinates

Given some coordinates in 3D (x-, y- and z-axes), what I would like to do is to get a polynomial (fifth order). I know how to do it in 2D (for example just in x- and y-direction) via numpy. So my question is: Is it possible to do it also with the third (z-) axes?
Sorry if I missed a question somewhere.
Thank you.
Numpy has functions for multi-variable polynomial evaluation in the polynomial package -- polyval2d, polyval3d -- the problem is getting the coefficients. For fitting, you need the polyvander2d, polyvander3d functions that create the design matrices for the least squares fit. The multi-variable polynomial coefficients thus determined can then be reshaped and used in the corresponding evaluation functions. See the documentation for those functions for more details.

3d Trapz (or Simps) like Matlab

I have a 3D array that I need to integrate numerically using Python. My array is a function of wavelength, depth and time. It is data that I have modelled numerically using another software package and don't have an analytical form of the function, just the 3d array output from the other package. I need to find the triple integral of this array. In Matlab I use trapz(my_array, 3) where 3 is the ndims to integrate over. The Scipy trapz only seems to work on a single integral.
I think I may have 2 options but I am I need some advice.
opt 1. use 3d interpolation in scipy that returns a function handle, do these exist? the 1d version returns a function, and then use scipy.integrate.tplquad to do the integration over the interpolated function where I use the max and in values in my array as the integration limits.
opt 2. use three nested trapz calls like this suggestion for 2d I found on another site. --> sp.trapz(sp.trapz(f, y[np.newaxis,:], axis=1), x, axis=0))
Can't quite get my head around to make either work. Any help/advice would be appreciated. I need to make sure that my integration error is as low as possible.

Cubic interpolation with derivatives in numpy

A recent immigrant to Python and scientific computing with Python. This is a question to avoid any duplication of code that might already exist.
I have a field that is sampled as a function of x and y in a regular grid. I would want to interpolate the data to obtain not only the value of the field at any point on the grid, but also the first and second derivatives. If I interpolate it with bicubic interpolation using interp2d, I can obtain the value of the field.
Does anyone have a suggestion on how to obtain the first and second derivatives of the field using an EXISTING numpy or scipy function?
Thanks!
The scipy.interpolate.interp2d.__call__ method has options dx and dy to evaluate higher derivatives at point (at least since version 0.14.0).

Categories

Resources