gray_code.py 931 B

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