Beispiel #1
0
def narrowBandGen(data,alpha,targets,Fs):
    """
    Usage:
       data = narrowBandGen(data,alpha,targets,Fs)
    Where:
       data is an array of size number of sensors by samples
          Matrix data contains simulated noise field for array
       alpha is the array constant
       targets is a list of tuples containing simulated narrow band target information
       Fs is the sample rate (Hz)
    """
    from math import pi as M_PI
    f={'mview_d':'vview_d','mview_f':'vview_f'}
    tp=data.type   
    N=data.rowlength
    M=data.collength
    t =pv.create(f[tp],N).ramp(0,1)
    tt = pv.create(f[tp],N)
    Xim = pv.create(tp,M,M+1,'ROW')
    m = pv.create(f[tp],M).ramp(0,1)
    Xi = pv.create(f[tp],M + 1).ramp(0,M_PI/M).cos
    pv.outer(alpha,m,Xi,Xim)
    for i in range(M):
        data_v = data.rowview(i)
        for j in range(len(targets)):
            tgt=targets[j];
            w0=float(2.0 * M_PI * tgt[0]/Fs); Theta=int(tgt[1])
            sc=tgt[2]
            Xim_val = Xim[i,Theta]
            pv.ma(t,w0,-w0 * Xim_val,tt)
            tt.cos
            tt *= sc
            data_v += tt
    return data
Beispiel #2
0
def narrowBandGen(data, alpha, targets, Fs):
    """
    Usage:
       data = narrowBandGen(data,alpha,targets,Fs)
    Where:
       data is an array of size number of sensors by samples
          Matrix data contains simulated noise field for array
       alpha is the array constant
       targets is a list of tuples containing simulated narrow band target information
       Fs is the sample rate (Hz)
    """
    from math import pi as M_PI
    f = {'mview_d': 'vview_d', 'mview_f': 'vview_f'}
    tp = data.type
    N = data.rowlength
    M = data.collength
    t = pv.create(f[tp], N).ramp(0, 1)
    tt = pv.create(f[tp], N)
    Xim = pv.create(tp, M, M + 1, 'ROW')
    m = pv.create(f[tp], M).ramp(0, 1)
    Xi = pv.create(f[tp], M + 1).ramp(0, M_PI / M).cos
    pv.outer(alpha, m, Xi, Xim)
    for i in range(M):
        data_v = data.rowview(i)
        for j in range(len(targets)):
            tgt = targets[j]
            w0 = float(2.0 * M_PI * tgt[0] / Fs)
            Theta = int(tgt[1])
            sc = tgt[2]
            Xim_val = Xim[i, Theta]
            pv.ma(t, w0, -w0 * Xim_val, tt)
            tt.cos
            tt *= sc
            data_v += tt
    return data
Beispiel #3
0
def view_read(fname):
    from pyJvsip import create as create
    fd = open(fname, 'r')
    t = fd.readline().split()[0]
    assert t in ['vview_f', 'mview_f', 'vview_d',
                 'mview_d'], 'Type <:%s:> not supported' % t
    if 'mview' in t:
        sz = fd.readline().split()
        M = pv.create(t, int(sz[0]), int(sz[1]))
        for lin in fd:
            a = lin.split()
            r = int(a[0])
            c = int(a[1])
            x = float(a[2])
            M[r, c] = x
    else:
        sz = fd.readline().split()
        M = create(t, int(sz))
        for lin in fd:
            a = lin.split()
            i = int(a[0])
            x = float(a[2])
            M[i] = x
    fd.close()
    return M
Beispiel #4
0
def localmax(A):
    """
    Note A is a vector of type real float
    % k = localmax(x)
    % finds location of local maxima
    """
    sptd=['vview_d','vview_f']
    assert A.type in sptd, 'local max only works with real vectors of type float'
    x=A 
    N = x.length
    b1=x[:N-1].lle(x[1:N]); b2=x[:N-1].lgt(x[1:N])
    k = b1[0:N-2].bband(b2[1:N-1]).indexbool
    k+=1
    if x[0] > x[1]:
        t=pv.create(k.type,k.length+1)
        t[:k.length]=k
        t[k.length]=0
        k=t.copy
    if x[N-1]>x[N-2]:
        t=pv.create(k.type,k.length+1)
        t[:k.length]=k
        t[k.length]=N-1
        k=t.copy
    k.sortip()
    return k
Beispiel #5
0
def localmax(A):
    """
    Note A is a vector of type real float
    % k = localmax(x)
    % finds location of local maxima
    """
    sptd = ['vview_d', 'vview_f']
    assert A.type in sptd, 'local max only works with real vectors of type float'
    x = A
    N = x.length
    b1 = x[:N - 1].lle(x[1:N])
    b2 = x[:N - 1].lgt(x[1:N])
    k = b1[0:N - 2].bband(b2[1:N - 1]).indexbool
    k += 1
    if x[0] > x[1]:
        t = pv.create(k.type, k.length + 1)
        t[:k.length] = k
        t[k.length] = 0
        k = t.copy
    if x[N - 1] > x[N - 2]:
        t = pv.create(k.type, k.length + 1)
        t[:k.length] = k
        t[k.length] = N - 1
        k = t.copy
    k.sortip()
    return k
Beispiel #6
0
 def coscoef(rs,n,V,Y):
     f={'vview_d':'mview_d','vview_f':'mview_f'}
     m=rs.length + 1
     y=pv.create(Y.type,m)
     y[:Y.length]=Y;y[m-1]=0.0
     A=pv.create(f[y.type],m,y.length)
     a=A[:rs.length,:n+1]
     a=pv.outer(1,rs,pv.create(rs.type,n+1).ramp(0,1),a).cos
     at=A.rowview(m-1)
     A.rowview(m-1)[:]=V[:]
     return A.luSolve(y)
Beispiel #7
0
 def coscoef(rs, n, V, Y):
     f = {'vview_d': 'mview_d', 'vview_f': 'mview_f'}
     m = rs.length + 1
     y = pv.create(Y.type, m)
     y[:Y.length] = Y
     y[m - 1] = 0.0
     A = pv.create(f[y.type], m, y.length)
     a = A[:rs.length, :n + 1]
     a = pv.outer(1, rs, pv.create(rs.type, n + 1).ramp(0, 1), a).cos
     at = A.rowview(m - 1)
     A.rowview(m - 1)[:] = V[:]
     return A.luSolve(y)
Beispiel #8
0
 def myView(a):
     fv={'float32':'vview_f','float64':'vview_d','complex64':'cvview_f','complex128':'cvview_d'}
     fm={'float32':'mview_f','float64':'mview_d','complex64':'cmview_f','complex128':'cmview_d'}
     ashp=a.shape
     t=a.dtype.name
     if len(a.shape) is 1: # create a vector
         v=create(fv[t],ashp[0])
     else: #create a matrix
         if a.flags.f_contiguous:
             v=create(fm[t],ashp[0],ashp[1],'COL')
         else:
             v=create(fm[t],ashp[0],ashp[1])
     return v
Beispiel #9
0
def eye(t, n):
    """
    Usage: I=eye(t,n)
       create and return an identity matrix of size n and type t
       t must be a matrix
    """
    return pv.create(t, n, n).identity
Beispiel #10
0
def eye(t,n):
    """
    Usage: I=eye(t,n)
       create and return an identity matrix of size n and type t
       t must be a matrix
    """
    return pv.create(t,n,n).identity
Beispiel #11
0
def dftCoefE(n):
    m = pv.create('cmview_d', n, n)
    for i in range(n):
        for j in range(n):
            t = (i * j) % n
            x = 2.0 * pi / n * float(t)
            m[i, j] = complex(cos(x), -sin(x))
    return m
Beispiel #12
0
def dftCoefE(n):
    m = pv.create("cmview_d", n, n)
    for i in range(n):
        for j in range(n):
            t = (i * j) % n
            x = 2.0 * pi / n * float(t)
            m[i, j] = complex(cos(x), -sin(x))
    return m
Beispiel #13
0
def checksvd(L, d, f, R):
    B = pv.create(L.type, L.collength, R.rowlength).fill(0.0)
    if 'cmview' in B.type:
        b = B.realview.diagview
    else:
        b = B.diagview
    b(0)[:] = d
    b(1)[:] = f
    L.prod(B).prod(R).mprint('%.3f')
Beispiel #14
0
def checksvd(L,d,f,R):
    B=pv.create(L.type,L.collength,R.rowlength).fill(0.0)
    if 'cmview' in B.type:
        b=B.realview.diagview
    else:
        b=B.diagview
    b(0)[:]=d
    b(1)[:]=f
    L.prod(B).prod(R).mprint('%.3f')
