Skip to content

Commit 5bba124

Browse files
neel1998mariobehling
authored andcommitted
Fourier Transform can be seen while playing back saved log (#1810)
1 parent a16190d commit 5bba124

File tree

2 files changed

+100
-25
lines changed

2 files changed

+100
-25
lines changed

app/src/main/java/io/pslab/activity/OscilloscopeActivity.java

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ public class OscilloscopeActivity extends AppCompatActivity implements View.OnCl
205205
private View mainLayout;
206206
private double lat;
207207
private double lon;
208+
public boolean isPlaybackFourierChecked = false;
209+
208210
private enum CHANNEL {CH1, CH2, CH3, MIC}
209211

210212
@SuppressLint("ClickableViewAccessibility")
@@ -610,13 +612,13 @@ private void setLayoutForPlayback() {
610612
recordButton.setVisibility(View.GONE);
611613
RelativeLayout.LayoutParams lineChartParams = (RelativeLayout.LayoutParams) mChartLayout.getLayoutParams();
612614
RelativeLayout.LayoutParams frameLayoutParams = (RelativeLayout.LayoutParams) frameLayout.getLayoutParams();
613-
lineChartParams.height = height * 4/5;
615+
lineChartParams.height = height * 4 / 5;
614616
lineChartParams.width = width;
615617
mChartLayout.setLayoutParams(lineChartParams);
616-
frameLayoutParams.height = height /5;
618+
frameLayoutParams.height = height / 5;
617619
frameLayoutParams.width = width;
618620
frameLayout.setLayoutParams(frameLayoutParams);
619-
replaceFragment(R.id.layout_dock_os2, playbackFragment, "Playback Fragment" );
621+
replaceFragment(R.id.layout_dock_os2, playbackFragment, "Playback Fragment");
620622
}
621623

622624
public void playRecordedData() {
@@ -639,16 +641,37 @@ public void run() {
639641
String[] xData = data.getDataX().split(" ");
640642
String[] yData = data.getDataY().split(" ");
641643

642-
int n = Math.min(xData.length, yData.length);
643-
for (int i = 0; i < n; i++) {
644-
if (xData[i].length() > 0 && yData[i].length() > 0) {
645-
entries.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData[i])));
644+
if (!isPlaybackFourierChecked) {
645+
int n = Math.min(xData.length, yData.length);
646+
for (int i = 0; i < n; i++) {
647+
if (xData[i].length() > 0 && yData[i].length() > 0) {
648+
entries.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData[i])));
649+
}
650+
}
651+
setLeftYAxisScale(16f, -16f);
652+
setRightYAxisScale(16f, -16f);
653+
setXAxisScale(data.getTimebase());
654+
} else {
655+
Complex[] yComplex = new Complex[yData.length];
656+
for (int i = 0; i < yData.length; i++) {
657+
yComplex[i] = Complex.valueOf(Double.valueOf(yData[i]));
646658
}
659+
Complex[] fftOut = fft(yComplex);
660+
int n = fftOut.length;
661+
double mA = 0;
662+
double factor = samples * timeGap * 1e-3;
663+
double mF = (n / 2 - 1) / factor;
664+
for (int i = 0; i < n / 2; i++) {
665+
float y = (float) fftOut[i].abs() / samples;
666+
if (y > mA) {
667+
mA = y;
668+
}
669+
entries.add(new Entry((float) (i / factor), y));
670+
}
671+
setLeftYAxisScale(mA, 0);
672+
setRightYAxisScale(mA, 0);
673+
setXAxisScale(mF);
647674
}
648-
649-
setLeftYAxisScale(16f, -16f);
650-
setRightYAxisScale(16f, -16f);
651-
setXAxisScale(data.getTimebase());
652675
LineDataSet dataSet = new LineDataSet(entries, data.getChannel());
653676
LineData lineData = new LineData(dataSet);
654677
dataSet.setDrawCircles(false);
@@ -665,18 +688,52 @@ public void run() {
665688
String[] xData = data.getDataX().split(" ");
666689
String[] yData1 = data.getDataY().split(" ");
667690
String[] yData2 = data2.getDataY().split(" ");
668-
int n = Math.min(xData.length, Math.min(yData1.length, yData2.length));
669-
for (int i = 0; i < n; i++) {
670-
if (xData[i].length() > 0 && yData1[i].length() > 0 && yData2[i].length() > 0) {
671-
entries1.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData1[i])));
672-
entries2.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData2[i])));
673-
}
674-
}
675691

676-
setLeftYAxisScale(16f, -16f);
677-
setRightYAxisScale(16f, -16f);
678-
setXAxisScale(data.getTimebase());
692+
if (!isPlaybackFourierChecked) {
693+
int n = Math.min(xData.length, Math.min(yData1.length, yData2.length));
694+
for (int i = 0; i < n; i++) {
695+
if (xData[i].length() > 0 && yData1[i].length() > 0 && yData2[i].length() > 0) {
696+
entries1.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData1[i])));
697+
entries2.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData2[i])));
698+
}
699+
}
679700

