Skip to content

Commit

Permalink
Updates to Python wrapper
Browse files Browse the repository at this point in the history
Python 3.12 support
Bug corrections
Remove dependencies to compute graph (now is CMSIS-Stream on
another repository).
  • Loading branch information
christophe0606 committed Jun 17, 2024
2 parents 5f5086f + b8db1e4 commit 3ebc686
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 138 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ include PythonWrapper_README.md
recursive-include Include *.h
recursive-include PrivateInclude *.h
recursive-include PythonWrapper/cmsisdsp_pkg/src *.h
include cmsisdsp/cg/scheduler/templates/*
include Source/DistanceFunctions/arm_boolean_distance_template.h

4 changes: 2 additions & 2 deletions PythonWrapper/build_linux/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ cmake -DHOST=YES \
-DLOOPUNROLL=ON \
-DWRAPPER=YES \
-DCMAKE_POSITION_INDEPENDENT_CODE=YES \
-DCMSISDSP="path to CMSIS-DSP folder" \
-DCMSISDSP=".." \
-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" ..

# For Mac universal lib
# -arch x86_64 -arch arm64 -mmacosx-version-min=11.0
# -arch x86_64 -arch arm64 -mmacosx-version-min=11.0
8 changes: 4 additions & 4 deletions PythonWrapper/cmsisdsp_pkg/src/cmsisdsp_distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ cmsis_arm_dtw_init_window_q7(PyObject *obj,
arm_matrix_instance_q7 pSrc_converted; // input


if (PyArg_ParseTuple(args,"iiO",&winType,&winSize,&pSrc));
if (PyArg_ParseTuple(args,"iiO",&winType,&winSize,&pSrc))
{

q7MatrixFromNumpy(&pSrc_converted,pSrc);
Expand Down Expand Up @@ -267,7 +267,7 @@ cmsis_arm_dtw_distance_f32(PyObject *obj,
arm_matrix_instance_f32 dtw_converted;


if (PyArg_ParseTuple(args,"OO",&pDist,&pWin));
if (PyArg_ParseTuple(args,"OO",&pDist,&pWin))
{

f32MatrixFromNumpy(&pDist_converted,pDist);
Expand Down Expand Up @@ -348,7 +348,7 @@ cmsis_arm_dtw_path_f32(PyObject *obj,
Py_DECREF(pDstOBJ);
return(pythonResult);
}

Py_RETURN_NONE;
}

static PyMethodDef CMSISDSPMethods[] = {
Expand Down Expand Up @@ -450,4 +450,4 @@ void CAT(init,MODINITNAME)(void)
#ifdef IS_PY3K
return module;
#endif
}
}
4 changes: 2 additions & 2 deletions PythonWrapper/cmsisdsp_pkg/src/cmsisdsp_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
#include <numpy/numpyconfig.h>

// API version used on google colab
// List on https://github.com/numpy/numpy/blob/main/numpy/core/include/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")
//#error("Error building with wrong NumPy API version")
#endif

#ifdef WIN
Expand Down
40 changes: 20 additions & 20 deletions PythonWrapper/cmsisdsp_pkg/src/cmsisdsp_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ cmsis_arm_welch_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -125,7 +125,7 @@ cmsis_arm_bartlett_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -178,7 +178,7 @@ cmsis_arm_hamming_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -231,7 +231,7 @@ cmsis_arm_hanning_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -284,7 +284,7 @@ cmsis_arm_nuttall3_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -337,7 +337,7 @@ cmsis_arm_nuttall4_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -390,7 +390,7 @@ cmsis_arm_nuttall3a_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -443,7 +443,7 @@ cmsis_arm_nuttall3b_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -496,7 +496,7 @@ cmsis_arm_nuttall4a_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -549,7 +549,7 @@ cmsis_arm_blackman_harris_92db_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -602,7 +602,7 @@ cmsis_arm_nuttall4b_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -655,7 +655,7 @@ cmsis_arm_nuttall4c_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -708,7 +708,7 @@ cmsis_arm_hft90d_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -761,7 +761,7 @@ cmsis_arm_hft95_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -814,7 +814,7 @@ cmsis_arm_hft116d_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -867,7 +867,7 @@ cmsis_arm_hft144d_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -920,7 +920,7 @@ cmsis_arm_hft169d_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -973,7 +973,7 @@ cmsis_arm_hft196d_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -1026,7 +1026,7 @@ cmsis_arm_hft223d_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down Expand Up @@ -1079,7 +1079,7 @@ cmsis_arm_hft248d_f64(PyObject *obj, PyObject *args)
{


float32_t *pDst=NULL; // output
float64_t *pDst=NULL; // output
int nb; // input

if (PyArg_ParseTuple(args,"i",&nb))
Expand Down
107 changes: 53 additions & 54 deletions PythonWrapper/examples/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import colorama
from colorama import init,Fore, Back, Style
from numpy.testing import assert_allclose
import scipy.spatial.distance as d

init()

Expand All @@ -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)

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)
Loading

0 comments on commit 3ebc686

Please sign in to comment.