You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

calibration.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import numpy as np
  2. class Calibration:
  3. def __init__(self):
  4. self.calibration_in_process = False
  5. self.calibrated = False
  6. self.phis = None
  7. self.magnitude_offsets = None
  8. self.current_feedback = None
  9. self.index = 0
  10. self.buffer_length = 100
  11. #self.new_calibration()
  12. def process_sample(self, sample):
  13. if self.calibration_in_process:
  14. if self.index < self.buffer_length:
  15. self.offsets[self.index, :] = sample
  16. self.index += 1
  17. else:
  18. self.offsets =np.mean(self.offsets, axis=0)
  19. self.update_arrays()
  20. print("-----Finished calibration")
  21. self.calibration_in_process = False
  22. self.index =0
  23. self.calibrated = True
  24. return None
  25. else:
  26. if self.calibrated:
  27. # sample = sample - self.offsets
  28. s_rotated = sample[0:20] * np.exp(-1j * self.phis)
  29. current_feedback_calibrated = sample[20] / self.current_feedback
  30. calibrated_sample = s_rotated - (current_feedback_calibrated * self.magnitude_offsets)
  31. return calibrated_sample
  32. else:
  33. return None
  34. def new_calibration(self):
  35. self.offsets = np.zeros((self.buffer_length, 21), dtype=np.complex)
  36. self.calibration_in_process = True
  37. def update_arrays(self):
  38. self.phis = np.angle(self.offsets[0:20], deg=False)
  39. self.magnitude_offsets = np.abs(self.offsets[0:20])
  40. self.current_feedback = self.offsets[20]