1import os.path
2import struct
3import numpy as np
4
5def normalize(a):
6  return(a/np.max(np.abs(a)))
7
8TAILONLY = 1
9BODYONLY = 2
10BODYANDTAIL = 3
11
12# Datatype formats
13F64 = 64
14F32 = 0
15F16 = 16
16Q63 = 63
17Q31 = 31
18Q15 = 15
19Q7 = 7
20
21def loopnb(format,loopkind):
22    nb = 0
23    if loopkind == TAILONLY:
24        if format == 64 or format == Q63:
25            nb = 2
26        if format == 0 or format == 31:
27            nb = 3
28        if format == 15 or format == 16:
29            nb = 7
30        if format == 7:
31            nb = 15
32    if loopkind == BODYONLY:
33        if format == 64 or format == Q63:
34            nb = 4
35        if format == 0 or format == 31:
36            nb = 8
37        if format == 15 or format == 16:
38            nb = 16
39        if format == 7:
40            nb = 32
41    if loopkind == BODYANDTAIL:
42        if format == 64 or format == Q63:
43            nb = 5
44        if format == 0 or format == 31:
45            nb = 11 # 9
46        if format == 15 or format == 16:
47            nb = 23 # 17
48        if format == 7:
49            nb = 47 # 33
50
51    return(nb)
52
53# Tools to generate pattern files
54
55def createMissingDir(destPath):
56  theDir=os.path.normpath(destPath)
57  if not os.path.exists(theDir):
58      os.makedirs(theDir)
59
60# Pack an array of boolean into uint32
61def packset(a):
62    b = np.packbits(a)
63    newSize = int(np.ceil(b.shape[0] / 4.0)) * 4
64    c = np.copy(b)
65    c.resize(newSize)
66    #print(c)
67    vecSize = round(newSize/4)
68    c=c.reshape(vecSize,4)
69    #print(c)
70    r = np.zeros(vecSize)
71    result = []
72    for i in range(0,vecSize):
73        #print(c[i,:])
74        #print("%X %X %X %X" % (c[i,0],c[i,1],c[i,2],c[i,3]))
75        d = (c[i,0] << 24) | (c[i,1] << 16) | (c[i,2] << 8) | c[i,3]
76        result.append(np.uint32(d))
77    return(result)
78
79def float_to_hex(f):
80    """ Convert and x86 float to an ARM unsigned long int.
81
82    Args:
83      f (float): value to be converted
84    Raises:
85      Nothing
86    Returns:
87      str : representation of the hex value
88    """
89    return hex(struct.unpack('<I', struct.pack('<f', f))[0])
90
91def float16_to_hex(f):
92    """ Convert and x86 float to an ARM unsigned long int.
93
94    Args:
95      f (float): value to be converted
96    Raises:
97      Nothing
98    Returns:
99      str : representation of the hex value
100    """
101    return hex(struct.unpack('<H', struct.pack('<e', f))[0])
102
103def float64_to_hex(f):
104    """ Convert and x86 float to an ARM unsigned long int.
105
106    Args:
107      f (float): value to be converted
108    Raises:
109      Nothing
110    Returns:
111      str : representation of the hex value
112    """
113    return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
114
115def to_q63(v):
116    r = int(round(v * 2**63))
117    if (r > 0x07FFFFFFFFFFFFFFF):
118      r = 0x07FFFFFFFFFFFFFFF
119    if (r < -0x08000000000000000):
120      r = -0x08000000000000000
121    return ("0x%s" % format(struct.unpack('<Q', struct.pack('<q', r))[0],'016X'))
122
123def to_q31(v):
124    r = int(round(v * 2**31))
125    if (r > 0x07FFFFFFF):
126      r = 0x07FFFFFFF
127    if (r < -0x080000000):
128      r = -0x080000000
129    return ("0x%s" % format(struct.unpack('<I', struct.pack('<i', r))[0],'08X'))
130
131def to_q15(v):
132    r = int(round(v * 2**15))
133    if (r > 0x07FFF):
134      r = 0x07FFF
135    if (r < -0x08000):
136      r = -0x08000
137    return ("0x%s" % format(struct.unpack('<H', struct.pack('<h', r))[0],'04X'))
138
139def to_q7(v):
140    r = int(round(v * 2**7))
141    if (r > 0x07F):
142      r = 0x07F
143    if (r < -0x080):
144      r = -0x080
145    return ("0x%s" % format(struct.unpack('<B', struct.pack('<b', r))[0],'02X'))
146
147def s8(r):
148  return ("0x%s" % format(struct.unpack('<B', struct.pack('<b', r))[0],'02X'))
149
150def u8(r):
151  return ("0x%s" % format(struct.unpack('<B', struct.pack('<B', r))[0],'02X'))
152
153def s16(r):
154  return ("0x%s" % format(struct.unpack('<H', struct.pack('<h', r))[0],'04X'))
155
156def u16(r):
157  return ("0x%s" % format(struct.unpack('<H', struct.pack('<H', r))[0],'04X'))
158
159def s32(r):
160  return ("0x%s" % format(struct.unpack('<I', struct.pack('<i', r))[0],'08X'))
161
162def u32(r):
163  return ("0x%s" % format(struct.unpack('<I', struct.pack('<I', r))[0],'08X'))
164
165def s64(r):
166  return ("0x%s" % format(struct.unpack('<Q', struct.pack('<q', r))[0],'016X'))
167
168def u32(r):
169  return ("0x%s" % format(struct.unpack('<I', struct.pack('<I', r))[0],'08X'))
170
171def u64(r):
172  return ("0x%s" % format(struct.unpack('<Q', struct.pack('<Q', r))[0],'016X'))
173
174class Config:
175    def __init__(self,patternDir,paramDir,ext):
176      self._patternDir = "%s%s" % (patternDir,ext.upper())
177      self._paramDir = "%s%s" % (paramDir,ext.upper())
178      self._ext = ext
179      self._overwrite=True
180
181      createMissingDir(self._patternDir)
182      createMissingDir(self._paramDir)
183
184    def setOverwrite(self,v):
185        self._overwrite=v
186
187    def canOverwrite(self,path):
188        return(self._overwrite or not os.path.exists(path))
189
190    def inputP(self,i,name=None):
191        """ Path to a reference pattern from the ID
192
193        Args:
194          i (int): ID to the reference pattern
195        Raises:
196          Nothing
197        Returns:
198          str : path to the file where to generate the pattern data
199        """
200        if name:
201          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,self._ext)))
202        else:
203          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,self._ext)))
204
205    def inputS64P(self,i,name=None):
206        """ Path to a reference pattern from the ID
207
208        Args:
209          i (int): ID to the reference pattern
210        Raises:
211          Nothing
212        Returns:
213          str : path to the file where to generate the pattern data
214        """
215        if name:
216          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s64")))
217        else:
218          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s64")))
219
220
221    def inputS32P(self,i,name=None):
222        """ Path to a reference pattern from the ID
223
224        Args:
225          i (int): ID to the reference pattern
226        Raises:
227          Nothing
228        Returns:
229          str : path to the file where to generate the pattern data
230        """
231        if name:
232          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s32")))
233        else:
234          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s32")))
235
236    def inputS16P(self,i,name=None):
237        """ Path to a reference pattern from the ID
238
239        Args:
240          i (int): ID to the reference pattern
241        Raises:
242          Nothing
243        Returns:
244          str : path to the file where to generate the pattern data
245        """
246        if name:
247          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s16")))
248        else:
249          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s16")))
250
251    def inputS8P(self,i,name=None):
252        """ Path to a reference pattern from the ID
253
254        Args:
255          i (int): ID to the reference pattern
256        Raises:
257          Nothing
258        Returns:
259          str : path to the file where to generate the pattern data
260        """
261        if name:
262          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s8")))
263        else:
264          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s8")))
265
266    def inputF64P(self,i,name=None):
267        """ Path to a reference pattern from the ID
268
269        Args:
270          i (int): ID to the reference pattern
271        Raises:
272          Nothing
273        Returns:
274          str : path to the file where to generate the pattern data
275        """
276        if name:
277          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f64")))
278        else:
279          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f64")))
280
281
282    def inputF32P(self,i,name=None):
283        """ Path to a reference pattern from the ID
284
285        Args:
286          i (int): ID to the reference pattern
287        Raises:
288          Nothing
289        Returns:
290          str : path to the file where to generate the pattern data
291        """
292        if name:
293          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f32")))
294        else:
295          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f32")))
296
297    def inputF16P(self,i,name=None):
298        """ Path to a reference pattern from the ID
299
300        Args:
301          i (int): ID to the reference pattern
302        Raises:
303          Nothing
304        Returns:
305          str : path to the file where to generate the pattern data
306        """
307        if name:
308          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f16")))
309        else:
310          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f16")))
311
312    def inputQ63P(self,i,name=None):
313        """ Path to a reference pattern from the ID
314
315        Args:
316          i (int): ID to the reference pattern
317        Raises:
318          Nothing
319        Returns:
320          str : path to the file where to generate the pattern data
321        """
322        if name:
323          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q63")))
324        else:
325          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q63")))
326
327    def inputQ31P(self,i,name=None):
328        """ Path to a reference pattern from the ID
329
330        Args:
331          i (int): ID to the reference pattern
332        Raises:
333          Nothing
334        Returns:
335          str : path to the file where to generate the pattern data
336        """
337        if name:
338          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q31")))
339        else:
340          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q31")))
341
342    def inputQ15P(self,i,name=None):
343        """ Path to a reference pattern from the ID
344
345        Args:
346          i (int): ID to the reference pattern
347        Raises:
348          Nothing
349        Returns:
350          str : path to the file where to generate the pattern data
351        """
352        if name:
353          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q15")))
354        else:
355          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q15")))
356
357    def inputQ7P(self,i,name=None):
358        """ Path to a reference pattern from the ID
359
360        Args:
361          i (int): ID to the reference pattern
362        Raises:
363          Nothing
364        Returns:
365          str : path to the file where to generate the pattern data
366        """
367        if name:
368          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q7")))
369        else:
370          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q7")))
371
372    def inputU64P(self,i,name=None):
373        """ Path to a reference pattern from the ID
374
375        Args:
376          i (int): ID to the reference pattern
377        Raises:
378          Nothing
379        Returns:
380          str : path to the file where to generate the pattern data
381        """
382        if name:
383          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u64")))
384        else:
385          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u64")))
386
387
388
389    def inputU32P(self,i,name=None):
390        """ Path to a reference pattern from the ID
391
392        Args:
393          i (int): ID to the reference pattern
394        Raises:
395          Nothing
396        Returns:
397          str : path to the file where to generate the pattern data
398        """
399        if name:
400          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u32")))
401        else:
402          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u32")))
403
404    def inputU16P(self,i,name=None):
405        """ Path to a reference pattern from the ID
406
407        Args:
408          i (int): ID to the reference pattern
409        Raises:
410          Nothing
411        Returns:
412          str : path to the file where to generate the pattern data
413        """
414        if name:
415          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u16")))
416        else:
417          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u16")))
418
419
420    def inputU8P(self,i,name=None):
421        """ Path to a reference pattern from the ID
422
423        Args:
424          i (int): ID to the reference pattern
425        Raises:
426          Nothing
427        Returns:
428          str : path to the file where to generate the pattern data
429        """
430        if name:
431          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u8")))
432        else:
433          return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u8")))
434
435
436
437    def refP(self,i,name=None):
438        """ Path to a reference pattern from the ID
439
440        Args:
441          i (int): ID to the reference pattern
442        Raises:
443          Nothing
444        Returns:
445          str : path to the file where to generate the pattern data
446        """
447        if name:
448          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,self._ext)))
449        else:
450          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,self._ext)))
451
452    def refS8P(self,i,name=None):
453        """ Path to a reference pattern from the ID
454
455        Args:
456          i (int): ID to the reference pattern
457        Raises:
458          Nothing
459        Returns:
460          str : path to the file where to generate the pattern data
461        """
462        if name:
463          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s8")))
464        else:
465          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s8")))
466
467    def refU8P(self,i,name=None):
468        """ Path to a reference pattern from the ID
469
470        Args:
471          i (int): ID to the reference pattern
472        Raises:
473          Nothing
474        Returns:
475          str : path to the file where to generate the pattern data
476        """
477        if name:
478          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u8")))
479        else:
480          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u8")))
481
482
483    def refS16P(self,i,name=None):
484        """ Path to a reference pattern from the ID
485
486        Args:
487          i (int): ID to the reference pattern
488        Raises:
489          Nothing
490        Returns:
491          str : path to the file where to generate the pattern data
492        """
493        if name:
494          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s16")))
495        else:
496          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s16")))
497
498    def refU16P(self,i,name=None):
499        """ Path to a reference pattern from the ID
500
501        Args:
502          i (int): ID to the reference pattern
503        Raises:
504          Nothing
505        Returns:
506          str : path to the file where to generate the pattern data
507        """
508        if name:
509          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u16")))
510        else:
511          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u16")))
512
513
514    def refS32P(self,i,name=None):
515        """ Path to a reference pattern from the ID
516
517        Args:
518          i (int): ID to the reference pattern
519        Raises:
520          Nothing
521        Returns:
522          str : path to the file where to generate the pattern data
523        """
524        if name:
525          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s32")))
526        else:
527          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s32")))
528
529    def refU32P(self,i,name=None):
530        """ Path to a reference pattern from the ID
531
532        Args:
533          i (int): ID to the reference pattern
534        Raises:
535          Nothing
536        Returns:
537          str : path to the file where to generate the pattern data
538        """
539        if name:
540          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u32")))
541        else:
542          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u32")))
543
544
545    def refS64P(self,i,name=None):
546        """ Path to a reference pattern from the ID
547
548        Args:
549          i (int): ID to the reference pattern
550        Raises:
551          Nothing
552        Returns:
553          str : path to the file where to generate the pattern data
554        """
555        if name:
556          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s64")))
557        else:
558          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s64")))
559
560
561    def refU64P(self,i,name=None):
562        """ Path to a reference pattern from the ID
563
564        Args:
565          i (int): ID to the reference pattern
566        Raises:
567          Nothing
568        Returns:
569          str : path to the file where to generate the pattern data
570        """
571        if name:
572          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u64")))
573        else:
574          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u64")))
575
576
577
578    def refQ63P(self,i,name=None):
579        """ Path to a reference pattern from the ID
580
581        Args:
582          i (int): ID to the reference pattern
583        Raises:
584          Nothing
585        Returns:
586          str : path to the file where to generate the pattern data
587        """
588        if name:
589          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q63")))
590        else:
591          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q63")))
592
593    def refQ31P(self,i,name=None):
594        """ Path to a reference pattern from the ID
595
596        Args:
597          i (int): ID to the reference pattern
598        Raises:
599          Nothing
600        Returns:
601          str : path to the file where to generate the pattern data
602        """
603        if name:
604          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q31")))
605        else:
606          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q31")))
607
608    def refF32P(self,i,name=None):
609        """ Path to a reference pattern from the ID
610
611        Args:
612          i (int): ID to the reference pattern
613        Raises:
614          Nothing
615        Returns:
616          str : path to the file where to generate the pattern data
617        """
618        if name:
619          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f32")))
620        else:
621          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f32")))
622
623    def refF16P(self,i,name=None):
624        """ Path to a reference pattern from the ID
625
626        Args:
627          i (int): ID to the reference pattern
628        Raises:
629          Nothing
630        Returns:
631          str : path to the file where to generate the pattern data
632        """
633        if name:
634          return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f16")))
635        else:
636          return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f16")))
637
638    def paramP(self,i,name=None):
639        """ Path to a parameters from the ID
640
641        Args:
642          i (int): ID to the params
643        Raises:
644          Nothing
645        Returns:
646          str : path to the file where to generate the pattern data
647        """
648        if name:
649          return(os.path.join(self._paramDir,"%s%d.txt" % (name,i)))
650        else:
651          return(os.path.join(self._paramDir,"Params%d.txt" % i))
652
653    def _writeVectorF64(self,i,data):
654        """ Write pattern data
655
656        The format is recognized by the text framework script.
657        First line is the sample width (B,H or W,D for 8,16,32 or 64 bits)
658        Second line is number of samples
659        Other lines are hexadecimal representation of the samples in format
660        which can be read on big endian ARM.
661
662          Args:
663            j (int): ID of pattern file
664            data (array): Vector containing the data
665          Raises:
666            Nothing
667          Returns:
668            Nothing
669        """
670        if self.canOverwrite(i):
671          with open(i,"w") as f:
672              # Write sample dimension nb sample header
673              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
674              f.write("D\n%d\n" % len(data))
675              for v in data:
676                  f.write("// %f\n" % v)
677                  f.write("%s\n" % float64_to_hex(v))
678
679    def _writeVectorF32(self,i,data):
680        """ Write pattern data
681
682        The format is recognized by the text framework script.
683        First line is the sample width (B,H or W for 8,16 or 32 bits)
684        Second line is number of samples
685        Other lines are hexadecimal representation of the samples in format
686        which can be read on big endian ARM.
687
688          Args:
689            j (int): ID of pattern file
690            data (array): Vector containing the data
691          Raises:
692            Nothing
693          Returns:
694            Nothing
695        """
696        if self.canOverwrite(i):
697          with open(i,"w") as f:
698              # Write sample dimension nb sample header
699              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
700              f.write("W\n%d\n" % len(data))
701              for v in data:
702                  f.write("// %f\n" % v)
703                  f.write("%s\n" % float_to_hex(v))
704
705    def _writeVectorF16(self,i,data):
706        """ Write pattern data
707
708        The format is recognized by the text framework script.
709        First line is the sample width (B,H or W for 8,16 or 32 bits)
710        Second line is number of samples
711        Other lines are hexadecimal representation of the samples in format
712        which can be read on big endian ARM.
713
714          Args:
715            j (int): ID of pattern file
716            data (array): Vector containing the data
717          Raises:
718            Nothing
719          Returns:
720            Nothing
721        """
722        if self.canOverwrite(i):
723          with open(i,"w") as f:
724              # Write sample dimension nb sample header
725              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
726              f.write("H\n%d\n" % len(data))
727              for v in data:
728                  f.write("// %f\n" % v)
729                  f.write("%s\n" % float16_to_hex(v))
730
731    def _writeVectorQ63(self,i,data):
732        """ Write pattern data
733
734        The format is recognized by the text framework script.
735        First line is the sample width (B,H or W for 8,16 or 32 bits)
736        Second line is number of samples
737        Other lines are hexadecimal representation of the samples in format
738        which can be read on big endian ARM.
739
740          Args:
741            j (int): ID of pattern file
742            data (array): Vector containing the data
743          Raises:
744            Nothing
745          Returns:
746            Nothing
747        """
748        if self.canOverwrite(i):
749          with open(i,"w") as f:
750              # Write sample dimension nb sample header
751              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
752              f.write("D\n%d\n" % len(data))
753              for v in data:
754                  f.write("// %f\n" % v)
755                  f.write("%s\n" % to_q63(v))
756
757    def _writeVectorQ31(self,i,data):
758        """ Write pattern data
759
760        The format is recognized by the text framework script.
761        First line is the sample width (B,H or W for 8,16 or 32 bits)
762        Second line is number of samples
763        Other lines are hexadecimal representation of the samples in format
764        which can be read on big endian ARM.
765
766          Args:
767            j (int): ID of pattern file
768            data (array): Vector containing the data
769          Raises:
770            Nothing
771          Returns:
772            Nothing
773        """
774        if self.canOverwrite(i):
775          with open(i,"w") as f:
776              # Write sample dimension nb sample header
777              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
778              f.write("W\n%d\n" % len(data))
779              for v in data:
780                  f.write("// %f\n" % v)
781                  f.write("%s\n" % to_q31(v))
782
783    def _writeVectorQ15(self,i,data):
784        """ Write pattern data
785
786        The format is recognized by the text framework script.
787        First line is the sample width (B,H or W for 8,16 or 32 bits)
788        Second line is number of samples
789        Other lines are hexadecimal representation of the samples in format
790        which can be read on big endian ARM.
791
792          Args:
793            j (int): ID of pattern file
794            data (array): Vector containing the data
795          Raises:
796            Nothing
797          Returns:
798            Nothing
799        """
800        if self.canOverwrite(i):
801          with open(i,"w") as f:
802              # Write sample dimension nb sample header
803              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
804              f.write("H\n%d\n" % len(data))
805              for v in data:
806                  f.write("// %f\n" % v)
807                  f.write("%s\n" % to_q15(v))
808
809    def _writeVectorS16(self,i,data):
810        """ Write pattern data
811
812        The format is recognized by the text framework script.
813        First line is the sample width (B,H or W for 8,16 or 32 bits)
814        Second line is number of samples
815        Other lines are hexadecimal representation of the samples in format
816        which can be read on big endian ARM.
817
818          Args:
819            j (int): ID of pattern file
820            data (array): Vector containing the data
821          Raises:
822            Nothing
823          Returns:
824            Nothing
825        """
826        if self.canOverwrite(i):
827          with open(i,"w") as f:
828              # Write sample dimension nb sample header
829              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
830              f.write("H\n%d\n" % len(data))
831              for v in data:
832                  f.write("// %d\n" % v)
833                  f.write("%s\n" % s16(v))
834
835    def _writeVectorU16(self,i,data):
836        """ Write pattern data
837
838        The format is recognized by the text framework script.
839        First line is the sample width (B,H or W for 8,16 or 32 bits)
840        Second line is number of samples
841        Other lines are hexadecimal representation of the samples in format
842        which can be read on big endian ARM.
843
844          Args:
845            j (int): ID of pattern file
846            data (array): Vector containing the data
847          Raises:
848            Nothing
849          Returns:
850            Nothing
851        """
852        if self.canOverwrite(i):
853          with open(i,"w") as f:
854              # Write sample dimension nb sample header
855              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
856              f.write("H\n%d\n" % len(data))
857              for v in data:
858                  f.write("// %d\n" % v)
859                  f.write("%s\n" % u16(v))
860
861    def _writeVectorS64(self,i,data):
862        """ Write pattern data
863
864        The format is recognized by the text framework script.
865        First line is the sample width (B,H or W for 8,16 or 32 bits)
866        Second line is number of samples
867        Other lines are hexadecimal representation of the samples in format
868        which can be read on big endian ARM.
869
870          Args:
871            j (int): ID of pattern file
872            data (array): Vector containing the data
873          Raises:
874            Nothing
875          Returns:
876            Nothing
877        """
878        if self.canOverwrite(i):
879          with open(i,"w") as f:
880              # Write sample dimension nb sample header
881              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
882              f.write("D\n%d\n" % len(data))
883              for v in data:
884                  f.write("// %d\n" % v)
885                  f.write("%s\n" % s64(v))
886
887
888    def _writeVectorU64(self,i,data):
889        """ Write pattern data
890
891        The format is recognized by the text framework script.
892        First line is the sample width (B,H or W for 8,16 or 32 bits)
893        Second line is number of samples
894        Other lines are hexadecimal representation of the samples in format
895        which can be read on big endian ARM.
896
897          Args:
898            j (int): ID of pattern file
899            data (array): Vector containing the data
900          Raises:
901            Nothing
902          Returns:
903            Nothing
904        """
905        if self.canOverwrite(i):
906          with open(i,"w") as f:
907              # Write sample dimension nb sample header
908              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
909              f.write("D\n%d\n" % len(data))
910              for v in data:
911                  f.write("// %d\n" % v)
912                  f.write("%s\n" % u64(v))
913
914    def _writeVectorS32(self,i,data):
915        """ Write pattern data
916
917        The format is recognized by the text framework script.
918        First line is the sample width (B,H or W for 8,16 or 32 bits)
919        Second line is number of samples
920        Other lines are hexadecimal representation of the samples in format
921        which can be read on big endian ARM.
922
923          Args:
924            j (int): ID of pattern file
925            data (array): Vector containing the data
926          Raises:
927            Nothing
928          Returns:
929            Nothing
930        """
931        if self.canOverwrite(i):
932          with open(i,"w") as f:
933              # Write sample dimension nb sample header
934              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
935              f.write("W\n%d\n" % len(data))
936              for v in data:
937                  f.write("// %d\n" % v)
938                  f.write("%s\n" % s32(v))
939
940    def _writeVectorU32(self,i,data):
941        """ Write pattern data
942
943        The format is recognized by the text framework script.
944        First line is the sample width (B,H or W for 8,16 or 32 bits)
945        Second line is number of samples
946        Other lines are hexadecimal representation of the samples in format
947        which can be read on big endian ARM.
948
949          Args:
950            j (int): ID of pattern file
951            data (array): Vector containing the data
952          Raises:
953            Nothing
954          Returns:
955            Nothing
956        """
957        if self.canOverwrite(i):
958          with open(i,"w") as f:
959              # Write sample dimension nb sample header
960              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
961              f.write("W\n%d\n" % len(data))
962              for v in data:
963                  f.write("// %s\n" % v)
964                  f.write("%s\n" % u32(v))
965
966    def _writeVectorQ7(self,i,data):
967        """ Write pattern data
968
969        The format is recognized by the text framework script.
970        First line is the sample width (B,H or W for 8,16 or 32 bits)
971        Second line is number of samples
972        Other lines are hexadecimal representation of the samples in format
973        which can be read on big endian ARM.
974
975          Args:
976            j (int): ID of pattern file
977            data (array): Vector containing the data
978          Raises:
979            Nothing
980          Returns:
981            Nothing
982        """
983        if self.canOverwrite(i):
984          with open(i,"w") as f:
985              # Write sample dimension nb sample header
986              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
987              f.write("B\n%d\n" % len(data))
988              for v in data:
989                  f.write("// %f\n" % v)
990                  f.write("%s\n" % to_q7(v))
991
992    def _writeVectorS8(self,i,data):
993        """ Write pattern data
994
995        The format is recognized by the text framework script.
996        First line is the sample width (B,H or W for 8,16 or 32 bits)
997        Second line is number of samples
998        Other lines are hexadecimal representation of the samples in format
999        which can be read on big endian ARM.
1000
1001          Args:
1002            j (int): ID of pattern file
1003            data (array): Vector containing the data
1004          Raises:
1005            Nothing
1006          Returns:
1007            Nothing
1008        """
1009        if self.canOverwrite(i):
1010          with open(i,"w") as f:
1011              # Write sample dimension nb sample header
1012              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
1013              f.write("B\n%d\n" % len(data))
1014              for v in data:
1015                  f.write("// %d\n" % v)
1016                  f.write("%s\n" % s8(v))
1017
1018    def _writeVectorU8(self,i,data):
1019        """ Write pattern data
1020
1021        The format is recognized by the text framework script.
1022        First line is the sample width (B,H or W for 8,16 or 32 bits)
1023        Second line is number of samples
1024        Other lines are hexadecimal representation of the samples in format
1025        which can be read on big endian ARM.
1026
1027          Args:
1028            j (int): ID of pattern file
1029            data (array): Vector containing the data
1030          Raises:
1031            Nothing
1032          Returns:
1033            Nothing
1034        """
1035        if self.canOverwrite(i):
1036          with open(i,"w") as f:
1037              # Write sample dimension nb sample header
1038              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
1039              f.write("B\n%d\n" % len(data))
1040              for v in data:
1041                  f.write("// %d\n" % v)
1042                  f.write("%s\n" % u8(v))
1043
1044    def writeReference(self,j,data,name=None):
1045        if (self._ext == "f64"):
1046          self._writeVectorF64(self.refP(j,name),data)
1047        if (self._ext == "f32"):
1048          self._writeVectorF32(self.refP(j,name),data)
1049        if (self._ext == "f16"):
1050          self._writeVectorF16(self.refP(j,name),data)
1051        if (self._ext == "q63"):
1052          self._writeVectorQ63(self.refP(j,name),data)
1053        if (self._ext == "q31"):
1054          self._writeVectorQ31(self.refP(j,name),data)
1055        if (self._ext == "q15"):
1056          self._writeVectorQ15(self.refP(j,name),data)
1057        if (self._ext == "q7"):
1058          self._writeVectorQ7(self.refP(j,name),data)
1059        if (self._ext == "s64"):
1060          self._writeVectorS64(self.refP(j,name),data)
1061        if (self._ext == "u64"):
1062          self._writeVectorU64(self.refP(j,name),data)
1063        if (self._ext == "s32"):
1064          self._writeVectorS32(self.refP(j,name),data)
1065        if (self._ext == "u32"):
1066          self._writeVectorU32(self.refP(j,name),data)
1067        if (self._ext == "s16"):
1068          self._writeVectorS16(self.refP(j,name),data)
1069        if (self._ext == "u16"):
1070          self._writeVectorU16(self.refP(j,name),data)
1071        if (self._ext == "s8"):
1072          self._writeVectorS8(self.refP(j,name),data)
1073        if (self._ext == "u8"):
1074          self._writeVectorU8(self.refP(j,name),data)
1075
1076    def writeReferenceQ63(self,j,data,name=None):
1077        self._writeVectorQ63(self.refQ63P(j,name),data)
1078
1079    def writeReferenceQ31(self,j,data,name=None):
1080        self._writeVectorQ31(self.refQ31P(j,name),data)
1081
1082    def writeReferenceS8(self,j,data,name=None):
1083        self._writeVectorS8(self.refS8P(j,name),data)
1084
1085    def writeReferenceS16(self,j,data,name=None):
1086        self._writeVectorS16(self.refS16P(j,name),data)
1087
1088    def writeReferenceS32(self,j,data,name=None):
1089        self._writeVectorS32(self.refS32P(j,name),data)
1090
1091    def writeReferenceS64(self,j,data,name=None):
1092        self._writeVectorS64(self.refS64P(j,name),data)
1093
1094    def writeReferenceU8(self,j,data,name=None):
1095        self._writeVectorU8(self.refU8P(j,name),data)
1096
1097    def writeReferenceU16(self,j,data,name=None):
1098        self._writeVectorU16(self.refU16P(j,name),data)
1099
1100    def writeReferenceU32(self,j,data,name=None):
1101        self._writeVectorU32(self.refU32P(j,name),data)
1102
1103    def writeReferenceU64(self,j,data,name=None):
1104        self._writeVectorU64(self.refU64P(j,name),data)
1105
1106    def writeReferenceF32(self,j,data,name=None):
1107        self._writeVectorF32(self.refF32P(j,name),data)
1108
1109    def writeReferenceF16(self,j,data,name=None):
1110        self._writeVectorF16(self.refF16P(j,name),data)
1111
1112    def writeInput(self,j,data,name=None):
1113        if (self._ext == "f64"):
1114          self._writeVectorF64(self.inputP(j,name),data)
1115        if (self._ext == "f32"):
1116          self._writeVectorF32(self.inputP(j,name),data)
1117        if (self._ext == "f16"):
1118          self._writeVectorF16(self.inputP(j,name),data)
1119        if (self._ext == "q63"):
1120          self._writeVectorQ63(self.inputP(j,name),data)
1121        if (self._ext == "q31"):
1122          self._writeVectorQ31(self.inputP(j,name),data)
1123        if (self._ext == "q15"):
1124          self._writeVectorQ15(self.inputP(j,name),data)
1125        if (self._ext == "q7"):
1126          self._writeVectorQ7(self.inputP(j,name),data)
1127        if (self._ext == "s64"):
1128          self._writeVectorS64(self.inputP(j,name),data)
1129        if (self._ext == "u64"):
1130          self._writeVectorU64(self.inputP(j,name),data)
1131        if (self._ext == "s32"):
1132          self._writeVectorS32(self.inputP(j,name),data)
1133        if (self._ext == "u32"):
1134          self._writeVectorU32(self.inputP(j,name),data)
1135        if (self._ext == "s8"):
1136          self._writeVectorS8(self.inputP(j,name),data)
1137        if (self._ext == "u8"):
1138          self._writeVectorU8(self.inputP(j,name),data)
1139
1140    def writeInputF64(self,j,data,name=None):
1141        self._writeVectorF64(self.inputF64P(j,name),data)
1142
1143    def writeInputF32(self,j,data,name=None):
1144        self._writeVectorF32(self.inputF32P(j,name),data)
1145
1146    def writeInputF16(self,j,data,name=None):
1147        self._writeVectorF16(self.inputF16P(j,name),data)
1148
1149    def writeInputQ63(self,j,data,name=None):
1150        self._writeVectorQ63(self.inputQ63P(j,name),data)
1151
1152    def writeInputQ31(self,j,data,name=None):
1153        self._writeVectorQ31(self.inputQ31P(j,name),data)
1154
1155    def writeInputQ15(self,j,data,name=None):
1156        self._writeVectorQ15(self.inputQ15P(j,name),data)
1157
1158    def writeInputQ7(self,j,data,name=None):
1159        self._writeVectorQ7(self.inputQ7P(j,name),data)
1160
1161    def writeInputS64(self,j,data,name=None):
1162        self._writeVectorS64(self.inputS64P(j,name),data)
1163
1164    def writeInputS32(self,j,data,name=None):
1165        self._writeVectorS32(self.inputS32P(j,name),data)
1166
1167    def writeInputS16(self,j,data,name=None):
1168        self._writeVectorS16(self.inputS16P(j,name),data)
1169
1170    def writeInputS8(self,j,data,name=None):
1171        self._writeVectorS8(self.inputS8P(j,name),data)
1172
1173    def writeInputU64(self,j,data,name=None):
1174        self._writeVectorU64(self.inputU64P(j,name),data)
1175
1176    def writeInputU32(self,j,data,name=None):
1177        self._writeVectorU32(self.inputU32P(j,name),data)
1178
1179    def writeInputU16(self,j,data,name=None):
1180        self._writeVectorU16(self.inputU16P(j,name),data)
1181
1182    def writeInputU8(self,j,data,name=None):
1183        self._writeVectorU8(self.inputU8P(j,name),data)
1184
1185    def writeParam(self,j,data,name=None):
1186        """ Write pattern data
1187
1188        The format is recognized by the text framework script.
1189        First line is the sample width (B,H or W for 8,16 or 32 bits)
1190        Second line is number of samples
1191        Other lines are hexadecimal representation of the samples in format
1192        which can be read on big endian ARM.
1193
1194          Args:
1195            j (int): ID of parameter file
1196            data (array): Vector containing the data
1197          Raises:
1198            Nothing
1199          Returns:
1200            Nothing
1201        """
1202        i=self.paramP(j,name)
1203        if self.canOverwrite(i):
1204          with open(i,"w") as f:
1205              # Write sample dimension nb sample header
1206              #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
1207              f.write("%d\n" % len(data))
1208              for v in data:
1209                  f.write("%d\n" % v)
1210
1211
1212
1213