promo.morph_utils.interpolation API documentation

# promo.morph_utils.interpolation

Created on Jun 29, 2016

@author: Tim

View Source

     1"""
     2Created on Jun 29, 2016
     3
     4@author: Tim
     5"""
     6
     7try:
     8    import numpy as np
     9except ImportError:
    10    hasNumpy = False
    11else:
    12    hasNumpy = True
    13
    14
    15def _numpyCheck():
    16    if not hasNumpy:
    17        raise ImportError(
    18            "Numpy required to do data interpolation. "
    19            "Install numpy or don't use data interpolation"
    20        )
    21
    22
    23def quadraticInterpolation(valueList2d, numDegrees, n, startTime=None, endTime=None):
    24    """
    25    Generates a series of points on a smooth curve that cross the given points
    26
    27    numDegrees - the degrees of the fitted polynomial
    28               - the curve gets weird if this value is too high for the input
    29    n - number of points to output
    30    startTime/endTime/n - n points will be generated at evenly spaced
    31                          intervals between startTime and endTime
    32    """
    33    _numpyCheck()
    34
    35    x, y = zip(*valueList2d)
    36
    37    if startTime is None:
    38        startTime = x[0]
    39    if endTime is None:
    40        endTime = x[-1]
    41
    42    polyFunc = np.poly1d(np.polyfit(x, y, numDegrees))
    43
    44    newX = np.linspace(startTime, endTime, n)
    45
    46    retList = [(n, polyFunc(n)) for n in newX]
    47
    48    return retList

def quadraticInterpolation(valueList2d, numDegrees, n, startTime=None, endTime=None): View Source

    24def quadraticInterpolation(valueList2d, numDegrees, n, startTime=None, endTime=None):
    25    """
    26    Generates a series of points on a smooth curve that cross the given points
    27
    28    numDegrees - the degrees of the fitted polynomial
    29               - the curve gets weird if this value is too high for the input
    30    n - number of points to output
    31    startTime/endTime/n - n points will be generated at evenly spaced
    32                          intervals between startTime and endTime
    33    """
    34    _numpyCheck()
    35
    36    x, y = zip(*valueList2d)
    37
    38    if startTime is None:
    39        startTime = x[0]
    40    if endTime is None:
    41        endTime = x[-1]
    42
    43    polyFunc = np.poly1d(np.polyfit(x, y, numDegrees))
    44
    45    newX = np.linspace(startTime, endTime, n)
    46
    47    retList = [(n, polyFunc(n)) for n in newX]
    48
    49    return retList

Generates a series of points on a smooth curve that cross the given points

numDegrees - the degrees of the fitted polynomial - the curve gets weird if this value is too high for the input n - number of points to output startTime/endTime/n - n points will be generated at evenly spaced intervals between startTime and endTime