example_1_5.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # New functions for version 1.5 of the Python wrapper
  2. import cmsisdsp as dsp
  3. import cmsisdsp.fixedpoint as f
  4. import numpy as np
  5. import math
  6. import colorama
  7. from colorama import init,Fore, Back, Style
  8. from numpy.testing import assert_allclose
  9. from numpy.linalg import qr
  10. def householder(x,eps=1e-16):
  11. #print(x)
  12. v=np.hstack([[1],x[1:]])
  13. alpha = x[0]
  14. xnorm2=x[1:].dot(x[1:])
  15. epsilon=eps
  16. #print(sigma)
  17. if xnorm2<=epsilon:
  18. tau = 0.0
  19. v = np.zeros(len(x))
  20. else:
  21. if np.sign(alpha) <= 0:
  22. beta = math.sqrt(alpha*alpha + xnorm2)
  23. else:
  24. beta = -math.sqrt(alpha*alpha + xnorm2)
  25. r = (alpha - beta)
  26. v = x / r
  27. tau = (beta - alpha) / beta
  28. v[0] = 1
  29. return(v,tau)
  30. init()
  31. def printTitle(s):
  32. print("\n" + Fore.GREEN + Style.BRIGHT + s + Style.RESET_ALL)
  33. def printSubTitle(s):
  34. print("\n" + Style.BRIGHT + s + Style.RESET_ALL)
  35. printTitle("Householder")
  36. VECDIM = 10
  37. a=np.random.randn(VECDIM)
  38. a = a / np.max(np.abs(a))
  39. # Reference
  40. vRef,betaRef = householder(a)
  41. printSubTitle("Householder F32")
  42. betaF32,vF32 = dsp.arm_householder_f32(a,dsp.DEFAULT_HOUSEHOLDER_THRESHOLD_F32)
  43. print(np.isclose(betaRef,betaF32,1e-6,1e-6))
  44. print(np.isclose(vRef,vF32,1e-6,1e-6))
  45. printSubTitle("Householder F64")
  46. betaF64,vF64 = dsp.arm_householder_f64(a,dsp.DEFAULT_HOUSEHOLDER_THRESHOLD_F64)
  47. print(np.isclose(betaRef,betaF64,1e-6,1e-6))
  48. print(np.isclose(vRef,vF64,1e-6,1e-6))
  49. printSubTitle("Householder Proportional F32")
  50. a=np.random.randn(5)
  51. # With the threshold defined with DEFAULT_HOUSEHOLDER_THRESHOLD_F32
  52. # this vector is considered as proportional to (1,0,...)
  53. # and thus the function will return (0,[0,...,0])
  54. a = a / np.max(np.abs(a)) * 1.0e-7
  55. resF32 = dsp.arm_householder_f32(a,dsp.DEFAULT_HOUSEHOLDER_THRESHOLD_F32)
  56. print(resF32)
  57. # With a smaller threshold, a computation is taking place
  58. resF32 = dsp.arm_householder_f32(a,0.001*dsp.DEFAULT_HOUSEHOLDER_THRESHOLD_F32)
  59. print(resF32)
  60. printTitle("QR decomposition")
  61. def checkOrtho(A,err=1e-10):
  62. product = A.T.dot(A)
  63. #print(A)
  64. np.fill_diagonal(product,0)
  65. #print(product)
  66. print(np.max(np.abs(product)))
  67. return (np.all(np.abs(product)<=err))
  68. m=np.array([[-0.35564874, -0.07809871, -0.10350569, -0.50633135, -0.65073484],
  69. [-0.71887395, 0.45257918, 0.29606363, 0.1497621 , 0.07002738],
  70. [-0.50586141, -0.50613839, -0.01650463, -0.29693649, 0.47667742],
  71. [ 0.06802137, 0.07689169, -0.02726221, -0.09996672, 0.15521956],
  72. [ 0.21220523, -0.22273009, 0.78247386, -0.2760002 , -0.24438688],
  73. [ 0.09683658, 0.62026597, 0.26771763, -0.26935342, 0.18443573],
  74. [-0.01014268, 0.27578087, -0.44635721, -0.21827312, -0.26463186],
  75. [-0.20420646, -0.12880459, 0.13207738, 0.65319578, -0.3956695 ]])
  76. rows,columns = m.shape
  77. # The CMSIS-DSP C functions is requiring two temporary arrays
  78. # To follow the C function as closely as possible, we create
  79. # two arrays. Their size will be used internally by the Python
  80. # wrapper to allocate two temporary buffers.
  81. # Like that you can check you have dimensionned the arrays in the
  82. # right way.
  83. # The content of the temporary buffers is not accesible from the
  84. # Python API. tmpa and tmpb are not modified.
  85. tmpa=np.zeros(rows)
  86. tmpb=np.zeros(rows)
  87. printSubTitle("QR F32")
  88. status,r,q,tau = dsp.arm_mat_qr_f32(m,dsp.DEFAULT_HOUSEHOLDER_THRESHOLD_F32,tmpa,tmpb)
  89. # Status different from 0 if matrix dimensions are not right
  90. # (rows must be >= columns)
  91. #print(status)
  92. #print(q)
  93. #print(r)
  94. #print(tau)
  95. # Check that the matrix Q is orthogonal
  96. assert(checkOrtho(q,err=1.0e-6))
  97. # Remove householder vectors from R matrix
  98. i=1
  99. for c in r.T:
  100. c[i:] = 0
  101. i = i+1
  102. # Check that M = Q R
  103. newm = np.dot(q,r)
  104. assert_allclose(newm,m,2e-6,1e-7)
  105. printSubTitle("QR F64")
  106. status,r,q,tau = dsp.arm_mat_qr_f64(m,dsp.DEFAULT_HOUSEHOLDER_THRESHOLD_F64,tmpa,tmpb)
  107. # Status different from 0 if matrix dimensions are not right
  108. # (rows must be >= columns)
  109. #print(status)
  110. #print(q)
  111. #print(r)
  112. #print(tau)
  113. # Check that the matrix Q is orthogonal
  114. assert(checkOrtho(q,err=1e-14))
  115. # Remove householder vectors from R matrix
  116. i=1
  117. for c in r.T:
  118. c[i:] = 0
  119. i = i+1
  120. # Check that M = Q R
  121. newm = np.dot(q,r)
  122. assert_allclose(newm,m)