Skip to content

Commit

Permalink
added phase sync
Browse files Browse the repository at this point in the history
  • Loading branch information
VBeleca committed Apr 26, 2024
1 parent 46a13dc commit 9c69455
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
12 changes: 6 additions & 6 deletions adi/adrv9009_zu11eg.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, uri="", jesd_monitor=False, jesd=None):
self._clock_chip_ext = self._ctx.find_device("hmc7044-ext")
# Phase calibration values:
self.num_elements = 4
self.pcal = [0.0 for i in range(0, (self.num_elements - 1))]
self.pcal_data = [0.0 for i in range(0, (self.num_elements - 1))]

def mcs_chips(self):
"""mcs_chips: MCS Synchronize both transceivers """
Expand Down Expand Up @@ -230,13 +230,13 @@ def trx_lo_chip_b(self, value):
@property
def pcal(self):
"""pcal: phase differences in degrees [(rx0 - rx1) (rx0 - rx2) (rx0 - rx3)]"""
return self.pcal
return self.pcal_data

@pcal.setter
def pcal(self, pcal):
if isinstance(pcal, list) and all(isinstance(item, float) for item in pcal):
if len(pcal) == (self.num_elements - 1):
self._data = pcal
def pcal(self, values):
if isinstance(values, list) and all(isinstance(item, float) for item in values):
if len(values) == (self.num_elements - 1):
self.pcal_data = values
else:
raise ValueError("Input array length doesn't match the expected length")
else:
Expand Down
Binary file added examples/talise/phase_cal_val.pkl
Binary file not shown.
62 changes: 55 additions & 7 deletions examples/talise/talise_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import time
from scipy import signal

from talise_functions import (
phase_calibration,
)
# from talise_functions import (
# phase_calibration,
# )

try:
import config_custom_talise as config # this has all the key parameters that the user would want to change (i.e. calibration phase and antenna element spacing)
Expand All @@ -30,7 +30,7 @@ def talise_init(my_talise):
print("Numeric value of discrete amplitude of transmitted signal: " + str(config.amplitude_discrete))
# frequency = 245760 # 245.760 kHz
# amplitude = 2**14
my_talise.rx_sample_rate = config.sample_rate
# my_talise.rx_sample_rate = config.sample_rate
print("Number of samples per call to rx(): " + str(config.num_samps))
# num_samps = int((20*sample_rate)/frequency) # number of samples per call to rx()

Expand Down Expand Up @@ -90,6 +90,19 @@ def measure_phase_degrees(chan0, chan1):
error = np.mean(errorV)
return error

def adjust_phase(talise_obj, rx_samples_ch1, rx_samples_ch2, rx_samples_ch3):
ph1 = np.deg2rad(talise_obj.pcal[0])
print("Phase of " + str(talise_obj.pcal[0]) + " to radians = " + str(ph1))
ph2 = np.deg2rad(talise_obj.pcal[1])
print("Phase of " + str(talise_obj.pcal[1]) + " to radians = " + str(ph2))
ph3 = np.deg2rad(talise_obj.pcal[2])
print("Phase of " + str(talise_obj.pcal[2]) + " to radians = " + str(ph3))
rx_samples_ch1 = rx_samples_ch1 * np.exp(1j * ph1)
rx_samples_ch2 = rx_samples_ch2 * np.exp(1j * ph2)
rx_samples_ch3 = rx_samples_ch3 * np.exp(1j * ph3)

return [rx_samples_ch1, rx_samples_ch2, rx_samples_ch3]

def do_cal_phase(my_talise):
# Configure talise and load calibration constants from file
talise_init(my_talise)
Expand Down Expand Up @@ -200,7 +213,7 @@ def do_cal_phase(my_talise):

