Frequency Swept Lock-In Amplifier with Harmonics option

I need to create a Lock-In Amplifier that sweeps its local oscillator frequency and plots the results. It also needs to “Lock” at a harmonic frequency. This is my code so far (Python). I still need to plot the results and have no idea how to proceed.

#################
from moku.instruments import LockInAmp
i = LockInAmp(‘192.168.###.###’)

strt_freq=100000
stp_freq=150000
freq_step=100
ac_vlt=1
dc_vlt=0.5
mult=3

freq=strt_freq
while freq < stp_freq:
i.set_frontend(1, impedance=“50Ohm”, coupling=“AC”, attenuation=“20dB”)
i.set_polar_mode(“2Vpp”)
i.set_pll_bandwidth(“10Hz”)
i.set_filter(corner_frequency=10,slope=“Slope24dB”)
i.set_gain(main=50,aux=0,aux_gain_range=14dB)
i.set_demodulation(mode=“Internal”,frequency=mult*freq,phase=0) # Locks on the “mult” harmonic
i.set_aux_output(frequency=freq, amplitude=ac_vlt) # Outputs the fundamental harmonic for excitation through the Aux output
i.set_outputs(main=“R”, main_offset=0)
i.set_outputs(aux=“Aux”, aux_offset=dc_vlt)
freq += freq_step

#Need to save the output at each point and plot it vs the frequency base
###########

Hi Alaaeldin,

You have made a great start with your code and are most of the way there.

I would recommend you moving some of the device configuration out of the while loop. For example, from “set_frontend()” down to “set_gain()”, as these are probably constant throughout your measurement process. By taking them out of the loop will help to improve the measurement response time.

For plotting your results, I would recommend referencing the Oscilloscope plotting example script. You can use the “get_data()” function to collect the measured results from Moku, then using Python’s plotting functions to generate the plot.

Here’s my script for reference:

import matplotlib.pyplot as plt
from moku.instruments import LockInAmp

# Connect to your Moku and deploy the Lock-In Amplifier instrument
i = LockInAmp('192.168.xxx.xxx')

# Kept the original settings
strt_freq=100000
stp_freq=150000
freq_step=100
ac_vlt=1
dc_vlt=0.5
mult=3

# Initial device configuration
i.set_frontend(1, impedance='50Ohm', coupling='AC', attenuation='20dB')
i.set_polar_mode('2Vpp')
i.set_pll_bandwidth('10Hz')
i.set_filter(corner_frequency=10,slope='Slope24dB')
i.set_gain(main=50,aux=0)
i.set_timebase(-1e-3,1e-3)

freq=strt_freq

# Set up the plotting parameters
plt.ion()
plt.show()
plt.grid(b=True)
plt.ylim([-1, 1])
plt.xlim([-1e-3,1e-3])

line1, = plt.plot([])
line2, = plt.plot([])

# Configure labels for axes
ax = plt.gca()

while freq < stp_freq:
    # Set up the demodulation process
	i.set_demodulation(mode='Internal',frequency=mult*freq,phase=0) # Locks on the “mult” harmonic
	i.set_aux_output(frequency=freq, amplitude=ac_vlt) # Outputs the fundamental harmonic for excitation through the Aux output
	i.set_outputs(main='R', aux='Aux', main_offset=0, aux_offset=dc_vlt)
	
    # Get data from Moku
	data = i.get_data()

    # Update the plot
	line1.set_ydata(data['ch1'])
	line2.set_ydata(data['ch2'])
	line1.set_xdata(data['time'])
	line2.set_xdata(data['time'])

	plt.pause(0.1)

	freq += freq_step

Hi NandiW,
Thanks for the reply.
I tried your approach and I have an issue. The get_data() command seems to return the time axis and then all zeros for all channels. No matter what I do, the list is just an array of zeros. How do I get the data from the main output of the lock-in amplifier?

Edit:Typos

Hi Alaaeldin,

Sorry I missed this step in my original post.

You will need to add a set_monitor() function in the initial device configuration part of the code, you can check out the documentation here.

Please let me know if this resolves your issue.

Thanks!
Nandi