Beispiel #15
0
def scale(gram_data):
    f={'cmview_d':'mview_d','cmview_f':'mview_f'}
    assert gram_data.type in ['cmview_d','cmview_f'], 'gram_data must be complex float matrix'
    M=gram_data.collength; N=gram_data.rowlength; t=f[gram_data.type]
    gram=pv.create(t,M,N,'ROW')
    pv.cmagsq(gram_data,gram)
    gram += (1.0-gram.minval)
    gram.log10
    gram *= 256.0/gram.maxval
    return center(gram)
Beispiel #16
0
def VU_vfreadxy(fname):
    fd = open(fname, 'r')
    N = int(fd.readline())
    x = pv.create('vview_f', N)
    y = x.empty
    for i in range(N):
        ln = fd.readline().partition(' ')
        x[i] = float(ln[0])
        y[i] = float(ln[2])
    fd.close()
    return (x, y)
Beispiel #17
0
def VU_vfreadxy(fname):
    fd = open(fname,'r')
    N=int(fd.readline())
    x=pv.create('vview_f',N)
    y=x.empty
    for i in range(N):
        ln=fd.readline().partition(' ')
        x[i]=float(ln[0])
        y[i]=float(ln[2])
    fd.close()
    return(x,y)
Beispiel #18
0
def view_read(fname):
    from pyJvsip import create as create
    fd=open(fname,'r')
    t=fd.readline().split()[0]
    assert t in ['vview_f','mview_f','vview_d','mview_d'], 'Type <:%s:> not supported'%t
    if 'mview' in t:
        sz=fd.readline().split()
        M=pv.create(t,int(sz[0]),int(sz[1]))
        for lin in fd:
            a=lin.split()
            r=int(a[0]);c=int(a[1]);x=float(a[2])
            M[r,c]=x
    else:
        sz=fd.readline().split()
        M=create(t,int(sz))
        for lin in fd:
            a=lin.split()
            i=int(a[0]);x=float(a[2])
            M[i]=x
    fd.close()
    return M
Beispiel #19
0
def noiseGen(t,alpha,Mp,Nn,Ns):
    """
    Usage:
        data = noiseGen(t,alpha,Mp,Nn,Ns)
    where:
        data is a matrix of type t and size Mp by Ns)
        alpha is a linear array constant
        Mp is the number of sensors in the linear array
        Nn is the size of the simulated noise vector
        Ns is the size of the output noise field for each sensor
    Note the noise field is returned in data
    """
    # program parameters
    from math import pi as M_PI
    kaiser=9
    Nfilter=10   # Kernel length for noise filter
    Nnoise=64    # number of Simulated Noise Directions
    cnst1 = M_PI/Nnoise
    offset0 = int(alpha * Mp + 1)
    nv = pv.create('vview_d',2 * Nn)
    noise=pv.create('mview_d',Mp,Nn)
    kernel = pv.create('vview_d',Nfilter).kaiser(kaiser)
    fir = pv.FIR('fir_d',kernel,'NONE',2 * Nn,2,'YES')
    data = pv.create('mview_d',Mp,Ns).fill(0.0)
    state = pv.Rand('PRNG',15)
    for j in range(Nnoise):
        noise_j=noise.rowview(j)
        state.randn(nv)
        fir.flt(nv,noise_j)
        noise_j *= 12.0/float(Nnoise)
    #view_store(noise,'noiseData');
    noise.putrowlength(Ns)
    for i in range(Mp):
        data_v = data.rowview(i)
        for j in range(Nnoise):
            noise_j=noise.rowview(j)
            noise_j.putoffset(offset0 +(int)( i * alpha * cos(j * cnst1)))
            pv.add(noise_j,data_v,data_v)
    #view_store(data,'Data');
    return data
Beispiel #20
0
def noiseGen(t, alpha, Mp, Nn, Ns):
    """
    Usage:
        data = noiseGen(t,alpha,Mp,Nn,Ns)
    where:
        data is a matrix of type t and size Mp by Ns)
        alpha is a linear array constant
        Mp is the number of sensors in the linear array
        Nn is the size of the simulated noise vector
        Ns is the size of the output noise field for each sensor
    Note the noise field is returned in data
    """
    # program parameters
    from math import pi as M_PI
    kaiser = 9
    Nfilter = 10  # Kernel length for noise filter
    Nnoise = 64  # number of Simulated Noise Directions
    cnst1 = M_PI / Nnoise
    offset0 = int(alpha * Mp + 1)
    nv = pv.create('vview_d', 2 * Nn)
    noise = pv.create('mview_d', Mp, Nn)
    kernel = pv.create('vview_d', Nfilter).kaiser(kaiser)
    fir = pv.FIR('fir_d', kernel, 'NONE', 2 * Nn, 2, 'YES')
    data = pv.create('mview_d', Mp, Ns).fill(0.0)
    state = pv.Rand('PRNG', 15)
    for j in range(Nnoise):
        noise_j = noise.rowview(j)
        state.randn(nv)
        fir.flt(nv, noise_j)
        noise_j *= 12.0 / float(Nnoise)
    #view_store(noise,'noiseData');
    noise.putrowlength(Ns)
    for i in range(Mp):
        data_v = data.rowview(i)
        for j in range(Nnoise):
            noise_j = noise.rowview(j)
            noise_j.putoffset(offset0 + (int)(i * alpha * cos(j * cnst1)))
            pv.add(noise_j, data_v, data_v)
    #view_store(data,'Data');
    return data
Beispiel #21
0
def scale(gram_data):
    f = {'cmview_d': 'mview_d', 'cmview_f': 'mview_f'}
    assert gram_data.type in ['cmview_d', 'cmview_f'
                              ], 'gram_data must be complex float matrix'
    M = gram_data.collength
    N = gram_data.rowlength
    t = f[gram_data.type]
    gram = pv.create(t, M, N, 'ROW')
    pv.cmagsq(gram_data, gram)
    gram += (1.0 - gram.minval)
    gram.log10
    gram *= 256.0 / gram.maxval
    return center(gram)
Beispiel #22
0
 def myView(a):
     fv = {
         'float32': 'vview_f',
         'float64': 'vview_d',
         'complex64': 'cvview_f',
         'complex128': 'cvview_d'
     }
     fm = {
         'float32': 'mview_f',
         'float64': 'mview_d',
         'complex64': 'cmview_f',
         'complex128': 'cmview_d'
     }
     ashp = a.shape
     t = a.dtype.name
     if len(a.shape) is 1:  # create a vector
         v = create(fv[t], ashp[0])
     else:  #create a matrix
         if a.flags.f_contiguous:
             v = create(fm[t], ashp[0], ashp[1], 'COL')
         else:
             v = create(fm[t], ashp[0], ashp[1])
     return v
Beispiel #23
0
def VHmatExtract(B): #B bidiagonalized with householder vectors stored in rows and columns.
    n = B.rowlength
    V=pv.create(B.type,n,n).fill(0.0);V.diagview(0).fill(1.0)
    if(n < 3):
        return V;
    for i in range(n-3,0,-1):
        j=i+1;
        v=B.rowview(i)[j:]
        t=v[0]
        v[0]=1.0
        prodHouse(V[j:,j:],v)
        v[0]=t
    v=B.rowview(0)[1:]
    t=v[0];v[0]=1.0
    prodHouse(V[1:,1:],v)
    v[0]=t
    return V
Beispiel #24
0
def UmatExtract(B):
    m = B.collength
    n = B.rowlength
    U=pv.create(B.type,m,m).fill(0.0);U.diagview(0).fill(1.0)
    if (m > n):
        i=n-1;
        v=B.colview(i)[i:]
        t=v[0]
        v[0]=1.0
        houseProd(v,U[i:,i:])
        v[0]=t
    for i in range(n-2,0,-1):
        v=B.colview(i)[i:]
        t=v[0];v[0]=1.0
        houseProd(v,U[i:,i:])
        v[0]=t
    v=B.colview(0)
    t=v[0];v[0]=1.0
    houseProd(v,U)
    v[0]=t
    return U
Beispiel #25
0
aFloatVectorView.mprint(f)
aNewVector.mprint(f)

# <markdowncell>

# Note above we didn't care about the vector on the whole block so we didn't create a reference to it. pyJvsip uses python's garbage collection methods to destroy pyJvsip objects which have no reference.  We can see now the block has been initialized so our second vector has a zero in every other spot.

# <markdowncell>

# ## Create Function
#
# The pyJvsip module has a create function which may be used to create pyJvsip objects including blocks and views. Most of the time you will use this function. When a view is created with this function it creates the needed block under the covers for you.

# <codecell>

aVector = pv.create('vview_d', 10)
aVector.ramp(1, 1)
aVector.mprint(f)

# <markdowncell>

# Be default when creating a matrix view the view is created as row major.

# <codecell>

aMatrix = pv.create('cmview_d', 4, 5)
aMatrix.block.vector.ramp(1, 1)
aMatrix.imagview.block.vector.ramp(-1, -1)
aMatrix.mprint('%.1f')

