1import os.path
2import numpy as np
3import itertools
4import Tools
5from scipy import signal
6#from pylab import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show,semilogx, semilogy
7
8# Those patterns are used for tests and benchmarks.
9# For tests, there is the need to add tests for saturation
10
11def cartesian(*somelists):
12   r=[]
13   for element in itertools.product(*somelists):
14       r.append(element)
15   return(r)
16
17def writeTests(config,format):
18    NBSAMPLES=256
19    NUMTAPS = 64
20
21    samples=np.random.randn(NBSAMPLES)
22    refs=np.random.randn(NBSAMPLES)
23    taps=np.random.randn(NUMTAPS)
24
25    samples = Tools.normalize(samples)
26    refs = Tools.normalize(refs)
27    taps = Tools.normalize(taps)
28
29
30    ### For benchmarks
31
32    config.writeInput(1, samples,"Samples")
33    config.writeInput(1, taps,"Coefs")
34    config.writeInput(1, refs,"Refs")
35
36    ### For tests
37
38    # blocksize 1 2 3 8 11
39    # taps 1 2 3 4 5 6 7 8 11 25
40    # state numTaps + blockSize - 1
41    # ref blockSize
42
43    # Maximum number of samples for all tested FIR configurations is 2*23
44    t = np.linspace(0, 1, 2*23)
45
46    x = np.sin(2*np.pi*50*t)+np.random.randn(len(t)) * 0.08
47    x = Tools.normalize(x)
48    # To avoid saturation
49    x = x / 30.0
50
51
52    config.writeInput(1, x,"FirInput")
53    tapConfigs=[]
54    output=[]
55    defs=[]
56
57    if format == Tools.F64 or format == 0 or format == 31:
58       blk = [1, 2, 3, 8, 9,10,11, 16, 23]
59       taps = [1, 2, 3, 4, 5, 6, 7, 8, 11, 16, 23, 25]
60    elif format == 15 or format == 16:
61       blk = [1, 2, 3, 12,13,14,15]
62       taps = [2, 3, 4, 5, 6, 7, 8, 11, 25]
63    elif format == 7:
64       blk = [1, 2, 3 ,20,21,22,23]
65       taps = [1, 2, 3, 4, 5, 6, 7, 8, 11, 25]
66
67    configs = cartesian(blk,taps)
68
69    nb=1
70
71    for (b,t) in configs:
72        nbTaps=t
73        # nbTaps + 2 to be sure all coefficients are not saturated
74        pythonCoefs = np.array(list(range(1,nbTaps+1)))/(1.0*(nbTaps+2))
75        coefs=pythonCoefs
76
77        if format == 15:
78          if t % 2 == 1:
79            nbTaps = nbTaps + 1
80            coefs = np.append(coefs,[0.0])
81
82        out=signal.lfilter(pythonCoefs,[1.0],x[0:2*b])
83
84        output += list(out)
85        coefs = list(coefs)
86        coefs.reverse()
87        tapConfigs += coefs
88        defs += [b,nbTaps]
89
90        nb = nb + 1
91
92    config.writeInput(1, output,"FirRefs")
93    config.writeInput(1, tapConfigs,"FirCoefs")
94    config.writeReferenceS16(1,defs,"FirConfigs")
95
96
97def generatePatterns():
98    PATTERNDIR = os.path.join("Patterns","DSP","Filtering","FIR","FIR")
99    PARAMDIR = os.path.join("Parameters","DSP","Filtering","FIR","FIR")
100
101    configf64=Tools.Config(PATTERNDIR,PARAMDIR,"f64")
102    configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
103    configf16=Tools.Config(PATTERNDIR,PARAMDIR,"f16")
104    configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
105    configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
106    configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
107
108    configf32.setOverwrite(False)
109    configf16.setOverwrite(False)
110    configq31.setOverwrite(False)
111    configq15.setOverwrite(False)
112    configq7.setOverwrite(False)
113
114    writeTests(configf64,Tools.F64)
115    writeTests(configf32,0)
116    writeTests(configf16,16)
117    writeTests(configq31,31)
118    writeTests(configq15,15)
119    writeTests(configq7,7)
120
121
122if __name__ == '__main__':
123  generatePatterns()
124