1# Bug corrections for version 1.9
2import cmsisdsp as dsp
3import cmsisdsp.fixedpoint as f
4import numpy as np
5import math
6import colorama
7from colorama import init,Fore, Back, Style
8from numpy.testing import assert_allclose
9#import matplotlib.pyplot as plt
10from scipy import signal
11import scipy.signal.windows as win
12
13init()
14
15def printTitle(s):
16    print("\n" + Fore.GREEN + Style.BRIGHT +  s + Style.RESET_ALL)
17
18def printSubTitle(s):
19    print("\n" + Style.BRIGHT + s + Style.RESET_ALL)
20
21def genwelch(n):
22    ik = 2*np.array(range(n)) / n
23    w = ik -1;
24    w = 1 - w**2
25    if len(w)!=n:
26        print("Error with window len in Welch")
27        exit(1)
28    return(w)
29
30
31def genbartlett(n):
32    w = win.bartlett(n,sym=False)
33    return(w)
34
35def genhamming(n):
36    w = win.hamming(n,sym=False)
37    return(w)
38
39def genhanning(n):
40    w = win.hann(n,sym=False)
41    return(w)
42
43def gennuttall3(n):
44    w = win.general_cosine(n,
45        [0.375, 0.5 , 0.125 ],sym=False)
46    return(w)
47
48def gennuttall4(n):
49    w = win.general_cosine(n,
50        [0.3125, 0.46875,0.1875 , 0.03125],sym=False)
51    return(w)
52
53def gennuttall3a(n):
54    w = win.general_cosine(n,
55        [0.40897, 0.5 ,0.09103],sym=False)
56    return(w)
57
58def gennuttall3b(n):
59    w = win.general_cosine(n,
60        [0.4243801, 0.4973406 , 0.0782793 ],sym=False)
61    return(w)
62
63def gennuttall4a(n):
64    w = win.general_cosine(n,
65        [0.338946, 0.481973,0.161054 , 0.018027 ],sym=False)
66    return(w)
67
68def genblackman_harris_92db(n):
69    w = win.blackmanharris(n,sym=False)
70    return(w)
71
72def gennuttall4b(n):
73    w = win.general_cosine(n,
74        [0.355768, 0.487396 ,
75        0.144232 , 0.012604 ],sym=False)
76    return(w)
77
78def gennuttall4c(n):
79    w = win.nuttall(n,sym=False)
80    return(w)
81
82def genhft90d(n):
83    w = win.general_cosine(n,
84        [1 ,1.942604 ,
85         1.340318 , 0.440811 , 0.043097 ],sym=False)
86    return(w)
87
88def genhft95(n):
89    w = win.general_cosine(n,
90        [1, 1.9383379 ,
91         1.3045202 ,0.4028270 ,0.0350665 ]
92 ,sym=False)
93    return(w)
94
95def genhft116d(n):
96    w = win.general_cosine(n,
97        [1, 1.9575375 ,
98         1.4780705 ,0.6367431 ,
99         0.1228389 ,0.0066288 ]
100 ,sym=False)
101    return(w)
102
103def genhft144d(n):
104    w = win.general_cosine(n,
105        [1 ,1.96760033 ,
106         1.57983607 , 0.81123644 ,
107         0.22583558 ,0.02773848 , 0.00090360  ]
108 ,sym=False)
109    return(w)
110
111def genhft169d(n):
112    w = win.general_cosine(n,
113        [1, 1.97441843 ,
114         1.65409889 , 0.95788187 ,
115         0.33673420 , 0.06364622 ,
116         0.00521942 ,0.00010599   ]
117 ,sym=False)
118    return(w)
119
120def genhft196d(n):
121    w = win.general_cosine(n,
122        [1, 1.979280420 ,
123         1.710288951 , 1.081629853 ,
124         0.448734314 , 0.112376628 ,
125         0.015122992 ,0.000871252 , 0.000011896 ]
126 ,sym=False)
127    return(w)
128
129def genhft223d(n):
130    w = win.general_cosine(n,
131        [1, 1.98298997309,
132         1.75556083063 , 1.19037717712 ,
133         0.56155440797 , 0.17296769663 ,
134         0.03233247087 ,0.00324954578 ,
135         0.00013801040 ,0.00000132725 ]
136 ,sym=False)
137    return(w)
138
139def genhft248d(n):
140    w = win.general_cosine(n,
141        [1, 1.985844164102 ,
142         1.791176438506 , 1.282075284005 ,
143         0.667777530266 , 0.240160796576 ,
144         0.056656381764 ,0.008134974479 ,
145         0.000624544650 ,0.000019808998 ,
146         0.000000132974 ]
147 ,sym=False)
148    return(w)
149
150printTitle("Regular windows")
151printSubTitle("Welch")
152ref = genwelch(128)
153wf32 = dsp.arm_welch_f32(128)
154assert_allclose(ref,wf32)
155
156wf64 = dsp.arm_welch_f64(128)
157assert_allclose(ref,wf64)
158
159printSubTitle("Bartlett")
160ref = genbartlett(128)
161wf32 = dsp.arm_bartlett_f32(128)
162assert_allclose(ref,wf32)
163
164wf64 = dsp.arm_bartlett_f64(128)
165assert_allclose(ref,wf64)
166
167printSubTitle("Hamming")
168ref = genhamming(128)
169wf32 = dsp.arm_hamming_f32(128)
170assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
171
172wf64 = dsp.arm_hamming_f64(128)
173assert_allclose(ref,wf64)
174
175printSubTitle("Hanning")
176ref = genhanning(128)
177wf32 = dsp.arm_hanning_f32(128)
178assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
179
180wf64 = dsp.arm_hanning_f64(128)
181assert_allclose(ref,wf64)
182
183printSubTitle("Nuttall3")
184ref = gennuttall3(128)
185wf32 = dsp.arm_nuttall3_f32(128)
186assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
187
188wf64 = dsp.arm_nuttall3_f64(128)
189assert_allclose(ref,wf64)
190
191printSubTitle("Nuttall4")
192ref = gennuttall4(128)
193wf32 = dsp.arm_nuttall4_f32(128)
194assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
195
196wf64 = dsp.arm_nuttall4_f64(128)
197assert_allclose(ref,wf64)
198
199printSubTitle("Nuttall3a")
200ref = gennuttall3a(128)
201wf32 = dsp.arm_nuttall3a_f32(128)
202assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
203
204wf64 = dsp.arm_nuttall3a_f64(128)
205assert_allclose(ref,wf64)
206
207printSubTitle("Nuttall3b")
208ref = gennuttall3b(128)
209wf32 = dsp.arm_nuttall3b_f32(128)
210assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
211
212wf64 = dsp.arm_nuttall3b_f64(128)
213assert_allclose(ref,wf64)
214
215printSubTitle("Nuttall4a")
216ref = gennuttall4a(128)
217wf32 = dsp.arm_nuttall4a_f32(128)
218assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
219
220wf64 = dsp.arm_nuttall4a_f64(128)
221assert_allclose(ref,wf64)
222
223printSubTitle("Blackman Harris")
224ref = genblackman_harris_92db(128)
225wf32 = dsp.arm_blackman_harris_92db_f32(128)
226assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
227
228wf64 = dsp.arm_blackman_harris_92db_f64(128)
229assert_allclose(ref,wf64)
230
231printSubTitle("Nuttall4b")
232ref = gennuttall4b(128)
233wf32 = dsp.arm_nuttall4b_f32(128)
234assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
235
236wf64 = dsp.arm_nuttall4b_f64(128)
237assert_allclose(ref,wf64)
238
239printSubTitle("Nuttall4c")
240ref = gennuttall4c(128)
241wf32 = dsp.arm_nuttall4c_f32(128)
242assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
243
244wf64 = dsp.arm_nuttall4c_f64(128)
245assert_allclose(ref,wf64)
246
247printTitle("Flat-top windows")
248
249printSubTitle("HFT90D")
250ref = genhft90d(128)
251wf32 = dsp.arm_hft90d_f32(128)
252assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
253
254wf64 = dsp.arm_hft90d_f64(128)
255assert_allclose(ref,wf64)
256
257printSubTitle("HFT95")
258ref = genhft95(128)
259wf32 = dsp.arm_hft95_f32(128)
260assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
261
262wf64 = dsp.arm_hft95_f64(128)
263assert_allclose(ref,wf64)
264
265printSubTitle("HFT116D")
266ref = genhft116d(128)
267wf32 = dsp.arm_hft116d_f32(128)
268assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
269
270wf64 = dsp.arm_hft116d_f64(128)
271assert_allclose(ref,wf64)
272
273printSubTitle("HFT144D")
274ref = genhft144d(128)
275wf32 = dsp.arm_hft144d_f32(128)
276assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
277
278wf64 = dsp.arm_hft144d_f64(128)
279assert_allclose(ref,wf64)
280
281printSubTitle("HFT196D")
282ref = genhft196d(128)
283wf32 = dsp.arm_hft196d_f32(128)
284assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
285
286wf64 = dsp.arm_hft196d_f64(128)
287assert_allclose(ref,wf64)
288
289printSubTitle("HFT223D")
290ref = genhft223d(128)
291wf32 = dsp.arm_hft223d_f32(128)
292assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
293
294wf64 = dsp.arm_hft223d_f64(128)
295assert_allclose(ref,wf64)
296
297printSubTitle("HFT248D")
298ref = genhft248d(128)
299wf32 = dsp.arm_hft248d_f32(128)
300assert_allclose(ref,wf32,rtol=1e-6,atol=2e-6)
301
302wf64 = dsp.arm_hft248d_f64(128)
303assert_allclose(ref,wf64,rtol=3e-15,atol=3e-15)
304
305