Skip to content

Commit

Permalink
Added some sample python code for generating arbitrary waveforms
Browse files Browse the repository at this point in the history
  • Loading branch information
peterska committed Feb 5, 2021
1 parent 659960f commit e9365c6
Show file tree
Hide file tree
Showing 6 changed files with 6,207 additions and 0 deletions.
17 changes: 17 additions & 0 deletions scripts/full-wave-rectified.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/env python

# Generate a full wave rectified waveform to use with the MHS-5200A function generator
from math import pi
from math import sin

num_points = 2048
amplitude = 1.0
print("# generated by scripts/full-wave-rectified.py")

# Flags to allow peaks only one point wide
for i in range(num_points):
x = i/num_points # Fraction along X axis
y = amplitude*sin(2*pi*x)
if y < 0.0:
y *= -1.0
print(y)
17 changes: 17 additions & 0 deletions scripts/half-wave-rectified.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/env python

# Generate a half wave rectified waveform to use with the MHS-5200A function generator
from math import pi
from math import sin

num_points = 2048
amplitude = 1.0
print("# generated by scripts/half-wave-rectified.py")

# Flags to allow peaks only one point wide
for i in range(num_points):
x = i/num_points # Fraction along X axis
y = amplitude*sin(2*pi*x)
if y < 0.0:
y = 0.0
print(y)
26 changes: 26 additions & 0 deletions scripts/sinewave-with-spike.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/env python

# example from https://www.bkprecision.com/support/downloads/function-and-arbitrary-waveform-generator-guidebook.html
from math import pi
from math import sin

num_points = 2048
amplitude = 4000
peak_amplitude = 8000
print("# generated by scripts/sinewave-with-spike.py")

# Flags to allow peaks only one point wide
positive_done = False
negative_done = False
threshold = 1e-4

for i in range(num_points):
x = i/num_points # Fraction along X axis
y = int(amplitude*sin(2*pi*x))
if not positive_done and abs(x - 1/4) < threshold:
positive_done = True
y = peak_amplitude
if not negative_done and abs(x - 3/4) < threshold:
negative_done = True
y = -peak_amplitude
print(y / peak_amplitude)
Loading

0 comments on commit e9365c6

Please sign in to comment.