Peak between different Peak values

2019-06-14 15:01发布

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 not correct one to find the two peak. What should I do please.

     //Read data.
      Recall = (Button) findViewById(R.id.Recall);
      Recall.setOnClickListener(new View.OnClickListener() {
        StringBuilder stringBuilder;

        @Override
        public void onClick(View v) {
            //readTextFile(this, R.raw.books);
            stringBuilder = new StringBuilder();
            String line;
            try {
                FileInputStream fIn = new FileInputStream(file);
                BufferedReader bufferedreader = new BufferedReader(new   
                InputStreamReader(fIn));
                while ((line = bufferedreader.readLine()) != null) {
                    stringBuilder.append(line);
                    stringBuilder.append(" ");
                    stringBuilder.length();

                }
                bufferedreader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String a;
            a = String.valueOf(stringBuilder);
            String dataArray[];
            int t;
            dataArray = a.split(" ");
            int MP = 0;
            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) {
                    if (y_0 > MP) {
                        MP = (int) y_0;
                    } else {
                        MP = (int) y_0;
                    }
                    Toast.makeText(getApplicationContext(), "Number of peaks  
                    founds\n: " + MP, Toast.LENGTH_SHORT).show();

                }
            }
            DataAlert alert = new DataAlert();
            alert.show(getFragmentManager(), "DataAlert");


        }
    });

enter image description here

1条回答
Evening l夕情丶
2楼-- · 2019-06-14 15:22

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.

查看更多
登录 后发表回答