Example #1
0
def rbfinterp(d,s,p,rbf,**rbfparms):
    """
    yp = rbfinterp(data, surface, evaluation points, rbf, *rbfparms)
    
    Use Radial Basis Functions (rbf) to interpolate using Infinitely Smooth
    RBF or Polyharmonic Spline (PHS)
    
    Arguments:
                    (TYPE ARRAY SHAPE N, 2)
    d = data                                            array shape (N,2)
    s = surface (curve) to be interpolated              array shape (N,1)
    p = evaluation points (s is interpolated)           array shape (M,2)
    *rbfparms = ep', 'm'
          ep = shape parameter for RBFs                scalar / list shape (N,)
          m  = exponent for polyharmonic spline (PHS)  scalar
    
    Output:
    yp = surface interpolated at the evaluation points  array shape (M,1)
    """
    # Construct the collocation matrices:
    # ep = shape parameter
    ep = rbfparms.get('shapeparm')
    #  m = power for PHS
    m = rbfparms.get('power')
    
    zoo = {
        'linear': linear,
        'phs': phs,
        'mq': mq,
        'gauss': gauss
        }
    if rbf in zoo:
        rbf = zoo[rbf]
    else:
        raise NameError('RBF not known.')
    
    # IM = interpolation matrix
    IM = rbf(d, shapeparm = ep, power = m)
    # EM = evaluation matrix
    EM = rbf(p, centers = d, shapeparm = ep, power = m)
    #***************************************************************************
    # Linear Algebra Remarks:
    # 
    # P*w = s is a system of equations where the coefficients, w, are unknown
    # This matrix system is called the "collocation problem," i.e. What weights
    # are needed so that a linear combination of basis functions and weights
    # yeilds a point on the surface?
    # Once the weights, w, are determined, they can be used to construct the
    # interpolant.
    #
    # Summary:
    # P*w = s => w = inv(P)*s
    # EM*w = yp where yp is the interpolant and EM is the matrix with entries
    # that are known basis functions at the evaluation points.
    #
    # Since w = inv(P)*s, EM*w = yp => EM*inv(P)*s
    #
    return dot(EM,linalg.solve(IM,s))
Example #2
0
def gauss(d,**parms):
    # gaussian, f(r) = exp(-(ep r)^2)
    c = parms.get('centers')
    #op = parms.get('operator','interp')
    ep = parms.get('shapeparm',1)
    DM = dmatrix(d, centers = c)
    # eps_r = epsilon*r where epsilon may be an array or scalar
    eps_r = dot(ep*eye(DM.shape[0]),DM)
    return exp(-(eps_r)**2)
Example #3
0
def mq(d,**parms):
    # multiquadric, f(r) = sqrt(1 + (ep r)^2)
    c = parms.get('centers')
    #op = parms.get('operator','interp')
    ep = parms.get('shapeparm',1)
    DM = dmatrix(d, centers = c)
    # eps_r = epsilon*r where epsilon may be an array or scalar
    eps_r = dot(ep*eye(DM.shape[0]),DM)
    return sqrt(1+(eps_r)**2)
Example #4
0
    def _icqft(self, V_hat, **kwargs):
        """
        ::

            Inverse constant-Q Fourier transform. Make a signal from a constant-Q transform.
        """
        if not self._have_cqft:
            return None
        fp = self._check_feature_params()
        X_hat = P.dot(self.Q.T, V_hat)
        if self.verbosity:
            print("iCQFT->X_hat")
        self._istftm(X_hat, **kwargs)
        return self.x_hat
Example #5
0
    def _mfcc(self):
        """
        ::

            DCT of the Log magnitude CQFT
        """
        fp = self._check_feature_params()
        if not self._cqft():
            return False
        self._make_dct()
        AA = P.log10(P.clip(self.CQFT, 0.0001, self.CQFT.max()))
        self.MFCC = P.dot(self.DCT, AA)
        self._have_mfcc = True
        if self.verbosity:
            print("Extracted MFCC: lcoef=%d, ncoef=%d, intensified=%d" %
                  (self.lcoef, self.ncoef, self.intensify))
        n = self.ncoef
        l = self.lcoef
        self.X = self.MFCC[l:l + n, :]
        return True
Example #6
0
    def _hcqft(self):
        """
        ::

            Apply high lifter to MFCC and invert to CQFT domain
        """
        fp = self._check_feature_params()
        if not self._mfcc():
            return False
        a, b = self.CQFT.shape
        n = self.ncoef
        l = self.lcoef
        AA = self.MFCC[n + l:a, :]  # apply Lifter
        self.HCQFT = 10**P.dot(self.DCT[n + l:a, :].T, AA)
        self._have_hcqft = True
        if self.verbosity:
            print("Extracted HCQFT: lcoef=%d, ncoef=%d, intensified=%d" %
                  (self.lcoef, self.ncoef, self.intensify))
        self.inverse = self.icqft
        self.X = self.HCQFT
        return True
print("Calculating error stats...")
ah = 8
a = y.reshape(-1)[:-ah]
ax = x.reshape(-1)
print("VARIANCE")
print(P.var(a))
print("STD")
print(P.std(a))
A = P.vstack([a[i:(i - ah)] for i in range(ah)])
AX = P.vstack([ax[i:(i - 2 * ah)] for i in range(2 * ah)])
A = P.vstack([A, AX, ax[2 * ah:]])
b = a[ah:]
A = A - P.mean(A, 1).reshape(-1, 1)
b = b - P.mean(b)
print("LMMSE with {0} taps".format(ah))
LMMSE = P.mean((b - A.T.dot(P.inv(P.dot(A, A.T)).dot(A.dot(b))))**2)
print(LMMSE)
print("LMRMSE with {0} taps".format(ah))
print(P.sqrt(LMMSE))


def mu_law(a, mu=256, MAX=None):
    mu = mu - 1
    a = P.array(a)
    MAX = a.max() if MAX is None else MAX
    a = a / MAX
    y = (1 + P.sign(a) * P.log(1 + mu * abs(a)) / P.log(1 + mu)) / 2
    inds = P.around(y * mu).astype(P.uint8)
    return inds