Tools.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. import os.path
  2. import struct
  3. import numpy as np
  4. def normalize(a):
  5. return(a/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. Q31 = 31
  14. Q15 = 15
  15. Q7 = 7
  16. def loopnb(format,loopkind):
  17. nb = 0
  18. if loopkind == TAILONLY:
  19. if format == 64:
  20. nb = 1
  21. if format == 0 or format == 31:
  22. nb = 3
  23. if format == 15 or format == 16:
  24. nb = 7
  25. if format == 7:
  26. nb = 15
  27. if loopkind == BODYONLY:
  28. if format == 64:
  29. nb = 4
  30. if format == 0 or format == 31:
  31. nb = 8
  32. if format == 15 or format == 16:
  33. nb = 16
  34. if format == 7:
  35. nb = 32
  36. if loopkind == BODYANDTAIL:
  37. if format == 64:
  38. nb = 5
  39. if format == 0 or format == 31:
  40. nb = 11 # 9
  41. if format == 15 or format == 16:
  42. nb = 23 # 17
  43. if format == 7:
  44. nb = 47 # 33
  45. return(nb)
  46. # Tools to generate pattern files
  47. def createMissingDir(destPath):
  48. theDir=os.path.normpath(destPath)
  49. if not os.path.exists(theDir):
  50. os.makedirs(theDir)
  51. # Pack an array of boolean into uint32
  52. def packset(a):
  53. b = np.packbits(a)
  54. newSize = int(np.ceil(b.shape[0] / 4.0)) * 4
  55. c = np.copy(b)
  56. c.resize(newSize)
  57. #print(c)
  58. vecSize = round(newSize/4)
  59. c=c.reshape(vecSize,4)
  60. #print(c)
  61. r = np.zeros(vecSize)
  62. result = []
  63. for i in range(0,vecSize):
  64. #print(c[i,:])
  65. #print("%X %X %X %X" % (c[i,0],c[i,1],c[i,2],c[i,3]))
  66. d = (c[i,0] << 24) | (c[i,1] << 16) | (c[i,2] << 8) | c[i,3]
  67. result.append(np.uint32(d))
  68. return(result)
  69. def float_to_hex(f):
  70. """ Convert and x86 float to an ARM unsigned long int.
  71. Args:
  72. f (float): value to be converted
  73. Raises:
  74. Nothing
  75. Returns:
  76. str : representation of the hex value
  77. """
  78. return hex(struct.unpack('<I', struct.pack('<f', f))[0])
  79. def float16_to_hex(f):
  80. """ Convert and x86 float to an ARM unsigned long int.
  81. Args:
  82. f (float): value to be converted
  83. Raises:
  84. Nothing
  85. Returns:
  86. str : representation of the hex value
  87. """
  88. return hex(struct.unpack('<H', struct.pack('<e', f))[0])
  89. def float64_to_hex(f):
  90. """ Convert and x86 float to an ARM unsigned long int.
  91. Args:
  92. f (float): value to be converted
  93. Raises:
  94. Nothing
  95. Returns:
  96. str : representation of the hex value
  97. """
  98. return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
  99. def to_q63(v):
  100. r = int(round(v * 2**63))
  101. if (r > 0x07FFFFFFFFFFFFFFF):
  102. r = 0x07FFFFFFFFFFFFFFF
  103. if (r < -0x08000000000000000):
  104. r = -0x08000000000000000
  105. return ("0x%s" % format(struct.unpack('<Q', struct.pack('<q', r))[0],'016X'))
  106. def to_q31(v):
  107. r = int(round(v * 2**31))
  108. if (r > 0x07FFFFFFF):
  109. r = 0x07FFFFFFF
  110. if (r < -0x080000000):
  111. r = -0x080000000
  112. return ("0x%s" % format(struct.unpack('<I', struct.pack('<i', r))[0],'08X'))
  113. def to_q15(v):
  114. r = int(round(v * 2**15))
  115. if (r > 0x07FFF):
  116. r = 0x07FFF
  117. if (r < -0x08000):
  118. r = -0x08000
  119. return ("0x%s" % format(struct.unpack('<H', struct.pack('<h', r))[0],'04X'))
  120. def to_q7(v):
  121. r = int(round(v * 2**7))
  122. if (r > 0x07F):
  123. r = 0x07F
  124. if (r < -0x080):
  125. r = -0x080
  126. return ("0x%s" % format(struct.unpack('<B', struct.pack('<b', r))[0],'02X'))
  127. def s8(r):
  128. return ("0x%s" % format(struct.unpack('<B', struct.pack('<b', r))[0],'02X'))
  129. def s16(r):
  130. return ("0x%s" % format(struct.unpack('<H', struct.pack('<h', r))[0],'04X'))
  131. def s32(r):
  132. return ("0x%s" % format(struct.unpack('<I', struct.pack('<i', r))[0],'08X'))
  133. def u32(r):
  134. return ("0x%s" % format(struct.unpack('<I', struct.pack('<I', r))[0],'08X'))
  135. class Config:
  136. def __init__(self,patternDir,paramDir,ext):
  137. self._patternDir = "%s%s" % (patternDir,ext.upper())
  138. self._paramDir = "%s%s" % (paramDir,ext.upper())
  139. self._ext = ext
  140. createMissingDir(self._patternDir)
  141. createMissingDir(self._paramDir)
  142. def inputP(self,i,name=None):
  143. """ Path to a reference pattern from the ID
  144. Args:
  145. i (int): ID to the reference pattern
  146. Raises:
  147. Nothing
  148. Returns:
  149. str : path to the file where to generate the pattern data
  150. """
  151. if name:
  152. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,self._ext)))
  153. else:
  154. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,self._ext)))
  155. def inputS32P(self,i,name=None):
  156. """ Path to a reference pattern from the ID
  157. Args:
  158. i (int): ID to the reference pattern
  159. Raises:
  160. Nothing
  161. Returns:
  162. str : path to the file where to generate the pattern data
  163. """
  164. if name:
  165. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s32")))
  166. else:
  167. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s32")))
  168. def inputS16P(self,i,name=None):
  169. """ Path to a reference pattern from the ID
  170. Args:
  171. i (int): ID to the reference pattern
  172. Raises:
  173. Nothing
  174. Returns:
  175. str : path to the file where to generate the pattern data
  176. """
  177. if name:
  178. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s16")))
  179. else:
  180. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s16")))
  181. def inputS8P(self,i,name=None):
  182. """ Path to a reference pattern from the ID
  183. Args:
  184. i (int): ID to the reference pattern
  185. Raises:
  186. Nothing
  187. Returns:
  188. str : path to the file where to generate the pattern data
  189. """
  190. if name:
  191. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s8")))
  192. else:
  193. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s8")))
  194. def inputF32P(self,i,name=None):
  195. """ Path to a reference pattern from the ID
  196. Args:
  197. i (int): ID to the reference pattern
  198. Raises:
  199. Nothing
  200. Returns:
  201. str : path to the file where to generate the pattern data
  202. """
  203. if name:
  204. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f32")))
  205. else:
  206. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f32")))
  207. def inputF16P(self,i,name=None):
  208. """ Path to a reference pattern from the ID
  209. Args:
  210. i (int): ID to the reference pattern
  211. Raises:
  212. Nothing
  213. Returns:
  214. str : path to the file where to generate the pattern data
  215. """
  216. if name:
  217. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f16")))
  218. else:
  219. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f16")))
  220. def inputQ31P(self,i,name=None):
  221. """ Path to a reference pattern from the ID
  222. Args:
  223. i (int): ID to the reference pattern
  224. Raises:
  225. Nothing
  226. Returns:
  227. str : path to the file where to generate the pattern data
  228. """
  229. if name:
  230. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q31")))
  231. else:
  232. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q31")))
  233. def inputQ15P(self,i,name=None):
  234. """ Path to a reference pattern from the ID
  235. Args:
  236. i (int): ID to the reference pattern
  237. Raises:
  238. Nothing
  239. Returns:
  240. str : path to the file where to generate the pattern data
  241. """
  242. if name:
  243. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q15")))
  244. else:
  245. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q15")))
  246. def inputQ7P(self,i,name=None):
  247. """ Path to a reference pattern from the ID
  248. Args:
  249. i (int): ID to the reference pattern
  250. Raises:
  251. Nothing
  252. Returns:
  253. str : path to the file where to generate the pattern data
  254. """
  255. if name:
  256. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q7")))
  257. else:
  258. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q7")))
  259. def inputU32P(self,i,name=None):
  260. """ Path to a reference pattern from the ID
  261. Args:
  262. i (int): ID to the reference pattern
  263. Raises:
  264. Nothing
  265. Returns:
  266. str : path to the file where to generate the pattern data
  267. """
  268. if name:
  269. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u32")))
  270. else:
  271. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u32")))
  272. def refP(self,i,name=None):
  273. """ Path to a reference pattern from the ID
  274. Args:
  275. i (int): ID to the reference pattern
  276. Raises:
  277. Nothing
  278. Returns:
  279. str : path to the file where to generate the pattern data
  280. """
  281. if name:
  282. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,self._ext)))
  283. else:
  284. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,self._ext)))
  285. def refS8P(self,i,name=None):
  286. """ Path to a reference pattern from the ID
  287. Args:
  288. i (int): ID to the reference pattern
  289. Raises:
  290. Nothing
  291. Returns:
  292. str : path to the file where to generate the pattern data
  293. """
  294. if name:
  295. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s8")))
  296. else:
  297. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s8")))
  298. def refS16P(self,i,name=None):
  299. """ Path to a reference pattern from the ID
  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,"s16")))
  309. else:
  310. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s16")))
  311. def refS32P(self,i,name=None):
  312. """ Path to a reference pattern from the ID
  313. Args:
  314. i (int): ID to the reference pattern
  315. Raises:
  316. Nothing
  317. Returns:
  318. str : path to the file where to generate the pattern data
  319. """
  320. if name:
  321. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s32")))
  322. else:
  323. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s32")))
  324. def refQ63P(self,i,name=None):
  325. """ Path to a reference pattern from the ID
  326. Args:
  327. i (int): ID to the reference pattern
  328. Raises:
  329. Nothing
  330. Returns:
  331. str : path to the file where to generate the pattern data
  332. """
  333. if name:
  334. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q63")))
  335. else:
  336. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q63")))
  337. def refQ31P(self,i,name=None):
  338. """ Path to a reference pattern from the ID
  339. Args:
  340. i (int): ID to the reference pattern
  341. Raises:
  342. Nothing
  343. Returns:
  344. str : path to the file where to generate the pattern data
  345. """
  346. if name:
  347. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q31")))
  348. else:
  349. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q31")))
  350. def refF32P(self,i,name=None):
  351. """ Path to a reference pattern from the ID
  352. Args:
  353. i (int): ID to the reference pattern
  354. Raises:
  355. Nothing
  356. Returns:
  357. str : path to the file where to generate the pattern data
  358. """
  359. if name:
  360. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f32")))
  361. else:
  362. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f32")))
  363. def refF16P(self,i,name=None):
  364. """ Path to a reference pattern from the ID
  365. Args:
  366. i (int): ID to the reference pattern
  367. Raises:
  368. Nothing
  369. Returns:
  370. str : path to the file where to generate the pattern data
  371. """
  372. if name:
  373. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f16")))
  374. else:
  375. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f16")))
  376. def paramP(self,i,name=None):
  377. """ Path to a parameters from the ID
  378. Args:
  379. i (int): ID to the params
  380. Raises:
  381. Nothing
  382. Returns:
  383. str : path to the file where to generate the pattern data
  384. """
  385. if name:
  386. return(os.path.join(self._paramDir,"%s%d.txt" % (name,i)))
  387. else:
  388. return(os.path.join(self._paramDir,"Params%d.txt" % i))
  389. def _writeVectorF64(self,i,data):
  390. """ Write pattern data
  391. The format is recognized by the text framework script.
  392. First line is the sample width (B,H or W,D for 8,16,32 or 64 bits)
  393. Second line is number of samples
  394. Other lines are hexadecimal representation of the samples in format
  395. which can be read on big endian ARM.
  396. Args:
  397. j (int): ID of pattern file
  398. data (array): Vector containing the data
  399. Raises:
  400. Nothing
  401. Returns:
  402. Nothing
  403. """
  404. with open(i,"w") as f:
  405. # Write sample dimension nb sample header
  406. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  407. f.write("D\n%d\n" % len(data))
  408. for v in data:
  409. f.write("// %f\n" % v)
  410. f.write("%s\n" % float64_to_hex(v))
  411. def _writeVectorF32(self,i,data):
  412. """ Write pattern data
  413. The format is recognized by the text framework script.
  414. First line is the sample width (B,H or W for 8,16 or 32 bits)
  415. Second line is number of samples
  416. Other lines are hexadecimal representation of the samples in format
  417. which can be read on big endian ARM.
  418. Args:
  419. j (int): ID of pattern file
  420. data (array): Vector containing the data
  421. Raises:
  422. Nothing
  423. Returns:
  424. Nothing
  425. """
  426. with open(i,"w") as f:
  427. # Write sample dimension nb sample header
  428. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  429. f.write("W\n%d\n" % len(data))
  430. for v in data:
  431. f.write("// %f\n" % v)
  432. f.write("%s\n" % float_to_hex(v))
  433. def _writeVectorF16(self,i,data):
  434. """ Write pattern data
  435. The format is recognized by the text framework script.
  436. First line is the sample width (B,H or W for 8,16 or 32 bits)
  437. Second line is number of samples
  438. Other lines are hexadecimal representation of the samples in format
  439. which can be read on big endian ARM.
  440. Args:
  441. j (int): ID of pattern file
  442. data (array): Vector containing the data
  443. Raises:
  444. Nothing
  445. Returns:
  446. Nothing
  447. """
  448. with open(i,"w") as f:
  449. # Write sample dimension nb sample header
  450. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  451. f.write("H\n%d\n" % len(data))
  452. for v in data:
  453. f.write("// %f\n" % v)
  454. f.write("%s\n" % float16_to_hex(v))
  455. def _writeVectorQ63(self,i,data):
  456. """ Write pattern data
  457. The format is recognized by the text framework script.
  458. First line is the sample width (B,H or W for 8,16 or 32 bits)
  459. Second line is number of samples
  460. Other lines are hexadecimal representation of the samples in format
  461. which can be read on big endian ARM.
  462. Args:
  463. j (int): ID of pattern file
  464. data (array): Vector containing the data
  465. Raises:
  466. Nothing
  467. Returns:
  468. Nothing
  469. """
  470. with open(i,"w") as f:
  471. # Write sample dimension nb sample header
  472. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  473. f.write("D\n%d\n" % len(data))
  474. for v in data:
  475. f.write("// %f\n" % v)
  476. f.write("%s\n" % to_q63(v))
  477. def _writeVectorQ31(self,i,data):
  478. """ Write pattern data
  479. The format is recognized by the text framework script.
  480. First line is the sample width (B,H or W for 8,16 or 32 bits)
  481. Second line is number of samples
  482. Other lines are hexadecimal representation of the samples in format
  483. which can be read on big endian ARM.
  484. Args:
  485. j (int): ID of pattern file
  486. data (array): Vector containing the data
  487. Raises:
  488. Nothing
  489. Returns:
  490. Nothing
  491. """
  492. with open(i,"w") as f:
  493. # Write sample dimension nb sample header
  494. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  495. f.write("W\n%d\n" % len(data))
  496. for v in data:
  497. f.write("// %f\n" % v)
  498. f.write("%s\n" % to_q31(v))
  499. def _writeVectorQ15(self,i,data):
  500. """ Write pattern data
  501. The format is recognized by the text framework script.
  502. First line is the sample width (B,H or W for 8,16 or 32 bits)
  503. Second line is number of samples
  504. Other lines are hexadecimal representation of the samples in format
  505. which can be read on big endian ARM.
  506. Args:
  507. j (int): ID of pattern file
  508. data (array): Vector containing the data
  509. Raises:
  510. Nothing
  511. Returns:
  512. Nothing
  513. """
  514. with open(i,"w") as f:
  515. # Write sample dimension nb sample header
  516. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  517. f.write("H\n%d\n" % len(data))
  518. for v in data:
  519. f.write("// %f\n" % v)
  520. f.write("%s\n" % to_q15(v))
  521. def _writeVectorS16(self,i,data):
  522. """ Write pattern data
  523. The format is recognized by the text framework script.
  524. First line is the sample width (B,H or W for 8,16 or 32 bits)
  525. Second line is number of samples
  526. Other lines are hexadecimal representation of the samples in format
  527. which can be read on big endian ARM.
  528. Args:
  529. j (int): ID of pattern file
  530. data (array): Vector containing the data
  531. Raises:
  532. Nothing
  533. Returns:
  534. Nothing
  535. """
  536. with open(i,"w") as f:
  537. # Write sample dimension nb sample header
  538. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  539. f.write("H\n%d\n" % len(data))
  540. for v in data:
  541. f.write("// %d\n" % v)
  542. f.write("%s\n" % s16(v))
  543. def _writeVectorS32(self,i,data):
  544. """ Write pattern data
  545. The format is recognized by the text framework script.
  546. First line is the sample width (B,H or W for 8,16 or 32 bits)
  547. Second line is number of samples
  548. Other lines are hexadecimal representation of the samples in format
  549. which can be read on big endian ARM.
  550. Args:
  551. j (int): ID of pattern file
  552. data (array): Vector containing the data
  553. Raises:
  554. Nothing
  555. Returns:
  556. Nothing
  557. """
  558. with open(i,"w") as f:
  559. # Write sample dimension nb sample header
  560. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  561. f.write("W\n%d\n" % len(data))
  562. for v in data:
  563. f.write("// %d\n" % v)
  564. f.write("%s\n" % s32(v))
  565. def _writeVectorU32(self,i,data):
  566. """ Write pattern data
  567. The format is recognized by the text framework script.
  568. First line is the sample width (B,H or W for 8,16 or 32 bits)
  569. Second line is number of samples
  570. Other lines are hexadecimal representation of the samples in format
  571. which can be read on big endian ARM.
  572. Args:
  573. j (int): ID of pattern file
  574. data (array): Vector containing the data
  575. Raises:
  576. Nothing
  577. Returns:
  578. Nothing
  579. """
  580. with open(i,"w") as f:
  581. # Write sample dimension nb sample header
  582. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  583. f.write("W\n%d\n" % len(data))
  584. for v in data:
  585. f.write("// %s\n" % v)
  586. f.write("%s\n" % u32(v))
  587. def _writeVectorQ7(self,i,data):
  588. """ Write pattern data
  589. The format is recognized by the text framework script.
  590. First line is the sample width (B,H or W for 8,16 or 32 bits)
  591. Second line is number of samples
  592. Other lines are hexadecimal representation of the samples in format
  593. which can be read on big endian ARM.
  594. Args:
  595. j (int): ID of pattern file
  596. data (array): Vector containing the data
  597. Raises:
  598. Nothing
  599. Returns:
  600. Nothing
  601. """
  602. with open(i,"w") as f:
  603. # Write sample dimension nb sample header
  604. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  605. f.write("B\n%d\n" % len(data))
  606. for v in data:
  607. f.write("// %f\n" % v)
  608. f.write("%s\n" % to_q7(v))
  609. def _writeVectorS8(self,i,data):
  610. """ Write pattern data
  611. The format is recognized by the text framework script.
  612. First line is the sample width (B,H or W for 8,16 or 32 bits)
  613. Second line is number of samples
  614. Other lines are hexadecimal representation of the samples in format
  615. which can be read on big endian ARM.
  616. Args:
  617. j (int): ID of pattern file
  618. data (array): Vector containing the data
  619. Raises:
  620. Nothing
  621. Returns:
  622. Nothing
  623. """
  624. with open(i,"w") as f:
  625. # Write sample dimension nb sample header
  626. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  627. f.write("B\n%d\n" % len(data))
  628. for v in data:
  629. f.write("// %d\n" % v)
  630. f.write("%s\n" % s8(v))
  631. def writeReference(self,j,data,name=None):
  632. if (self._ext == "f64"):
  633. self._writeVectorF64(self.refP(j,name),data)
  634. if (self._ext == "f32"):
  635. self._writeVectorF32(self.refP(j,name),data)
  636. if (self._ext == "f16"):
  637. self._writeVectorF16(self.refP(j,name),data)
  638. if (self._ext == "q63"):
  639. self._writeVectorQ63(self.refP(j,name),data)
  640. if (self._ext == "q31"):
  641. self._writeVectorQ31(self.refP(j,name),data)
  642. if (self._ext == "q15"):
  643. self._writeVectorQ15(self.refP(j,name),data)
  644. if (self._ext == "q7"):
  645. self._writeVectorQ7(self.refP(j,name),data)
  646. if (self._ext == "u32"):
  647. self._writeVectorU32(self.refP(j,name),data)
  648. if (self._ext == "s8"):
  649. self._writeVectorS8(self.refP(j,name),data)
  650. def writeReferenceQ63(self,j,data,name=None):
  651. self._writeVectorQ63(self.refQ63P(j,name),data)
  652. def writeReferenceQ31(self,j,data,name=None):
  653. self._writeVectorQ31(self.refQ31P(j,name),data)
  654. def writeReferenceS8(self,j,data,name=None):
  655. self._writeVectorS8(self.refS8P(j,name),data)
  656. def writeReferenceS16(self,j,data,name=None):
  657. self._writeVectorS16(self.refS16P(j,name),data)
  658. def writeReferenceS32(self,j,data,name=None):
  659. self._writeVectorS32(self.refS32P(j,name),data)
  660. def writeReferenceF32(self,j,data,name=None):
  661. self._writeVectorF32(self.refF32P(j,name),data)
  662. def writeReferenceF16(self,j,data,name=None):
  663. self._writeVectorF16(self.refF16P(j,name),data)
  664. def writeInput(self,j,data,name=None):
  665. if (self._ext == "f64"):
  666. self._writeVectorF64(self.inputP(j,name),data)
  667. if (self._ext == "f32"):
  668. self._writeVectorF32(self.inputP(j,name),data)
  669. if (self._ext == "f16"):
  670. self._writeVectorF16(self.inputP(j,name),data)
  671. if (self._ext == "q31"):
  672. self._writeVectorQ31(self.inputP(j,name),data)
  673. if (self._ext == "q15"):
  674. self._writeVectorQ15(self.inputP(j,name),data)
  675. if (self._ext == "q7"):
  676. self._writeVectorQ7(self.inputP(j,name),data)
  677. if (self._ext == "u32"):
  678. self._writeVectorU32(self.inputP(j,name),data)
  679. if (self._ext == "s8"):
  680. self._writeVectorS8(self.inputP(j,name),data)
  681. def writeInputF32(self,j,data,name=None):
  682. self._writeVectorF32(self.inputF32P(j,name),data)
  683. def writeInputF16(self,j,data,name=None):
  684. self._writeVectorF16(self.inputF16P(j,name),data)
  685. def writeInputQ31(self,j,data,name=None):
  686. self._writeVectorQ31(self.inputQ31P(j,name),data)
  687. def writeInputQ15(self,j,data,name=None):
  688. self._writeVectorQ15(self.inputQ15P(j,name),data)
  689. def writeInputQ7(self,j,data,name=None):
  690. self._writeVectorQ7(self.inputQ7P(j,name),data)
  691. def writeInputS32(self,j,data,name=None):
  692. self._writeVectorS32(self.inputS32P(j,name),data)
  693. def writeInputS16(self,j,data,name=None):
  694. self._writeVectorS16(self.inputS16P(j,name),data)
  695. def writeInputS8(self,j,data,name=None):
  696. self._writeVectorS8(self.inputS8P(j,name),data)
  697. def writeInputU32(self,j,data,name=None):
  698. self._writeVectorU32(self.inputU32P(j,name),data)
  699. def writeParam(self,j,data,name=None):
  700. """ Write pattern data
  701. The format is recognized by the text framework script.
  702. First line is the sample width (B,H or W for 8,16 or 32 bits)
  703. Second line is number of samples
  704. Other lines are hexadecimal representation of the samples in format
  705. which can be read on big endian ARM.
  706. Args:
  707. j (int): ID of parameter file
  708. data (array): Vector containing the data
  709. Raises:
  710. Nothing
  711. Returns:
  712. Nothing
  713. """
  714. i=self.paramP(j,name)
  715. with open(i,"w") as f:
  716. # Write sample dimension nb sample header
  717. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  718. f.write("%d\n" % len(data))
  719. for v in data:
  720. f.write("%d\n" % v)