Source code for spm1d.util._proc


'''
General processing utilities.
'''

# Copyright (C) 2026  Todd Pataky


import numpy as np


def interp(y, Q=101):
    '''
    Deprecated. Use the registration1d package instead.

    Linear interpolation to *Q* values.

    :Parameters:

    - *y* --- 1D array or list of J separate 1D arrays
    - *Q* --- number of nodes in the interpolated continuum

    :Returns:

    - Q-component 1D array or a (J,Q) array

    :Example:

    >>> Y  = spm1d.util.interp([y0, y1, y2], Q=101)
    '''
    import warnings
    from .. exceptions import SPM1DDeprecationWarning
    warnings.warn( '"spm1d.util.interp" is deprecated and will be removed from a future version of spm1d. Please use the registration1d package: https://github.com/spm1d-org/registration1d', SPM1DDeprecationWarning, stacklevel=2 )
    return _interp( y, Q )


def _interp(y, Q):
    if isinstance(y, (list, tuple)) and (len(y) > 0) and (not np.isscalar(y[0])):
        #  A list of continua of possibly unequal lengths.
        return np.asarray( [_interp(yy, Q)   for yy in y] )
    y          = np.asarray(y)
    if y.ndim==2:
        return np.asarray( [_interp(yy, Q)   for yy in y] )
    else:
        x0     = range(y.size)
        x1     = np.linspace(0, y.size, Q)
        return np.interp(x1, x0, y, left=None, right=None)
        
        
        


[docs] def smooth(Y, fwhm=5.0): ''' Smooth a set of 1D continua with a Gaussian kernel. Uses **scipy.ndimage.gaussian_filter1d** with kernel width specified by *fwhm*. :Parameters: - *Y* --- (J,Q) numpy array - *fwhm* --- Gaussian kernel full-width at half-maximum. :Returns: - (J,Q) numpy array :Example: >>> Y0 = np.random.rand(5, 101) >>> Y = spm1d.util.smooth(Y0, fwhm=10.0) .. note:: A Gaussian kernel's *fwhm* is related to its standard deviation (*sd*) as follows: >>> fwhm = sd * np.sqrt( 8 * np.log(2) ) ''' from math import sqrt,log from scipy.ndimage import gaussian_filter1d sd = fwhm / sqrt(8*log(2)) return gaussian_filter1d(Y, sd, mode='wrap')