testdsp6.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import cmsisdsp as dsp
  2. import numpy as np
  3. from numpy.testing import assert_allclose
  4. from scipy.interpolate import CubicSpline
  5. from sklearn.naive_bayes import GaussianNB
  6. from sklearn import svm
  7. import math
  8. a=[1,4,2,6,7,0,-3,5]
  9. ref=sorted(a)
  10. print(ref)
  11. SORT_BITONIC=0
  12. SORT_BUBBLE=1
  13. SORT_HEAP=2
  14. SORT_INSERTION=3
  15. SORT_QUICK=4
  16. SORT_SELECTION=5
  17. SORT_DESCENDING = 0
  18. SORT_ASCENDING = 1
  19. sortinst=dsp.arm_sort_instance_f32()
  20. for mode in range(6):
  21. dsp.arm_sort_init_f32(sortinst,mode,SORT_ASCENDING)
  22. res=dsp.arm_sort_f32(sortinst,a)
  23. print(res)
  24. assert (res==ref).all()
  25. print("")
  26. ref.reverse()
  27. print(ref)
  28. for mode in range(6):
  29. # Problem with bitonic probably in the C code
  30. if mode > 0:
  31. dsp.arm_sort_init_f32(sortinst,mode,SORT_DESCENDING)
  32. res=dsp.arm_sort_f32(sortinst,a)
  33. print(res)
  34. assert (res==ref).all()
  35. print("Spline")
  36. x = np.arange(0, 2*np.pi+np.pi/4, np.pi/4)
  37. y = np.sin(x)
  38. xnew = np.arange(0, 2*np.pi+np.pi/16, np.pi/16)
  39. ynew = CubicSpline(x,y,bc_type="natural")
  40. yref=ynew(xnew)
  41. print(yref)
  42. splineInst = dsp.arm_spline_instance_f32()
  43. dsp.arm_spline_init_f32(splineInst,0,x,y)
  44. yres=dsp.arm_spline_f32(splineInst,xnew)
  45. print(yres)
  46. assert_allclose(yref,yres,1e-6,1e-6)
  47. print("Bayes")
  48. # Reusing example from https://developer.arm.com/documentation/102052/0000/Train-your-Bayesian-estimator-with-scikit-learn
  49. NBVECS = 100
  50. VECDIM = 2
  51. # 3 cluster of points are generated (3 classes)
  52. ballRadius = 1.0
  53. x1 = [1.5, 1] + ballRadius * np.random.randn(NBVECS,VECDIM)
  54. x2 = [-1.5, 1] + ballRadius * np.random.randn(NBVECS,VECDIM)
  55. x3 = [0, -3] + ballRadius * np.random.randn(NBVECS,VECDIM)
  56. # All points are concatenated
  57. X_train=np.concatenate((x1,x2,x3))
  58. # The classes are 0,1 and 2.
  59. Y_train=np.concatenate((np.zeros(NBVECS),np.ones(NBVECS),2*np.ones(NBVECS)))
  60. gnb = GaussianNB()
  61. gnb.fit(X_train, Y_train)
  62. src1=[1.5,1.0]
  63. src2=[-1.5,1]
  64. src3=[0,-3]
  65. ref1 = gnb.predict([src1])
  66. print(ref1)
  67. ref2 = gnb.predict([src2])
  68. print(ref2)
  69. ref3 = gnb.predict([src3])
  70. print(ref3)
  71. #print(gnb.predict_log_proba([src]))
  72. theta=list(np.reshape(gnb.theta_,np.size(gnb.theta_)))
  73. # Gaussian variances
  74. sigma=list(np.reshape(gnb.var_,np.size(gnb.var_)))
  75. # Class priors
  76. prior=list(np.reshape(gnb.class_prior_,np.size(gnb.class_prior_)))
  77. epsilon=gnb.epsilon_
  78. bayesInst = dsp.arm_gaussian_naive_bayes_instance_f32(
  79. vectorDimension=VECDIM,numberOfClasses=3,
  80. theta=theta,sigma=sigma,classPriors=prior,epsilon=epsilon)
  81. _,res1=dsp.arm_gaussian_naive_bayes_predict_f32(bayesInst,src1)
  82. print(res1)
  83. _,res2=dsp.arm_gaussian_naive_bayes_predict_f32(bayesInst,src2)
  84. print(res2)
  85. _,res3=dsp.arm_gaussian_naive_bayes_predict_f32(bayesInst,src3)
  86. print(res3)
  87. assert res1 == ref1
  88. assert res2 == ref2
  89. assert res3 == ref3
  90. print("SVM")
  91. NBVECS = 100
  92. VECDIM = 2
  93. ballRadius = 0.5
  94. x = ballRadius * np.random.randn(NBVECS, 2)
  95. angle = 2.0 * math.pi * np.random.randn(1, NBVECS)
  96. radius = 3.0 + 0.1 * np.random.randn(1, NBVECS)
  97. xa = np.zeros((NBVECS,2))
  98. xa[:, 0] = radius * np.cos(angle)
  99. xa[:, 1] = radius * np.sin(angle)
  100. X_train = np.concatenate((x, xa))
  101. Y_train = np.concatenate((np.zeros(NBVECS), np.ones(NBVECS)))
  102. clf = svm.SVC(kernel='poly', gamma='auto', coef0=1.1)
  103. clf.fit(X_train, Y_train)
  104. test1 = np.array([0.4,0.1])
  105. test1 = test1.reshape(1,-1)
  106. refpredicted1 = clf.predict(test1)
  107. print(refpredicted1)
  108. test2 = np.array([3.1,0.1])
  109. test2 = test2.reshape(1,-1)
  110. refpredicted2 = clf.predict(test2)
  111. print(refpredicted2)
  112. supportShape = clf.support_vectors_.shape
  113. nbSupportVectors = supportShape[0]
  114. vectorDimensions = supportShape[1]
  115. degree=clf.degree
  116. coef0=clf.coef0
  117. gamma=clf._gamma
  118. intercept=clf.intercept_
  119. dualCoefs = clf.dual_coef_
  120. dualCoefs = dualCoefs.reshape(nbSupportVectors)
  121. supportVectors = clf.support_vectors_
  122. supportVectors = supportVectors.reshape(nbSupportVectors * VECDIM)
  123. svmInst=dsp.arm_svm_polynomial_instance_f32()
  124. dsp.arm_svm_polynomial_init_f32(svmInst,nbSupportVectors,vectorDimensions,
  125. intercept,dualCoefs,supportVectors,
  126. [0,1],degree,coef0,gamma)
  127. test1 = np.array([0.4,0.1])
  128. predicted1 = dsp.arm_svm_polynomial_predict_f32(svmInst,test1)
  129. print(predicted1)
  130. test2 = np.array([3.1,0.1])
  131. predicted2 = dsp.arm_svm_polynomial_predict_f32(svmInst,test2)
  132. print(predicted2)
  133. assert predicted1==refpredicted1
  134. assert predicted2==refpredicted2