# <markdowncell>
Beispiel #26
0
import pyJvsip as pv
N=6
A = pv.create('cvview_d',N).randn(7)
B = A.empty.fill(5.0)
C = A.empty.fill(0.0)
print('A = '+A.mstring('%+.2f'))
print('B = '+B.mstring('%+.2f'))
pv.add(A,B,C)
print('C = A+B') 
print('C = '+C.mstring('%+.2f'))
""" OUTPUT
A = [+0.16+0.50i -0.21-0.75i -0.56-0.09i \
     +1.15+0.45i +0.10+0.43i +0.63-1.05i]

B = [+5.00+0.00i +5.00+0.00i +5.00+0.00i \
     +5.00+0.00i +5.00+0.00i +5.00+0.00i]

C = A+B
C = [+5.16+0.50i +4.79-0.75i +4.44-0.09i \
     +6.15+0.45i +5.10+0.43i +5.63-1.05i]
"""
Beispiel #27
0
# run some matrices to see if the SVD works
import pyJvsip as pv
from decompositionUtilities import *
fmt='%.5f'
def checkBack(A,U,S,VH):
    chk=(A-U[:,0:S.length].prod(S.mmul(VH.copy.COL))).normFro
    print('%.7f'%chk)
    if chk < A.normFro/(A.rowlength * 1E10):
        print('SVD Decomposition appears to agree with input matrix')
    elif chk < A.normFro/(A.rowlength*1E5):
        print('SVD Decomposition appears to agree witin 5 decimal places')
    else:
        print('SVD Decomposition does not agree with input matrix')
 
print('\nTEST')
A=pv.create('cmview_d',9,7).fill(0.0)
A.block.vector.realview.randn(3)
A.block.vector.imagview.randn(7)
U,S,VH = svd(A)
checkBack(A,U,S,VH)
print('Singular values are');S.mprint(fmt)
print('for matrix of type ' + A.type);A.mprint(fmt)
print('%.7f, %.7f'%(A.normFro,S.normFro))
print('Frobenius norm difference %.10e'%abs(A.normFro - S.normFro))

print('\nTEST')
A=pv.create('cmview_f',7,7).fill(0.0)
A.block.vector.realview.randn(3)
A.block.vector.imagview.randn(7)
A[:,0]=A[:,4];
A[:,1] = 3 * A[:,4]
Beispiel #28
0
import pyJvsip as pv
import timeit
# NOTE myDet is not efficient. Keep N below 7 or 8
N=6
# Function myDet uses definition of Determinant in most 
# Linear Algebra Texts by expansion of cofactors along a row 
def myDet(A):
    m = A.collength
    det = 0
    if m == 1:
        return A[0,0]
    for i in range(m):
        det += A[0,i] * pow(-1,i) * myDet(A.submatrix(0,i))
    return det
# For example we need some data so we Create a non-singular matrix 'A'
A=pv.create('mview_d',N,N).fill(-1.0)
A.diagview(0).ramp(1,1.1)
A.diagview(1).fill(2)
A.diagview(-1).fill(2)
# Find the determinant with pyJvsip det method
d0=A.copy.det
# Find the determinant using myDet funciton
d1=myDet(A)
#compare
print('difference d0 - d1 = %f - %f = %f'%(d0,d1,d0-d1))
# Below we do some timing measureents to show inefficiency of myDet.
sMyDet='def myDet(A):\n    m = A.collength\n    det = 0\n    if m == 1:\n'
sMyDet+='        return A[0,0]\n    for i in range(m):\n'
sMyDet+='        det += A[0,i] * pow(-1,i) * myDet(A.submatrix(0,i))\n'
sMyDet+='    return det\n'
Amake='A=pv.create(\'mview_d\',N,N).fill(-1.0)\n'
Beispiel #29
0
import pyJvsip as pjv
from math import pi as pi
n=1000
x=pjv.create('vview_d',n).ramp(0,2*pi/float(n))
c=pjv.cos(x,x.empty)
cinvClip=pjv.invclip(c,-.6,0.0,.6,-.7,.7,c.empty)
Beispiel #30
0
#! python
# Estimate two-norm (sigma0) and singular values (sigma0, sigma1)
# of a two by two matrix by searching through a dense space of vectors.
# Check by reproducing estimate for A matrix using Aest=U Sigma V^t
# Motivating text Numerical Linear Algebra, Trefethen and Bau
import pyJvsip as pv
from numpy import pi, arccos
from matplotlib.pyplot import *
N = 1000  #number of points to search through for estimates
# create a vector 'arg' of evenly spaced angles between [zero, 2 pi)
arg = pv.create('vview_d', N).ramp(0.0, 2.0 * pi / float(N))
# Create a matrix 'X' of unit vectors representing Unit Ball in R2
X = pv.create('mview_d', 2, N, 'ROW').fill(0.0)
x0 = X.rowview(0)
x1 = X.rowview(1)
pv.sin(arg, x0)
pv.cos(arg, x1)
# create matrix 'A' to examine
A = pv.create('mview_d', 2, 2).fill(0.0)
A[0, 0] = 1.0
A[0, 1] = 2.0
A[1, 1] = 2.0
A[1, 0] = 0.0
# create matrix Y=AX
Y = A.prod(X)
y0 = Y.rowview(0)
y1 = Y.rowview(1)
# create vector 'n' of 2-norms for Y col vectors (sqrt(y0_i^2+y1_i^2))
n = (y0.copy.sq + y1.copy.sq).sqrt
# Find index of (first) maximum value and (first) minimum value
i = n.maxvalindx
Beispiel #31
0
F1=300.0     # Target Frequency
F2=150.0     # Target Frequency
F3=50.0      # Target Frequency
Theta_o=40   # Target Direction
Ns=512       # length of sampled time series (samples)
Nn=1024      # length of (simulated) noise series
Mp=128       # number of sensors in linear array
c=1500.0     # Propagation Speed (Meters/Second)
# Calculate input data
alpha = (D * Fs) / c #Array Constant
data = noiseGen('mview_d',alpha,Mp,Nn,Ns)
targets=[(F0,Theta_o,1.5),(F1,Theta_o,2.0),(F2,Theta_o,2.0),(F3,Theta_o,3.0)]
narrowBandGen(data,alpha,targets,Fs)
# beamform data and output in a form sutiable for display
ccfftmip = pv.FFT('ccfftmip_d',(Mp,int(Ns/2) + 1,pv.VSIP_FFT_FWD,1,pv.VSIP_COL,0,0))
windowt=pv.create('vview_d',Ns).hanning
windowp=pv.create('vview_d',Mp).hanning
pv.vmmul(windowt.ROW,data,data) #window to reduce sidelobes
pv.vmmul(windowp.COL,data,data)  
gram_data=data.rcfft
ccfftmip.dft(gram_data)
gram = scale(gram_data)
# save data
view_store(gram,'gram_output')
fig = plt.figure()
ax = fig.add_subplot(111)
plt.imshow(gram.list,origin='lower')
ax.set_yticks([0,31,63,95,127])
ax.set_xticks([0,51,103,154,206,256])
ax.set_yticklabels([r'$-\kappa$','$-\kappa /2$','$0$','$\kappa /2$','$\kappa$'])
ax.set_xticklabels([' 0 ','100','200','300','400','500'])
Beispiel #32
0
import pyJvsip as pjv

# Elementwise add using columns. 
# This is just to demo pyJvsip API features. There is no good reason to actually use this code

def madd(A,B,C):
    assert 'pyJvsip' in repr(A) and '__View' in repr(A),'Input parameters must be views of type pyJvsip.__View.'
    assert type(A) == type(B) and type(A) == type(C),'Input paramteters must be the same type'
    assert 'mview' in A.type,'Only matrix views are supported for madd.'
    L = A.rowlength
    a = A.colview
    b = B.colview
    c = C.colview
    for i in range(L):
        pjv.add(a(i),b(i),c(i))
    return C
