Skip to content

Commit 94fe035

Browse files
committed
bindings/python: add example for DC sweep
Signed-off-by: Adrian-Stanea <Adrian.Stanea@analog.com>
1 parent c54ed7a commit 94fe035

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#
2+
# Copyright (c) 2025 Analog Devices Inc.
3+
#
4+
# This file is part of libm2k
5+
# (see http://www.github.com/analogdevicesinc/libm2k).
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 2.1 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
"""
20+
Requirements: x1 ADALM2000
21+
22+
This example assumes the following connections:
23+
W1 -> 1+
24+
W2 -> 2+
25+
GND -> 1-
26+
GND -> 2-
27+
"""
28+
29+
import libm2k
30+
import numpy as np
31+
import matplotlib.pyplot as plt
32+
33+
# Configuration
34+
URI = "ip:192.168.2.1"
35+
36+
ADC_SR = 100_000_000 # [Hz]
37+
TRIGGER_SOURCE = libm2k.ANALOG_IN_CHANNEL_1
38+
DELAY = -2000 # [samples]
39+
INPUT_BUFFER_SIZE = 7_000
40+
41+
# NOTE: Period = N_SAMPLES / DAC_SR
42+
DAC_SR = 75_000_000 # [Hz]
43+
N_SAMPLES = 2048
44+
45+
AMPLITUDE = 2 # [V]
46+
OFFSET = 0 # [V]
47+
48+
49+
def main():
50+
try:
51+
context: libm2k.M2k = libm2k.m2kOpen(URI)
52+
if context is None:
53+
print("Connection Error: No ADALM2000 device available/connected to your PC.")
54+
exit(1)
55+
56+
ain: libm2k.M2kAnalogIn = context.getAnalogIn()
57+
aout: libm2k.M2kAnalogOut = context.getAnalogOut()
58+
trig: libm2k.M2kHardwareTrigger = ain.getTrigger()
59+
60+
# Prevent bad initial configuration
61+
context.reset()
62+
context.calibrateADC()
63+
context.calibrateDAC()
64+
65+
# Configure both input channels
66+
ain.setSampleRate(ADC_SR)
67+
ain.enableChannel(libm2k.ANALOG_IN_CHANNEL_1, True)
68+
ain.enableChannel(libm2k.ANALOG_IN_CHANNEL_2, True)
69+
# NOTE: decreased range increases the resolution
70+
ain.setRange(libm2k.ANALOG_IN_CHANNEL_1, libm2k.PLUS_MINUS_2_5V)
71+
ain.setRange(libm2k.ANALOG_IN_CHANNEL_2, libm2k.PLUS_MINUS_2_5V)
72+
73+
# Configure both output channels
74+
aout.setSampleRate(0, DAC_SR)
75+
aout.setSampleRate(1, DAC_SR)
76+
aout.enableChannel(0, True)
77+
aout.enableChannel(1, True)
78+
79+
# NOTE: non-cyclic will generate a single sweep
80+
aout.setCyclic(False)
81+
82+
signal = OFFSET + AMPLITUDE * np.linspace(-1, 1, N_SAMPLES)
83+
84+
# Configure the trigger source for signal acquisition
85+
trig.setAnalogSource(TRIGGER_SOURCE)
86+
trig.setAnalogCondition(TRIGGER_SOURCE, libm2k.RISING_EDGE_ANALOG)
87+
trig.setAnalogLevel(libm2k.ANALOG_IN_CHANNEL_1, -(OFFSET + AMPLITUDE) / 2)
88+
trig.setAnalogDelay(DELAY)
89+
trig.setAnalogMode(TRIGGER_SOURCE, libm2k.ANALOG)
90+
91+
print("Starting signal acquisition...")
92+
ain.startAcquisition(INPUT_BUFFER_SIZE)
93+
94+
print("Generating DC Sweep...")
95+
aout.push([signal, signal])
96+
97+
# Visualize the results
98+
data = np.array(ain.getSamples(INPUT_BUFFER_SIZE))
99+
ain0 = data[0, :]
100+
ain1 = data[1, :]
101+
102+
plt.figure()
103+
plt.plot(ain0, label="AIN0", color="orange")
104+
plt.xlabel("Sample")
105+
plt.ylabel("Voltage [V]")
106+
plt.title("DC Sweep AIN0")
107+
plt.legend(loc="upper right")
108+
plt.grid(True)
109+
plt.show()
110+
111+
plt.figure()
112+
plt.plot(ain1, label="AIN1", color="blue")
113+
plt.xlabel("Sample")
114+
plt.ylabel("Voltage [V]")
115+
plt.title("DC Sweep AIN1")
116+
plt.legend(loc="upper right")
117+
plt.grid(True)
118+
plt.show()
119+
120+
except Exception as e:
121+
print("An error occurred:", e)
122+
123+
libm2k.contextClose(context)
124+
125+
126+
if __name__ == "__main__":
127+
main()

0 commit comments

Comments
 (0)