Tools.py 39 KB

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