a = pjv.create('vview_d',50)
b = pjv.create('vview_d',50)
c = pjv.create('vview_d',50)
A=a.block.bind(0,4,3,1,4)
B=b.block.bind(0,4,3,1,4)
C=c.block.bind(0,4,3,1,4)
a.ramp(0.0,.01)
b.ramp(0.0,1.0)
madd(A,B,C);
print('A = ');        A.mprint('%.2f')
print('B= ');         B.mprint('%.2f')
print("A + B = C = ");C.mprint('%.2f')
Beispiel #33
0
def faffine(M, wp, ws, Kp, Ks, eta_p, eta_s):
    """
    Usage:
         ans= faffine(M,wp,ws,Kp,Ks,eta_p,eta_s)
         h=ans[0], rs=ans[1],del_p=ans[2], del_s=ans[3]
    Notes from matlab code below.
    % [h,rs,del_p,del_s] = faffine(M,wp,ws,Kp,Ks,eta_p,eta_s);
    % Lowpass linear phase FIR filter design by a REMEZ-like algorithm
    %  h   : filter of length 2*M+1
    %  rs  : reference set upon convergence
    %  del_p : passband error, del_p = Kp * del + eta_p
    %  del_s : stopband error, del_s = Kp * del + eta_s
    %  wp,ws : band edges
    % EXAMPLE
    %  M  =  17;
    %  wp = 0.30*pi;
    %  ws = 0.37*pi;
    %  Kp = 0; Ks = 1; eta_p = .05; eta_s = 0;
    %  [h,rs,del_p,del_s] = faffine(M,wp,ws,Kp,Ks,eta_p,eta_s);

    % author: IVAN SELESNICK
    %
    % required subprograms : frefine.m, localmax.m
    """
    # ------------------ initialize some constants ------------------------------
    L = int(pow(2, ceil(log(10 * M, 2))))
    SN = 1e-7
    w = pv.create('vview_d', int(L + 1)).ramp(0, pi / L)
    S = int(M + 2)
    np = round(S * (wp + ws) / (2 * pi))
    np = int(max(np, 1))
    if np == S:
        np = np - 1
    ns = int(S - np)
    r_pass = pv.create('vview_d', np)
    r_stop = pv.create('vview_d', ns)
    if np > 1:
        r_pass.ramp(0, wp / (np - 1))
    else:
        r_pass[0] = wp
    if ns > 1:
        r_stop.ramp(ws, (pi - ws) / (ns - 1))
    else:
        r_stop[0] = ws
    rs = pv.create('vview_d', S)
    rs[0:np] = r_pass
    rs[np:] = r_stop  #initial ref. set
    D = pv.create('vview_d', int(np + ns)).fill(0.0)
    D[:np].fill(1.0)
    sp = pv.create('vview_d', int(S)).fill(0.0)
    if np % 2:
        sp[:int(np):2].fill(-1.0)
        sp[1:int(np):2].fill(1.0)
    else:
        sp[:int(np):2].fill(1.0)
        sp[1:int(np):2].fill(-1.0)
    ss = sp.empty.fill(0.0)
    ss[int(np):int(S):2].fill(1.0)
    ss[int(np) + 1:int(S):2].fill(-1.0)
    Z = int(2 * (L + 1 - M) - 3)
    # begin looping
    Err = 1
    while Err > SN:
        # --------------- calculate cosine coefficients ---------------------------
        x1 = pv.create('mview_d', M + 4, M + 4).fill(0.0)
        x1[M + 3, M + 3] = -Ks
        x1[M + 3, M + 2] = 1.0
        x1[M + 2, M + 1] = 1
        x1[M + 2, M + 3] = -Kp
        x1[:S, :M + 1] = rs.outer(pv.create('vview_d', M + 1).ramp(0, 1)).cos
        x1.colview(M + 1)[:S] = sp.neg
        x1.colview(M + 2)[:S] = ss.neg
        x2 = pv.create('vview_d', D.length + 2)
        x2[:D.length] = D
        x2[D.length + 1] = eta_s
        x2[D.length] = eta_p
        x = x1.luSolve(x2)
        a = x[0:M + 1].copy
        del_p = x[M + 1]
        del_s = x[M + 2]
        del_ = x[M + 3]
        if (del_p < 0) and (del_s < 0):
            print('both del_p and del_s are negative!')
        elif del_s < 0:
            # set del_s equal to 0
            print('del_s < 0')
            x1[:S, :M + 1] = rs.outer(pv.create('vview_d', M + 1).ramp(0,
                                                                       1)).cos
            x1.colview(M + 1)[:S] = sp.neg
            x = x1[:S, :M + 2].luSolve(D.copy)
            a = x[0:M + 1].copy
            del_p = x[M + 1]
            del_s = 0
        elif del_p < 0:
            # set del_p equal to 0
            print('del_p < 0')
            x1[:S, :M + 1] = rs.outer(pv.create('vview_d', M + 1).ramp(0,
                                                                       1)).cos
            x1.colview(M + 1)[:S] = ss.neg
            x = x1[:S, :M + 2].luSolve(D.copy)
            a = x[0:M + 1].copy
            del_p = 0
            del_s = x[M + 1]
        A = pv.create('vview_d', a.length * 2 - 1 + Z).fill(0.0)
        A[:a.length] = a
        A[1:a.length] *= 0.5
        A[a.length + Z:] = A.block.bind(a.length - 1, -1, a.length - 1)
        Ac = A.rcfft
        A = Ac.realview.copy
        # --------------- determine new reference set-----------------------------
        # Below neg done in place so undo is required
        # Could use A.copy.neg instead but pay a create/destroy penalty
        lmAn = localmax(A.neg)
        lmA = localmax(A.neg)
        ri = pv.create('vview_d', lmA.length + lmAn.length).fill(0.0)
        ri[:lmA.length] = lmA
        ri[lmA.length:] = lmAn
        ri.sortip()
        rs = frefine(a, ri * (pi / float(L)))
        rsp = rs.gather(rs.llt(wp).indexbool)
        rss = rs.gather(rs.lgt(ws).indexbool)
        rs = pv.create(rsp.type, rsp.length + rss.length + 2).fill(0.0)
        rs[:rsp.length] = rsp
        rs[rsp.length:][0] = wp
        rs[rsp.length:][1] = ws
        rs[rsp.length + 2:] = rss
        np = rsp.length + 1
        ns = rss.length + 1
        D = pv.create('vview_d', np + ns).fill(0.0)
        D[:np].fill(1.0)
        sp = D.empty.fill(0.0)
        if np % 2:
            sp[:int(np):2].fill(-1.0)
            sp[1:int(np):2].fill(1.0)
        else:
            sp[:int(np):2].fill(1.0)
            sp[1:int(np):2].fill(-1.0)
        ss = D.empty.fill(0.0)
        ss[np::2].fill(1.0)
        ss[np + 1::2].fill(-1.0)
        lr = rs.length
        Ar = rs.outer(pv.create(rs.type, M + 1).ramp(0, 1)).cos.prod(a)

        if lr > M + 2:
            sp.putlength(M + 2)
            ss.putlength(M + 2)
            D.putlength(M + 2)
            Ar.putlength(M + 2)
            rs.putlength(M + 2)
            if A[0] < A[1]:
                cp_ = -1.
            else:
                cp_ = 1.
            if A[L] < A[L - 1]:
                cs = -1.
            else:
                cs = 1.
            if ((A[0] - 1.) * cp_ - del_p) < (A[L] * cs - del_s):
                sp.putoffset(1)
                ss.putoffset(1)
                D.putoffset(1)
                Ar.putoffset(1)
                rs.putoffset(1)
        # -------- calculate stopping criterion ------------
        Err = ((Ar - D) - ((Kp * del_ + eta_p) * sp +
                           (Ks * del_ + eta_s) * ss)).mag.maxval
        print('Err = %20.15f\n' % Err)
    h = pv.create(a.type, 2 * a.length - 1).fill(0.0)
    a[1:] *= 0.5
    a2 = a[1:]
    a1 = a.block.bind(a2.length - 1 + a2.offset, -1, a2.length)
    h[:a1.length] = a1
    h[a1.length] = a[0]
    h[a1.length + 1:] = a2  # h : filter coefficients
    pi_inv = 1.0 / pi
    pyplot.figure(1)
    pyplot.plot((w * pi_inv).list, A.list, 'r')
    pyplot.plot((rs * pi_inv).list, Ar.list, '+')
    pyplot.xlabel('w/pi', fontsize=14)
    pyplot.ylabel('A', fontsize=14)
    pyplot.title('Frequency Response Amplitude', fontsize=16)
    return (h, rs, del_p, del_s)
