Math Utilities

Useful math functions for LWA work.

lsl.misc.mathutil.regrid(x, y, newx, allow_extrapolation=False, method='spline')

Regrid data from x,y onto newx. If allow_extrapolation is True, extrapolation is attempted if the method supports it. Supported methods are:

  • linear
  • spline

Use of this function may require the scipy extension package.

lsl.misc.mathutil.downsample(vector, factor, rescale=True)

Downsample (i.e. co-add consecutive numbers) a vector by an integer factor. Trims the input timeseries to be a multiple of the downsample factor, if needed. If rescale == True, then divides each sum by factor to produce a mean value, otherwise just adds the values in the vector.

lsl.misc.mathutil.smooth(x, window_len=10, window='hanning')

Smooth the data using a window with requested size. Stolen from SciPy Cookbook at http://www.scipy.org/Cookbook/SignalSmooth

This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the begining and end part of the output signal.

Input:
  • x: the input signal
  • window_len: the dimension of the smoothing window
  • window: the type of window from ‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman’ flat window will produce a moving average smoothing.
Output:
  • the smoothed signal
Example:
>>> from numpy import *
>>> t=linspace(-2,2,0.1)
>>> x=sin(t)+randn(len(t))*0.1
>>> y=smooth(x)

See also

numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve scipy.signal.lfilter

TODO: the window parameter could be the window itself if an array instead of a string

lsl.misc.mathutil.cmagnitude(cmplx)

Return the polar magnitudes of complex values.

lsl.misc.mathutil.cphase(cmplx)

Return the polar phases of complex values as radians.

lsl.misc.mathutil.cpolar(cmplx)

Return the polar (magnitude, phase) representation of complex values (real, imaginary). The return value is an array of shape (N,2), where N is the length of the cmplx input array.

lsl.misc.mathutil.crect(cmplx)

Return the rectilinear (real, imaginary) representation of complex values (magnitude, phase).

lsl.misc.mathutil.creal(cmplx)

Return the real rectilinear component from complex values expressed in polar form (magnitude, phase).

lsl.misc.mathutil.cimag(cmplx)

Return the imaginary rectilinear component from complex values expressed in polar form (magnitude, phase).

lsl.misc.mathutil.to_dB(factor)

Convert from linear units to decibels.

lsl.misc.mathutil.from_dB(dB)

Convert from decibels to linear units.

lsl.misc.mathutil.ndft(t, x)

Given a list of times and a list of data values, compute a non-uniform discrete Fourier transform (NDFT) of the data. Returns a two element tuple of frequency and the complex NDFT result.

lsl.misc.mathutil.savitzky_golay(y, window_size, order, deriv=0)

Smooth (and optionally differentiate) data with a Savitzky-Golay filter. The Savitzky-Golay filter removes high frequency noise from data. It has the advantage of preserving the original shape and features of the signal better than other types of filtering approaches, such as moving averages techhniques.

Parameters:
  • y: array_like, shape (N,) = the values of the time history of the signal.
  • window_size: int = the length of the window. Must be an odd integer number.
  • order: int = the order of the polynomial used in the filtering. Must be less then window_size - 1.
  • deriv: int = the order of the derivative to compute (default = 0 means only smoothing)
Returns:
  • ys: ndarray, shape (N) = the smoothed signal (or it’s n-th derivative).

Notes: The Savitzky-Golay is a type of low-pass filter, particularly suited for smoothing noisy data. The main idea behind this approach is to make for each point a least-square fit with a polynomial of high order over a odd-sized window centered at the point.

Examples:
>>> t = numpy.linspace(-4, 4, 500)
>>> y = numpy.exp( -t**2 ) + numpy.random.normal(0, 0.05, t.shape)
>>> ysg = savitzky_golay(y, window_size=31, order=4)
>>> import matplotlib.pyplot as plt
>>> plt.plot(t, y, label='Noisy signal')
>>> plt.plot(t, numpy.exp(-t**2), 'k', lw=1.5, label='Original signal')
>>> plt.plot(t, ysg, 'r', label='Filtered signal')
>>> plt.legend()
>>> plt.show()
References:
  1. A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of Data by Simplified Least Squares Procedures. Analytical Chemistry, 1964, 36 (8), pp 1627-1639.
  2. Numerical Recipes 3rd Edition: The Art of Scientific Computing W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery Cambridge University Press ISBN-13: 9780521880688
