def wrtUSV(filename,BB,ss,nm=20): fV=open(filename+'_V'+pl,'w');fF=open(filename+'_F'+pl,'w') fS=open(filename+'_S'+pl,'w');fU=open(filename+'_U'+pl,'w') fS.write('* INDEX S\n$ %le %le\n') BB=(BB-average(BB,0))/sqrt(len(BB)) U,S,V = svd(BB,full_matrices=0) if nm>len(V): nm=len(V); print '# of modes =',nm F=abs(fftt(U,axis=0));F0=arange(len(F))/1.0/len(F) fV.write('* S ');fF.write('* INDEX '); for j in range(1,nm+1): fV.write('MOD_'+str(j)+' ') for j in range(1,nm+1): fF.write('MOD_'+str(j)+' ') fV.write('\n$ %le ');fF.write('\n$ %le ') for j in range(1,nm+1): fV.write('%le ') for j in range(1,nm+1): fF.write('%le ') fV.write('\n'); fF.write('\n') for k in range(len(ss)): fV.write(str(ss[k])+' ') for j in range(nm):fV.write(str(V[j,k])+' ') fV.write('\n') for k in range(len(F)): fF.write(str(F0[k])+' ') for j in range(nm):fF.write(str(F[k,j])+' ') fF.write('\n') for k in range(len(U)): fU.write(str(k)+' ') for j in range(nm):fU.write(str(U[k,j])+' ') fU.write('\n') for j in range(len(S)): fS.write(str(j)+' '+str(S[j])+'\n') fV.close();fF.close();fS.close();fU.close() return U,S,V,F
def fTune(BX,BY):#-- pk finder in window + quad interolate txx=[];tx=abs(fftt(BX-average(BX,0), axis=0)) tyy=[];ty=abs(fftt(BY-average(BY,0), axis=0)) qs=int((qx0-win)*len(tx));qe=int((qx0+win)*len(tx)) for j in range(shape(tx)[1]): pk,am=peak(tx[:,j],qs,qe) if qx0-win>pk>qx0+win: print "Warning: Qx outside window:",aD.H[j].name txx.append(pk) qs=int((qy0-win)*len(ty));qe=int((qy0+win)*len(ty)) for j in range(shape(ty)[1]): pk,am=peak(ty[:,j],qs,qe) if qy0-win>pk>qy0+win: print "Warning: Qy outside window:",aD.V[j].name tyy.append(pk) return txx,tyy
def fTuneOld(BX,BY):#--dumb pk finder in window txx=[];tx=abs(fftt(BX-average(BX,0), axis=0)) tyy=[];ty=abs(fftt(BY-average(BY,0), axis=0)) q1=int((qx0-win)*len(tx));q2=int((qx0+win)*len(tx)) for j in range(shape(tx)[1]): aa=argmax(tx[q1:q2,j])+q1; aa1=argmax(tx[:,j]) if aa!=aa1: print "warning, Qx outside window:",aD.H[j].name txx.append(aa/1.0/len(tx)) q1=int((qy0-win)*len(ty));q2=int((qy0+win)*len(ty)) for j in range(shape(ty)[1]): aa=argmax(ty[q1:q2,j])+q1; aa1=argmax(ty[:,j]) if aa!=aa1: print "warning, Qy outside window:", aD.V[j].name tyy.append(aa/1.0/len(ty)) return txx, tyy
def fmodXY(BB,m1=0,m2=1,m3=2,m4=4,tol=10.0): BB=(BB-average(BB,0))/sqrt(len(BB)) U,S,V = svd(BB,full_matrices=0);F=abs(fftt(U,axis=0)) if S[m1]/S[m2]>tol or S[m3]/S[m4]>tol: print "warning: singular vals are very different" S=take(S,[m1,m2,m3,m4]);U=take(U,[m1,m2,m3,m4],1) V=tp(take(V,[m1,m2,m3,m4],0));F=take(F,[m1,m2,m3,m4],1) return U,S,V,F
def pUSV(U,V,ss,st=0,nm=4): F=abs(fftt(U,axis=0));F0=arange(len(F))/1.0/len(F) k=0;jj=arange(nm*3)+1 for j in range(st,st+nm): pylab.subplot(nm,3,jj[k]);pylab.plot(U[:,j]);k+=1 pylab.subplot(nm,3,jj[k]);pylab.plot(F0,F[:,j]);k+=1 pylab.subplot(nm,3,jj[k]);pylab.plot(V[j,:]);k+=1 pylab.show()
def wrtUSV(filename, BB, ss, nm=20): fV = open(filename + "_V" + pl, "w") fF = open(filename + "_F" + pl, "w") fS = open(filename + "_S" + pl, "w") fU = open(filename + "_U" + pl, "w") fS.write("* INDEX S\n$ %le %le\n") BB = (BB - average(BB, 0)) / sqrt(len(BB)) U, S, V = svd(BB, full_matrices=0) if nm > len(V): nm = len(V) print "# of modes =", nm F = abs(fftt(U, axis=0)) F0 = arange(len(F)) / 1.0 / len(F) fV.write("* S ") fF.write("* INDEX ") for j in range(1, nm + 1): fV.write("MOD_" + str(j) + " ") for j in range(1, nm + 1): fF.write("MOD_" + str(j) + " ") fV.write("\n$ %le ") fF.write("\n$ %le ") for j in range(1, nm + 1): fV.write("%le ") for j in range(1, nm + 1): fF.write("%le ") fV.write("\n") fF.write("\n") for k in range(len(ss)): fV.write(str(ss[k]) + " ") for j in range(nm): fV.write(str(V[j, k]) + " ") fV.write("\n") for k in range(len(F)): fF.write(str(F0[k]) + " ") for j in range(nm): fF.write(str(F[k, j]) + " ") fF.write("\n") for k in range(len(U)): fU.write(str(k) + " ") for j in range(nm): fU.write(str(U[k, j]) + " ") fU.write("\n") for j in range(len(S)): fS.write(str(j) + " " + str(S[j]) + "\n") fV.close() fF.close() fS.close() fU.close() return U, S, V, F