gray_code.py 908 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from scipy.spatial import Voronoi, voronoi_plot_2d
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. def get_gray_code(n: int):
  5. return n ^ (n >> 1)
  6. def difference(sym0: int, sym1: int):
  7. return bit_count(sym0 ^ sym1)
  8. def bit_count(i: int):
  9. """
  10. Hamming weight algorithm, just counts number of 1s
  11. """
  12. assert 0 <= i < 0x100000000
  13. i = i - ((i >> 1) & 0x55555555)
  14. i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
  15. return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24
  16. def compute_optimal(points, show_graph=False):
  17. available = set(range(len(points)))
  18. map = {}
  19. vor = Voronoi(points)
  20. if show_graph:
  21. voronoi_plot_2d(vor)
  22. plt.show()
  23. pass
  24. if __name__ == '__main__':
  25. a = np.array([[-1, -1], [-1, 1], [1, 1], [1, -1]])
  26. # a = basic.load_alphabet('16qam', polar=False)
  27. compute_optimal(a, show_graph=True)
  28. pass