701+
setLeftYAxisScale(16f, -16f);
702+
setRightYAxisScale(16f, -16f);
703+
setXAxisScale(data.getTimebase());
704+
} else {
705+
Complex[] yComplex1 = new Complex[yData1.length];
706+
Complex[] yComplex2 = new Complex[yData2.length];
707+
for (int i = 0; i < Math.min(yData1.length, yData2.length); i++) {
708+
yComplex1[i] = Complex.valueOf(Double.valueOf(yData1[i]));
709+
yComplex2[i] = Complex.valueOf(Double.valueOf(yData2[i]));
710+
}
711+
Complex[] fftOut1 = fft(yComplex1);
712+
Complex[] fftOut2 = fft(yComplex2);
713+
int n = Math.min(fftOut1.length, fftOut2.length);
714+
double mA = 0;
715+
716+
float maxAmp1 = 0;
717+
float maxAmp2 = 0;
718+
double factor = samples * timeGap * 1e-3;
719+
double mF = (n / 2 - 1) / factor;
720+
for (int i = 0; i < n / 2; i++) {
721+
float y1 = (float) fftOut1[i].abs() / samples;
722+
if (y1 > maxAmp1) {
723+
maxAmp1 = y1;
724+
}
725+
entries1.add(new Entry((float) (i / factor), y1));
726+
float y2 = (float) fftOut2[i].abs() / samples;
727+
if (y2 > maxAmp2) {
728+
maxAmp2 = y2;
729+
}
730+
entries2.add(new Entry((float) (i / factor), y2));
731+
}
732+
mA = Math.max(maxAmp1, maxAmp2);
733+
setXAxisScale(mF);
734+
setLeftYAxisScale(mA, 0);
735+
setRightYAxisScale(mA, 0);
736+
}
680737
LineDataSet dataSet1 = new LineDataSet(entries1, data.getChannel());
681738
LineDataSet dataSet2 = new LineDataSet(entries2, data2.getChannel());
682739
dataSet1.setDrawCircles(false);
@@ -1041,7 +1098,7 @@ protected Void doInBackground(String... params) {
10411098
entries = new ArrayList<>();
10421099
for (int i = 0; i < xData.length; i++) {
10431100
xData[i] = xData[i] / ((timebase == 875) ? 1 : 1000);
1044-
entries.add(new Entry((float)xData[i], (float)yData[i]));
1101+
entries.add(new Entry((float) xData[i], (float) yData[i]));
10451102

10461103
xString[i] = String.valueOf(xData[i]);
10471104
yString[i] = String.valueOf(yData[i]);
@@ -1135,10 +1192,10 @@ else if (triggerChannel.equals(analogInput2))
11351192
String[] xString = new String[n];
11361193
String[] y1String = new String[n];
11371194
String[] y2String = new String[n];
1138-
for (int i = 0; i < n ; i++) {
1195+
for (int i = 0; i < n; i++) {
11391196
xData[i] = xData[i] / ((timebase == 875) ? 1 : 1000);
1140-
entries1.add(new Entry((float)xData[i], (float) y1Data[i]));
1141-
entries2.add(new Entry((float)xData[i], (float) y2Data[i]));
1197+
entries1.add(new Entry((float) xData[i], (float) y1Data[i]));
1198+
entries2.add(new Entry((float) xData[i], (float) y2Data[i]));
11421199
xString[i] = String.valueOf(xData[i]);
11431200
y1String[i] = String.valueOf(y1Data[i]);
11441201
y2String[i] = String.valueOf(y2Data[i]);
@@ -1186,6 +1243,9 @@ protected void onPostExecute(Void aVoid) {
11861243
dataSets.add(dataSet2);
11871244

11881245
LineData data = new LineData(dataSets);
1246+
setXAxisScale(xAxisScale);
1247+
setLeftYAxisScale(16, -16);
1248+
setRightYAxisScale(16, -16);
11891249
mChart.setData(data);
11901250
mChart.notifyDataSetChanged();
11911251
mChart.invalidate();
@@ -1583,6 +1643,7 @@ else if (triggerChannel.equals("CH2"))
15831643
}
15841644
entries2.add(new Entry((float) (i / factor), y2));
15851645
}
1646+
maxAmp = Math.max(maxAmp1, maxAmp2);
15861647
} catch (NullPointerException e) {
15871648
cancel(true);
15881649
} catch (InterruptedException e) {
@@ -1611,6 +1672,9 @@ protected void onPostExecute(Void aVoid) {
16111672
dataSets.add(dataSet2);
16121673

16131674
LineData data = new LineData(dataSets);
1675+
setXAxisScale(maxFreq);
1676+
setLeftYAxisScale(maxAmp, 0);
1677+
setRightYAxisScale(maxAmp, 0);
16141678
mChart.setData(data);
16151679
mChart.notifyDataSetChanged();
16161680
mChart.invalidate();

app/src/main/java/io/pslab/fragment/OscilloscopePlaybackFragment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.view.LayoutInflater;
88
import android.view.View;
99
import android.view.ViewGroup;
10+
import android.widget.CheckBox;
11+
import android.widget.CompoundButton;
1012
import android.widget.ImageView;
1113
import android.widget.TextView;
1214

@@ -30,6 +32,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
3032
View rootView = inflater.inflate(R.layout.fragment_oscilloscope_playback, container, false);
3133
timebaseTextView = rootView.findViewById(R.id.timebase_data);
3234
playButton = rootView.findViewById(R.id.play_button);
35+
CheckBox fourierCheckBox = rootView.findViewById(R.id.fourier_checkbox);
36+
3337
playButton.setOnClickListener(new View.OnClickListener() {
3438
@Override
3539
public void onClick(View v) {
@@ -44,6 +48,13 @@ public void onClick(View v) {
4448
}
4549
}
4650
});
51+
52+
fourierCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
53+
@Override
54+
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
55+
oscilloscopeActivity.isPlaybackFourierChecked = isChecked;
56+
}
57+
});
4758
return rootView;
4859
}
4960

0 commit comments

Comments
 (0)