不同峰值之间的峰 [英] Peak between different Peak values

查看:94
本文介绍了不同峰值之间的峰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试一次,我从我的文本文件中读取得到这两个峰。我已经定义了两个字符串,以便找到高峰比较两个值,但我觉得我的如果(y_0> MP)语句不正确找到两个峰值。我应该怎么做吧。

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");


        }
    });

在这里输入的形象描述

推荐答案

您对如果(y_0> MP)行的怀疑是正确的。如果你想显示找到的峰值数敬酒,那么您可能需要保持峰值,或计数器的列表,每一个峰值时发现添加到它。随后,的之后的for循环已完成寻找峰,你敬酒说许多山峰是如何被发现的。

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

结果

有关备查,code的这一部分。

For future reference, this section of code

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

时相同的:

MP = (int) y_0;

您指定 MP =(INT)y_0 无论if语句是真的还是假的。

You assign MP = (int) y_0 regardless of whether the if statement is true or false.

这篇关于不同峰值之间的峰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