def gen_roots_and_weights(n, an_func, sqrt_bn_func, mu): """[x,w] = gen_roots_and_weights(n,an_func,sqrt_bn_func,mu) Returns the roots (x) of an nth order orthogonal polynomial, and weights (w) to use in appropriate Gaussian quadrature with that orthogonal polynomial. The polynomials have the recurrence relation P_n+1(x) = (x - A_n) P_n(x) - B_n P_n-1(x) an_func(n) should return A_n sqrt_bn_func(n) should return sqrt(B_n) mu ( = h_0 ) is the integral of the weight over the orthogonal interval """ nn = np.arange(1.0,n) sqrt_bn = sqrt_bn_func(nn) an = an_func(np.concatenate(([0], nn))) x, v = eig((np.diagflat(an) + np.diagflat(sqrt_bn,1) + np.diagflat(sqrt_bn,-1))) answer = [] sortind = x.real.argsort() answer.append(x[sortind]) answer.append((mu*v[0]**2)[sortind]) return answer
def gaussian_sample(mu, covar, nsamp): """ %GSAMP Sample from a Gaussian distribution. % % Description % % X = GSAMP(MU, COVAR, NSAMP) generates a sample of size NSAMP from a % D-dimensional Gaussian distribution. The Gaussian density has mean % vector MU and covariance matrix COVAR, and the matrix X has NSAMP % rows in which each row represents a D-dimensional sample vector. % % See also % GAUSS, DEMGAUSS % % Copyright (c) Ian T Nabney (1996-2001) """ d = covar.shape[0] mu = np.reshape(mu, (1, d)) # Ensure that mu is a row vector eigval, evec = eig(covar) eigval = np.diag(eigval) coeffs = np.dot(np.random.randn(nsamp, d), np.sqrt(eigval)) x = np.dot(np.ones((nsamp, 1)), mu) + np.dot(coeffs, evec.T) return x
def XYchange(ob): for k in range(0,ob.nombreCapteurs): kk=k+1 #index for sensor references t=array([ob.accelerationSurCapteur(kk).projectionSurX()]+[ob.accelerationSurCapteur(kk).projectionSurY()]) u=array([ob.gyrometrieSurCapteur(kk).projectionSurX()]+[ob.gyrometrieSurCapteur(kk).projectionSurY()]) v=array([ob.magneticsSurCapteur(kk).projectionSurX()]+[ob.magneticsSurCapteur(kk).projectionSurY()]) m=cov(t) temp=eig(m) p=temp[1] #p is the transformation matrix d=temp[0] #d is the diagonal matrix if d[0]<d[1]: #if X has a lower variance than Y in the new referential, then we inverse X and Y roles t=array([ob.accelerationSurCapteur(kk).projectionSurY()]+[ob.accelerationSurCapteur(kk).projectionSurX()]) u=array([ob.gyrometrieSurCapteur(kk).projectionSurY()]+[ob.gyrometrieSurCapteur(kk).projectionSurX()]) v=array([ob.magneticsSurCapteur(kk).projectionSurY()]+[ob.magneticsSurCapteur(kk).projectionSurX()]) m=cov(t) p=eig(m)[1] p=matrix(p).getI() ob.accelerationSurCapteur(kk).tableauDonnees=array(dot(p,t).tolist()+[ob.accelerationSurCapteur(kk).projectionSurZ()]).transpose().tolist() ob.gyrometrieSurCapteur(kk).tableauDonnees=array(dot(p,u).tolist()+[ob.gyrometrieSurCapteur(kk).projectionSurZ()]).transpose().tolist() ob.magneticsSurCapteur(kk).tableauDonnees=array(dot(p,v).tolist()+[ob.magneticsSurCapteur(kk).projectionSurZ()]).transpose().tolist() return(ob)
def trainKFD(trainKernel, trainLabels): classKernels = getClassKernels(trainKernel, trainLabels) M = calcM(classKernels, trainLabels) N = calcN(classKernels, trainLabels) ''' print "train kernel:",trainKernel print "Class kernels:", classKernels print "M",M print "N",N ''' try: solutionMatrix = dot(inv(N), M) except LinAlgError: #if we get a singular matrix here, there isn't much we can do about it #just skip this configuration solutionMatrix = identity(N.shape[0], Float64) solutionMatrix = nan_to_num(solutionMatrix) eVals, eVects = eig(solutionMatrix) #find the 'leading' term i.e. find the eigenvector with the highest eigenvalue alphaVect = eVects[:, absolute(eVals).argmax()].real.astype(Float64) trainProjections = dot(trainKernel, alphaVect) ''' print 'alpha = ', alphaVect print 'train kernel = ', trainKernel print 'train projction = ', trainProjections ''' #train sigmoid based on evaluation accuracy #accuracyError = lambda x: 100.0 - evaluations(trainLabels, classifyKFDValues(trainProjections, *list(x)))[0] accuracyError = lambda x: 100.0 - evaluations(trainLabels, classifyKFDValues(trainProjections, *x))[0] #get an initial guess by brute force #ranges = ((-100, 100, 1), (-100, 100, 1)) #x0 = brute(accuracyError, ranges) #popt = minimize(accuracyError, x0.tolist(), method="Powell").x rc = LSFAIL niter = 0 i = 0 while rc in (LSFAIL, INFEASIBLE, CONSTANT, NOPROGRESS, USERABORT, MAXFUN) or niter <= 1: if i == 10: break #get a 'smarter' x0 #ranges = ((-1000, 1000, 100), (-1000, 1000, 100)) ranges = ((-10**(i + 1), 10**(i + 1), 10**i),) * 2 x0 = brute(accuracyError, ranges) (popt, niter, rc) = fmin_tnc(accuracyError, x0, approx_grad=True) #popt = fmin_tnc(accuracyError, x0.tolist(), approx_grad=True)[0] i += 1 return (alphaVect, popt)
def cascade(hk, J=7): """ Return (x, phi, psi) at dyadic points ``K/2**J`` from filter coefficients. Parameters ---------- hk : array_like Coefficients of low-pass filter. J : int, optional Values will be computed at grid points ``K/2**J``. Default is 7. Returns ------- x : ndarray The dyadic points ``K/2**J`` for ``K=0...N * (2**J)-1`` where ``len(hk) = len(gk) = N+1``. phi : ndarray The scaling function ``phi(x)`` at `x`: ``phi(x) = sum(hk * phi(2x-k))``, where k is from 0 to N. psi : ndarray, optional The wavelet function ``psi(x)`` at `x`: ``phi(x) = sum(gk * phi(2x-k))``, where k is from 0 to N. `psi` is only returned if `gk` is not None. Notes ----- The algorithm uses the vector cascade algorithm described by Strang and Nguyen in "Wavelets and Filter Banks". It builds a dictionary of values and slices for quick reuse. Then inserts vectors into final vector at the end. """ N = len(hk) - 1 if (J > 30 - np.log2(N + 1)): raise ValueError("Too many levels.") if (J < 1): raise ValueError("Too few levels.") # construct matrices needed nn, kk = np.ogrid[:N, :N] s2 = np.sqrt(2) # append a zero so that take works thk = np.r_[hk, 0] gk = qmf(hk) tgk = np.r_[gk, 0] indx1 = np.clip(2 * nn - kk, -1, N + 1) indx2 = np.clip(2 * nn - kk + 1, -1, N + 1) m = np.zeros((2, 2, N, N), 'd') m[0, 0] = np.take(thk, indx1, 0) m[0, 1] = np.take(thk, indx2, 0) m[1, 0] = np.take(tgk, indx1, 0) m[1, 1] = np.take(tgk, indx2, 0) m *= s2 # construct the grid of points x = np.arange(0, N * (1 << J), dtype=float) / (1 << J) phi = 0 * x psi = 0 * x # find phi0, and phi1 lam, v = eig(m[0, 0]) ind = np.argmin(np.absolute(lam - 1)) # a dictionary with a binary representation of the # evaluation points x < 1 -- i.e. position is 0.xxxx v = np.real(v[:, ind]) # need scaling function to integrate to 1 so find # eigenvector normalized to sum(v,axis=0)=1 sm = np.sum(v) if sm < 0: # need scaling function to integrate to 1 v = -v sm = -sm bitdic = {} bitdic['0'] = v / sm bitdic['1'] = np.dot(m[0, 1], bitdic['0']) step = 1 << J phi[::step] = bitdic['0'] phi[(1 << (J - 1))::step] = bitdic['1'] psi[::step] = np.dot(m[1, 0], bitdic['0']) psi[(1 << (J - 1))::step] = np.dot(m[1, 1], bitdic['0']) # descend down the levels inserting more and more values # into bitdic -- store the values in the correct location once we # have computed them -- stored in the dictionary # for quicker use later. prevkeys = ['1'] for level in range(2, J + 1): newkeys = ['%d%s' % (xx, yy) for xx in [0, 1] for yy in prevkeys] fac = 1 << (J - level) for key in newkeys: # convert key to number num = 0 for pos in range(level): if key[pos] == '1': num += (1 << (level - 1 - pos)) pastphi = bitdic[key[1:]] ii = int(key[0]) temp = np.dot(m[0, ii], pastphi) bitdic[key] = temp phi[num * fac::step] = temp psi[num * fac::step] = np.dot(m[1, ii], pastphi) prevkeys = newkeys return x, phi, psi
def cascade(hk,J=7): """(x,phi,psi) at dyadic points K/2**J from filter coefficients. Inputs: hk -- coefficients of low-pass filter J -- values will be computed at grid points $K/2^J$ Outputs: x -- the dyadic points $K/2^J$ for $K=0...N*(2^J)-1$ where len(hk)=len(gk)=N+1 phi -- the scaling function phi(x) at x $\phi(x) = \sum_{k=0}^{N} h_k \phi(2x-k)$ psi -- the wavelet function psi(x) at x $\psi(x) = \sum_{k=0}^N g_k \phi(2x-k)$ Only returned if gk is not None Algorithm: Uses the vector cascade algorithm described by Strang and Nguyen in "Wavelets and Filter Banks" Builds a dictionary of values and slices for quick reuse. Then inserts vectors into final vector at then end """ N = len(hk)-1 if (J > 30 - np.log2(N+1)): raise ValueError, "Too many levels." if (J < 1): raise ValueError, "Too few levels." # construct matrices needed nn,kk = np.ogrid[:N,:N] s2 = np.sqrt(2) # append a zero so that take works thk = np.r_[hk,0] gk = qmf(hk) tgk = np.r_[gk,0] indx1 = np.clip(2*nn-kk,-1,N+1) indx2 = np.clip(2*nn-kk+1,-1,N+1) m = np.zeros((2,2,N,N),'d') m[0,0] = np.take(thk,indx1,0) m[0,1] = np.take(thk,indx2,0) m[1,0] = np.take(tgk,indx1,0) m[1,1] = np.take(tgk,indx2,0) m *= s2 # construct the grid of points x = np.arange(0,N*(1<<J),dtype=np.float) / (1<<J) phi = 0*x psi = 0*x # find phi0, and phi1 lam, v = eig(m[0,0]) ind = np.argmin(np.absolute(lam-1)) # a dictionary with a binary representation of the # evaluation points x < 1 -- i.e. position is 0.xxxx v = np.real(v[:,ind]) # need scaling function to integrate to 1 so find # eigenvector normalized to sum(v,axis=0)=1 sm = np.sum(v) if sm < 0: # need scaling function to integrate to 1 v = -v sm = -sm bitdic = {} bitdic['0'] = v / sm bitdic['1'] = np.dot(m[0,1],bitdic['0']) step = 1<<J phi[::step] = bitdic['0'] phi[(1<<(J-1))::step] = bitdic['1'] psi[::step] = np.dot(m[1,0],bitdic['0']) psi[(1<<(J-1))::step] = np.dot(m[1,1],bitdic['0']) # descend down the levels inserting more and more values # into bitdic -- store the values in the correct location once we # have computed them -- stored in the dictionary # for quicker use later. prevkeys = ['1'] for level in range(2,J+1): newkeys = ['%d%s' % (xx,yy) for xx in [0,1] for yy in prevkeys] fac = 1<<(J-level) for key in newkeys: # convert key to number num = 0 for pos in range(level): if key[pos] == '1': num += (1<<(level-1-pos)) pastphi = bitdic[key[1:]] ii = int(key[0]) temp = np.dot(m[0,ii],pastphi) bitdic[key] = temp phi[num*fac::step] = temp psi[num*fac::step] = np.dot(m[1,ii],pastphi) prevkeys = newkeys return x, phi, psi
def cascade(hk, J=7): """(x,phi,psi) at dyadic points K/2**J from filter coefficients. Inputs: hk -- coefficients of low-pass filter J -- values will be computed at grid points $K/2^J$ Outputs: x -- the dyadic points $K/2^J$ for $K=0...N*(2^J)-1$ where len(hk)=len(gk)=N+1 phi -- the scaling function phi(x) at x $\phi(x) = \sum_{k=0}^{N} h_k \phi(2x-k)$ psi -- the wavelet function psi(x) at x $\psi(x) = \sum_{k=0}^N g_k \phi(2x-k)$ Only returned if gk is not None Algorithm: Uses the vector cascade algorithm described by Strang and Nguyen in "Wavelets and Filter Banks" Builds a dictionary of values and slices for quick reuse. Then inserts vectors into final vector at then end """ N = len(hk) - 1 if (J > 30 - np.log2(N + 1)): raise ValueError, "Too many levels." if (J < 1): raise ValueError, "Too few levels." # construct matrices needed nn, kk = np.ogrid[:N, :N] s2 = np.sqrt(2) # append a zero so that take works thk = np.r_[hk, 0] gk = qmf(hk) tgk = np.r_[gk, 0] indx1 = np.clip(2 * nn - kk, -1, N + 1) indx2 = np.clip(2 * nn - kk + 1, -1, N + 1) m = np.zeros((2, 2, N, N), 'd') m[0, 0] = np.take(thk, indx1, 0) m[0, 1] = np.take(thk, indx2, 0) m[1, 0] = np.take(tgk, indx1, 0) m[1, 1] = np.take(tgk, indx2, 0) m *= s2 # construct the grid of points x = np.arange(0, N * (1 << J), dtype=np.float) / (1 << J) phi = 0 * x psi = 0 * x # find phi0, and phi1 lam, v = eig(m[0, 0]) ind = np.argmin(np.absolute(lam - 1)) # a dictionary with a binary representation of the # evaluation points x < 1 -- i.e. position is 0.xxxx v = np.real(v[:, ind]) # need scaling function to integrate to 1 so find # eigenvector normalized to sum(v,axis=0)=1 sm = np.sum(v) if sm < 0: # need scaling function to integrate to 1 v = -v sm = -sm bitdic = {} bitdic['0'] = v / sm bitdic['1'] = np.dot(m[0, 1], bitdic['0']) step = 1 << J phi[::step] = bitdic['0'] phi[(1 << (J - 1))::step] = bitdic['1'] psi[::step] = np.dot(m[1, 0], bitdic['0']) psi[(1 << (J - 1))::step] = np.dot(m[1, 1], bitdic['0']) # descend down the levels inserting more and more values # into bitdic -- store the values in the correct location once we # have computed them -- stored in the dictionary # for quicker use later. prevkeys = ['1'] for level in range(2, J + 1): newkeys = ['%d%s' % (xx, yy) for xx in [0, 1] for yy in prevkeys] fac = 1 << (J - level) for key in newkeys: # convert key to number num = 0 for pos in range(level): if key[pos] == '1': num += (1 << (level - 1 - pos)) pastphi = bitdic[key[1:]] ii = int(key[0]) temp = np.dot(m[0, ii], pastphi) bitdic[key] = temp phi[num * fac::step] = temp psi[num * fac::step] = np.dot(m[1, ii], pastphi) prevkeys = newkeys return x, phi, psi