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