Examples

You can download all nifgen examples for latest version here

nifgen_arb_waveform.py

 1#!/usr/bin/python
 2
 3import argparse
 4import math
 5import nifgen
 6import sys
 7import time
 8
 9
10def create_waveform_data(number_of_samples):
11    waveform_data = []
12    angle_per_sample = (2 * math.pi) / number_of_samples
13    for i in range(number_of_samples):
14        waveform_data.append(math.sin(i * angle_per_sample) * math.sin(i * angle_per_sample * 20))
15    return waveform_data
16
17
18def example(resource_name, options, samples, gain, offset, gen_time):
19    waveform_data = create_waveform_data(samples)
20    with nifgen.Session(resource_name=resource_name, options=options) as session:
21        session.output_mode = nifgen.OutputMode.ARB
22        waveform = session.create_waveform(waveform_data_array=waveform_data)
23        session.configure_arb_waveform(waveform_handle=waveform, gain=gain, offset=offset)
24        with session.initiate():
25            time.sleep(gen_time)
26
27
28def _main(argsv):
29    parser = argparse.ArgumentParser(description='Continuously generates an arbitrary waveform.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
30    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI arbitrary waveform generator.')
31    parser.add_argument('-s', '--samples', default=100000, type=int, help='Number of samples')
32    parser.add_argument('-g', '--gain', default=1.0, type=float, help='Gain')
33    parser.add_argument('-o', '--offset', default=0.0, type=float, help='DC offset (V)')
34    parser.add_argument('-t', '--time', default=5.0, type=float, help='Generation time (s)')
35    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
36    args = parser.parse_args(argsv)
37    example(args.resource_name, args.option_string, args.samples, args.gain, args.offset, args.time)
38
39
40def main():
41    _main(sys.argv[1:])
42
43
44def test_example():
45    options = {'simulate': True, 'driver_setup': {'Model': '5433 (2CH)', 'BoardType': 'PXIe', }, }
46    example('PXI1Slot2', options, 100000, 1.0, 0.0, 5.0)
47
48
49def test_main():
50    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5433 (2CH);BoardType:PXIe', ]
51    _main(cmd_line)
52
53
54if __name__ == '__main__':
55    main()
56
57

nifgen_script.py

  1#!/usr/bin/python
  2
  3import argparse
  4import nifgen
  5
  6import math
  7import sys
  8import time
  9
 10# waveform size should be a multiple of the quantum, which is 4, 2 or 1, for all devices
 11# minimum waveform size needed to prevent underflow varies with sample rate and device.
 12# If underflow occurs, increase this value.
 13NUMBER_OF_SAMPLES = 2096
 14
 15
 16# waveforms finish just short of 360 degrees, so that we don't repeat the first point
 17# if we repeat the waveform
 18SINE_WAVE = [math.sin(math.pi * 2 * x / (NUMBER_OF_SAMPLES)) for x in range(NUMBER_OF_SAMPLES)]
 19RAMP_UP = [x / (NUMBER_OF_SAMPLES) for x in range(NUMBER_OF_SAMPLES)]
 20RAMP_DOWN = [-1.0 * x for x in RAMP_UP]
 21SQUARE_WAVE = [1.0 if x < (NUMBER_OF_SAMPLES / 2) else -1.0 for x in range(NUMBER_OF_SAMPLES)]
 22SAWTOOTH_WAVE = RAMP_UP[::2] + [(-1 + x) for x in RAMP_UP][::2]
 23
 24
 25SCRIPT_ALL = '''
 26script scriptmulti
 27  repeat until scriptTrigger0
 28    generate rampup
 29    generate sine
 30    generate rampdown
 31  end repeat
 32  repeat until scriptTrigger0
 33    generate rampdown
 34    generate square
 35    generate rampup
 36  end repeat
 37  repeat until scriptTrigger0
 38    generate rampup
 39    generate rampdown
 40  end repeat
 41  repeat until scriptTrigger0
 42    generate sine
 43  end repeat
 44  repeat until scriptTrigger0
 45    generate sawtooth
 46  end repeat
 47  repeat until scriptTrigger0
 48    generate rampdown
 49    generate rampup
 50  end repeat
 51end script
 52
 53script scriptsine
 54  repeat until scriptTrigger0
 55    generate sine
 56  end repeat
 57end script
 58
 59script scriptrampup
 60  repeat until scriptTrigger0
 61    generate rampup
 62  end repeat
 63end script
 64
 65script scriptrampdown
 66  repeat until scriptTrigger0
 67    generate rampdown
 68  end repeat
 69end script
 70
 71script scriptsquare
 72  repeat until scriptTrigger0
 73    generate square
 74  end repeat
 75end script
 76
 77script scriptsawtooth
 78  repeat until scriptTrigger0
 79    generate sawtooth
 80  end repeat
 81end script
 82'''
 83
 84
 85def example(resource_name, options, shape, channel):
 86    with nifgen.Session(resource_name=resource_name, options=options, channel_name=channel) as session:
 87        # CONFIGURATION
 88        # 1 - Set the mode to Script
 89        session.output_mode = nifgen.OutputMode.SCRIPT
 90
 91        # 2 - Configure Trigger:
 92        # SOFTWARE TRIGGER: used in the script
 93        session.script_triggers[0].script_trigger_type = nifgen.ScriptTriggerType.SOFTWARE_EDGE  # TRIG_NONE / DIGITAL_EDGE / DIGITAL_LEVEL / SOFTWARE_EDGE
 94        session.script_triggers[0].digital_edge_script_trigger_edge = nifgen.ScriptTriggerDigitalEdgeEdge.RISING  # RISING / FAILING
 95
 96        # 3 - Calculate and write different waveform data to the device's onboard memory
 97        session.channels[channel].write_waveform('sine', SINE_WAVE)        # (waveform_name, data)
 98        session.channels[channel].write_waveform('rampup', RAMP_UP)
 99        session.channels[channel].write_waveform('rampdown', RAMP_DOWN)
100        session.channels[channel].write_waveform('square', SQUARE_WAVE)
101        session.channels[channel].write_waveform('sawtooth', SAWTOOTH_WAVE)
102
103        # 4 - Script to generate
104        # supported shapes: SINE / SQUARE / SAWTOOTH / RAMPUP / RAMPDOWN / MULTI
105        script_name = 'script{}'.format(shape.lower())
106        num_triggers = 6 if shape.upper() == 'MULTI' else 1  # Only multi needs multiple triggers, all others need one
107
108        session.channels[channel].write_script(SCRIPT_ALL)
109        session.script_to_generate = script_name
110
111        # LAUNCH
112        with session.initiate():
113            for x in range(num_triggers):
114                time.sleep(10)
115                session.script_triggers[0].send_software_edge_trigger()
116
117
118def _main(argsv):
119    parser = argparse.ArgumentParser(description='Generate different shape waveforms.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
120    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI arbitrary waveform generator.')
121    parser.add_argument('-s', '--shape', default='SINE', help='Shape of the signal to generate')
122    parser.add_argument('-c', '--channel', default='0', help='Channel to use when generating')
123    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
124    args = parser.parse_args(argsv)
125    example(args.resource_name, args.option_string, args.shape.upper(), args.channel)
126
127
128def test_example():
129    options = {'simulate': True, 'driver_setup': {'Model': '5433 (2CH)', 'BoardType': 'PXIe', }, }
130    example('PXI1Slot2', options, 'SINE', '0')
131
132
133def test_main():
134    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5433 (2CH);BoardType:PXIe', '--channel', '0', ]
135    _main(cmd_line)
136
137
138def main():
139    _main(sys.argv[1:])
140
141
142if __name__ == '__main__':
143    main()
144
145
146
147

nifgen_standard_function.py

 1#!/usr/bin/python
 2
 3import argparse
 4import nifgen
 5import sys
 6import time
 7
 8
 9def example(resource_name, options, waveform, frequency, amplitude, offset, phase, gen_time):
10    with nifgen.Session(resource_name=resource_name, options=options) as session:
11        session.output_mode = nifgen.OutputMode.FUNC
12        session.configure_standard_waveform(waveform=nifgen.Waveform[waveform], amplitude=amplitude, frequency=frequency, dc_offset=offset, start_phase=phase)
13        with session.initiate():
14            time.sleep(gen_time)
15
16
17def _main(argsv):
18    supported_waveforms = list(nifgen.Waveform.__members__.keys())[:-1]  # no support for user-defined waveforms in example
19    parser = argparse.ArgumentParser(description='Generates the standard function.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
20    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI function generator.')
21    parser.add_argument('-w', '--waveform', default=supported_waveforms[0], choices=supported_waveforms, type=str.upper, help='Standard waveform')
22    parser.add_argument('-f', '--frequency', default=1000, type=float, help='Frequency (Hz)')
23    parser.add_argument('-a', '--amplitude', default=1.0, type=float, help='Amplitude (Vpk-pk)')
24    parser.add_argument('-o', '--offset', default=0.0, type=float, help='DC offset (V)')
25    parser.add_argument('-p', '--phase', default=0.0, type=float, help='Start phase (deg)')
26    parser.add_argument('-t', '--time', default=5.0, type=float, help='Generation time (s)')
27    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
28    args = parser.parse_args(argsv)
29    example(args.resource_name, args.option_string, args.waveform, args.frequency, args.amplitude, args.offset, args.phase, args.time)
30
31
32def main():
33    _main(sys.argv[1:])
34
35
36def test_example():
37    options = {'simulate': True, 'driver_setup': {'Model': '5433 (2CH)', 'BoardType': 'PXIe', }, }
38    example('PXI1Slot2', options, 'SINE', 1000, 1.0, 0.0, 0.0, 5.0)
39
40
41def test_main():
42    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5433 (2CH);BoardType:PXIe', ]
43    _main(cmd_line)
44
45
46if __name__ == '__main__':
47    main()
48
49
50

nifgen_trigger.py

 1import argparse
 2import nifgen
 3import sys
 4import time
 5
 6
 7def example(resource_name1, resource_name2, options, waveform, gen_time):
 8    with nifgen.Session(resource_name=resource_name1, options=options) as session1, nifgen.Session(resource_name=resource_name2, options=options) as session2:
 9        session_list = [session1, session2]
10        for session in session_list:
11            session.output_mode = nifgen.OutputMode.FUNC
12            session.configure_standard_waveform(waveform=nifgen.Waveform[waveform], amplitude=1.0, frequency=1000, dc_offset=0.0, start_phase=0.0)
13        session1.start_trigger_type = nifgen.StartTriggerType.SOFTWARE_EDGE
14        session2.start_trigger_type = nifgen.StartTriggerType.DIGITAL_EDGE
15        session2.digital_edge_start_trigger_edge = nifgen.StartTriggerDigitalEdgeEdge.RISING
16        session2.digital_edge_start_trigger_source = '/' + resource_name1 + '/0/StartTrigger'
17        with session2.initiate():
18            with session1.initiate():
19                session1.send_software_edge_trigger(nifgen.Trigger.START)
20                time.sleep(gen_time)
21
22
23def _main(argsv):
24    supported_waveforms = list(nifgen.Waveform.__members__.keys())[:-1]  # no support for user-defined waveforms in example
25    parser = argparse.ArgumentParser(description='Triggers one device on the start trigger of another device.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
26    parser.add_argument('-n1', '--resource-name1', default='PXI1Slot2', help='Resource name of an NI function generator.')
27    parser.add_argument('-n2', '--resource-name2', default='PXI1Slot3', help='Resource name of an NI function generator.')
28    parser.add_argument('-w', '--waveform', default=supported_waveforms[0], choices=supported_waveforms, type=str.upper, help='Standard waveform')
29    parser.add_argument('-t', '--time', default=5.0, type=float, help='Generation time (s)')
30    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
31    args = parser.parse_args(argsv)
32    example(args.resource_name1, args.resource_name2, args.option_string, args.waveform, args.time)
33
34
35def main():
36    _main(sys.argv[1:])
37
38
39def test_example():
40    options = {'simulate': True, 'driver_setup': {'Model': '5433 (2CH)', 'BoardType': 'PXIe', }, }
41    example('PXI1Slot2', 'PXI1Slot3', options, 'SINE', 5.0)
42
43
44def test_main():
45    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5433 (2CH);BoardType:PXIe', ]
46    _main(cmd_line)
47
48
49if __name__ == '__main__':
50    main()