1import os.path
2import numpy as np
3import itertools
4import Tools
5import statsmodels.tsa.stattools
6
7# Those patterns are used for tests and benchmarks.
8# For tests, there is the need to add tests for saturation
9
10def cartesian(*somelists):
11   r=[]
12   for element in itertools.product(*somelists):
13       r.append(element)
14   return(r)
15
16def autocorr(x):
17    result = np.correlate(x, x, mode='full')
18    return result[result.size//2:]
19
20def writeTests(config,format):
21
22    config.setOverwrite(False)
23
24    NBSAMPLES=128
25
26    inputsA=np.random.randn(NBSAMPLES)
27    inputsB=np.random.randn(NBSAMPLES)
28
29    inputsA = Tools.normalize(inputsA)
30    inputsB = Tools.normalize(inputsB)
31
32    if format==31:
33      # To avoid overflow. There is no saturation in CMSIS code for Q31 conv/corr
34      inputsA = inputsA / 16
35      inputsB = inputsB / 16
36
37
38    config.writeInput(1, inputsA,"InputsA")
39    config.writeInput(1, inputsB,"InputsB")
40
41
42    if format == 15:
43       nbs = [(14, 15), (14, 16), (14, 17), (14, 18), (14, 33), (15, 15),
44              (15, 16), (15, 17), (15, 18), (15, 33), (16, 15), (16, 16),
45              (16, 17), (16, 18), (16, 33), (17, 15), (17, 16), (17, 17),
46              (17, 18), (17, 33), (32, 15), (32, 16), (32, 17), (32, 18), (32, 33)]
47    elif format == 7 :
48       nbs = [(30, 31), (30, 32), (30, 33), (30, 34), (30, 49), (31, 31),
49              (31,32), (31, 33), (31, 34), (31, 49), (32, 31), (32, 32),
50              (32, 33), (32,34), (32, 49), (33, 31), (33, 32), (33, 33), (33, 34),
51              (33, 49), (48,31), (48, 32), (48, 33), (48, 34), (48, 49)]
52    else:
53       nbs = [(4, 1), (4, 2), (4, 3), (4, 8), (4, 11), (5, 1), (5, 2), (5, 3), (5, 8), (5, 11), (6, 1), (6, 2), (6, 3), (6, 8), (6, 11), (9, 1), (9, 2),
54              (9, 3), (9, 8), (9, 11), (10, 1), (10, 2), (10, 3), (10, 8), (10, 11), (11, 1), (11, 2), (11, 3), (11, 8), (11, 11), (12, 1), (12, 2),
55              (12, 3), (12, 8), (12, 11), (13, 1), (13, 2), (13, 3), (13, 8), (13, 11)]
56
57    nbTest = 1
58
59    for (na,nb) in nbs:
60        #print(na,nb)
61
62        ref = np.correlate(inputsA[0:na],inputsB[0:nb],"full")
63        if na > nb:
64           padding = na - nb
65           z = np.zeros(padding)
66           ref = np.concatenate((z,ref))
67        else:
68           padding = nb - na
69           z = np.zeros(padding)
70           ref = np.concatenate((ref,z))
71        config.writeReference(nbTest, ref)
72        nbTest = nbTest + 1
73
74    for (na,nb) in nbs:
75        #print(na,nb)
76
77        ref = np.convolve(inputsA[0:na],inputsB[0:nb],"full")
78        config.writeReference(nbTest, ref)
79        nbTest = nbTest + 1
80
81    # Levinson durbin tests
82
83    a = [Tools.loopnb(format,Tools.TAILONLY),
84    Tools.loopnb(format,Tools.BODYONLY),
85    Tools.loopnb(format,Tools.BODYANDTAIL),
86    ]
87
88    a = list(np.unique(np.array(a)))
89
90    #a = [3]
91
92    # Errors of each levinson durbin test
93    err=[]
94
95    errTestID = nbTest
96
97    for na in a:
98
99      s = np.random.randn(na+1)
100      s = Tools.normalize(s)
101      phi = autocorr(s)
102
103      phi = Tools.normalize(phi)
104
105      config.writeInput(nbTest, phi,"InputPhi")
106
107      sigmav,arcoef,pacf,sigma,phi=statsmodels.tsa.stattools.levinson_durbin(phi,nlags=na,isacov=True)
108
109      err.append(sigmav)
110
111      config.writeReference(nbTest, arcoef)
112      nbTest = nbTest + 1
113
114      config.writeReference(errTestID, err,"LDErrors")
115
116    # Partial convolutions
117    config.setOverwrite(True)
118
119    inputsA=np.random.randn(NBSAMPLES)
120    inputsB=np.random.randn(NBSAMPLES)
121
122    inputsA = Tools.normalize(inputsA)
123    inputsB = Tools.normalize(inputsB)
124
125    config.writeInput(2, inputsA,"InputsA")
126    config.writeInput(2, inputsB,"InputsB")
127
128    (na,nb) = (6, 8)
129    # First = 3
130    numPoints=4
131    ref = np.convolve(inputsA[0:na],inputsB[0:nb],"full")
132
133    first=3
134    config.writeReference(nbTest, ref[first:first+numPoints])
135    nbTest = nbTest + 1
136
137    first=9
138    config.writeReference(nbTest, ref[first:first+numPoints])
139    nbTest = nbTest + 1
140
141    first=7
142    config.writeReference(nbTest, ref[first:first+numPoints])
143    nbTest = nbTest + 1
144
145
146
147
148
149
150def generatePatterns():
151    PATTERNDIR = os.path.join("Patterns","DSP","Filtering","MISC","MISC")
152    PARAMDIR = os.path.join("Parameters","DSP","Filtering","MISC","MISC")
153
154    configf64=Tools.Config(PATTERNDIR,PARAMDIR,"f64")
155    configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
156    configf16=Tools.Config(PATTERNDIR,PARAMDIR,"f16")
157    configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
158    configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
159    configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
160
161    configf32.setOverwrite(False)
162    configf16.setOverwrite(False)
163    configq31.setOverwrite(False)
164    configq15.setOverwrite(False)
165    configq7.setOverwrite(False)
166
167    writeTests(configf64,Tools.F64)
168    writeTests(configf32,0)
169    writeTests(configf16,16)
170    writeTests(configq31,31)
171    writeTests(configq15,15)
172    writeTests(configq7,7)
173
174
175if __name__ == '__main__':
176  generatePatterns()
177