Peak Between Different Peak Values
Am trying to get these two peaks once I read from my text file. I have defined two string to compare two value in order to find the peak, but I think my if(y_0>MP) statement is
Solution 1:
Your suspicions about the if (y_0 > MP) line are correct. If you want to make a toast showing the number of peaks found, then you either need to keep a list of the peaks, or a counter, and add to it every time a peak is found. Then, after the for-loop has finished searching for peaks, you would make a toast saying how many peaks were found.
List<Integer> peaks = new ArrayList<>();
for (t = 1; t < dataArray.length - 1; t++) {
float y_0 = Float.valueOf(dataArray[t]);
float y_1 = Float.valueOf(dataArray[t + 1]);
float y_2 = Float.valueOf(dataArray[t - 1]);
float left = y_0 - y_2;
float right = y_1 - y_0;
if (left > 0 && right < 0)
peaks.add(t);
}
Toast.makeText(getApplicationContext(), "Number of peaks founds\n: " + peaks.size(), Toast.LENGTH_SHORT).show();
for (Integer peak : peaks) {
float value = Float.valueOf(dataArray[peak]);
Toast.makeText(getApplicationContext(), "Peak of height " + value + " found at index " + peak, Toast.LENGTH_SHORT).show();
}
For future reference, this section of code
if (y_0 > MP) {
MP = (int) y_0;
} else {
MP = (int) y_0;
}
Is equivalent to this:
MP = (int) y_0;
You assign MP = (int) y_0
regardless of whether the if statement is true or false.
Post a Comment for "Peak Between Different Peak Values"