Beispiel #34
0
def faffine(M,wp,ws,Kp,Ks,eta_p,eta_s):
    """
    Usage:
         ans= faffine(M,wp,ws,Kp,Ks,eta_p,eta_s)
         h=ans[0], rs=ans[1],del_p=ans[2], del_s=ans[3]
    Notes from matlab code below.
    % [h,rs,del_p,del_s] = faffine(M,wp,ws,Kp,Ks,eta_p,eta_s);
    % Lowpass linear phase FIR filter design by a REMEZ-like algorithm
    %  h   : filter of length 2*M+1
    %  rs  : reference set upon convergence
    %  del_p : passband error, del_p = Kp * del + eta_p
    %  del_s : stopband error, del_s = Kp * del + eta_s
    %  wp,ws : band edges
    % EXAMPLE
    %  M  =  17;
    %  wp = 0.30*pi;
    %  ws = 0.37*pi;
    %  Kp = 0; Ks = 1; eta_p = .05; eta_s = 0;
    %  [h,rs,del_p,del_s] = faffine(M,wp,ws,Kp,Ks,eta_p,eta_s);

    % author: IVAN SELESNICK
    %
    % required subprograms : frefine.m, localmax.m
    """
    # ------------------ initialize some constants ------------------------------
    L = int(pow(2,ceil(log(10*M,2))))
    SN = 1e-7 
    w  = pv.create('vview_d',int(L+1)).ramp(0,pi/L)
    S = int(M + 2)
    np = round(S*(wp+ws)/(2*pi))
    np = int(max(np, 1))
    if np == S:
        np = np - 1
    ns = int(S - np)
    r_pass =pv.create('vview_d',np); r_stop=pv.create('vview_d',ns)
    if np > 1:
        r_pass.ramp(0,wp/(np-1))
    else:
        r_pass[0] = wp
    if ns > 1:
        r_stop.ramp(ws,(pi-ws)/(ns-1))
    else:
       r_stop[0] = ws
    rs= pv.create('vview_d',S);rs[0:np]=r_pass; rs[np:]=r_stop #initial ref. set
    D = pv.create('vview_d',int(np+ns)).fill(0.0);D[:np].fill(1.0)
    sp = pv.create('vview_d',int(S)).fill(0.0);
    if np % 2:
        sp[:int(np):2].fill(-1.0);sp[1:int(np):2].fill(1.0)
    else:
        sp[:int(np):2].fill(1.0);sp[1:int(np):2].fill(-1.0)
    ss = sp.empty.fill(0.0);
    ss[int(np):int(S):2].fill(1.0);ss[int(np)+1:int(S):2].fill(-1.0)
    Z = int(2*(L+1-M)-3)
    # begin looping
    Err = 1
    while Err > SN:
       # --------------- calculate cosine coefficients ---------------------------
       x1=pv.create('mview_d',M+4,M+4).fill(0.0)
       x1[M+3,M+3]=-Ks; x1[M+3,M+2]=1.0;x1[M+2,M+1]=1;x1[M+2,M+3]=-Kp
       x1[:S,:M+1]=rs.outer(pv.create('vview_d',M+1).ramp(0,1)).cos
       x1.colview(M+1)[:S]=sp.neg; x1.colview(M+2)[:S]=ss.neg; 
       x2=pv.create('vview_d',D.length+2)
       x2[:D.length]=D;x2[D.length+1]=eta_s;x2[D.length]=eta_p
       x = x1.luSolve(x2)
       a = x[0:M+1].copy
       del_p = x[M+1]
       del_s = x[M+2]
       del_  = x[M+3]
       if (del_p<0) and (del_s<0):
           print('both del_p and del_s are negative!')
       elif del_s < 0:
           # set del_s equal to 0
           print('del_s < 0')
           x1[:S,:M+1]=rs.outer(pv.create('vview_d',M+1).ramp(0,1)).cos
           x1.colview(M+1)[:S]=sp.neg;
           x = x1[:S,:M+2].luSolve(D.copy)
           a = x[0:M+1].copy
           del_p = x[M+1]
           del_s = 0;
       elif del_p < 0:
           # set del_p equal to 0
           print('del_p < 0')
           x1[:S,:M+1]=rs.outer(pv.create('vview_d',M+1).ramp(0,1)).cos
           x1.colview(M+1)[:S]=ss.neg;
           x = x1[:S,:M+2].luSolve(D.copy)
           a = x[0:M+1].copy
           del_p = 0;
           del_s = x[M+1];
       A=pv.create('vview_d',a.length*2-1+Z).fill(0.0)
       A[:a.length]=a; A[1:a.length] *= 0.5; A[a.length+Z:]=A.block.bind(a.length-1,-1,a.length-1)
       Ac=A.rcfft
       A=Ac.realview.copy
       # --------------- determine new reference set-----------------------------
       # Below neg done in place so undo is required
       # Could use A.copy.neg instead but pay a create/destroy penalty
       lmAn=localmax(A.neg);lmA=localmax(A.neg)
       ri=pv.create('vview_d',lmA.length+lmAn.length).fill(0.0)
       ri[:lmA.length]=lmA;ri[lmA.length:]=lmAn;ri.sortip()
       rs=frefine(a,ri*(pi/float(L)))
       rsp=rs.gather(rs.llt(wp).indexbool)
       rss=rs.gather(rs.lgt(ws).indexbool)
       rs=pv.create(rsp.type,rsp.length+rss.length+2).fill(0.0)
       rs[:rsp.length]=rsp;rs[rsp.length:][0]=wp;rs[rsp.length:][1]=ws;
       rs[rsp.length+2:]=rss
       np = rsp.length+1
       ns = rss.length+1
       D = pv.create('vview_d',np+ns).fill(0.0);D[:np].fill(1.0)
       sp = D.empty.fill(0.0)
       if np % 2:
           sp[:int(np):2].fill(-1.0);sp[1:int(np):2].fill(1.0)
       else:
           sp[:int(np):2].fill(1.0);sp[1:int(np):2].fill(-1.0)
       ss = D.empty.fill(0.0);ss[np::2].fill(1.0);ss[np+1::2].fill(-1.0)
       lr = rs.length
       Ar = rs.outer(pv.create(rs.type,M+1).ramp(0,1)).cos.prod(a)

       if lr > M+2:
           sp.putlength(M+2)
           ss.putlength(M+2)
           D.putlength(M+2)
           Ar.putlength(M+2)
           rs.putlength(M+2)
           if A[0] < A[1]:
               cp_ = -1.;
           else:
               cp_ = 1.
           if A[L] < A[L-1]:
               cs = -1.
           else:
               cs = 1.
           if ((A[0]-1.)*cp_ - del_p) < (A[L] * cs - del_s):
               sp.putoffset(1)
               ss.putoffset(1)
               D.putoffset(1)
               Ar.putoffset(1)
               rs.putoffset(1)
       # -------- calculate stopping criterion ------------
       Err = ((Ar - D) - ((Kp*del_+eta_p)*sp + (Ks*del_+eta_s)*ss)).mag.maxval
       print('Err = %20.15f\n'%Err)
    h = pv.create(a.type,2 * a.length -1).fill(0.0)
    a[1:] *= 0.5;a2=a[1:];a1=a.block.bind(a2.length -1 + a2.offset,-1,a2.length);
    h[:a1.length]=a1; h[a1.length]=a[0]; h[a1.length+1:]=a2   # h : filter coefficients
    pi_inv=1.0/pi
    pyplot.figure(1) 
    pyplot.plot((w*pi_inv).list,A.list,'r')
    pyplot.plot((rs*pi_inv).list,Ar.list,'+')
    pyplot.xlabel('w/pi',fontsize=14);pyplot.ylabel('A',fontsize=14)
    pyplot.title('Frequency Response Amplitude',fontsize=16)
    return (h,rs,del_p,del_s)
Beispiel #35
0
aFloatVectorView.mprint(f)
aNewVector.mprint(f)

# <markdowncell>

# Note above we didn't care about the vector on the whole block so we didn't create a reference to it. pyJvsip uses python's garbage collection methods to destroy pyJvsip objects which have no reference.  We can see now the block has been initialized so our second vector has a zero in every other spot. 

# <markdowncell>

# ## Create Function   
# 
# The pyJvsip module has a create function which may be used to create pyJvsip objects including blocks and views. Most of the time you will use this function. When a view is created with this function it creates the needed block under the covers for you.

# <codecell>

aVector=pv.create('vview_d',10)
aVector.ramp(1,1)
aVector.mprint(f)

# <markdowncell>

# Be default when creating a matrix view the view is created as row major.

# <codecell>

aMatrix = pv.create('cmview_d',4,5)
aMatrix.block.vector.ramp(1,1)
aMatrix.imagview.block.vector.ramp(-1,-1)
aMatrix.mprint('%.1f')

# <markdowncell>
Beispiel #36
0
#! python
# Estimate two-norm (sigma0) and singular values (sigma0, sigma1) 
# of a two by two matrix by searching through a dense space of vectors.
# Check by reproducing estimate for A matrix using Aest=U Sigma V^t
# Motivating text Numerical Linear Algebra, Trefethen and Bau
import pyJvsip as pv
from numpy import pi,arccos
from matplotlib.pyplot import *
N=1000 #number of points to search through for estimates
# create a vector 'arg' of evenly spaced angles between [zero, 2 pi)
arg=pv.create('vview_d',N).ramp(0.0,2.0 * pi/float(N))
# Create a matrix 'X' of unit vectors representing Unit Ball in R2
X=pv.create('mview_d',2,N,'ROW').fill(0.0)
x0=X.rowview(0);x1=X.rowview(1)
pv.sin(arg,x0);pv.cos(arg,x1)
# create matrix 'A' to examine
A=pv.create('mview_d',2,2).fill(0.0)
A[0,0]=1.0;A[0,1]=2.0; A[1,1]=2.0;A[1,0]=0.0
# create matrix Y=AX
Y=A.prod(X)
y0=Y.rowview(0);y1=Y.rowview(1)
# create vector 'n' of 2-norms for Y col vectors (sqrt(y0_i^2+y1_i^2))
n=(y0.copy.sq+y1.copy.sq).sqrt 
# Find index of (first) maximum value and (first) minimum value
i=n.maxvalindx; j=n.minvalindx
sigma0=n[i];sigma1=n[j]
# create estimates for V, U, Sigma (S)
V=A.empty.fill(0)
U=V.copy
S=V.copy
S[0,0]=sigma0;S[1,1]=sigma1
Beispiel #37
0
import pyJvsip as pjv
from math import pi as pi
n = 1000
x = pjv.create('vview_d', n).ramp(0, 2 * pi / float(n))
c = pjv.cos(x, x.empty)
cClip = pjv.clip(c, -.9, .9, -.8, .8, c.empty)
Beispiel #38
0
fmt = '%.5f'


