Skip to content

Commit b8db1e4

Browse files
Fixes to Python wrapper examples. Changes to work with Python 3.12
1 parent 60f22e0 commit b8db1e4

File tree

12 files changed

+108
-92
lines changed

12 files changed

+108
-92
lines changed

PythonWrapper/build/create.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake -DHOST=YES ^
22
-DLOOPUNROLL=ON ^
33
-DWRAPPER=YES ^
4-
-DCMSISDSP=".." ^
4+
-DCMSISDSP="path to CMSIS-DSP folder" ^
55
-DCMAKE_C_FLAGS_RELEASE="-std=c11 -Ofast -ffast-math -DNDEBUG -Wall -Wextra" ^
66
-DCMAKE_CXX_FLAGS_RELEASE="-fno-rtti -std=c++11 -Ofast -ffast-math -DNDEBUG -Wall -Wextra -Wno-unused-parameter" ^
77
-G "Unix Makefiles" ..

PythonWrapper/cmsisdsp_pkg/src/cmsisdsp_module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <numpy/numpyconfig.h>
3333

3434
// API version used on google colab
35-
// List in https://github.com/numpy/numpy numpyconfig.h
35+
// https://github.com/numpy/numpy/blob/main/numpy/_core/include/numpy/numpyconfig.h
3636
#if (NPY_API_VERSION != 0x0000000F )
3737
//#error("Error building with wrong NumPy API version")
3838
#endif

PythonWrapper/examples/debug.py

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import colorama
1010
from colorama import init,Fore, Back, Style
1111
from numpy.testing import assert_allclose
12+
import scipy.spatial.distance as d
1213

1314
init()
1415

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

2122

22-
def chop(A, eps = 1e-6):
23-
B = np.copy(A)
24-
B[np.abs(A) < eps] = 0
25-
return B
23+
def packset(a):
24+
b = np.packbits(a)
25+
newSize = int(np.ceil(b.shape[0] / 4.0)) * 4
26+
c = np.copy(b).astype(np.uint32)
27+
c.resize(newSize)
28+
#print(c)
29+
vecSize = round(newSize/4)
30+
c=c.reshape(vecSize,4)
31+
#print(c)
32+
r = np.zeros(vecSize)
33+
result = []
34+
for i in range(0,vecSize):
35+
print(c[i,:])
36+
#print("%X %X %X %X" % (c[i,0],c[i,1],c[i,2],c[i,3]))
37+
d = (c[i,0] << 24) | (c[i,1] << 16) | (c[i,2] << 8) | c[i,3]
38+
result.append(np.uint32(d))
39+
return(result)
2640

27-
nb = 32
28-
signal = np.cos(2 * np.pi * np.arange(nb) / nb)*np.cos(0.2*2 * np.pi * np.arange(nb) / nb)
41+
nb = 34
42+
#va = np.random.choice([0,1],nb)
43+
# Array of word32 containing all of our bits
44+
#pva = packset(va)
2945

30-
ref=scipy.fft.rfft(signal)
31-
invref = scipy.fft.irfft(ref)
3246

33-
print(f"ref length = {len(ref)}")
34-
print(ref)
47+
#vb = np.random.choice([0,1],nb)
48+
# Array of word32 containing all of our bits
49+
#pvb = packset(vb)
50+
#
51+
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]
52+
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]
53+
54+
va = np.array(va)
55+
vb = np.array(vb)
56+
57+
pva=packset(va)
58+
pvb=packset(vb)
59+
60+
#pva = [np.uint32(167), np.uint32(0)]
61+
#pvb = [np.uint32(152), np.uint32(0)]
62+
63+
#print(va,pva)
64+
#print(vb,pvb)
65+
66+
ctt=1.0*np.count_nonzero((va==1) & (vb==1))
67+
ctf=1.0*np.count_nonzero((va==1) & (vb==0))
68+
cft=1.0*np.count_nonzero((va==0) & (vb==1))
69+
70+
res=(cft+ctf)/(2*ctt+cft+ctf)
3571

