不同峰值之间的峰值

时间:2016-01-04 00:05:39

标签: android android-intent android-activity

我从文本文件中读取后,试图获得这两个峰值。我已经定义了两个字符串来比较两个值以便找到峰值,但我认为我的 if(y_0> MP)语句不正确,找到两个峰值。我该怎么办

     //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 个答案:

答案 0 :(得分:0)

您对 if(y_0&gt; MP)行的怀疑是正确的。如果您想要显示找到的峰数,那么您需要保留一个峰值列表或一个计数器,并在每次找到峰值时添加它。然后, for循环完成搜索峰值之后,你会发出一个祝酒词,说明找到了多少个峰值。

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();
}


供将来参考,此部分代码

if (y_0 > MP) {
    MP = (int) y_0;
} else {
    MP = (int) y_0;
}

等同于:

MP = (int) y_0;

无论if语句是true还是false,都指定MP = (int) y_0