Skip to content

Commit 257f46e

Browse files
authored
examples: Speed up voltage_acq_clk_plot_wfm.py and handle multiple channels correctly (#940)
* examples: Speed up voltage_acq_clk_plot_wfm.py and make it handle multiple channels correctly * docs: Update CHANGELOG.md * examples: Fix lint errors * examples: Remove type hints from new code because the API does not have enough type hints for them to work * examples: Exclude X-axis endpoint from time_data
1 parent d805ebc commit 257f46e

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ All notable changes to this project will be documented in this file.
3232
* [Full changelog: 1.4.1...1.5.0](https://github.com/ni/nidaqmx-python/compare/1.4.1...1.5.0)
3333

3434
* ### Resolved Issues
35-
* ...
35+
* [936: New example voltage_acq_int_clk_plot_wfm.py does not behave as expected](https://github.com/ni/nidaqmx-python/issues/936)
3636

3737
* ### Major Changes
3838
* ...

examples/analog_in/voltage_acq_int_clk_plot_wfm.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,41 @@
66
"""
77

88
import matplotlib.pyplot as plot
9+
import numpy as np
910

1011
import nidaqmx
1112
from nidaqmx.constants import READ_ALL_AVAILABLE, AcquisitionType
1213

14+
15+
def plot_analog_waveform(waveform, min_start_time=None):
16+
"""Plot a single analog waveform."""
17+
# For multiplexed devices, each channel has a different time offset, based on the AI Convert
18+
# Clock rate. Calculate the time offset for this channel by subtracting the minimum start time.
19+
time_offset = 0.0
20+
if min_start_time is not None:
21+
time_offset = (waveform.timing.start_time - min_start_time).total_seconds()
22+
duration = waveform.sample_count * waveform.timing.sample_interval.total_seconds()
23+
time_data = np.linspace(
24+
time_offset, time_offset + duration, waveform.sample_count, endpoint=False
25+
)
26+
plot.plot(time_data, waveform.scaled_data, label=waveform.channel_name)
27+
28+
1329
with nidaqmx.Task() as task:
1430
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
1531
task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=50)
1632

17-
waveform = task.read_waveform(READ_ALL_AVAILABLE)
33+
waveforms = task.read_waveform(READ_ALL_AVAILABLE)
34+
if not isinstance(waveforms, list):
35+
waveforms = [waveforms]
1836

19-
timestamps = list(waveform.timing.get_timestamps(0, waveform.sample_count))
20-
time_offsets = [(ts - timestamps[0]).total_seconds() for ts in timestamps]
21-
plot.plot(time_offsets, waveform.scaled_data)
37+
min_start_time = min(waveform.timing.start_time for waveform in waveforms)
38+
for waveform in waveforms:
39+
plot_analog_waveform(waveform, min_start_time)
2240
plot.xlabel("Seconds")
23-
plot.ylabel(waveform.units)
24-
plot.title(waveform.channel_name)
41+
plot.ylabel(waveforms[0].units) # assume all channels have the same units
42+
plot.title("Waveforms")
43+
plot.legend()
2544
plot.grid(True)
2645

2746
plot.show()

0 commit comments

Comments
 (0)