def checkBack(A, U, S, VH):
    chk = (A - U[:, 0:S.length].prod(S.mmul(VH.copy.COL))).normFro
    print('%.7f' % chk)
    if chk < A.normFro / (A.rowlength * 1E10):
        print('SVD Decomposition appears to agree with input matrix')
    elif chk < A.normFro / (A.rowlength * 1E5):
        print('SVD Decomposition appears to agree witin 5 decimal places')
    else:
        print('SVD Decomposition does not agree with input matrix')


print('\nTEST')
A = pv.create('cmview_d', 9, 7).fill(0.0)
A.block.vector.realview.randn(3)
A.block.vector.imagview.randn(7)
U, S, VH = svd(A)
checkBack(A, U, S, VH)
print('Singular values are')
S.mprint(fmt)
print('for matrix of type ' + A.type)
A.mprint(fmt)
print('%.7f, %.7f' % (A.normFro, S.normFro))
print('Frobenius norm difference %.10e' % abs(A.normFro - S.normFro))

print('\nTEST')
A = pv.create('cmview_f', 7, 7).fill(0.0)
A.block.vector.realview.randn(3)
A.block.vector.imagview.randn(7)
Beispiel #39
0
# <codecell>

import pyJvsip as pv
f = '%.3f'

# <markdowncell>

# We create a vector and put some date into it. Most elementary operations are done as properties if a method is used. Methods are frequently in-place. Use a function for out of place, or a copy of the input vector.

# <codecell>

try:
    pi
except:
    from math import pi
a = pv.create('vview_d', 10).ramp(0, 2 * pi / 10.0)

# <markdowncell>

# In-Place using method

# <codecell>

a.mprint(f)
a.sin
a.mprint(f)

# <markdowncell>

# out-of-place using method
Beispiel #40
0
    fd = open(fname, 'r')
    N = int(fd.readline())
    x = pv.create('vview_f', N)
    y = x.empty
    for i in range(N):
        ln = fd.readline().partition(' ')
        x[i] = float(ln[0])
        y[i] = float(ln[2])
    fd.close()
    return (x, y)


N_data = 24576
dec1 = 1
dec3 = 3
kernel = pv.create('vview_f', 128).kaiser(15.0)
r_state = pv.Rand('NPRNG', 11)
data = pv.create('vview_f', 2 * N_data)
noise = pv.create('vview_f', 3 * N_data)
avg = pv.create('vview_f', 4 * N_data)
data.putlength(int((N_data - 1) / dec1) + 1)
avg.putlength(int((N_data - 1) / dec1) + 1)
data.putstride(2)
avg.putstride(4)
noise.putlength(N_data)
noise.putstride(3)
conv = pv.CONV('conv1d_f', kernel, pv.VSIP_NONSYM, N_data, dec1,
               pv.VSIP_SUPPORT_SAME, 0, 0)
fir = pv.FIR('fir_f', kernel, 'NONE', N_data, dec1, 'NO')
avg.fill(0.0)
for i in range(10):
Beispiel #41
0
# read vector data from file fname; counterpart of vwritexy
def VU_vfreadxy(fname):
    fd = open(fname,'r')
    N=int(fd.readline())
    x=pv.create('vview_f',N)
    y=x.empty
    for i in range(N):
        ln=fd.readline().partition(' ')
        x[i]=float(ln[0])
        y[i]=float(ln[2])
    fd.close()
    return(x,y)
N_data = 24576
dec1 = 1
dec3 = 3
kernel=pv.create('vview_f',128).kaiser(15.0)
r_state  = pv.Rand('NPRNG',11)
data  = pv.create('vview_f', 2 * N_data)
noise = pv.create('vview_f', 3 * N_data)
avg   = pv.create('vview_f', 4 * N_data)
data.putlength(int((N_data-1)/dec1)+1)
avg.putlength(int((N_data-1)/dec1)+1)
data.putstride(2)
avg.putstride(4)
noise.putlength(N_data)
noise.putstride(3)
conv = pv.CONV('conv1d_f',kernel,pv.VSIP_NONSYM, N_data,dec1,pv.VSIP_SUPPORT_SAME,0,0)
fir  = pv.FIR('fir_f', kernel,'NONE', N_data,dec1,'NO')
avg.fill(0.0)
for i in range(10):
    r_state.randn(noise)
Beispiel #42
0
#see tasp_core_plus_book.pdf example 11 for more info on this example
import pyJvsip as pv
from matplotlib import pyplot
N=1024
avg=1000
D=2
init=17
dataIn = pv.create('vview_f',D*N)
dataFFT = pv.create('cvview_f',int(N/2) + 1)
dataOut  = pv.create('vview_f',N)
spect_avg = pv.create('vview_f',int(N/2) + 1)
spect_new = pv.create('vview_f',int(N/2) + 1)
state = pv.Rand('NPRNG',N)
fft=pv.FFT('rcfftop_f',(N,1,0,0))
b = [0.0234, -0.0094, -0.0180, -0.0129,  0.0037, 
    0.0110, -0.0026, -0.0195, -0.0136,  0.0122,
    0.0232, -0.0007, -0.0314, -0.0223,  0.0250,
    0.0483, -0.0002, -0.0746, -0.0619,  0.0930,
    0.3023,  0.3999,  0.3023,  0.0930, -0.0619,
    -0.0746, -0.0002,  0.0483,  0.0250, -0.0223,
    -0.0314, -0.0007,  0.0232,  0.0122, -0.0136,
    -0.0195, -0.0026,  0.0110,  0.0037, -0.0129,
    -0.0180 ,-0.0094,  0.0234]
fir = pv.FIR('fir_f',pv.listToJv('vview_f',b),'NONE',D*N,D,'NO')
spect_avg.fill(0.0)
for i in range(avg):
    state.randu(dataIn)
    pv.add(-.5,dataIn,dataIn)
    fir.flt(dataIn,dataOut)
    fft.dft(dataOut,dataFFT)
    pv.cmagsq(dataFFT,spect_new)
Beispiel #43
0
#* Created RJudd */
#* pyJvsip version of c_VSIP_example example1

import pyJvsip as pv
N = 8
A = pv.create('vview_f', N).ramp(0, 1)
B = A.empty.fill(5.0)
C = A.empty.fill(0.0)
print('A = ')
A.mprint('%2.0f')
print('B = ')
B.mprint('%2.0f')
pv.add(A, B, C)
print('C = A+B')
print('C = ')
C.mprint('%2.0f')
Beispiel #44
0
# These are examples of operations defined in the C VSIPL spec in the Vector and Elementwise chapter under Elementary Math Functions.

# <codecell>

import pyJvsip as pv
f='%.3f'

# <markdowncell>

# We create a vector and put some date into it. Most elementary operations are done as properties if a method is used. Methods are frequently in-place. Use a function for out of place, or a copy of the input vector.

# <codecell>

try: pi
except: from math import pi
a=pv.create('vview_d',10).ramp(0,2*pi/10.0)

# <markdowncell>

# In-Place using method

# <codecell>

a.mprint(f)
a.sin
a.mprint(f)

# <markdowncell>

# out-of-place using method
Beispiel #45
0
def dftCoef(n):
    m = pv.create('mview_d', n, n)
    for i in range(n):
        for j in range(n):
            m[i, j] = (i * j) % n
    return m
Beispiel #46
0
#!python
# Example of solving Ax=b using LU
import pyJvsip as pv
import cProfile
#create a non-singular matrix
def mySolve(A,x):
    p,l,u=A.plu
    u.usolve(l.lsolve(x.permute(p)))
N=100
A=pv.create('mview_d',N,N).fill(-1.0)
A.diagview(0).ramp(1,1.1)
A.diagview(1).fill(2)
A.diagview(-1).fill(2)
#create a test 'good x' vector
x0=pv.create('vview_d',A.rowlength).ramp(.1,.1)
# create and calculate a y vector based on x vector
y0=A.prod(x0)
#Create a copy of y0 to solve for x0
xy=y0.copy
#check to make sure A non-singular. Det uses up A so make copy 
if A.copy.det < .0001:
    print('Matrix may be singular')
