Skip to content

Commit 31a20ad

Browse files
committed
Add SpectrogramEnums.pde for spectrogram settings
1 parent 0db7671 commit 31a20ad

3 files changed

Lines changed: 157 additions & 97 deletions

File tree

OpenBCI_GUI/SessionSettings.pde

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ class SessionSettings {
6464
//Analog Read settings
6565
int arVertScaleSave;
6666
int arHorizScaleSave;
67-
//Spectrogram Widget settings
68-
//FIX ME REMOVE THIS
69-
int spectMaxFrqSave;
70-
int spectSampleRateSave;
7167

7268
//default configuration settings file location and file name variables
7369
private String sessionPath = "";
@@ -98,10 +94,6 @@ class SessionSettings {
9894
String[] arVertScaleArray = {"Auto", "50", "100", "200", "400", "1000", "10000"};
9995
String[] arHorizScaleArray = {"Sync", "1 sec", "3 sec", "5 sec", "10 sec", "20 sec"};
10096

101-
//Used to set text in dropdown menus when loading Spectrogram Setings
102-
String[] spectMaxFrqArray = {"20 Hz", "40 Hz", "60 Hz", "100 Hz", "120 Hz", "250 Hz"};
103-
String[] spectSampleRateArray = {"30 Min.", "6 Min.", "3 Min.", "1.5 Min.", "1 Min."};
104-
10597
//Load Accel. dropdown variables
10698
int loadAccelVertScale;
10799
int loadAccelHorizScale;
@@ -782,11 +774,14 @@ class SessionSettings {
782774

783775
////////////////////////////Apply Spectrogram settings
784776
//Apply Max Freq dropdown
777+
//FIX ME
778+
/*
785779
SpectrogramMaxFreq(spectMaxFrqLoad);
786780
w_spectrogram.cp5_widget.getController("SpectrogramMaxFreq").getCaptionLabel().setText(spectMaxFrqArray[spectMaxFrqLoad]);
787781
SpectrogramSampleRate(spectSampleRateLoad);
788782
w_spectrogram.cp5_widget.getController("SpectrogramSampleRate").getCaptionLabel().setText(spectSampleRateArray[spectSampleRateLoad]);
789783
SpectrogramLogLin(spectLogLinLoad);
784+
*/
790785
//FIX ME
791786
//w_spectrogram.cp5_widget.getController("SpectrogramLogLin").getCaptionLabel().setText(fftLogLinArray[spectLogLinLoad]);
792787
try {

OpenBCI_GUI/SpectrogramEnums.pde

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
public enum SpectrogramMaxFrequency implements IndexingInterface {
2+
MAX_20 (0, 20, "20 Hz", new int[]{20, 15, 10, 5, 0, 5, 10, 15, 20}),
3+
MAX_40 (1, 40, "40 Hz", new int[]{40, 30, 20, 10, 0, 10, 20, 30, 40}),
4+
MAX_60 (2, 60, "60 Hz", new int[]{60, 45, 30, 15, 0, 15, 30, 45, 60}),
5+
MAX_100 (3, 100, "100 Hz", new int[]{100, 75, 50, 25, 0, 25, 50, 75, 100}),
6+
MAX_120 (4, 120, "120 Hz", new int[]{120, 90, 60, 30, 0, 30, 60, 90, 120}),
7+
MAX_250 (5, 250, "250 Hz", new int[]{250, 188, 125, 63, 0, 63, 125, 188, 250});
8+
9+
private final int index;
10+
private final int value;
11+
private final String label;
12+
private final int[] axisLabels;
13+
private static final SpectrogramMaxFrequency[] values = values();
14+
15+
SpectrogramMaxFrequency(int index, int value, String label, int[] axisLabels) {
16+
this.index = index;
17+
this.value = value;
18+
this.label = label;
19+
this.axisLabels = axisLabels;
20+
}
21+
22+
public int getValue() {
23+
return value;
24+
}
25+
26+
public int[] getAxisLabels() {
27+
return axisLabels;
28+
}
29+
30+
@Override
31+
public String getString() {
32+
return label;
33+
}
34+
35+
@Override
36+
public int getIndex() {
37+
return index;
38+
}
39+
40+
public static List<String> getEnumStringsAsList() {
41+
List<String> enumStrings = new ArrayList<>();
42+
for (IndexingInterface enumValue : values) {
43+
enumStrings.add(enumValue.getString());
44+
}
45+
return enumStrings;
46+
}
47+
}
48+
49+
public enum SpectrogramWindowSize implements IndexingInterface {
50+
ONE_MINUTE (0, 1f, "1 Min.", new float[]{1, .5, 0}, 1000),
51+
ONE_MINUTE_THIRTY (1, 1.5f, "1.5 Min.", new float[]{1.5, 1, .5, 0}, 50),
52+
THREE_MINUTES (2, 3f, "3 Min.", new float[]{3, 2, 1, 0}, 100),
53+
SIX_MINUTES (3, 6f, "6 Min.", new float[]{6, 5, 4, 3, 2, 1, 0}, 200),
54+
THIRTY_MINUTES (4, 30f, "30 Min.", new float[]{30, 25, 20, 15, 10, 5, 0}, 1000);
55+
56+
private final int index;
57+
private final float value;
58+
private final String label;
59+
private final float[] axisLabels;
60+
private final int scrollSpeed;
61+
62+
SpectrogramWindowSize(int index, float value, String label, float[] axisLabels, int scrollSpeed) {
63+
this.index = index;
64+
this.value = value;
65+
this.label = label;
66+
this.axisLabels = axisLabels;
67+
this.scrollSpeed = scrollSpeed;
68+
}
69+
70+
public float getValue() {
71+
return value;
72+
}
73+
74+
public float[] getAxisLabels() {
75+
return axisLabels;
76+
}
77+
78+
public int getScrollSpeed() {
79+
return scrollSpeed;
80+
}
81+
82+
@Override
83+
public String getString() {
84+
return label;
85+
}
86+
87+
@Override
88+
public int getIndex() {
89+
return index;
90+
}
91+
92+
public static List<String> getEnumStringsAsList() {
93+
List<String> enumStrings = new ArrayList<>();
94+
for (IndexingInterface enumValue : values()) {
95+
enumStrings.add(enumValue.getString());
96+
}
97+
return enumStrings;
98+
}
99+
}

OpenBCI_GUI/W_Spectrogram.pde

Lines changed: 55 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,13 @@ class W_Spectrogram extends Widget {
3939
int paddingRight = 26;
4040
int paddingTop = 8;
4141
int paddingBottom = 50;
42-
int numHorizAxisDivs = 2; // == 40Hz
43-
int numVertAxisDivs = 8;
44-
final int[][] vertAxisLabels = {
45-
{20, 15, 10, 5, 0, 5, 10, 15, 20},
46-
{40, 30, 20, 10, 0, 10, 20, 30, 40},
47-
{60, 45, 30, 15, 0, 15, 30, 45, 60},
48-
{100, 75, 50, 25, 0, 25, 50, 75, 100},
49-
{120, 90, 60, 30, 0, 30, 60, 90, 120},
50-
{250, 188, 125, 63, 0, 63, 125, 188, 250}
51-
};
52-
int[] vertAxisLabel;
53-
final float[][] horizAxisLabels = {
54-
{30, 25, 20, 15, 10, 5, 0},
55-
{6, 5, 4, 3, 2, 1, 0},
56-
{3, 2, 1, 0},
57-
{1.5, 1, .5, 0},
58-
{1, .5, 0}
59-
};
60-
float[] horizAxisLabel;
61-
StringList horizAxisLabelStrings;
42+
StringList horizontalAxisLabelStrings;
6243

6344
float[] topFFTAvg;
6445
float[] botFFTAvg;
6546

47+
private SpectrogramMaxFrequency maxFrequency = SpectrogramMaxFrequency.MAX_60;
48+
private SpectrogramWindowSize windowSize = SpectrogramWindowSize.ONE_MINUTE;
6649
private FFTLogLin logLin = FFTLogLin.LIN;
6750

6851
W_Spectrogram(PApplet _parent) {
@@ -84,25 +67,18 @@ class W_Spectrogram extends Widget {
8467
graphW = w - paddingRight - paddingLeft;
8568
graphH = h - paddingBottom - paddingTop;
8669

87-
//FIX ME REMOVE THESE
88-
settings.spectMaxFrqSave = 2;
89-
settings.spectSampleRateSave = 4;
90-
91-
vertAxisLabel = vertAxisLabels[settings.spectMaxFrqSave];
92-
horizAxisLabel = horizAxisLabels[settings.spectSampleRateSave];
93-
horizAxisLabelStrings = new StringList();
9470
//Fetch/calculate the time strings for the horizontal axis ticks
95-
fetchTimeStrings(numHorizAxisDivs);
71+
horizontalAxisLabelStrings = fetchTimeStrings();
9672

9773
//This is the protocol for setting up dropdowns.
9874
//Note that these 3 dropdowns correspond to the 3 global functions below
9975
//You just need to make sure the "id" (the 1st String) has the same name as the corresponding function
100-
addDropdown("SpectrogramMaxFreq", "Max Freq", Arrays.asList(settings.spectMaxFrqArray), settings.spectMaxFrqSave);
101-
addDropdown("SpectrogramSampleRate", "Window", Arrays.asList(settings.spectSampleRateArray), settings.spectSampleRateSave);
102-
addDropdown("SpectrogramLogLin", "Log/Lin", logLin.getEnumStringsAsList(), logLin.getIndex());
76+
addDropdown("spectrogramMaxFrequencyDropdown", "Max Hz", maxFrequency.getEnumStringsAsList(), maxFrequency.getIndex());
77+
addDropdown("spectrogramSampleRateDropdown", "Window", windowSize.getEnumStringsAsList(), windowSize.getIndex());
78+
addDropdown("spectrogramLogLinDropdown", "Log/Lin", logLin.getEnumStringsAsList(), logLin.getIndex());
10379

10480
//Resize the height of the data image using default
105-
dataImageH = vertAxisLabel[0] * 2;
81+
dataImageH = maxFrequency.getAxisLabels()[0] * 2;
10682
//Create image using correct dimensions! Fixes bug where image size and labels do not align on session start.
10783
dataImg = createImage(dataImageW, dataImageH, RGB);
10884
}
@@ -133,7 +109,8 @@ class W_Spectrogram extends Widget {
133109
//Make sure we are always draw new pixels on the right
134110
xPos = dataImg.width - 1;
135111
//Fetch/calculate the time strings for the horizontal axis ticks
136-
fetchTimeStrings(numHorizAxisDivs);
112+
horizontalAxisLabelStrings.clear();
113+
horizontalAxisLabelStrings = fetchTimeStrings();
137114
}
138115

139116
//State change check
@@ -281,17 +258,18 @@ class W_Spectrogram extends Widget {
281258
pushStyle();
282259
//draw horizontal axis ticks from left to right
283260
int tickMarkSize = 7; //in pixels
284-
float horizAxisX = graphX;
285-
float horizAxisY = graphY + scaledH * dataImageH;
261+
float horizontalAxisX = graphX;
262+
float horizontalAxisY = graphY + scaledH * dataImageH;
286263
stroke(255);
287264
fill(255);
288265
strokeWeight(2);
289266
textSize(11);
290-
for (int i = 0; i <= numHorizAxisDivs; i++) {
291-
float offset = scaledW * dataImageW * (float(i) / numHorizAxisDivs);
292-
line(horizAxisX + offset, horizAxisY, horizAxisX + offset, horizAxisY + tickMarkSize);
293-
if (horizAxisLabelStrings.get(i) != null) {
294-
text(horizAxisLabelStrings.get(i), horizAxisX + offset - (int)textWidth(horizAxisLabelStrings.get(i))/2, horizAxisY + tickMarkSize * 3);
267+
int horizontalAxisDivCount = windowSize.getAxisLabels().length;
268+
for (int i = 0; i < horizontalAxisDivCount; i++) {
269+
float offset = scaledW * dataImageW * (float(i) / horizontalAxisDivCount);
270+
line(horizontalAxisX + offset, horizontalAxisY, horizontalAxisX + offset, horizontalAxisY + tickMarkSize);
271+
if (horizontalAxisLabelStrings.get(i) != null) {
272+
text(horizontalAxisLabelStrings.get(i), horizontalAxisX + offset - (int)textWidth(horizontalAxisLabelStrings.get(i))/2, horizontalAxisY + tickMarkSize * 3);
295273
}
296274
}
297275
popStyle();
@@ -312,19 +290,20 @@ class W_Spectrogram extends Widget {
312290

313291
pushStyle();
314292
//draw vertical axis ticks from top to bottom
315-
float vertAxisX = graphX;
316-
float vertAxisY = graphY;
293+
float verticalAxisX = graphX;
294+
float verticalAxisY = graphY;
317295
stroke(255);
318296
fill(255);
319297
textSize(12);
320298
strokeWeight(2);
321-
for (int i = 0; i <= numVertAxisDivs; i++) {
322-
float offset = scaledH * dataImageH * (float(i) / numVertAxisDivs);
323-
//if (i <= numVertAxisDivs/2) offset -= 2;
324-
line(vertAxisX, vertAxisY + offset, vertAxisX - tickMarkSize, vertAxisY + offset);
325-
if (vertAxisLabel[i] == 0) midLineY = int(vertAxisY + offset);
299+
int verticalAxisDivCount = maxFrequency.getAxisLabels().length - 1;
300+
for (int i = 0; i < verticalAxisDivCount; i++) {
301+
float offset = scaledH * dataImageH * (float(i) / verticalAxisDivCount);
302+
//if (i <= verticalAxisDivCount/2) offset -= 2;
303+
line(verticalAxisX, verticalAxisY + offset, verticalAxisX - tickMarkSize, verticalAxisY + offset);
304+
if (maxFrequency.getAxisLabels()[i] == 0) midLineY = int(verticalAxisY + offset);
326305
offset += paddingTop/2;
327-
text(vertAxisLabel[i], vertAxisX - tickMarkSize*2 - textWidth(Integer.toString(vertAxisLabel[i])), vertAxisY + offset);
306+
text(maxFrequency.getAxisLabels()[i], verticalAxisX - tickMarkSize*2 - textWidth(Integer.toString(maxFrequency.getAxisLabels()[i])), verticalAxisY + offset);
328307
}
329308
popStyle();
330309

@@ -415,23 +394,22 @@ class W_Spectrogram extends Widget {
415394
return sum / _activeChan.size();
416395
}
417396

418-
void fetchTimeStrings(int numAxisTicks) {
419-
horizAxisLabelStrings.clear();
397+
private StringList fetchTimeStrings() {
398+
StringList output = new StringList();
420399
LocalDateTime time;
421400
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
422-
423401
if (getCurrentTimeStamp() == 0) {
424402
time = LocalDateTime.now();
425403
} else {
426404
time = LocalDateTime.ofInstant(Instant.ofEpochMilli(getCurrentTimeStamp()),
427405
TimeZone.getDefault().toZoneId());
428406
}
429-
430-
for (int i = 0; i <= numAxisTicks; i++) {
431-
long l = (long)(horizAxisLabel[i] * 60f);
407+
for (int i = 0; i < windowSize.getAxisLabels().length; i++) {
408+
long l = (long)(windowSize.getAxisLabels()[i] * 60f);
432409
LocalDateTime t = time.minus(l, ChronoUnit.SECONDS);
433-
horizAxisLabelStrings.append(t.format(formatter));
410+
output.append(t.format(formatter));
434411
}
412+
return output;
435413
}
436414

437415
//Identical to the method in TimeSeries, but allows spectrogram to get the data directly from the playback data in the background
@@ -457,45 +435,33 @@ class W_Spectrogram extends Widget {
457435
public void setLogLin(int n) {
458436
logLin = logLin.values()[n];
459437
}
438+
439+
public void setMaxFrequency(int n) {
440+
maxFrequency = maxFrequency.values()[n];
441+
// Resize the height of the data image
442+
dataImageH = maxFrequency.getAxisLabels()[0] * 2;
443+
// Overwrite the existing image
444+
dataImg = createImage(dataImageW, dataImageH, RGB);
445+
}
446+
447+
public void setWindowSize(int n) {
448+
windowSize = windowSize.values()[n];
449+
setScrollSpeed(windowSize.getScrollSpeed());
450+
horizontalAxisLabelStrings.clear();
451+
horizontalAxisLabelStrings = fetchTimeStrings();
452+
dataImg = createImage(dataImageW, dataImageH, RGB);
453+
454+
}
460455
};
461456

462-
//These functions need to be global! These functions are activated when an item from the corresponding dropdown is selected
463-
//triggered when there is an event in the Spectrogram Widget MaxFreq. Dropdown
464-
void SpectrogramMaxFreq(int n) {
465-
settings.spectMaxFrqSave = n;
466-
//reset the vertical axis labels
467-
w_spectrogram.vertAxisLabel = w_spectrogram.vertAxisLabels[n];
468-
//Resize the height of the data image
469-
w_spectrogram.dataImageH = w_spectrogram.vertAxisLabel[0] * 2;
470-
//overwrite the existing image because the sample rate is about to change
471-
w_spectrogram.dataImg = createImage(w_spectrogram.dataImageW, w_spectrogram.dataImageH, RGB);
457+
public void spectrogramMaxFrequencyDropdown(int n) {
458+
w_spectrogram.setMaxFrequency(n);
472459
}
473460

474-
void SpectrogramSampleRate(int n) {
475-
settings.spectSampleRateSave = n;
476-
//overwrite the existing image because the sample rate is about to change
477-
w_spectrogram.dataImg = createImage(w_spectrogram.dataImageW, w_spectrogram.dataImageH, RGB);
478-
w_spectrogram.horizAxisLabel = w_spectrogram.horizAxisLabels[n];
479-
if (n == 0) {
480-
w_spectrogram.numHorizAxisDivs = 6;
481-
w_spectrogram.setScrollSpeed(1000);
482-
} else if (n == 1) {
483-
w_spectrogram.numHorizAxisDivs = 6;
484-
w_spectrogram.setScrollSpeed(200);
485-
} else if (n == 2) {
486-
w_spectrogram.numHorizAxisDivs = 3;
487-
w_spectrogram.setScrollSpeed(100);
488-
} else if (n == 3) {
489-
w_spectrogram.numHorizAxisDivs = 3;
490-
w_spectrogram.setScrollSpeed(50);
491-
} else if (n == 4) {
492-
w_spectrogram.numHorizAxisDivs = 2;
493-
w_spectrogram.setScrollSpeed(25);
494-
}
495-
w_spectrogram.horizAxisLabelStrings.clear();
496-
w_spectrogram.fetchTimeStrings(w_spectrogram.numHorizAxisDivs);
461+
public void spectrogramWindowDropdown(int n) {
462+
w_spectrogram.setWindowSize(n);
497463
}
498464

499-
public void SpectrogramLogLin(int n) {
465+
public void spectrogramLogLinDropdown(int n) {
500466
w_spectrogram.setLogLin(n);
501467
}

0 commit comments

Comments
 (0)