复杂的矢量和阵列的形状

时间:2017-07-17 20:36:18

标签: python numpy filtering

我对python稍微有点新意,我试图转换一些代码。这是一种近似方法。哪个不重要。在我的oddev函数中,我得到了回复

        c2[1:modes+1] = v* 1j
ValueError: could not broadcast input array from shape (25) into shape (25,1) 

当我这样做时,我相信它会自动转换它,并将存储复杂的数组。该函数是从部分正弦变换得到系数来做到这一点。起初我尝试使用np.matlib方法存储随机矩阵,它只是一个数组,并且它具有相同的形状,但我相信当我投射它时,我将失去滤波器的实际值。我该如何存储?

import math
import numpy as np
def quickcontmin(datain):

n = np.shape(datain)[0]
m = math.floor(n / 2)
modes = math.floor(m / 2)
addl = 20
nn = 20 * n
chi = 10 ** -13

def evenhp(xv):
    "Even high pass"
    n1 = np.shape(xv)[0]
    vx = np.array(xv[:-1])
    vx = vx[::-1]
    c1 = np.append(xv,vx)
    c1 = np.fft.fft(c1)       
    c1[0:modes-1] = 0.0
    c1[-1 - modes + 2:-1] = 0.0
    evenl = np.real(np.fft.ifft(c1))
    even = evenl[0:n1-1]
    return even

def evenhpt(xv):
    " Transpose of EvenHP"
    n1 = np.shape(xv)[0]
    xy = np.zeros((n1- 2, 1))
    c1 = np.append(xv,xy)
    c1 = np.fft.fft(c1)
    c1[0:modes-1] = 0.0
    c1[-1 - modes + 1:-1] = 0.0
    evenl = np.real(np.fft.ifft(c1))
    even = evenl[0:n1-1]
    even[1:-2] = even[1:-2] + evenl[-1:-1:n1+1]
    return even``

def evenlp(xv):
    " Low pass cosine filter"
    n1 = np.shape(xv)[0]
    vx = np.array(xv[:-1])
    vx = vx[::-1]
    c1 = np.append(xv,vx)
    c1 = np.fft.fft(c1)
    c1[modes + 1:-1 - modes + 1] = 0.0
    evenl = np.real(np.fft.ifft(c1))
    even = evenl[0:n1-1]
    return even

def oddev(xv):
    "Evaluate the sine modes on the grid"
    c2 = np.zeros((2 *n - 2, 1))*1j
    v = np.array(xv[:])
    v1 = v[:-1]
    v1 = v[::-1]
    c2[1:modes+1] = v* 1j
    c2[-1 - modes + 1:-1] = -v1* 1j
    evall = np.fft.ifft(c2) * math.sqrt(2 * n - 2)
    eva = evall[0:n-1]
    return eva

def oddevt(xv):
    " Transpose the sine modes on the function OddEv"
    c1 = np.array(xv[1:-2])
    c1 = np.insert(c1,0.0,0)
    c1 = np.append(c1,0.0)
    c1 = np.append(c1,xv[-2:-1:2])
    c1a = np.divide(np.fft.fft(c1),math.sqrt(2 * n - 2))
    fcoef = np.imag(c1a[1:modes])
    return fcoef

def eextnd(xv):
    "Obtain cosine coefficients and evalue on the refined grid"  
    vx = np.array(xv[:-1])
    vx = vx[::-1]
    c1  = np.append(xv,vx)
    c1 = np.fft.fft(c1)
    cL = np.zeros((2*nn-2,1))
    cL[0:modes-1] = c1[0:modes-1]
    cL[-1 - modes + 1:-1] = c1[-1 - modes + 1:-1]
    evenexL = np.multiply(np.fft.ifft(cL) , (nn - 1) / (n - 1))
    evenex = evenexL[0:nn-1]
    return evenex

def oextnd(xv):
    "Evaluate sine coefficients on the refined grid"
    c2 = np.zeros((2 * nn - 2, 1))
    c2[0] = 0.0
    c2[1:modes + 1] = np.multiply(xv[0:-1],1j)
    c2[-1 - modes + 1:-1] = np.multiply(-xv[-1:-1:1],1j)
    evall = np.real(np.multiply(np.fft.ifft(c2), math.sqrt(2 * n - 2) * (2 *nn - 2) / (2 * n - 2)))
    oox = evall[0:nn-1]
    return oox

dc = evenlp(datain)
#L in paper, number of vectors used to sample the columnspace
lll = round(4 * math.log(m )/ math.log(2)) + addl
lll = int(lll)
#The following should be straightforward from the psuedo-code
w=2 * np.random.rand(modes , lll) - 1 
p=np.matlib.zeros(shape=(n,lll))
for j in range(lll):
    p[:,j] = evenhp(oddev(w[:,j]))
q,r = np.linalg.qr(p , mode='reduced')
z = np.zeros(shape=(modes,lll))
for j in range(lll):
    z[:,j]= oddevt(evenhpt(q[:,j]))
un,s,v = np.linalg.svd(z,full_matrices='False')
ds=np.diag(s)
aa=np.extract(np.diag(s)>(chi))
aa[-1] = aa
aa = int(aa)
s = 0 * s
for j in range(aa):
    s[j,j] = 1.0 / ds(j)
#find the sine coefficents
b=un*s* v.T* q.T* evenhp(datain)
#Constructing the continuation
exs=oddev(b)
pexs = evenlp(exs)

dataCont=exs-pexs+dc
dataCont[n+1:2*n-2]=-exs[-2:-1:1]-pexs[-2:-1:1]+dc[-2:-1:1]
#Evaluate the continuation on the refined grid
dataRefined=eextnd(dc-exs)+oextnd(b)

return dataRefined, dataCont   



n1 = 100
t = np.linspace(0,2*math.pi,n1)
y = np.sin(t)
data = quickcontmin(y)    
dc1 = data[1]
dc1 = dc1[0:n1-1]`

1 个答案:

答案 0 :(得分:0)

c2[1:modes+1] = v* 1j替换c2[1:modes+1, 0] = v* 1j应修复该特定错误。 更一致的是取代:

v = np.array(xv[:])
v1 = v[:-1]
v1 = v[::-1]

通过

v = xv
v1 = v[:-1]

v已经是一个列向量,因此当您以后需要列向量时,不需要将其转换为1d向量。