for i in range(repeat_ph_calculations):
rx_samples = my_talise.rx()
print("Iteration " + str(repeat_ph_calculations) + ":")
print("Iteration " + str(i) + ":")
print("Ph Diff Between ch0 and ch1: " + str(measure_phase_degrees(rx_samples[0], rx_samples[1])))
ph_diff_ch0_minus_ch1.append(measure_phase_degrees(rx_samples[0], rx_samples[1]))
print("Ph Diff Between ch0 and ch2: " + str(measure_phase_degrees(rx_samples[0], rx_samples[2])))
Expand All @@ -217,9 +230,16 @@ def do_cal_phase(my_talise):
avg_ph_diff_ch0_minus_ch3 = sum_ph_diff_ch0_minus_ch3/repeat_ph_calculations

# Save Phase differences in degrees in .pkl file
# values = [avg_ph_diff_ch0_minus_ch1, avg_ph_diff_ch0_minus_ch2, avg_ph_diff_ch0_minus_ch3]
# if isinstance(values, list) and all(isinstance(item, float) for item in values):
# if len(values) == (my_talise.num_elements - 1):
# x = values
# print("IF OK: " + str(x))
my_talise.pcal = [avg_ph_diff_ch0_minus_ch1, avg_ph_diff_ch0_minus_ch2, avg_ph_diff_ch0_minus_ch3]
print("pcal values: " + str(my_talise.pcal))
my_talise.save_phase_cal()
my_talise.load_phase_cal()
print("pcal values after save: " + str(my_talise.pcal))

print("Avg ph diff for ch0 - ch1: " + str(avg_ph_diff_ch0_minus_ch1))
print("Avg ph diff for ch0 - ch2: " + str(avg_ph_diff_ch0_minus_ch2))
Expand All @@ -242,9 +262,37 @@ def do_cal_phase(my_talise):
print("Min diff in phase ch0-ch3: " + str(min_ph_diff_ch0_minus_ch3))

print("Max variance: " + str(max((max_ph_diff_ch0_minus_ch1 - min_ph_diff_ch0_minus_ch1), (max_ph_diff_ch0_minus_ch2 - min_ph_diff_ch0_minus_ch2), (max_ph_diff_ch0_minus_ch3 - max_ph_diff_ch0_minus_ch3))))

# Adjust phase
arrays_adjusted = adjust_phase(my_talise, rx_samples[1], rx_samples[2], rx_samples[3])
rx_samples[1] = arrays_adjusted[0]
rx_samples[2] = arrays_adjusted[1]
rx_samples[3] = arrays_adjusted[2]
# Display time plot with phase adjusted
# Time values
t = np.arange(config.num_samps) / config.sample_rate

# Plot Rx time domain
plt.figure(5)

plt.plot(np.real(rx_samples[0]), label = "Ch0 I (Real)")
plt.plot(np.imag(rx_samples[0]), label = "Ch0 I (Real)")

plt.plot(np.real(rx_samples[1]), label = "Ch1 I (Real)")
plt.plot(np.imag(rx_samples[1]), label = "Ch1 I (Real)")

plt.plot(np.real(rx_samples[2]), label = "Ch2 I (Real)")
plt.plot(np.imag(rx_samples[2]), label = "Ch2 I (Real)")

plt.plot(np.real(rx_samples[3]), label = "Ch3 I (Real)")
plt.plot(np.imag(rx_samples[3]), label = "Ch3 I (Real)")

plt.legend()
plt.title('Rx time domain after Ph Cal')
plt.xlabel('Time (seconds)')
plt.ylabel('Amplitude')
# For testing:
print(my_talise.pcal)
# print(my_talise.pcal)

# TODO: test untill here

Expand Down Expand Up @@ -284,7 +332,7 @@ def do_cal_phase(my_talise):
if func == "cal":
print("Calibrating Phase, verbosely, then saving cal file...")
# TODO: add do cal_gain
do_cal_phase() # Start Phase Calibration
do_cal_phase(my_talise) # Start Phase Calibration
print("Done calibration")


Expand Down

0 comments on commit 9c69455

Please sign in to comment.