defs.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import math
  2. import numpy as np
  3. import tensorflow as tf
  4. class Signal:
  5. @property
  6. def rect_x(self) -> np.ndarray:
  7. return self.rect[:, 0]
  8. @property
  9. def rect_y(self) -> np.ndarray:
  10. return self.rect[:, 1]
  11. @property
  12. def rect(self) -> np.ndarray:
  13. raise NotImplemented("Not implemented")
  14. def set_rect_xy(self, x_mat: np.ndarray, y_mat: np.ndarray):
  15. raise NotImplemented("Not implemented")
  16. def set_rect(self, mat: np.ndarray):
  17. raise NotImplemented("Not implemented")
  18. @property
  19. def apf(self):
  20. raise NotImplemented("Not implemented")
  21. class COMComponent:
  22. def __init__(self, epoch_size=100):
  23. self._epoch = epoch_size
  24. class Channel(COMComponent):
  25. """
  26. Communication base module containing all model structure.
  27. This model is just empty therefore just bypasses any input to output
  28. """
  29. def forward(self, values: Signal) -> Signal:
  30. raise NotImplemented("Need to define forward function")
  31. def forward_tensor(self, tensor: tf.Tensor) -> tf.Tensor:
  32. """
  33. Forward operation optimised for tensorflow tensors
  34. """
  35. raise NotImplemented("Need to define forward_tensor function")
  36. class ModComponent(COMComponent):
  37. def __init__(self, alphabet_size, **kwargs):
  38. super().__init__(**kwargs)
  39. self.N = math.ceil(math.log2(alphabet_size))
  40. self.alphabet_size = alphabet_size
  41. class Modulator(ModComponent):
  42. def forward(self, binary: np.ndarray) -> Signal:
  43. """
  44. :param binary: raw bytes as input (most be dtype=bool)
  45. :return: amplitude, phase, frequency
  46. """
  47. raise NotImplemented("Need to define forward function")
  48. class Demodulator(ModComponent):
  49. def forward(self, values: Signal) -> np.ndarray:
  50. """
  51. :param values: value generator, each iteration returns tuple of (amplitude, phase, frequency)
  52. :return: binary resulting values (dtype=bool)
  53. """
  54. raise NotImplemented("Need to define forward function")