Skip to content

Commit

Permalink
Update python example with header information
Browse files Browse the repository at this point in the history
Updated the python example to show how the header can be constructed in python and sent along with the data.
  • Loading branch information
bparks13 committed Mar 8, 2024
1 parent 929618f commit d4e04c2
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Resources/python-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,34 @@
# ---- SPECIFY THE SIGNAL PROPERTIES ---- #
totalDuration = 10 # the total duration of the signal
numChannels = 4 # number of channels to send
bufferSize = 500 # size of the data buffer
numSamples = 500 # size of the data buffer
Freq = 30000 # sample rate of the signal
testingValue1 = 100 # high value
testingValue2= -100 # low value

# ---- DEFINE HEADER VALUES ---- #
headerSize = 22 # Specifies that there are 22 bytes in the header
offset = 0 # Offset of bytes in this packet; only used for buffers > ~64 kB
dataType = 2 # Enumeration value based on OpenCV.Mat data types
elementSize = 2 # Number of bytes per element. elementSize = 2 for U16
# Data types: [ U8, S8, U16, S16, S32, F32, F64 ]
# Enum value: [ 0, 1, 2, 3, 4, 5, 6 ]
# Element Size: [ 1, 1, 2, 2, 4, 4, 8 ]
bytesPerBuffer = numChannels * numSamples * elementSize

header = np.array([offset, bytesPerBuffer], dtype='i4').tobytes() + \
np.array([dataType], dtype='i2').tobytes() + \
np.array([elementSize, numChannels, numSamples], dtype='i4').tobytes()

# ---- COMPUTE SOME USEFUL VALUES ---- #
bytesPerBuffer = bufferSize * 2 * numChannels
buffersPerSecond = Freq / bufferSize
buffersPerSecond = Freq / numSamples
bufferInterval = 1 / buffersPerSecond

# ---- GENERATE THE DATA ---- #
OpenEphysOffset = 32768
convertedValue1 = OpenEphysOffset+(testingValue1/0.195)
convertedValue2 = OpenEphysOffset+(testingValue2/0.195)
intList_1 = (np.ones((int(Freq/2),)) * convertedValue1).astype('uint16')
OpenEphysOffset = 100
convertedValue1 = OpenEphysOffset+(testingValue1)
convertedValue2 = OpenEphysOffset+(testingValue2)
intList_1 = (np.ones((int(Freq/2),)) * convertedValue1).astype('uint16') # if dataType == 2, use astype('uint16')
intList_2 = (np.ones((int(Freq/2),)) * convertedValue2).astype('uint16')
oneCycle = np.concatenate((intList_1, intList_2))
allData = np.tile(oneCycle, (numChannels, totalDuration)).T
Expand All @@ -47,12 +60,12 @@ def currentTime():
# ---- STREAM DATA ---- #
while (bufferIndex < totalBytes):
t1 = currentTime()
UDPClientSocket.sendto(bytesToSend[bufferIndex:bufferIndex+bytesPerBuffer], serverAddressPort)
rc = UDPClientSocket.sendto(header + bytesToSend[bufferIndex:bufferIndex+bytesPerBuffer], serverAddressPort)
t2 = currentTime()

while ((t2 - t1) < bufferInterval):
t2 = currentTime()

bufferIndex += bytesPerBuffer

print("Done")

0 comments on commit d4e04c2

Please sign in to comment.