Math Utilities

Useful math functions for LWA work.

lsl.misc.mathutils.cimag(cmplx)

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

lsl.misc.mathutils.cmagnitude(cmplx)

Return the polar magnitudes of complex values.

lsl.misc.mathutils.cphase(cmplx)

Return the polar phases of complex values as radians.

lsl.misc.mathutils.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.mathutils.creal(cmplx)

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

lsl.misc.mathutils.crect(cmplx)

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

lsl.misc.mathutils.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.mathutils.from_dB(dB)

Convert from decibels to linear units.

lsl.misc.mathutils.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.mathutils.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.mathutils.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.mathutils.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.mathutils.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.mathutils.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.mathutils.sphfit(az, alt, data, lmax=5, degrees=False, real_only=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 real_only 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

  • real_only: 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 real_only 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.mathutils.sphval(terms, az, alt, degrees=False, real_only=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

  • real_only: 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.

lsl.misc.mathutils.to_dB(factor)

Convert from linear units to decibels.