Commit b8db1e48 authored by Christophe Favergeon's avatar Christophe Favergeon
Browse files

Fixes to Python wrapper examples. Changes to work with Python 3.12

parent 60f22e09
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
cmake -DHOST=YES ^
  -DLOOPUNROLL=ON ^
  -DWRAPPER=YES ^
  -DCMSISDSP=".." ^
  -DCMSISDSP="path to CMSIS-DSP folder" ^
  -DCMAKE_C_FLAGS_RELEASE="-std=c11 -Ofast -ffast-math -DNDEBUG -Wall -Wextra" ^
  -DCMAKE_CXX_FLAGS_RELEASE="-fno-rtti -std=c++11 -Ofast -ffast-math -DNDEBUG -Wall -Wextra -Wno-unused-parameter" ^
  -G "Unix Makefiles" ..
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@
#include <numpy/numpyconfig.h>

// API version used on google colab
// List in https://github.com/numpy/numpy numpyconfig.h
// https://github.com/numpy/numpy/blob/main/numpy/_core/include/numpy/numpyconfig.h
#if (NPY_API_VERSION != 0x0000000F )
//#error("Error building with wrong NumPy API version")
#endif
+53 −54
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import scipy.fft
import colorama
from colorama import init,Fore, Back, Style
from numpy.testing import assert_allclose
import scipy.spatial.distance as d 

init()

@@ -19,63 +20,61 @@ def printSubTitle(s):
    print("\n" + Style.BRIGHT + s + Style.RESET_ALL)


def chop(A, eps = 1e-6):
    B = np.copy(A)
    B[np.abs(A) < eps] = 0
    return B
def packset(a):
    b = np.packbits(a)
    newSize = int(np.ceil(b.shape[0] / 4.0)) * 4
    c = np.copy(b).astype(np.uint32)
    c.resize(newSize)
    #print(c)
    vecSize = round(newSize/4)
    c=c.reshape(vecSize,4)
    #print(c)
    r = np.zeros(vecSize)
    result = []
    for i in range(0,vecSize):
        print(c[i,:])
        #print("%X %X %X %X" % (c[i,0],c[i,1],c[i,2],c[i,3]))
        d = (c[i,0] << 24) | (c[i,1] << 16) | (c[i,2] << 8) | c[i,3] 
        result.append(np.uint32(d))
    return(result) 

nb = 32
signal = np.cos(2 * np.pi * np.arange(nb) / nb)*np.cos(0.2*2 * np.pi * np.arange(nb) / nb)
nb = 34
#va = np.random.choice([0,1],nb)
# Array of word32 containing all of our bits
#pva = packset(va)

ref=scipy.fft.rfft(signal)
invref = scipy.fft.irfft(ref)

print(f"ref length = {len(ref)}")
print(ref)
#vb = np.random.choice([0,1],nb)
# Array of word32 containing all of our bits
#pvb = packset(vb)
#
va=[1, 0, 1, 0, 1, 1, 1, 0 ,0, 1, 1, 0, 1, 0, 0, 0, 0, 1,0,0,0,1,1,0,1,0,1,0,0,1,1,1,1,1]
vb=[0,1,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,0]

va = np.array(va)
vb = np.array(vb)

pva=packset(va)
pvb=packset(vb)

#pva = [np.uint32(167), np.uint32(0)]
#pvb = [np.uint32(152), np.uint32(0)]

#print(va,pva)
#print(vb,pvb)

ctt=1.0*np.count_nonzero((va==1) & (vb==1))
ctf=1.0*np.count_nonzero((va==1) & (vb==0))
cft=1.0*np.count_nonzero((va==0) & (vb==1))

res=(cft+ctf)/(2*ctt+cft+ctf)

# Convert ref to CMSIS-DSP format 
referenceFloat=np.zeros(2*len(ref))
print(f"referenceFloat length = {len(referenceFloat)}")
# Replace complex datatype by real datatype
referenceFloat[0::2] = np.real(ref)
referenceFloat[1::2] = np.imag(ref)
# Copy Nyquist frequency value into first 
# sample.This is just a storage trick so that the
# output of the RFFT has same length as input
# It is legacy behavior that we need to keep
# for backward compatibility but it is not
# very pretty
#referenceFloat[1] = np.real(ref[-1])

rifftQ31=dsp.arm_rfft_instance_q31()
status=dsp.arm_rfft_init_q31(rifftQ31,nb,1,1)
# Apply CMSIS-DSP scaling
referenceQ31 = f.toQ31(referenceFloat / nb) 

resultQ31 = dsp.arm_rfft_q31(rifftQ31,referenceQ31)
resultF = f.Q31toF32(resultQ31)

print(f"resultF length = {len(resultF)}")
assert_allclose(invref/nb,resultF,atol=1e-6)

