|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | import matplotlib.pyplot as plot |
| 9 | +import numpy as np |
9 | 10 |
|
10 | 11 | import nidaqmx |
11 | 12 | from nidaqmx.constants import READ_ALL_AVAILABLE, AcquisitionType |
12 | 13 |
|
| 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 | + |
13 | 29 | with nidaqmx.Task() as task: |
14 | 30 | task.ai_channels.add_ai_voltage_chan("Dev1/ai0") |
15 | 31 | task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=50) |
16 | 32 |
|
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] |
18 | 36 |
|
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) |
22 | 40 | 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() |
25 | 44 | plot.grid(True) |
26 | 45 |
|
27 | 46 | plot.show() |
0 commit comments