Interpolation and Curve Fitting¶
Linear, cubic and spline interpolators, 2-D griddata and RBF interpolation, and curve_fit wrappers for exponential and Gaussian models.
linear_interpolate(x, y)
¶
Create a linear piecewise interpolation function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
1D array of x-coordinates (must be monotonically increasing). |
required |
y
|
array - like
|
1D array of y-values at each x point. |
required |
Returns:
| Type | Description |
|---|---|
interp1d
|
Callable interpolator. Raises ValueError outside the x range. |
Source code in utils/interpolation_utils.py
cubic_interpolate(x, y)
¶
Create a cubic spline interpolation function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
1D array of x-coordinates (must be monotonically increasing). |
required |
y
|
array - like
|
1D array of y-values at each x point. |
required |
Returns:
| Type | Description |
|---|---|
interp1d
|
Callable cubic interpolator. Smoother than linear but may oscillate. |
Source code in utils/interpolation_utils.py
spline_interpolate(x, y, s=0)
¶
Fit a univariate smoothing spline to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
1D array of x-coordinates. |
required |
y
|
array - like
|
1D array of y-values. |
required |
s
|
float
|
Smoothing factor. s=0 means the spline interpolates exactly. Larger s allows more deviation from data points for smoother fit. |
0
|
Returns:
| Type | Description |
|---|---|
UnivariateSpline
|
Callable spline object. |
Source code in utils/interpolation_utils.py
multivariate_interpolate(points, values, xi, method='linear')
¶
points: shape (n, 2) → input x, y
values: shape (n,) → values at those points
xi: query points for interpolation
Source code in utils/interpolation_utils.py
fit_curve(func, xdata, ydata, p0=None, maxfev=5000, bounds=(-np.inf, np.inf))
¶
Fits a parametric curve to data using scipy's curve_fit. Dynamically chooses p0 if not provided. Handles convergence errors.
Source code in utils/interpolation_utils.py
exponential_model(x, a, b, c)
¶
Exponential model: f(x) = a * exp(b * x) + c.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
Input values. |
required |
a
|
float
|
Amplitude scaling factor. |
required |
b
|
float
|
Exponential growth/decay rate. |
required |
c
|
float
|
Vertical offset. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Computed values. |
Source code in utils/interpolation_utils.py
gaussian_model(x, mu, sigma, A)
¶
Gaussian (normal bell curve) model: f(x) = A * exp(-(x - mu)^2 / (2 * sigma^2)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
Input values. |
required |
mu
|
float
|
Center (mean) of the Gaussian. |
required |
sigma
|
float
|
Width (standard deviation). |
required |
A
|
float
|
Peak amplitude. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Computed values. |
Source code in utils/interpolation_utils.py
safe_gaussian_fit(xdata, ydata, maxfev=10000)
¶
Performs a robust Gaussian curve fit with safe initial guesses and bounds. Returns popt, pcov.
Source code in utils/interpolation_utils.py
interpolate_2d(x, y, z, method='linear', grid_res=100)
¶
Performs 2D interpolation over scattered data points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
1D coordinates of known data points. |
required |
y
|
array - like
|
1D coordinates of known data points. |
required |
z
|
array - like
|
Values at data points (same length as x and y). |
required |
method
|
str
|
Interpolation method ('linear', 'nearest', 'cubic'). |
'linear'
|
grid_res
|
int
|
Resolution of the output grid. |
100
|
Returns:
| Type | Description |
|---|---|
grid_x, grid_y, grid_z : ndarray
|
Interpolated grid coordinates and interpolated values. |
Source code in utils/interpolation_utils.py
rbf_interpolation(x, y, z, function='multiquadric', grid_res=100)
¶
Performs RBF (Radial Basis Function) interpolation for 2D data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
1D coordinates of known data points. |
required |
y
|
array - like
|
1D coordinates of known data points. |
required |
z
|
array - like
|
Values at data points (same length as x and y). |
required |
function
|
str
|
Type of RBF kernel ('multiquadric', 'inverse', 'gaussian', etc.) |
'multiquadric'
|
grid_res
|
int
|
Resolution of the output grid. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
grid_z |
ndarray
|
Interpolated Z values on the grid. |