lsl.misc.mathutil.gaussian1d(height, center, width)

Return a function that generates a 1-D gaussian with the specified height, mean, and standard deviation.

Example:
>>> height = 1
>>> center = 5.0
>>> width = 2.1
>>> gauFnc = guassian1d(height, center, width)
>>> value = gauFnc(numpy.arange(0, 100))

Based on: http://code.google.com/p/agpy/source/browse/trunk/agpy/gaussfitter.py

lsl.misc.mathutil.gaussian2d(height, centerX, centerY, widthMaj, widthMin, angle=0.0)

Return a function that generates a 2-D gaussian with the specified height, mean (for both X and Y), standard deviation (for both major and minor axes), and rotation angle from the X axis in degrees.

Based on: http://code.google.com/p/agpy/source/browse/trunk/agpy/gaussfitter.py

lsl.misc.mathutil.gaussparams(data, x=None, y=None)

Estimate the parameters (height, center, width) for a gaussian. The return order is:

1-D: height, center, width 2-D: height, center x, center y, width x, width y, position angle

Note

The 2-D fits always return a position angle of zero since the routine decomposes the process into two 1-D fits.

lsl.misc.mathutil.sphfit(az, alt, data, lmax=5, degrees=False, realOnly=False)

Decompose a spherical or semi-spherical data set into spherical harmonics.

Inputs:
  • az: 2-D numpy array of azimuth coordinates in radians or degrees if the

degrees keyword is set * alt: 2-D numpy array of altitude coordinates in radian or degrees if the degrees keyword is set * data: 2-D numpy array of the data to be fit. If the data array is purely real, then the realOnly keyword can be set which speeds up the decomposition * lmax: integer setting the maximum order harmonic to fit

Keywords:
  • degrees: boolean of whether or not the input azimuth and altitude coordinates

are in degrees or not * realOnly: boolean of whether or not the input data is purely real or not. If the data are real, only coefficients for modes >=0 are computed.

Returned is a 1-D complex numpy array with the spherical harmonic coefficients packed packed in order of increasing harmonic order and increasing mode, i.e., (0,0), (1,-1), (1,0), (1,1), (2,-2), etc. If the realOnly keyword has been set, the negative coefficients for the negative modes are excluded from the output array.

Note

sphfit was designed to fit the LWA dipole response pattern as a function of azimuth and elevation. Elevation angles are mapped to theta angles by adding pi/2 so that an elevation of 90 degrees corresponds to a theta of 180 degrees. To fit in terms of spherical coordianates, subtract pi/2 from the theta values before running.

lsl.misc.mathutil.sphval(terms, az, alt, degrees=False, realOnly=False)

Evaluate a set of spherical harmonic coefficents at a specified set of azimuth and altitude coordinates.

Inputs:
  • terms: 1-D complex numpy array, typically from sphfit
  • az: : 2-D numpy array of azimuth coordinates in radians or degrees if the

degrees keyword is set * alt: 2-D numpy array of altitude coordinates in radian or degrees if the degrees keyword is set

Keywords:
  • degrees: boolean of whether or not the input azimuth and altitude coordinates

are in degrees or not * realOnly: boolean of whether or not the input data is purely real or not. If the data are real, only coefficients for modes >=0 are computed.

Returns a 2-D numpy array of the harmoics evalated and summed at the given coordinates.

Note

sphfit was designed to fit the LWA dipole response pattern as a function of azimuth and elevation. Elevation angles are mapped to theta angles by adding pi/2 so that an elevation of 90 degrees corresponds to a theta of 180 degrees. To spherical harmonics in terms of spherical coordianates, subtract pi/2 from the theta values before running.

Previous topic

Time and Position Transformation

Next topic

Robust Statistics

This Page