Skip to content

Commit df7f022

Browse files
committed
Fix bug streaming Band Power over LSL for all channels
1 parent 1d57505 commit df7f022

3 files changed

Lines changed: 42 additions & 31 deletions

File tree

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
"""Example program to show how to read a multi-channel time series from LSL."""
1+
"""Example program to show how to read a GUI BandPower data over LSL."""
22
import time
33
from pylsl import StreamInlet, resolve_stream
44
from time import sleep
55

6-
# first resolve an EEG stream on the lab network
6+
# Example Sample
7+
# F
8+
#[0.0, 0.8485075831413269, 9.373364448547363, 0.0013413801789283752, 0.001849484397098422]
9+
10+
# First resolve an EEG stream on the lab network
711
print("looking for an EEG stream...")
812
streams = resolve_stream('type', 'EEG')
913

10-
# create a new inlet to read from the stream
14+
# Create a new inlet to read from the stream
1115
inlet = StreamInlet(streams[0])
12-
duration = 10
16+
duration = 5
17+
num_channels = 8
1318

1419
sleep(1)
1520

1621
def testLSLSamplingRate():
1722
start = time.time()
18-
totalNumSamples = 0
19-
validSamples = 0
20-
numChunks = 0
23+
total_samples = 0
24+
num_samples_channel_0 = 0
2125
print( "Testing Sampling Rates..." )
2226

2327
while time.time() <= start + duration:
24-
# get chunks of samples
25-
chunk, timestamp = inlet.pull_chunk()
26-
if timestamp:
27-
print("\nNew chunk! Chunk size == {}".format(len(chunk)) )
28-
numChunks += 1
29-
totalNumSamples += len(chunk)
30-
print(chunk, timestamp)
31-
for sample in chunk:
32-
#print(sample)
33-
validSamples += 1
34-
35-
print( "Number of Chunks and Samples == {} , {}".format(numChunks, totalNumSamples) )
36-
print( "Average Number of Samples per Chunk == {}".format(totalNumSamples / numChunks) )
37-
print( "Valid Chunks and Duration == {} / {}".format(numChunks, duration) )
38-
print( "Average Sampling Rate == {}".format(numChunks / duration) )
28+
# Get chunks of samples
29+
sample, timestamp = inlet.pull_sample()
30+
if sample:
31+
print("\nNew chunk! Sample size == {}".format(len(sample)) )
32+
total_samples += 1
33+
print(sample, timestamp)
34+
if sample[0] == 0.0:
35+
num_samples_channel_0 += 1
36+
37+
38+
print( "Valid Samples and Duration == {} / {}".format(total_samples, duration) )
39+
print( "Average Sampling Rate == {}".format(total_samples / duration) )
40+
print( "Valid Samples Channel 0 and Duration == {} / {}".format(num_samples_channel_0, duration) )
41+
print( "Average Sampling Rate Channel 0 == {}".format(num_samples_channel_0 / duration) )
3942

4043

4144
testLSLSamplingRate()

OpenBCI_GUI/NetworkStreamOut.pde

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,23 @@ class NetworkStreamOut extends Thread {
527527

528528
} else if (this.protocol.equals("LSL")) {
529529

530-
// DELTA, THETA, ALPHA, BETA, GAMMA
530+
// Send out band powers for each channel sequentially via push_sample
531+
// Prepend channel number to each array
532+
// push_chunk will send out all channels at once...but doesn't seem to gaurantee all X channels of data will be pulled at once, despite extensive testing
533+
// Example sample: [Channel Number, DELTA, THETA, ALPHA, BETA, GAMMA]
534+
// [0.0, 0.8451713919639587, 9.27791690826416, 0.00044474846799857914, 0.0014277123846113682, 0.0029974353965371847]
531535
int numChannels = numExgChannels;
532-
float[] dataToSend = new float[numChannels * NUM_BAND_POWERS];
533-
for (int band = 0; band < NUM_BAND_POWERS; band++) {
534-
for (int channel = 0; channel < numChannels; channel++) {
535-
dataToSend[channel + band * numChannels] = dataProcessing.avgPowerInBins[channel][band];
536+
float[] dataToSend = new float[NUM_BAND_POWERS + 1];
537+
for (int channel = 0; channel < numChannels; channel++) {
538+
for (int band = 0; band < NUM_BAND_POWERS + 1; band++) {
539+
if (band == 0) {
540+
dataToSend[band] = (float) channel;
541+
} else {
542+
dataToSend[band] = dataProcessing.avgPowerInBins[channel][band - 1];
543+
}
536544
}
545+
outlet_data.push_sample(dataToSend);
537546
}
538-
double unixTime = System.currentTimeMillis() / 1000d;
539-
//println(unixTime);
540-
outlet_data.push_chunk(dataToSend, unixTime, true);
541547

542548
} else if (this.protocol.equals("Serial")) {
543549
for (int i = 0; i < numExgChannels; i++) {

OpenBCI_GUI/W_Networking.pde

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,9 @@ class W_Networking extends Widget {
10841084
} else if (dataType.equals("AvgBandPower")) {
10851085
return 5;
10861086
} else if (dataType.equals("BandPower")) {
1087-
return 5;
1087+
//Send out band powers for each channel sequentially
1088+
//Prepend channel number to each array
1089+
return 5 + 1;
10881090
} else if (dataType.equals("Pulse")) {
10891091
return 2;
10901092
} else if (dataType.equals("Accel/Aux")) {

0 commit comments

Comments
 (0)