Skip to content

Commit b64228e

Browse files
Add Waveform Examples (#846)
* add a few more examples * use time_offsets for plotting the waveform
1 parent 19e4c7f commit b64228e

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Example of generating visualizations for acquired data using the AnalogWaveform data type.
2+
3+
This example demonstrates how to plot the acquired waveform data.
4+
This example requires the matplotlib module.
5+
Run 'pip install matplotlib' to install the matplotlib module.
6+
"""
7+
8+
import os
9+
10+
os.environ["NIDAQMX_ENABLE_WAVEFORM_SUPPORT"] = "1"
11+
12+
import matplotlib.pyplot as plot # noqa: E402 # Must import after setting environment variable
13+
14+
import nidaqmx # noqa: E402
15+
from nidaqmx.constants import READ_ALL_AVAILABLE, AcquisitionType # noqa: E402
16+
17+
with nidaqmx.Task() as task:
18+
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
19+
task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=50)
20+
21+
waveform = task.read_waveform(READ_ALL_AVAILABLE)
22+
23+
timestamps = list(waveform.timing.get_timestamps(0, waveform.sample_count))
24+
time_offsets = [(ts - timestamps[0]).total_seconds() for ts in timestamps]
25+
plot.plot(time_offsets, waveform.scaled_data)
26+
plot.xlabel("Seconds")
27+
plot.ylabel(waveform.units)
28+
plot.title(waveform.channel_name)
29+
plot.grid(True)
30+
31+
plot.show()

examples/analog_out/gen_voltage_wfm_int_clk.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
sample clock.
66
"""
77

8-
import nidaqmx
9-
from nidaqmx.constants import AcquisitionType
8+
import os
9+
10+
os.environ["NIDAQMX_ENABLE_WAVEFORM_SUPPORT"] = "1"
11+
12+
from nitypes.waveform import AnalogWaveform # noqa: E402
13+
14+
import nidaqmx # noqa: E402 # Must import after setting environment variable
15+
from nidaqmx.constants import AcquisitionType # noqa: E402
1016

1117
with nidaqmx.Task() as task:
12-
data = []
1318
total_samples = 1000
1419
task.ao_channels.add_ao_voltage_chan("Dev1/ao0")
1520
task.timing.cfg_samp_clk_timing(
1621
1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=total_samples
1722
)
1823

19-
data = [5.0 * i / total_samples for i in range(total_samples)]
20-
number_of_samples_written = task.write(data, auto_start=True)
24+
waveform = AnalogWaveform(sample_count=total_samples)
25+
waveform.raw_data[:] = [5.0 * i / total_samples for i in range(total_samples)]
26+
waveform.units = "Volts"
27+
28+
number_of_samples_written = task.write(waveform, auto_start=True)
2129
print(f"Generating {number_of_samples_written} voltage samples.")
2230
task.wait_until_done()
2331
task.stop()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Example for generating digital signals using the DigitalWaveform data type.
2+
3+
This example demonstrates how to output a continuous digital
4+
pattern using the DAQ device's clock.
5+
"""
6+
7+
import os
8+
9+
os.environ["NIDAQMX_ENABLE_WAVEFORM_SUPPORT"] = "1"
10+
11+
from nitypes.waveform import DigitalWaveform # noqa: E402
12+
13+
import nidaqmx # noqa: E402 # Must import after setting environment variable
14+
from nidaqmx.constants import AcquisitionType, LineGrouping # noqa: E402
15+
16+
with nidaqmx.Task() as task:
17+
waveform = DigitalWaveform(sample_count=100, signal_count=16)
18+
for i in range(100):
19+
for j in range(16):
20+
waveform.data[i][j] = (i >> j) & 1
21+
22+
task.do_channels.add_do_chan("Dev1/port0", line_grouping=LineGrouping.CHAN_FOR_ALL_LINES)
23+
task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.CONTINUOUS)
24+
task.write(waveform)
25+
task.start()
26+
27+
input("Generating voltage continuously. Press Enter to stop.\n")
28+
29+
task.stop()

0 commit comments

Comments
 (0)