Example Python script to implement the Phasemeter (streaming).
# Moku example: Phasemeter networking streaming
#
# This example starts a 10-second network stream of Channel 1 Phasemeter data
# and processes it live. The contents of each data sample are printed out,
# along with the signal amplitude which may be calculated as A = sqrt(I^2 + Q^2).
#
# (c) 2023 Liquid Instruments Pty. Ltd.
#
from moku.instruments import Phasemeter
import numpy as np
import matplotlib.pyplot as plt
# Launch Phasemeter and connect to your device via IP
i = Phasemeter('192.168.###.###', force_connect=False)
try:
# Set samplerate to 150 Hz/s
i.set_acquisition_speed(speed='150Hz')
# Set channel 1 to DC coupled, 1 MOhm impedance, and 4Vpp range
i.set_frontend(1, coupling='DC', impedance='1MOhm', range='4Vpp')
# Get auto acquired frequency for channel 1
i.get_auto_acquired_frequency(channel=1)
# Stop and existing streaming session and start a new one for 10s
i.stop_streaming()
i.start_streaming(duration=10)
# This loop continuously updates the plot with new data
while True:
# Get stream data
data = i.get_stream_data()
# Process the received data
# Here we just print the contents of Channel 1 data
# Along with signal amplitude (I^2 + Q^2)
print(f"Ch1 - fs: {np.array2string(np.array(data['ch1_set_frequency']))}\nf: {np.array2string(np.array(data['ch1_frequency']))}\nphase: {np.array2string(np.array(data['ch1_phase']))}\namplitude: {np.array2string(np.sqrt(np.array(data['ch1_i'])**2+np.array(data['ch1_q'])**2))}\n")
except Exception as e:
print(f'Exception Occured: {e}')
finally:
# Close the connection to the Moku device
# This ensures network resources are released correctly
i.stop_streaming()
i.relinquish_ownership()