36-
# Convert ref to CMSIS-DSP format
37-
referenceFloat=np.zeros(2*len(ref))
38-
print(f"referenceFloat length = {len(referenceFloat)}")
39-
# Replace complex datatype by real datatype
40-
referenceFloat[0::2] = np.real(ref)
41-
referenceFloat[1::2] = np.imag(ref)
42-
# Copy Nyquist frequency value into first
43-
# sample.This is just a storage trick so that the
44-
# output of the RFFT has same length as input
45-
# It is legacy behavior that we need to keep
46-
# for backward compatibility but it is not
47-
# very pretty
48-
#referenceFloat[1] = np.real(ref[-1])
49-
50-
rifftQ31=dsp.arm_rfft_instance_q31()
51-
status=dsp.arm_rfft_init_q31(rifftQ31,nb,1,1)
52-
# Apply CMSIS-DSP scaling
53-
referenceQ31 = f.toQ31(referenceFloat / nb)
54-
55-
resultQ31 = dsp.arm_rfft_q31(rifftQ31,referenceQ31)
56-
resultF = f.Q31toF32(resultQ31)
57-
58-
print(f"resultF length = {len(resultF)}")
59-
assert_allclose(invref/nb,resultF,atol=1e-6)
60-
61-
signalQ31 = f.toQ31(signal)
62-
rfftQ31=dsp.arm_rfft_instance_q31()
63-
status=dsp.arm_rfft_init_q31(rfftQ31,nb,0,1)
64-
resultQ31 = dsp.arm_rfft_q31(rfftQ31,signalQ31)
65-
print(len(resultQ31))
66-
print(2*nb)
67-
resultF = f.Q31toF32(resultQ31) * nb
68-
69-
def compareWithConjugatePart(r):
70-
res = r[0::2] + 1j * r[1::2]
71-
conjPart = res[nb:nb//2:-1].conj()
72-
refPart = res[1:nb//2]
73-
assert(np.equal(refPart , conjPart).all())
74-
75-
compareWithConjugatePart(resultF)
76-
77-
res = resultF[0::2] + 1j * resultF[1::2]
7872
print(res)
7973

80-
print(res[0:nb//2+1])
81-
print(res[0:nb//2+1].shape)
74+
75+
print("\nDice")
76+
ref=d.dice(va,vb)
77+
res=dsp.arm_dice_distance(pva,pvb,nb)
78+
print(ref)
79+
print(res)
80+
assert_allclose(ref,res,1e-6)

PythonWrapper/examples/example_1_11.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,28 @@ def printSubTitle(s):
122122

123123
printSubTitle("With a window")
124124

125+
nan = np.nan
126+
125127
referenceDistance = 0.617099940776825
126-
referenceCost=np.array([[9.1612804e-01, 9.9920368e-01, np.NAN, np.NAN,
127-
np.NAN],
128-
[1.2353053e+00, 1.6792301e+00, np.NAN, np.NAN,
129-
np.NAN],
130-
[1.3028694e+00, 2.3696373e+00, 4.4372001e+00, np.NAN,
131-
np.NAN],
132-
[np.NAN, 3.0795674e+00, 4.9687119e+00, np.NAN,
133-
np.NAN],
134-
[np.NAN, 3.5039051e+00, 4.9290380e+00, 5.3565612e+00,
135-
np.NAN],
136-
[np.NAN, np.NAN, 4.8520918e+00, 5.1756082e+00,
137-
np.NAN],
138-
[np.NAN, np.NAN, 5.0427418e+00, 5.8497019e+00,
128+
referenceCost=np.array([[9.1612804e-01, 9.9920368e-01, nan, nan,
129+
nan],
130+
[1.2353053e+00, 1.6792301e+00, nan, nan,
131+
nan],
132+
[1.3028694e+00, 2.3696373e+00, 4.4372001e+00, nan,
133+
nan],
134+
[nan, 3.0795674e+00, 4.9687119e+00, nan,
135+
nan],
136+
[nan, 3.5039051e+00, 4.9290380e+00, 5.3565612e+00,
137+
nan],
138+
[nan, nan, 4.8520918e+00, 5.1756082e+00,
139+
nan],
140+
[nan, nan, 5.0427418e+00, 5.8497019e+00,
139141
7.6590457e+00],
140-
[np.NAN, np.NAN, np.NAN, 6.7571073e+00,
142+
[nan, nan, nan, 6.7571073e+00,
141143
8.6668968e+00],
142-
[np.NAN, np.NAN, np.NAN, 7.3949833e+00,
144+
[nan, nan, nan, 7.3949833e+00,
143145
9.0352430e+00],
144-
[np.NAN, np.NAN, np.NAN, np.NAN,
146+
[nan, nan, nan, nan,
145147
9.2564993e+00]], dtype=np.float32)
146148

147149

PythonWrapper/examples/testdistance.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
a=[1,2,3]
77
b=[1,5,2]
88

9+
def kulsinski(va,vb):
10+
n = len(va)
11+
ctt=1.0*np.count_nonzero((va==1) & (vb==1))
12+
ctf=1.0*np.count_nonzero((va==1) & (vb==0))
13+
cft=1.0*np.count_nonzero((va==0) & (vb==1))
14+
return(1.0*(ctf + cft - ctt+n)/(cft + ctf + n))
15+
16+
917
print("\nBray-Curtis")
1018
ref=d.braycurtis(a,b)
1119
res=dsp.arm_braycurtis_distance_f32(a,b)
@@ -96,7 +104,7 @@
96104
def packset(a):
97105
b = np.packbits(a)
98106
newSize = int(np.ceil(b.shape[0] / 4.0)) * 4
99-
c = np.copy(b)
107+
c = np.copy(b).astype(np.uint32)
100108
c.resize(newSize)
101109
#print(c)
102110
vecSize = round(newSize/4)
@@ -105,7 +113,7 @@ def packset(a):
105113
r = np.zeros(vecSize)
106114
result = []
107115
for i in range(0,vecSize):
108-
#print(c[i,:])
116+
print(c[i,:])
109117
#print("%X %X %X %X" % (c[i,0],c[i,1],c[i,2],c[i,3]))
110118
d = (c[i,0] << 24) | (c[i,1] << 16) | (c[i,2] << 8) | c[i,3]
111119
result.append(np.uint32(d))
@@ -143,7 +151,7 @@ def packset(a):
143151
assert_allclose(ref,res,1e-6)
144152

145153
print("\nKulsinski")
146-
ref=d.kulsinski(va,vb)
154+
ref=kulsinski(va,vb)
147155
res=dsp.arm_kulsinski_distance(pva,pvb,nb)
148156
print(ref)
149157
print(res)

PythonWrapper/examples/testdsp6.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,14 @@
169169
supportVectors = supportVectors.reshape(nbSupportVectors * VECDIM)
170170

171171
svmInst=dsp.arm_svm_polynomial_instance_f32()
172+
173+
172174
dsp.arm_svm_polynomial_init_f32(svmInst,nbSupportVectors,vectorDimensions,
173-
intercept,dualCoefs,supportVectors,
175+
intercept[0],dualCoefs,supportVectors,
174176
[0,1],degree,coef0,gamma)
175177

178+
exit(0)
179+
176180
test1 = np.array([0.4,0.1])
177181
predicted1 = dsp.arm_svm_polynomial_predict_f32(svmInst,test1)
178182
print(predicted1)

PythonWrapper/examples/testmfcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
freq_high = sample_rate / 2
1818
numOfMelFilters = 20
1919

20-
window = sig.hamming(FFTSize, sym=False)
20+
window = sig.windows.hamming(FFTSize, sym=False)
2121

2222
filtLen,filtPos,packedFilters = mfcc.melFilterMatrix(F32,freq_min, freq_high, numOfMelFilters,sample_rate,FFTSize)
2323

PythonWrapper/examples/testmfccq15.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
freq_high = sample_rate / 2
1818
numOfMelFilters = 20
1919

20-
windowQ15 = dt.convert(sig.hamming(FFTSize, sym=False),Q15)
20+
windowQ15 = dt.convert(sig.windows.hamming(FFTSize, sym=False),Q15)
2121
filtLen,filtPos,packedFiltersQ15 = mfcc.melFilterMatrix(Q15,freq_min, freq_high, numOfMelFilters,sample_rate,FFTSize)
2222
dctMatrixFiltersQ15 = mfcc.dctMatrix(Q15,numOfDctOutputs, numOfMelFilters)
2323

PythonWrapper/examples/testmfccq31.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
freq_high = sample_rate / 2
1818
numOfMelFilters = 20
1919

20-
windowQ31 = dt.convert(sig.hamming(FFTSize, sym=False),Q31)
20+
windowQ31 = dt.convert(sig.windows.hamming(FFTSize, sym=False),Q31)
2121
filtLen,filtPos,packedFiltersQ31 = mfcc.melFilterMatrix(Q31,freq_min, freq_high, numOfMelFilters,sample_rate,FFTSize)
2222
dctMatrixFiltersQ31 = mfcc.dctMatrix(Q31,numOfDctOutputs, numOfMelFilters)
2323

PythonWrapper_README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ MEL filters are represented as 3 arrays to encode a sparse array.
234234

235235
# Change history
236236

237+
## Version 1.9.9:
238+
* Supports Python 3.12
239+
237240
## Version 1.9.8:
238241
* Compute graph API has been removed
239242
* Dependency on numpy 1.22 has been lifted, tested through numpy 1.26

0 commit comments

Comments
 (0)