#solve longhand using plu and triangular backsolverssolvers (mySolve Above)
print('RESULTS for mySolve function using plu method and backsolvers')
cProfile.run('mySolve(A,xy)')
#check to see if xy is x0
chk=(xy-x0).sumsqval
if chk > .000001:
    print('We don\'t seem to have the right answer using mySolve; check=%e'%chk)
else:
    print('Check only %e. This is probably correct for mySolve function'%chk)
Beispiel #47
0
import pyJvsip as pjv
L = 9
a=pjv.create('vview_f',L);
b=a.empty
ab_bl=pjv.create('vview_bl',L);
a.ramp(-2.0,1)
b.ramp(2.0,-1)
print('index    A       B\n')
for i in range(L):
    print('%3i %7.1f %7.1f\n'%(i,a[i],b[i]))
_=pjv.leq(a,b,ab_bl)
if ab_bl.anytrue:
    ab_vi=ab_bl.indexbool
    for i in range(ab_vi.length):
        print('A = B at index %3i\n'%int(ab_vi[i]))
else:
    print('No true cases')
Beispiel #48
0
f = "%.5f"

# <markdowncell>

# Solve using LU Class

# <markdowncell>

# Create some data A x = b
# Note we create an x and calculate a b directly. To demonstrate we use A and b to estimate x.

# <codecell>

n = 5
A = pv.create("mview_d", n, n).fill(0.0)
A.randn(5)
x = pv.create("vview_d", n).randn(9)
print("Matrix A")
A.mprint(f)
print("Known x vector")
x.mprint(f)
b = A.prod(x)
print("Calculated b=Ax vector")
b.mprint(f)

# <markdowncell>

# Both LU and SV overwrite the input matrix; so to preserve our original matrix we use a copy. The LU (and SV) object will keep a reference to the copy (which means python will not garbage collect it). We solve this first using SVD. We don't overwrite our original vector so we can compare the estimated solution to the actual solution. SVD does not have a solver so we do it long hand.

# <codecell>
def eye(t,n): # create and return an identity matrix of size n and type t
    return pv.create(t,n,n).identity
Beispiel #50
0
 def srmp(t, s, e):
     assert isinstance(e, int) and isinstance(
         s, int), 'start and end must be intergers'
     assert e > s, 'simple ramps alwasy go up; end > start '
     return pv.create(t, e - s + 1).ramp(s, 1)
Beispiel #51
0
def svdBidiag(A,eps):
    eps0 = A.normFro/float(A.rowlength) * eps
    B=A.copy
    bidiag(B)
    L=UmatExtract(B)
    R=VHmatExtract(B)
    b=B.diagview
    d,f=biDiagPhaseToZero(B,L, b(0), b(1), R, eps0)
    return(L,d,f,R,eps0)


# #### Example

# In[11]:

A=pv.create('mview_f',6,5).fill(0.0)
A[2,2]=3.0; A[3,4]=5.0
L,d,f,R,eps0=svdBidiag(A,1E-10)


# In[12]:

#make up some data
A=pv.create('cmview_f',6,5).randn(5)
A.mprint('%.3f')


# In[13]:

