1
1
package com .example .brainflowplot .ui .psdplot ;
2
2
3
- import android .content .SharedPreferences ;
4
3
import android .os .Bundle ;
5
4
import android .os .Handler ;
6
5
import android .util .Log ;
10
9
11
10
import androidx .annotation .NonNull ;
12
11
import androidx .fragment .app .Fragment ;
13
- import androidx .preference .PreferenceManager ;
14
12
15
13
import com .example .brainflowplot .DataActivity ;
16
14
import com .example .brainflowplot .R ;
29
27
30
28
public class PSDPlotFragment extends Fragment {
31
29
32
- public int windowSize = 2 ;
33
- public int timeSleep = 100 ;
30
+ public int windowSize = 8 ;
31
+ public int timeSleep = 50 ;
34
32
private GraphView graph = null ;
35
33
private LineGraphSeries <DataPoint >[] series = null ;
36
34
37
35
private Runnable worker = null ;
38
36
private final Handler handler = new Handler ();
39
- private double bandPassCenter = 0 ;
40
- private double bandPassWidth = 0 ;
41
- private double bandStopWidth = 0 ;
42
- private double bandStopCenter = 0 ;
43
37
44
38
public View onCreateView (@ NonNull LayoutInflater inflater ,
45
39
ViewGroup container , Bundle savedInstanceState ) {
@@ -59,20 +53,7 @@ public View onCreateView(@NonNull LayoutInflater inflater,
59
53
graph .getViewport ().setMaxY (10 );
60
54
graph .getViewport ().setYAxisBoundsManual (true );
61
55
graph .getViewport ().setXAxisBoundsManual (true );
62
- graph .getGridLabelRenderer ().setNumHorizontalLabels (10 );
63
-
64
- SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (rootView .getContext ());
65
- String bp = prefs .getString (getString (R .string .bp_key ), "1-50" );
66
- String bs = prefs .getString (getString (R .string .bs_key ), "58-62" );
67
- int bpStart = Integer .valueOf (bp .split ("-" )[0 ]);
68
- int bpEnd = Integer .valueOf (bp .split ("-" )[1 ]);
69
- int bsStart = Integer .valueOf (bs .split ("-" )[0 ]);
70
- int bsEnd = Integer .valueOf (bs .split ("-" )[1 ]);
71
-
72
- bandStopWidth = Math .abs (bsEnd - bsStart );
73
- bandStopCenter = (bsStart + bsEnd ) / 2.0 ;
74
- bandPassWidth = Math .abs (bpEnd - bpStart );
75
- bandPassCenter = (bpStart + bpEnd ) / 2.0 ;
56
+ graph .getGridLabelRenderer ().setNumHorizontalLabels (20 );
76
57
77
58
return rootView ;
78
59
}
@@ -95,26 +76,25 @@ public void run() {
95
76
try {
96
77
int numDataPoints = DataFilter .get_nearest_power_of_two (DataActivity .samplingRate * windowSize );
97
78
double [][] tmpArray = DataActivity .boardShim .get_current_board_data (numDataPoints );
98
- if (tmpArray [0 ].length != numDataPoints ) {
99
- // dont prepend, just wait for more data
100
- return ;
101
- }
102
79
for (int i = 0 ; i < DataActivity .channels .length ; i ++) {
103
- DataFilter .perform_bandstop (tmpArray [DataActivity .channels [i ]], DataActivity .samplingRate , bandStopCenter , bandStopWidth , 2 ,
80
+ DataFilter .perform_bandstop (tmpArray [DataActivity .channels [i ]], DataActivity .samplingRate , 50.0 , 4.0 , 2 ,
81
+ FilterTypes .BUTTERWORTH .get_code (), 0.0 );
82
+ DataFilter .perform_bandstop (tmpArray [DataActivity .channels [i ]], DataActivity .samplingRate , 60.0 , 4.0 , 2 ,
104
83
FilterTypes .BUTTERWORTH .get_code (), 0.0 );
105
- DataFilter .perform_bandpass (tmpArray [DataActivity .channels [i ]], DataActivity .samplingRate , bandPassCenter , bandPassWidth , 2 ,
84
+ DataFilter .perform_bandpass (tmpArray [DataActivity .channels [i ]], DataActivity .samplingRate , 24.0 , 47.0 , 2 ,
106
85
FilterTypes .BUTTERWORTH .get_code (), 0.0 );
107
86
}
108
87
// prepare data to plot
109
88
for (int i = 0 ; i < DataActivity .channels .length ; i ++) {
110
89
double [] channel = tmpArray [DataActivity .channels [i ]];
111
- Pair <double [], double []> psd = DataFilter .get_log_psd (channel , 0 , channel .length , DataActivity .samplingRate , WindowFunctions .HAMMING .get_code ());
90
+ int nfft = DataFilter .get_nearest_power_of_two (DataActivity .samplingRate ) * 2 ;
91
+ Pair <double [], double []> psd = DataFilter .get_psd_welch (channel , nfft , nfft / 2 , DataActivity .samplingRate , WindowFunctions .HANNING .get_code ());
112
92
int count = psd .getKey ().length ;
113
93
DataPoint [] values = new DataPoint [count ];
114
94
for (int j = 0 ; j < count ; j ++) {
115
95
double x = psd .getValue ()[j ];
116
96
double y = psd .getKey ()[j ];
117
- DataPoint v = new DataPoint (x , y );
97
+ DataPoint v = new DataPoint (x , Math . log10 ( y ) );
118
98
values [j ] = v ;
119
99
}
120
100
series [i ].resetData (values );
0 commit comments