# Frequency Swept Lock-In Amplifier with Harmonics option

**URL:** <https://forum.liquidinstruments.com/t/frequency-swept-lock-in-amplifier-with-harmonics-option/538>\
**Category:** Software & APIs\
**Created:** [June 6, 2022, 3:34pm UTC](https://forum.liquidinstruments.com/t/frequency-swept-lock-in-amplifier-with-harmonics-option/538 "2022-06-06T15:34:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alaaeldin](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alaaeldin](https://forum.liquidinstruments.com/u/alaaeldin)\
**Post date:** [June 6, 2022, 3:34pm UTC](https://forum.liquidinstruments.com/t/frequency-swept-lock-in-amplifier-with-harmonics-option/538/1 "2022-06-06T15:34:09Z")

</div>

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  
###########

---

<div class="post-metadata">

**Author:** ![NandiW](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.liquidinstruments.com/nandiw/32/238_2.png) [@NandiW](https://forum.liquidinstruments.com/u/NandiW)\
**Post date:** [June 7, 2022, 12:52am UTC](https://forum.liquidinstruments.com/t/frequency-swept-lock-in-amplifier-with-harmonics-option/538/2 "2022-06-07T00:52:19Z")

</div>

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](https://apis.liquidinstruments.com/examples/python/#oscilloscope-plotting-py). 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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![alaaeldin](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alaaeldin](https://forum.liquidinstruments.com/u/alaaeldin)\
**Post date:** [June 16, 2022, 6:03pm UTC](https://forum.liquidinstruments.com/t/frequency-swept-lock-in-amplifier-with-harmonics-option/538/3 "2022-06-16T18:03:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NandiW](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.liquidinstruments.com/nandiw/32/238_2.png) [@NandiW](https://forum.liquidinstruments.com/u/NandiW)\
**Post date:** [June 17, 2022, 7:41am UTC](https://forum.liquidinstruments.com/t/frequency-swept-lock-in-amplifier-with-harmonics-option/538/4 "2022-06-17T07:41:49Z")

</div>

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](https://apis.liquidinstruments.com/reference/lia/set_monitor.html).

Please let me know if this resolves your issue.

Thanks!  
Nandi
