Tools.py 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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 == Tools.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 == Tools.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 == Tools.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 inputF32P(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,"f32")))
  234. else:
  235. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f32")))
  236. def inputF16P(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,"f16")))
  247. else:
  248. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f16")))
  249. def inputQ63P(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,"q63")))
  260. else:
  261. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q63")))
  262. def inputQ31P(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,"q31")))
  273. else:
  274. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q31")))
  275. def inputQ15P(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,"q15")))
  286. else:
  287. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q15")))
  288. def inputQ7P(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,"q7")))
  299. else:
  300. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q7")))
  301. def inputU64P(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,"u64")))
  312. else:
  313. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u64")))
  314. def inputU32P(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,"u32")))
  325. else:
  326. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u32")))
  327. def inputU16P(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,"u16")))
  338. else:
  339. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u16")))
  340. def inputU8P(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,"u8")))
  351. else:
  352. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u8")))
  353. def refP(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,self._ext)))
  364. else:
  365. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,self._ext)))
  366. def refS8P(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,"s8")))
  377. else:
  378. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s8")))
  379. def refU8P(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,"u8")))
  390. else:
  391. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u8")))
  392. def refS16P(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,"s16")))
  403. else:
  404. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s16")))
  405. def refU16P(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,"u16")))
  416. else:
  417. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u16")))
  418. def refS32P(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,"s32")))
  429. else:
  430. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s32")))
  431. def refU32P(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,"u32")))
  442. else:
  443. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u32")))
  444. def refS64P(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,"s64")))
  455. else:
  456. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s64")))
  457. def refU64P(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,"u64")))
  468. else:
  469. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"u64")))
  470. def refQ63P(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,"q63")))
  481. else:
  482. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q63")))
  483. def refQ31P(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,"q31")))
  494. else:
  495. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q31")))
  496. def refF32P(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,"f32")))
  507. else:
  508. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f32")))
  509. def refF16P(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,"f16")))
  520. else:
  521. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f16")))
  522. def paramP(self,i,name=None):
  523. """ Path to a parameters from the ID
  524. Args:
  525. i (int): ID to the params
  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._paramDir,"%s%d.txt" % (name,i)))
  533. else:
  534. return(os.path.join(self._paramDir,"Params%d.txt" % i))
  535. def _writeVectorF64(self,i,data):
  536. """ Write pattern data
  537. The format is recognized by the text framework script.
  538. First line is the sample width (B,H or W,D for 8,16,32 or 64 bits)
  539. Second line is number of samples
  540. Other lines are hexadecimal representation of the samples in format
  541. which can be read on big endian ARM.
  542. Args:
  543. j (int): ID of pattern file
  544. data (array): Vector containing the data
  545. Raises:
  546. Nothing
  547. Returns:
  548. Nothing
  549. """
  550. if self.canOverwrite(i):
  551. with open(i,"w") as f:
  552. # Write sample dimension nb sample header
  553. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  554. f.write("D\n%d\n" % len(data))
  555. for v in data:
  556. f.write("// %f\n" % v)
  557. f.write("%s\n" % float64_to_hex(v))
  558. def _writeVectorF32(self,i,data):
  559. """ Write pattern data
  560. The format is recognized by the text framework script.
  561. First line is the sample width (B,H or W for 8,16 or 32 bits)
  562. Second line is number of samples
  563. Other lines are hexadecimal representation of the samples in format
  564. which can be read on big endian ARM.
  565. Args:
  566. j (int): ID of pattern file
  567. data (array): Vector containing the data
  568. Raises:
  569. Nothing
  570. Returns:
  571. Nothing
  572. """
  573. if self.canOverwrite(i):
  574. with open(i,"w") as f:
  575. # Write sample dimension nb sample header
  576. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  577. f.write("W\n%d\n" % len(data))
  578. for v in data:
  579. f.write("// %f\n" % v)
  580. f.write("%s\n" % float_to_hex(v))
  581. def _writeVectorF16(self,i,data):
  582. """ Write pattern data
  583. The format is recognized by the text framework script.
  584. First line is the sample width (B,H or W for 8,16 or 32 bits)
  585. Second line is number of samples
  586. Other lines are hexadecimal representation of the samples in format
  587. which can be read on big endian ARM.
  588. Args:
  589. j (int): ID of pattern file
  590. data (array): Vector containing the data
  591. Raises:
  592. Nothing
  593. Returns:
  594. Nothing
  595. """
  596. if self.canOverwrite(i):
  597. with open(i,"w") as f:
  598. # Write sample dimension nb sample header
  599. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  600. f.write("H\n%d\n" % len(data))
  601. for v in data:
  602. f.write("// %f\n" % v)
  603. f.write("%s\n" % float16_to_hex(v))
  604. def _writeVectorQ63(self,i,data):
  605. """ Write pattern data
  606. The format is recognized by the text framework script.
  607. First line is the sample width (B,H or W for 8,16 or 32 bits)
  608. Second line is number of samples
  609. Other lines are hexadecimal representation of the samples in format
  610. which can be read on big endian ARM.
  611. Args:
  612. j (int): ID of pattern file
  613. data (array): Vector containing the data
  614. Raises:
  615. Nothing
  616. Returns:
  617. Nothing
  618. """
  619. if self.canOverwrite(i):
  620. with open(i,"w") as f:
  621. # Write sample dimension nb sample header
  622. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  623. f.write("D\n%d\n" % len(data))
  624. for v in data:
  625. f.write("// %f\n" % v)
  626. f.write("%s\n" % to_q63(v))
  627. def _writeVectorQ31(self,i,data):
  628. """ Write pattern data
  629. The format is recognized by the text framework script.
  630. First line is the sample width (B,H or W for 8,16 or 32 bits)
  631. Second line is number of samples
  632. Other lines are hexadecimal representation of the samples in format
  633. which can be read on big endian ARM.
  634. Args:
  635. j (int): ID of pattern file
  636. data (array): Vector containing the data
  637. Raises:
  638. Nothing
  639. Returns:
  640. Nothing
  641. """
  642. if self.canOverwrite(i):
  643. with open(i,"w") as f:
  644. # Write sample dimension nb sample header
  645. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  646. f.write("W\n%d\n" % len(data))
  647. for v in data:
  648. f.write("// %f\n" % v)
  649. f.write("%s\n" % to_q31(v))
  650. def _writeVectorQ15(self,i,data):
  651. """ Write pattern data
  652. The format is recognized by the text framework script.
  653. First line is the sample width (B,H or W for 8,16 or 32 bits)
  654. Second line is number of samples
  655. Other lines are hexadecimal representation of the samples in format
  656. which can be read on big endian ARM.
  657. Args:
  658. j (int): ID of pattern file
  659. data (array): Vector containing the data
  660. Raises:
  661. Nothing
  662. Returns:
  663. Nothing
  664. """
  665. if self.canOverwrite(i):
  666. with open(i,"w") as f:
  667. # Write sample dimension nb sample header
  668. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  669. f.write("H\n%d\n" % len(data))
  670. for v in data:
  671. f.write("// %f\n" % v)
  672. f.write("%s\n" % to_q15(v))
  673. def _writeVectorS16(self,i,data):
  674. """ Write pattern data
  675. The format is recognized by the text framework script.
  676. First line is the sample width (B,H or W for 8,16 or 32 bits)
  677. Second line is number of samples
  678. Other lines are hexadecimal representation of the samples in format
  679. which can be read on big endian ARM.
  680. Args:
  681. j (int): ID of pattern file
  682. data (array): Vector containing the data
  683. Raises:
  684. Nothing
  685. Returns:
  686. Nothing
  687. """
  688. if self.canOverwrite(i):
  689. with open(i,"w") as f:
  690. # Write sample dimension nb sample header
  691. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  692. f.write("H\n%d\n" % len(data))
  693. for v in data:
  694. f.write("// %d\n" % v)
  695. f.write("%s\n" % s16(v))
  696. def _writeVectorU16(self,i,data):
  697. """ Write pattern data
  698. The format is recognized by the text framework script.
  699. First line is the sample width (B,H or W for 8,16 or 32 bits)
  700. Second line is number of samples
  701. Other lines are hexadecimal representation of the samples in format
  702. which can be read on big endian ARM.
  703. Args:
  704. j (int): ID of pattern file
  705. data (array): Vector containing the data
  706. Raises:
  707. Nothing
  708. Returns:
  709. Nothing
  710. """
  711. if self.canOverwrite(i):
  712. with open(i,"w") as f:
  713. # Write sample dimension nb sample header
  714. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  715. f.write("H\n%d\n" % len(data))
  716. for v in data:
  717. f.write("// %d\n" % v)
  718. f.write("%s\n" % u16(v))
  719. def _writeVectorS64(self,i,data):
  720. """ Write pattern data
  721. The format is recognized by the text framework script.
  722. First line is the sample width (B,H or W for 8,16 or 32 bits)
  723. Second line is number of samples
  724. Other lines are hexadecimal representation of the samples in format
  725. which can be read on big endian ARM.
  726. Args:
  727. j (int): ID of pattern file
  728. data (array): Vector containing the data
  729. Raises:
  730. Nothing
  731. Returns:
  732. Nothing
  733. """
  734. if self.canOverwrite(i):
  735. with open(i,"w") as f:
  736. # Write sample dimension nb sample header
  737. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  738. f.write("D\n%d\n" % len(data))
  739. for v in data:
  740. f.write("// %d\n" % v)
  741. f.write("%s\n" % s64(v))
  742. def _writeVectorU64(self,i,data):
  743. """ Write pattern data
  744. The format is recognized by the text framework script.
  745. First line is the sample width (B,H or W for 8,16 or 32 bits)
  746. Second line is number of samples
  747. Other lines are hexadecimal representation of the samples in format
  748. which can be read on big endian ARM.
  749. Args:
  750. j (int): ID of pattern file
  751. data (array): Vector containing the data
  752. Raises:
  753. Nothing
  754. Returns:
  755. Nothing
  756. """
  757. if self.canOverwrite(i):
  758. with open(i,"w") as f:
  759. # Write sample dimension nb sample header
  760. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  761. f.write("D\n%d\n" % len(data))
  762. for v in data:
  763. f.write("// %d\n" % v)
  764. f.write("%s\n" % u64(v))
  765. def _writeVectorS32(self,i,data):
  766. """ Write pattern data
  767. The format is recognized by the text framework script.
  768. First line is the sample width (B,H or W for 8,16 or 32 bits)
  769. Second line is number of samples
  770. Other lines are hexadecimal representation of the samples in format
  771. which can be read on big endian ARM.
  772. Args:
  773. j (int): ID of pattern file
  774. data (array): Vector containing the data
  775. Raises:
  776. Nothing
  777. Returns:
  778. Nothing
  779. """
  780. if self.canOverwrite(i):
  781. with open(i,"w") as f:
  782. # Write sample dimension nb sample header
  783. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  784. f.write("W\n%d\n" % len(data))
  785. for v in data:
  786. f.write("// %d\n" % v)
  787. f.write("%s\n" % s32(v))
  788. def _writeVectorU32(self,i,data):
  789. """ Write pattern data
  790. The format is recognized by the text framework script.
  791. First line is the sample width (B,H or W for 8,16 or 32 bits)
  792. Second line is number of samples
  793. Other lines are hexadecimal representation of the samples in format
  794. which can be read on big endian ARM.
  795. Args:
  796. j (int): ID of pattern file
  797. data (array): Vector containing the data
  798. Raises:
  799. Nothing
  800. Returns:
  801. Nothing
  802. """
  803. if self.canOverwrite(i):
  804. with open(i,"w") as f:
  805. # Write sample dimension nb sample header
  806. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  807. f.write("W\n%d\n" % len(data))
  808. for v in data:
  809. f.write("// %s\n" % v)
  810. f.write("%s\n" % u32(v))
  811. def _writeVectorQ7(self,i,data):
  812. """ Write pattern data
  813. The format is recognized by the text framework script.
  814. First line is the sample width (B,H or W for 8,16 or 32 bits)
  815. Second line is number of samples
  816. Other lines are hexadecimal representation of the samples in format
  817. which can be read on big endian ARM.
  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("B\n%d\n" % len(data))
  831. for v in data:
  832. f.write("// %f\n" % v)
  833. f.write("%s\n" % to_q7(v))
  834. def _writeVectorS8(self,i,data):
  835. """ Write pattern data
  836. The format is recognized by the text framework script.
  837. First line is the sample width (B,H or W for 8,16 or 32 bits)
  838. Second line is number of samples
  839. Other lines are hexadecimal representation of the samples in format
  840. which can be read on big endian ARM.
  841. Args:
  842. j (int): ID of pattern file
  843. data (array): Vector containing the data
  844. Raises:
  845. Nothing
  846. Returns:
  847. Nothing
  848. """
  849. if self.canOverwrite(i):
  850. with open(i,"w") as f:
  851. # Write sample dimension nb sample header
  852. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  853. f.write("B\n%d\n" % len(data))
  854. for v in data:
  855. f.write("// %d\n" % v)
  856. f.write("%s\n" % s8(v))
  857. def _writeVectorU8(self,i,data):
  858. """ Write pattern data
  859. The format is recognized by the text framework script.
  860. First line is the sample width (B,H or W for 8,16 or 32 bits)
  861. Second line is number of samples
  862. Other lines are hexadecimal representation of the samples in format
  863. which can be read on big endian ARM.
  864. Args:
  865. j (int): ID of pattern file
  866. data (array): Vector containing the data
  867. Raises:
  868. Nothing
  869. Returns:
  870. Nothing
  871. """
  872. if self.canOverwrite(i):
  873. with open(i,"w") as f:
  874. # Write sample dimension nb sample header
  875. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  876. f.write("B\n%d\n" % len(data))
  877. for v in data:
  878. f.write("// %d\n" % v)
  879. f.write("%s\n" % u8(v))
  880. def writeReference(self,j,data,name=None):
  881. if (self._ext == "f64"):
  882. self._writeVectorF64(self.refP(j,name),data)
  883. if (self._ext == "f32"):
  884. self._writeVectorF32(self.refP(j,name),data)
  885. if (self._ext == "f16"):
  886. self._writeVectorF16(self.refP(j,name),data)
  887. if (self._ext == "q63"):
  888. self._writeVectorQ63(self.refP(j,name),data)
  889. if (self._ext == "q31"):
  890. self._writeVectorQ31(self.refP(j,name),data)
  891. if (self._ext == "q15"):
  892. self._writeVectorQ15(self.refP(j,name),data)
  893. if (self._ext == "q7"):
  894. self._writeVectorQ7(self.refP(j,name),data)
  895. if (self._ext == "s64"):
  896. self._writeVectorS64(self.refP(j,name),data)
  897. if (self._ext == "u64"):
  898. self._writeVectorU64(self.refP(j,name),data)
  899. if (self._ext == "s32"):
  900. self._writeVectorS32(self.refP(j,name),data)
  901. if (self._ext == "u32"):
  902. self._writeVectorU32(self.refP(j,name),data)
  903. if (self._ext == "s16"):
  904. self._writeVectorS16(self.refP(j,name),data)
  905. if (self._ext == "u16"):
  906. self._writeVectorU16(self.refP(j,name),data)
  907. if (self._ext == "s8"):
  908. self._writeVectorS8(self.refP(j,name),data)
  909. if (self._ext == "u8"):
  910. self._writeVectorU8(self.refP(j,name),data)
  911. def writeReferenceQ63(self,j,data,name=None):
  912. self._writeVectorQ63(self.refQ63P(j,name),data)
  913. def writeReferenceQ31(self,j,data,name=None):
  914. self._writeVectorQ31(self.refQ31P(j,name),data)
  915. def writeReferenceS8(self,j,data,name=None):
  916. self._writeVectorS8(self.refS8P(j,name),data)
  917. def writeReferenceS16(self,j,data,name=None):
  918. self._writeVectorS16(self.refS16P(j,name),data)
  919. def writeReferenceS32(self,j,data,name=None):
  920. self._writeVectorS32(self.refS32P(j,name),data)
  921. def writeReferenceS64(self,j,data,name=None):
  922. self._writeVectorS64(self.refS64P(j,name),data)
  923. def writeReferenceU8(self,j,data,name=None):
  924. self._writeVectorU8(self.refU8P(j,name),data)
  925. def writeReferenceU16(self,j,data,name=None):
  926. self._writeVectorU16(self.refU16P(j,name),data)
  927. def writeReferenceU32(self,j,data,name=None):
  928. self._writeVectorU32(self.refU32P(j,name),data)
  929. def writeReferenceU64(self,j,data,name=None):
  930. self._writeVectorU64(self.refU64P(j,name),data)
  931. def writeReferenceF32(self,j,data,name=None):
  932. self._writeVectorF32(self.refF32P(j,name),data)
  933. def writeReferenceF16(self,j,data,name=None):
  934. self._writeVectorF16(self.refF16P(j,name),data)
  935. def writeInput(self,j,data,name=None):
  936. if (self._ext == "f64"):
  937. self._writeVectorF64(self.inputP(j,name),data)
  938. if (self._ext == "f32"):
  939. self._writeVectorF32(self.inputP(j,name),data)
  940. if (self._ext == "f16"):
  941. self._writeVectorF16(self.inputP(j,name),data)
  942. if (self._ext == "q63"):
  943. self._writeVectorQ63(self.inputP(j,name),data)
  944. if (self._ext == "q31"):
  945. self._writeVectorQ31(self.inputP(j,name),data)
  946. if (self._ext == "q15"):
  947. self._writeVectorQ15(self.inputP(j,name),data)
  948. if (self._ext == "q7"):
  949. self._writeVectorQ7(self.inputP(j,name),data)
  950. if (self._ext == "s64"):
  951. self._writeVectorS64(self.inputP(j,name),data)
  952. if (self._ext == "u64"):
  953. self._writeVectorU64(self.inputP(j,name),data)
  954. if (self._ext == "s32"):
  955. self._writeVectorS32(self.inputP(j,name),data)
  956. if (self._ext == "u32"):
  957. self._writeVectorU32(self.inputP(j,name),data)
  958. if (self._ext == "s8"):
  959. self._writeVectorS8(self.inputP(j,name),data)
  960. if (self._ext == "u8"):
  961. self._writeVectorU8(self.inputP(j,name),data)
  962. def writeInputF32(self,j,data,name=None):
  963. self._writeVectorF32(self.inputF32P(j,name),data)
  964. def writeInputF16(self,j,data,name=None):
  965. self._writeVectorF16(self.inputF16P(j,name),data)
  966. def writeInputQ63(self,j,data,name=None):
  967. self._writeVectorQ63(self.inputQ63P(j,name),data)
  968. def writeInputQ31(self,j,data,name=None):
  969. self._writeVectorQ31(self.inputQ31P(j,name),data)
  970. def writeInputQ15(self,j,data,name=None):
  971. self._writeVectorQ15(self.inputQ15P(j,name),data)
  972. def writeInputQ7(self,j,data,name=None):
  973. self._writeVectorQ7(self.inputQ7P(j,name),data)
  974. def writeInputS64(self,j,data,name=None):
  975. self._writeVectorS64(self.inputS64P(j,name),data)
  976. def writeInputS32(self,j,data,name=None):
  977. self._writeVectorS32(self.inputS32P(j,name),data)
  978. def writeInputS16(self,j,data,name=None):
  979. self._writeVectorS16(self.inputS16P(j,name),data)
  980. def writeInputS8(self,j,data,name=None):
  981. self._writeVectorS8(self.inputS8P(j,name),data)
  982. def writeInputU64(self,j,data,name=None):
  983. self._writeVectorU64(self.inputU64P(j,name),data)
  984. def writeInputU32(self,j,data,name=None):
  985. self._writeVectorU32(self.inputU32P(j,name),data)
  986. def writeInputU16(self,j,data,name=None):
  987. self._writeVectorU16(self.inputU16P(j,name),data)
  988. def writeInputU8(self,j,data,name=None):
  989. self._writeVectorU8(self.inputU8P(j,name),data)
  990. def writeParam(self,j,data,name=None):
  991. """ Write pattern data
  992. The format is recognized by the text framework script.
  993. First line is the sample width (B,H or W for 8,16 or 32 bits)
  994. Second line is number of samples
  995. Other lines are hexadecimal representation of the samples in format
  996. which can be read on big endian ARM.
  997. Args:
  998. j (int): ID of parameter file
  999. data (array): Vector containing the data
  1000. Raises:
  1001. Nothing
  1002. Returns:
  1003. Nothing
  1004. """
  1005. i=self.paramP(j,name)
  1006. if self.canOverwrite(i):
  1007. with open(i,"w") as f:
  1008. # Write sample dimension nb sample header
  1009. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  1010. f.write("%d\n" % len(data))
  1011. for v in data:
  1012. f.write("%d\n" % v)