DebugTools.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import struct
  2. import numpy as np
  3. # Tools to read the generated pattern files and the hex output files
  4. def readQ31Pattern(r):
  5. l = []
  6. with open(r, 'r') as f:
  7. f.readline()
  8. nb = int(f.readline())
  9. for i in range(nb):
  10. f.readline()
  11. # Read the hex and interpret the sign
  12. r=int(f.readline(),16)
  13. r=struct.unpack('<i', struct.pack('<I',r))[0]
  14. l.append(r)
  15. l = (1.0*np.array(l)) / 2**31
  16. #print(l)
  17. return(l)
  18. def readQ15Pattern(r):
  19. l = []
  20. with open(r, 'r') as f:
  21. f.readline()
  22. nb = int(f.readline())
  23. for i in range(nb):
  24. f.readline()
  25. # Read the hex and interpret the sign
  26. r=int(f.readline(),16)
  27. r=struct.unpack('<h', struct.pack('<H',r))[0]
  28. l.append(r)
  29. l = (1.0*np.array(l)) / 2**15
  30. #print(l)
  31. return(l)
  32. def hex2float(h):
  33. return(struct.unpack('<f', struct.pack('<I', int(h,16)))[0])
  34. def readF32Pattern(r):
  35. l = []
  36. with open(r, 'r') as f:
  37. f.readline()
  38. nb = int(f.readline())
  39. for i in range(nb):
  40. f.readline()
  41. l.append(hex2float(f.readline()))
  42. l = (1.0*np.array(l))
  43. #print(l)
  44. return(l)
  45. # Read the hex and interpret the sign
  46. def hexToQ31(s):
  47. r = int(s,0)
  48. r=struct.unpack('<i', struct.pack('<I',r))[0]
  49. return(r)
  50. # Read the hex and interpret the sign
  51. def hexToQ15(s):
  52. r = int(s,0)
  53. r=struct.unpack('<i', struct.pack('<I',r))[0]
  54. return(r)
  55. def readQ31Output(path):
  56. sig = np.loadtxt(path, delimiter=',',dtype="int64",converters= {0:hexToQ31})
  57. sig = 1.0 * sig / 2**31
  58. return(sig)
  59. def readQ15Output(path):
  60. sig = np.loadtxt(path, delimiter=',',dtype="int64",converters= {0:hexToQ15})
  61. sig = 1.0 * sig / 2**15
  62. return(sig)
  63. def readF32Output(path):
  64. sig = np.loadtxt(path, delimiter=',',dtype="float",converters= {0:hex2float})
  65. sig = 1.0 * sig
  66. return(sig)
  67. def SNR(ref,sig):
  68. energy = np.dot(ref,np.conj(ref))
  69. error = np.dot(ref-sig,np.conj(ref-sig))
  70. snr = 10 * np.log10(energy/error)
  71. return(snr)