Skip to content

Commit

Permalink
Simplify NI-FGEN example and remove its use of numpy and scipy (#1817)
Browse files Browse the repository at this point in the history
* Remove scipy usage from square_wave function

* Remove scipy use from sawtooth wave construction

* Remove scipy installation from system test setup

* Regenerate

* Updated changelog

* Fix flake8 failures

* Revert "Updated changelog"

This reverts commit 1a7b78c.

* Simplify nifgen example and eliminate its use of numpy

* Fix parentheses

* import random and use gaussian noise

* Rename number_of_points to number_of_samples

* Replace wfm functions with variables

* make number_of_samples uppercase; it's a constant

* Update number_of_samples to avoid underflow

* Update src/nifgen/examples/nifgen_script.py

Make sawtooth wave correct length

Co-authored-by: Marcos <marcoskirsch@users.noreply.github.com>

* Correct comment about number of triggers

* Increase NUMBER_OF_SAMPLES

Co-authored-by: Marcos <marcoskirsch@users.noreply.github.com>
  • Loading branch information
ni-jfitzger and marcoskirsch authored Aug 30, 2022
1 parent 3e4372d commit c4106c5
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 63 deletions.
1 change: 0 additions & 1 deletion build/templates/tox-system_tests.ini.mako
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ deps =
${module_name}-system_tests: coverage
${module_name}-system_tests: numpy
${module_name}-system_tests: hightime
${module_name}-system_tests: scipy
${module_name}-system_tests: fasteners
${module_name}-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nidcpower/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ deps =
nidcpower-system_tests: coverage
nidcpower-system_tests: numpy
nidcpower-system_tests: hightime
nidcpower-system_tests: scipy
nidcpower-system_tests: fasteners
nidcpower-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nidigital/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ deps =
nidigital-system_tests: coverage
nidigital-system_tests: numpy
nidigital-system_tests: hightime
nidigital-system_tests: scipy
nidigital-system_tests: fasteners
nidigital-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nidmm/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ deps =
nidmm-system_tests: coverage
nidmm-system_tests: numpy
nidmm-system_tests: hightime
nidmm-system_tests: scipy
nidmm-system_tests: fasteners
nidmm-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nifake/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ deps =
nifake-system_tests: coverage
nifake-system_tests: numpy
nifake-system_tests: hightime
nifake-system_tests: scipy
nifake-system_tests: fasteners
nifake-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nifgen/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ deps =
nifgen-system_tests: coverage
nifgen-system_tests: numpy
nifgen-system_tests: hightime
nifgen-system_tests: scipy
nifgen-system_tests: fasteners
nifgen-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nimodinst/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ deps =
nimodinst-system_tests: coverage
nimodinst-system_tests: numpy
nimodinst-system_tests: hightime
nimodinst-system_tests: scipy
nimodinst-system_tests: fasteners
nimodinst-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/niscope/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ deps =
niscope-system_tests: coverage
niscope-system_tests: numpy
niscope-system_tests: hightime
niscope-system_tests: scipy
niscope-system_tests: fasteners
niscope-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nise/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ deps =
nise-system_tests: coverage
nise-system_tests: numpy
nise-system_tests: hightime
nise-system_tests: scipy
nise-system_tests: fasteners
nise-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/niswitch/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ deps =
niswitch-system_tests: coverage
niswitch-system_tests: numpy
niswitch-system_tests: hightime
niswitch-system_tests: scipy
niswitch-system_tests: fasteners
niswitch-system_tests: pytest-json

Expand Down
1 change: 0 additions & 1 deletion generated/nitclk/tox-system_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ deps =
nitclk-system_tests: coverage
nitclk-system_tests: numpy
nitclk-system_tests: hightime
nitclk-system_tests: scipy
nitclk-system_tests: fasteners
nitclk-system_tests: pytest-json

Expand Down
78 changes: 26 additions & 52 deletions src/nifgen/examples/nifgen_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,26 @@

import argparse
import nifgen
import numpy as np
from scipy import signal

import math
import random
import sys
import time

number_of_points = 256


def calculate_sinewave():
time = np.linspace(start=0, stop=10, num=number_of_points) # np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
amplitude = np.sin(time)
sinewave = amplitude.tolist() # List of Float
return sinewave


def calculate_rampup():
ramp = np.linspace(start=0, stop=0.5, num=number_of_points) # np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
ramp_up = ramp.tolist() # List of Float
return ramp_up


def calculate_rampdown():
ramp = np.linspace(start=0, stop=0.5, num=number_of_points) # np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
ramp_down = ramp.tolist() # List of Float
ramp_down.reverse() # Reverse list to get a ramp down
return ramp_down


def calculate_square():
time = np.linspace(start=0, stop=10, num=number_of_points) # np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
square_build = signal.square(t=time, duty=0.5) # signal.square(t, duty=0.5)
square = square_build.tolist() # List of Float
return square


def calculate_triangle():
time = np.linspace(start=0, stop=1, num=number_of_points) # np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
triangle_build = signal.sawtooth(t=time) # signal.sawtooth(t, width=1)
triangle = triangle_build.tolist() # List of Float
return triangle
# waveform size should be a multiple of the quantum, which is 4, 2 or 1, for all devices
# minimum waveform size needed to prevent underflow varies with sample rate and device.
# If underflow occurs, increase this value.
NUMBER_OF_SAMPLES = 2096


def calculate_gaussian_noise():
random_noise = np.random.normal(loc=0, scale=0.1, size=number_of_points) # random.normal(loc=0.0, scale=1.0, size=None)
noise = random_noise.tolist() # List of Float
return noise
# waveforms finish just short of 360 degrees, so that we don't repeat the first point
# if we repeat the waveform
SINE_WAVE = [math.sin(math.pi * 2 * x / (NUMBER_OF_SAMPLES)) for x in range(NUMBER_OF_SAMPLES)]
RAMP_UP = [x / (NUMBER_OF_SAMPLES) for x in range(NUMBER_OF_SAMPLES)]
RAMP_DOWN = [-1.0 * x for x in RAMP_UP]
SQUARE_WAVE = [1.0 if x < (NUMBER_OF_SAMPLES / 2) else -1.0 for x in range(NUMBER_OF_SAMPLES)]
SAWTOOTH_WAVE = RAMP_UP[::2] + [(-1 + x) for x in RAMP_UP][::2]
GAUSSIAN_NOISE = [random.gauss(0, 0.2) for x in range(NUMBER_OF_SAMPLES)]


SCRIPT_ALL = '''
Expand All @@ -70,7 +44,7 @@ def calculate_gaussian_noise():
generate sine
end repeat
repeat until scriptTrigger0
generate triangle
generate sawtooth
end repeat
repeat until scriptTrigger0
generate rampdown
Expand Down Expand Up @@ -103,9 +77,9 @@ def calculate_gaussian_noise():
end repeat
end script
script scripttriangle
script scriptsawtooth
repeat until scriptTrigger0
generate triangle
generate sawtooth
end repeat
end script
Expand All @@ -129,17 +103,17 @@ def example(resource_name, options, shape, channel):
session.script_triggers[0].digital_edge_script_trigger_edge = nifgen.ScriptTriggerDigitalEdgeEdge.RISING # RISING / FAILING

# 3 - Calculate and write different waveform data to the device's onboard memory
session.channels[channel].write_waveform('sine', calculate_sinewave()) # (waveform_name, data)
session.channels[channel].write_waveform('rampup', calculate_rampup())
session.channels[channel].write_waveform('rampdown', calculate_rampdown())
session.channels[channel].write_waveform('square', calculate_square())
session.channels[channel].write_waveform('triangle', calculate_triangle())
session.channels[channel].write_waveform('noise', calculate_gaussian_noise())
session.channels[channel].write_waveform('sine', SINE_WAVE) # (waveform_name, data)
session.channels[channel].write_waveform('rampup', RAMP_UP)
session.channels[channel].write_waveform('rampdown', RAMP_DOWN)
session.channels[channel].write_waveform('square', SQUARE_WAVE)
session.channels[channel].write_waveform('sawtooth', SAWTOOTH_WAVE)
session.channels[channel].write_waveform('noise', GAUSSIAN_NOISE)

# 4 - Script to generate
# supported shapes: SINE / SQUARE / TRIANGLE / RAMPUP / RAMPDOWN / NOISE / MULTI
# supported shapes: SINE / SQUARE / SAWTOOTH / RAMPUP / RAMPDOWN / NOISE / MULTI
script_name = 'script{}'.format(shape.lower())
num_triggers = 6 if shape.upper() == 'MULTI' else 1 # Only multi needs two triggers, all others need one
num_triggers = 6 if shape.upper() == 'MULTI' else 1 # Only multi needs multiple triggers, all others need one

session.channels[channel].write_script(SCRIPT_ALL)
session.script_to_generate = script_name
Expand Down

0 comments on commit c4106c5

Please sign in to comment.