Interpolate.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import os.path
  2. import numpy as np
  3. import itertools
  4. import Tools
  5. from scipy.interpolate import interp1d,interp2d
  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:
  21. data = data / 2.0**11
  22. if format != 0:
  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:
  65. inputSamples = inputSamples / 2.0**11
  66. data = inputSamples.reshape(np.size(inputSamples))
  67. if format != 0:
  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. def generatePatterns():
  75. PATTERNDIR = os.path.join("Patterns","DSP","Interpolation","Interpolation")
  76. PARAMDIR = os.path.join("Parameters","DSP","Interpolation","Interpolation")
  77. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  78. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  79. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  80. configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
  81. writeTests(configf32,0)
  82. writeTests(configq31,31)
  83. writeTests(configq15,15)
  84. writeTests(configq7,7)
  85. if __name__ == '__main__':
  86. generatePatterns()