# bidiagonlize and then check to see if we can get back the origional matrix 
L,d,f,R,eps0=svdBidiag(A,1E-10)
Beispiel #52
0
def firebp(n, w1, w2, Del):
    """[h,rs,be] = firebp(n,w1,w2,Del);
       Bandpass linear phase FIR filter design with specified ripple sizes.
       - the derivative of the frequency response amplitude at w1 is
         set equal to the negative derivative of the amplitude at w2 -
       author : Ivan Selesnick, Rice University, Dec 94
       parameters
        h   : filter of length 2*n+1
        rs  : reference set upon convergence
        be  : band edges of h
        w1  : first half-magnitude frequency
        w2  : second half-magnitude frequency
           w1 < w2,  w1,w2 should be in the interval (0,pi)
        Del : [ripple size in first stopband, passband, second stopband]
       subprograms needed
        localmax, frefine, firfbe
         EXAMPLE
        n   =  23;
        w1  = 0.14 * 2*pi;
        w2  = 0.34 * 2*pi;
        Del = [0.01 0.03 0.01];
        [h,rs,be] = firebp(n,w1,w2,Del);
    """
    def rem(a, b):
        return a % b

    try:
        log2
    except:

        def log2(a):
            return log(a, 2)

    def mp(t, lngth):  #minus_plus vector [-1 1 -1 ... (-1)^lngth]
        assert isinstance(lngth,
                          int), 'Length argument to mp must be an integer. '
        assert isinstance(t,str) and t in ['vview_d','vview_f'],\
                     "type argument must be 'vview_d' or 'vview_f'"
        retview = create(t, lngth).fill(1.0)
        retview[:lngth:2].fill(-1.0)
        return retview

    def diff(a):
        assert 'pyJvsip' in repr(a), 'diff only works with pyJvsip views'
        assert 'vview' in a.type, 'diff only works with vector views'
        if a.length > 1:
            retview = a[1:a.length] - a[:a.length - 1]
            return retview
        else:
            return None

    def srmp(t, s, e):
        assert isinstance(e, int) and isinstance(
            s, int), 'start and end must be intergers'
        assert e > s, 'simple ramps alwasy go up; end > start '
        return pv.create(t, e - s + 1).ramp(s, 1)

    def coscoef(rs, n, V, Y):
        f = {'vview_d': 'mview_d', 'vview_f': 'mview_f'}
        m = rs.length + 1
        y = pv.create(Y.type, m)
        y[:Y.length] = Y
        y[m - 1] = 0.0
        A = pv.create(f[y.type], m, y.length)
        a = A[:rs.length, :n + 1]
        a = pv.outer(1, rs, pv.create(rs.type, n + 1).ramp(0, 1), a).cos
        at = A.rowview(m - 1)
        A.rowview(m - 1)[:] = V[:]
        return A.luSolve(y)

    # ------------------ initialize constants ---------------------------
    assert 'pyJvsip' in repr(Del) and Del.type in ['vview_f','vview_d'],\
                "last argument is a vector view of type 'vview_f' or 'vview_d'"
    t = Del.type
    L = int(pow(2, int(ceil(log2(10 * n)))))
    w = pv.create(t, L + 1).ramp(0, pi / float(L))  # frequency axis
    N = n - 2  # number of _non-specified interpolation points
    SN = 1e-8  # Small Number (stopping criterion)
    H0 = pv.create(t, 2 * (L + 1 - n) + 2 * n - 2).fill(0.0)
    V = pv.create(t, n + 1).ramp(0, 1)
    V *= V.empty.ramp(0, w1).sin + V.empty.ramp(0, w2).sin
    ideal = Del.empty.fill(0.0)
    ideal[1] = 1.0
    up = ideal + Del
    lo = ideal - Del
    D1 = 0.5  # half-magnitude value at w1
    D2 = 0.5  # half-magnitude value at w2
    PF = False  # PF : flag : Plot Figures
    # ------------------ initialize reference set ----------------------------
    # n1 : number of reference frequencies in first stopband
    # n2 : number of reference frequencies in passband
    # n3 : number of reference frequencies in second stopband
    n2 = int(round(N * (w2 - w1) / pi))
    if rem(n2, 2) == 0:
        n2 = n2 - 1
    n1 = int(floor((N - n2) * w1 / (pi - (w2 - w1))))
    n3 = N - n2 - n1
    rs = pv.create(t, n1 + n2 + n3 + 2)
    R1 = rs[0:n1].ramp(0, 1)
    R1 *= w1 / float(n1)
    R2 = rs[n1 + 1:n1 + 1 + n2].ramp(1, 1)
    R2 *= (w2 - w1) / float(n2 + 1)
    R2 += w1
    R3 = rs[n1 + n2 + 2:n1 + n2 + 2 + n3].ramp(1, 1)
    R3 *= (pi - w2) / float(n3)
    R3 += w2
    rs[n1] = w1
    rs[n1 + 1 + n2] = w2
    # ------------------ initialize interpolation values ---------------------
    Y = pv.create(t, n1 + n2 + n3 + 2).fill(0.0)
    if n1 % 2 == 0:
        Y[0:n1:2].fill(up[0])
        Y[1:n1:2].fill(lo[0])
    else:
        Y[0:n1:2].fill(lo[0])
        Y[1:n1:2].fill(up[0])
    Y[n1] = D1
    o = n1 + 1
    l = n1 + 1 + n2
    Y[o:l:2].fill(up[1])
    Y[o + 1:l:2].fill(lo[1])
    Y[l] = D2
    o = l + 1
    l = o + n3
    Y[o:l:2].fill(lo[2])
    Y[o + 1:l:2].fill(up[2])
    # begin looping
    Err = 1
    while Err > SN:
        # --------------- calculate cosine coefficients -----------------------
        a = coscoef(rs, n, V, Y)
        # --------------- calculate frequency response ------------------------
        #H = real(fft([a(1);a(2:n+1)/2;Z;a(n+1:-1:2)/2])); H = H(1:L+1);
        attr = a.attrib
        ot = attr['offset']
        lt = attr['length']
        st = attr['stride']
        ot += (lt - 1) * st
        st = -st
        attr['offset'] = ot
        attr['stride'] = st
        a_rev = a.cloneview
        a_rev.putattrib(attr)
        H0.fill(0.0)
        H0[0] = a[0]
        H0[1:lt] = a[1:] * 0.5
        H0[H0.length - n:] = a_rev[:lt - 1] * 0.5
        Hc = H0.rcfft
        H = Hc.realview[:L + 1].copy
        # --------------- determine local max and min -------------------------
        v1 = localmax(H)
        v2 = localmax(H.copy.neg)
        if v1[0] < v2[0]:
            s = 0
        else:
            s = 1
        #ri = sortip([v1; v2]);
        ri = pv.create(H.type, v1.length + v2.length)
        ri[:v1.length] = v1
        ri[v1.length:] = v2
        ri.sortip()
        rs = (ri) * (pi / L)
        rs = frefine(a, rs)
        n1 = rs.llt(w1).sumval
        n2 = pv.bb_and(rs.lgt(w1), rs.llt(w2),
                       pv.create('vview_bl', rs.length)).sumval
        n3 = rs.lgt(w2).sumval
        # --------------- calculate frequency response at local max and min ---
        Hr = rs.outer(srmp(rs.type, 0, n)).cos.prod(a)
        Id = pv.create(Del.type, n1 + n2 + n3)
        Dr = Id.empty
        Id[:n1].fill(ideal[0])
        Id[n1:n1 + n2].fill(ideal[1])
        Id[n1 + n2:].fill(ideal[2])
        Dr[:n1].fill(Del[0])
        Dr[n1:n1 + n2].fill(Del[1])
        Dr[n1 + n2:].fill(Del[2])
        Er = (Hr - Id) * Dr.recip
        # Plot Figures if PF is True
        if PF:
            figure(1)
            plot((w * (1. / pi)).list, H.list)
            hold(True)
            plot((rs * (1.0 / pi)).list, Hr.list, 'o')
            hold(False)
            axis([0, 1, -.2, 1.2])
            figure(2)
            plot(Er.list)
            hold(True)
            plot(Er.list, 'o'),
            hold(False)
            pause(0.05)
        # --------------- calculate new interpolation points  -----------------
        Y = Id.empty
        if n1 % 2 == 0:
            Y[0:n1:2].fill(up[0])
            Y[1:n1:2].fill(lo[0])
        else:
            Y[0:n1:2].fill(lo[0])
            Y[1:n1:2].fill(up[0])
        Y[n1 + 1:n1 + n2:2].fill(lo[1])
        Y[n1:n1 + n2:2].fill(up[1])
        Y[n1 + n2 + 1::2].fill(up[2])
        Y[n1 + n2::2].fill(lo[2])
        # --------------- slim down the set of local max and min --------------
        # --------------- to obtain new reference set of correct size ---------
        if (rs.length - N) == 1:  # remove one frequency from the reference set
            if abs(Er[0] - Er[1]) < abs(Er[rs.length - 1] - Er[rs.length - 2]):
                I = 1
                s = s + 1
            else:
                I = rs.length
            del rs[I - 1]
            del Y[I - 1]
            del Er[I - 1]
        elif (rs.length - N) == 2:  # remove two frequencies
            # remove either the two endpoints or remove two adjacent points in the ref. set
            k = (diff(Er) * (mp(Er.type, rs.length - 1) + s)).minvalindex + 1
            if (k == 1) | (k == (rs.length - 1)):
                if k == 1:
                    I = 1
                    s = s + 1
                else:
                    I = rs.length
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
                if abs(Er[0] - Er[1]) < abs(Er[rs.length - 1] -
                                            Er[rs.length - 2]):
                    I = 1
                    s = s + 1
                else:
                    I = rs.length
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
            else:
                I = k
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
        elif (rs.length - N) == 3:  # remove three frequencies
            # just use the code for (rs.length-N)==1, followed by the code for (rs.length-N)==2
            if abs(Er[0] - Er[1]) < abs(Er[rs.length - 1] - Er[rs.length - 2]):
                I = 1
                s = s + 1
            else:
                I = rs.length
            del rs[I - 1]
            del Y[I - 1]
            del Er[I - 1]
            k = (diff(Er) * (mp(Er.type, rs.length - 1) + s)).minvalindex + 1
            if (k == 1) or (k == (rs.length - 1)):
                if k == 1:
                    I = 1
                    s = s + 1
                else:
                    I = rs.length
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
                if abs(Er[0] - Er[1]) < abs(Er[rs.length - 1] -
                                            Er[rs.length - 2]):
                    I = 1
                    s = s + 1
                else:
                    I = rs.length
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
            else:
                I = k
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
                del rs[I - 1]
                del Y[I - 1]
                del Er[I - 1]
        # END IF
        # calculate error
        Err = Er.maxmgval - 1  #max(abs(Er))-1
        print('    Err = %20.15f\n' % Err)

        if Err > SN:
            # --------------- update new interpolation points ---------------------
            n1 = rs.llt(w1).sumval
            n2 = pv.bb_and(rs.lgt(w1), rs.llt(w2),
                           pv.create('vview_bl', rs.length)).sumval
            n3 = rs.lgt(w2).sumval
            Y1 = Y[0:n1].copy
            Y2 = Y[n1:n1 + n2].copy
            Y3 = Y[n1 + n2:].copy
            Y = pv.create(Y1.type, n1 + n2 + n3 + 2)
            Y[0:n1] = Y1
            Y[n1] = D1
            Y[n1 + 1:n1 + n2 + 1] = Y2
            Y[n1 + n2 + 1] = D2
            Y[n1 + n2 + 2:] = Y3
            # --------------- update new reference set ----------------------------
            #rs = [rs(1:n1); w1; rs(n1+1:n1+n2); w2; rs(n1+n2+1:n1+n2+n3)]
            rst1 = rs[0:n1].copy
            rst2 = rs[n1:n1 + n2].copy
            rst3 = rs[n1 + n2:].copy
            rs = pv.create(rst1.type, rs.length + 2)
            rs[:n1] = rst1
            rs[n1] = w1
            rs[n1 + 1:n1 + n2 + 1] = rst2
            rs[n1 + n2 + 1] = w2
            rs[n1 + n2 + 2:] = rst3
    #end while
    # ------------------ calculate band edges ----------------------------
    be = firfbe(a, pv.listToJv(Del.type, [w1, w1, w2, w2]),
                pv.listToJv(Del.type, [up[0], lo[1], lo[1], up[2]]))
    # ------------------ calcuate filter coefficients ----------------------------
    #h = [a(n+1:-1:2)/2; a(1); a(2:n+1)/2]
    a[1:] *= 0.5
    atr = a[1:].attrib
    h = pv.create(Del.type, 2 * a.length - 1)
    h[a.length - 1] = a[0]
    h[a.length:] = a[1:]
    atr = a[1:].attrib
    atr['offset'] = atr['length']
    atr['stride'] *= -1
    a.putattrib(atr)
    h[:a.length] = a
    figure(1)
    plot((w * (1.0 / pi)).list, H.list)
    hold(True)
    plot((rs * (1.0 / pi)).list, Y.list, 'x')
    hold(False)
    axis([0, 1, -.2, 1.2])
    xlabel('w')
    ylabel('H')
    title('Frequency Response Amplitude')
    return (h, rs, be)
Beispiel #53
0

# Function myDet uses definition of Determinant in most
# Linear Algebra Texts by expansion of cofactors along a row
def myDet(A):
    m = A.collength
    det = 0
    if m == 1:
        return A[0, 0]
    for i in range(m):
        det += A[0, i] * pow(-1, i) * myDet(A.submatrix(0, i))
    return det


# For example we need some data so we Create a non-singular matrix 'A'
A = pv.create('mview_d', N, N).fill(-1.0)
A.diagview(0).ramp(1, 1.1)
A.diagview(1).fill(2)
A.diagview(-1).fill(2)
# Find the determinant with pyJvsip det method
d0 = A.copy.det
# Find the determinant using myDet funciton
d1 = myDet(A)
#compare
print('difference d0 - d1 = %f - %f = %f' % (d0, d1, d0 - d1))
# Below we do some timing measureents to show inefficiency of myDet.
sMyDet = 'def myDet(A):\n    m = A.collength\n    det = 0\n    if m == 1:\n'
sMyDet += '        return A[0,0]\n    for i in range(m):\n'
sMyDet += '        det += A[0,i] * pow(-1,i) * myDet(A.submatrix(0,i))\n'
sMyDet += '    return det\n'
Amake = 'A=pv.create(\'mview_d\',N,N).fill(-1.0)\n'