signalQ31 = f.toQ31(signal)
rfftQ31=dsp.arm_rfft_instance_q31()
status=dsp.arm_rfft_init_q31(rfftQ31,nb,0,1)
resultQ31 = dsp.arm_rfft_q31(rfftQ31,signalQ31)
print(len(resultQ31))
print(2*nb)
resultF = f.Q31toF32(resultQ31) * nb

def compareWithConjugatePart(r):
    res = r[0::2] + 1j * r[1::2]
    conjPart = res[nb:nb//2:-1].conj()
    refPart = res[1:nb//2]
    assert(np.equal(refPart , conjPart).all())

compareWithConjugatePart(resultF)

res = resultF[0::2] + 1j * resultF[1::2]
print(res)

print(res[0:nb//2+1])
print(res[0:nb//2+1].shape)
 No newline at end of file

print("\nDice")
ref=d.dice(va,vb)
res=dsp.arm_dice_distance(pva,pvb,nb)
print(ref)
print(res)
assert_allclose(ref,res,1e-6)
+18 −16
Original line number Diff line number Diff line
@@ -122,26 +122,28 @@ plt.show()

printSubTitle("With a window")

nan = np.nan 

referenceDistance = 0.617099940776825
referenceCost=np.array([[9.1612804e-01, 9.9920368e-01, np.NAN, np.NAN,
        np.NAN],
       [1.2353053e+00, 1.6792301e+00, np.NAN, np.NAN,
        np.NAN],
       [1.3028694e+00, 2.3696373e+00, 4.4372001e+00, np.NAN,
        np.NAN],
       [np.NAN, 3.0795674e+00, 4.9687119e+00, np.NAN,
        np.NAN],
       [np.NAN, 3.5039051e+00, 4.9290380e+00, 5.3565612e+00,
        np.NAN],
       [np.NAN, np.NAN, 4.8520918e+00, 5.1756082e+00,
        np.NAN],
       [np.NAN, np.NAN, 5.0427418e+00, 5.8497019e+00,
referenceCost=np.array([[9.1612804e-01, 9.9920368e-01, nan, nan,
        nan],
       [1.2353053e+00, 1.6792301e+00, nan, nan,
        nan],
       [1.3028694e+00, 2.3696373e+00, 4.4372001e+00, nan,
        nan],
       [nan, 3.0795674e+00, 4.9687119e+00, nan,
        nan],
       [nan, 3.5039051e+00, 4.9290380e+00, 5.3565612e+00,
        nan],
       [nan, nan, 4.8520918e+00, 5.1756082e+00,
        nan],
       [nan, nan, 5.0427418e+00, 5.8497019e+00,
        7.6590457e+00],
       [np.NAN, np.NAN, np.NAN, 6.7571073e+00,
       [nan, nan, nan, 6.7571073e+00,
        8.6668968e+00],
       [np.NAN, np.NAN, np.NAN, 7.3949833e+00,
       [nan, nan, nan, 7.3949833e+00,
        9.0352430e+00],
       [np.NAN, np.NAN, np.NAN, np.NAN,
       [nan, nan, nan, nan,
        9.2564993e+00]], dtype=np.float32)


+11 −3
Original line number Diff line number Diff line
@@ -6,6 +6,14 @@ from numpy.testing import assert_allclose
a=[1,2,3]
b=[1,5,2]

def kulsinski(va,vb):
    n = len(va)
    ctt=1.0*np.count_nonzero((va==1) & (vb==1))
    ctf=1.0*np.count_nonzero((va==1) & (vb==0))
    cft=1.0*np.count_nonzero((va==0) & (vb==1))
    return(1.0*(ctf + cft - ctt+n)/(cft + ctf + n))


print("\nBray-Curtis")
ref=d.braycurtis(a,b)
res=dsp.arm_braycurtis_distance_f32(a,b)
@@ -96,7 +104,7 @@ assert_allclose(ref,res,1e-6)
def packset(a):
    b = np.packbits(a)
    newSize = int(np.ceil(b.shape[0] / 4.0)) * 4
    c = np.copy(b)
    c = np.copy(b).astype(np.uint32)
    c.resize(newSize)
    #print(c)
    vecSize = round(newSize/4)
@@ -105,7 +113,7 @@ def packset(a):
    r = np.zeros(vecSize)
    result = []
    for i in range(0,vecSize):
        #print(c[i,:])
        print(c[i,:])
        #print("%X %X %X %X" % (c[i,0],c[i,1],c[i,2],c[i,3]))
        d = (c[i,0] << 24) | (c[i,1] << 16) | (c[i,2] << 8) | c[i,3] 
        result.append(np.uint32(d))
@@ -143,7 +151,7 @@ print(res)
assert_allclose(ref,res,1e-6)

print("\nKulsinski")
ref=d.kulsinski(va,vb)
ref=kulsinski(va,vb)
res=dsp.arm_kulsinski_distance(pva,pvb,nb)
print(ref)
print(res)
Loading