Interpolate.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import os.path
  2. import numpy as np
  3. import itertools
  4. import Tools
  5. from scipy.interpolate import interp1d,interp2d,CubicSpline
  6. # Those patterns are used for tests and benchmarks.
  7. # For tests, there is the need to add tests for saturation
  8. # Get lists of points in row order for use in CMSIS function
  9. def getLinearPoints(x,y):
  10. return(np.array([[p[1],p[0]] for p in np.array(np.meshgrid(y,x)).T.reshape(-1,2)]))
  11. def writeTests(config,format):
  12. # Linear interpolation test
  13. NBSAMPLES=40
  14. x = np.linspace(0, NBSAMPLES, num=NBSAMPLES+1, endpoint=True)
  15. y = np.cos(-x**2/(NBSAMPLES - 1))
  16. f = interp1d(x, y)
  17. data=x+0.5
  18. data=data[:-1]
  19. z = f(data)
  20. if format != 0 and format != 16:
  21. data = data / 2.0**11
  22. if format != 0 and format != 16:
  23. config.writeInputQ31(1, data,"Input")
  24. else:
  25. config.writeInput(1, data)
  26. config.writeInput(1, y,"YVals")
  27. ref = z
  28. config.writeReference(1, ref)
  29. # Bilinear interpolation test
  30. x = np.arange(-3.14, 3.14, 1.0)
  31. y = np.arange(-3.14, 3.14, 0.8)
  32. xx, yy = np.meshgrid(x, y)
  33. z = np.sin(xx**2+yy**2)
  34. f = interp2d(x, y, z, kind='linear')
  35. # Configuration for the test (to initialize the bilinear structure)
  36. matrixSize=[np.size(x),np.size(y)]
  37. # Generate reference value for bilinear instance
  38. # getLinearPoints ensure they are in row order
  39. samples = getLinearPoints(x,y)
  40. # We recompute the value of the function on the samples in row
  41. # order
  42. yvals = np.array([np.sin(i[0]**2+i[1]**2) for i in samples])
  43. # Now we generate other points. The points where we want to evaluate
  44. # the function.
  45. # In Python they must be rescale between -3.14 and tghe max x or max y defined above.
  46. # In CMSIS they will be between 1 and numRow-1 or numCols-1.
  47. # Since we add 0.5 to be sure we are between grid point, we use
  48. # numCols-2 as bound to be sured we are <= numCols-1
  49. numCols = np.size(x)
  50. numRows = np.size(y)
  51. NBX = 10
  52. NBY = 15
  53. # The CMSIS indexes
  54. ix = np.linspace(0, numCols-3, num=NBX, endpoint=True)+0.5
  55. iy = np.linspace(0, numRows-3, num=NBY, endpoint=True)+0.5
  56. # The corresponding Python values
  57. ixVal = ((ix ) / (numCols-1)) * (x[-1] + 3.14) - 3.14
  58. iyVal = ((iy ) / (numRows-1)) * (y[-1] + 3.14) - 3.14
  59. # Input samples for CMSIS.
  60. inputSamples = getLinearPoints(ix,iy)
  61. # We compute the Python interpolated function on the values
  62. inputVals = getLinearPoints(ixVal,iyVal)
  63. ref=np.array([f(i[0],i[1]) for i in inputVals])
  64. if format != 0 and format != 16:
  65. inputSamples = inputSamples / 2.0**11
  66. data = inputSamples.reshape(np.size(inputSamples))
  67. if format != 0 and format != 16:
  68. config.writeInputQ31(2, data,"Input")
  69. else:
  70. config.writeInput(2, data)
  71. config.writeInput(2, yvals.reshape(np.size(yvals)),"YVals")
  72. config.writeReference(2, ref.reshape(np.size(ref)))
  73. config.writeInputS16(2, matrixSize,"Config")
  74. x = [0,3,10,20]
  75. config.writeInput(3,x,"InputX")
  76. y = [0,9,100,400]
  77. config.writeInput(3,y,"InputY")
  78. xnew = np.arange(0,20,1)
  79. config.writeInput(3,xnew,"OutputX")
  80. ynew = CubicSpline(x,y)
  81. config.writeReference(3, ynew(xnew))
  82. x = np.arange(0, 2*np.pi+np.pi/4, np.pi/4)
  83. config.writeInput(4,x,"InputX")
  84. y = np.sin(x)
  85. config.writeInput(4,y,"InputY")
  86. xnew = np.arange(0, 2*np.pi+np.pi/16, np.pi/16)
  87. config.writeInput(4,xnew,"OutputX")
  88. ynew = CubicSpline(x,y,bc_type="natural")
  89. config.writeReference(4, ynew(xnew))
  90. x = [0,3,10]
  91. config.writeInput(5,x,"InputX")
  92. y = x
  93. config.writeInput(5,y,"InputY")
  94. xnew = np.arange(-10,20,1)
  95. config.writeInput(5,xnew,"OutputX")
  96. ynew = CubicSpline(x,y)
  97. config.writeReference(5, ynew(xnew))
  98. def generatePatterns():
  99. PATTERNDIR = os.path.join("Patterns","DSP","Interpolation","Interpolation")
  100. PARAMDIR = os.path.join("Parameters","DSP","Interpolation","Interpolation")
  101. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  102. configf16=Tools.Config(PATTERNDIR,PARAMDIR,"f16")
  103. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  104. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  105. configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
  106. writeTests(configf32,0)
  107. writeTests(configf16,16)
  108. writeTests(configq31,31)
  109. writeTests(configq15,15)
  110. writeTests(configq7,7)
  111. if __name__ == '__main__':
  112. generatePatterns()