Example #1
0
def lpc_ref(signal, order):
    """ Return the order + 1 LPC coefficients 
    for the signal. This is just for reference, as it is using
    the direct inversion of the toeplitz matrix, which is really slow"""
    if signal.ndim > 1:
        print "Warning, not tested for rank > 1"
    p   = order + 1
    r   = autocorr_oneside_nofft(signal, order) / signal.shape[0]
    return _N.concatenate(([1.],  _N.dot(inv(toeplitz(r[:-1])), -r[1:])))
Example #2
0
def lpc2(signal, order, axis = -1):
    """ Returns ar coeff, err and k coeff"""
    sz      = signal.shape[axis]
    if order >= sz:
        raise RuntimeError("order should be strictly smaller than the length of signal")

    # rank 1
    if signal.ndim < 2:
        if signal.dtype == _N.float64:
            coeff   = _N.zeros((order+1), _N.float64)
            kcoeff  = _N.zeros((order), _N.float64)
            err     = _N.zeros((1), _N.float64)
            st  = _lpc.dbl_lpc(signal, signal.size, order, coeff, kcoeff, err)
        elif signal.dtype == _N.float32:
            coeff   = _N.zeros((order+1), _N.float32)
            kcoeff  = _N.zeros((order), _N.float32)
            err     = _N.zeros((1), _N.float32)
            st  = _lpc.flt_lpc(signal, signal.size, order, coeff, kcoeff, err)
        else:
            raise TypeError("Sorry, only float32 and float64 supported")

        if not (st == 0):
            raise RuntimeError("Error while using levinson algo, returns err is %d", st)

        return coeff, err, kcoeff
    # rank 2
    elif signal.ndim == 2:
        # Compute biased autocorrelation up to lag = order
        bias    = 1. / sz
        corr    = bias * autocorr_oneside_nofft(signal, order, axis)

        if axis  % 2 == 0:
            # we transpose to have a major row order
            icorr   = corr.T
            icorr   = _N.require(icorr, requirements = 'C')
            at      = _N.zeros((icorr.shape[0], order+1), icorr.dtype)
            kt      = _N.zeros((icorr.shape[0], order), icorr.dtype)
            et      = _N.zeros(icorr.shape[0], icorr.dtype)

            if icorr.dtype == _N.float64:
                _lpc.dbl_levinson2d(icorr, icorr.shape[0], icorr.shape[1], at, et, kt)
            elif icorr.dtype == _N.float32:
                _lpc.flt_levinson2d(icorr, icorr.shape[0], icorr.shape[1], at, et, kt)
            else:
                raise TypeError("Only float32 and float64 supported")

            return at.T, et.T, kt.T
        elif axis % 2 == 1:
            icorr   = _N.require(corr, requirements = 'C')
            at      = _N.zeros((icorr.shape[0], order+1), icorr.dtype)
            kt      = _N.zeros((icorr.shape[0], order), icorr.dtype)
            et      = _N.zeros(icorr.shape[0], icorr.dtype)

            if icorr.dtype == _N.float64:
                _lpc.dbl_levinson2d(icorr, icorr.shape[0], icorr.shape[1], at, et, kt)
            elif icorr.dtype == _N.float32:
                _lpc.flt_levinson2d(icorr, icorr.shape[0], icorr.shape[1], at, et, kt)
            else:
                raise TypeError("Only float32 and float64 supported")

            return at, et, kt
        else:
            raise RuntimeError("This should not happen, this is a bug")
    else:
        raise RuntimeError("Sorry, only rank <= 2 supported for now")