通过一个数组找到第二个峰值,然后继续前进,直到一个山谷被击中,然后继续前进,直到找到第一个山峰

时间:2018-05-24 18:38:55

标签: java android

通过一个数组找到第二个峰值,然后继续前进,直到一个山谷被击中,然后继续往返,直到找到第一个山峰穿过一个阵列找到第二个山峰,然后一直走到一个山谷被击中,然后继续回去,直到找到第一个峰值。当我想要找到峰值时,第一个峰值会被图中的小峰抛出。所以我需要找到第一个峰值40左右的点。这两个山峰应该被5岁以下的山谷分开。有人可以帮帮忙吗?

public class Accelerometer extends AppCompatActivity implements SensorEventListener {
  private TextView xText, yText, zText, resultText, resultText2;
  private Sensor mySensor;
  private SensorManager SM;

  int pos = 0;
  double array[] = new double[10000];


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accelerometer);

    // Create our Sensor Manager
    SM = (SensorManager) getSystemService(SENSOR_SERVICE);

    // Accelerometer Sensor
    mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    // Register sensor Listener
    SM.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);

    Button buttonStop = (Button) findViewById(R.id.buttonStop);

    buttonStop.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        stopSensor();
      }
    });

    // Assign TextView
    xText = (TextView) findViewById(R.id.xText);
    yText = (TextView) findViewById(R.id.yText);
    zText = (TextView) findViewById(R.id.zText);
    resultText = (TextView) findViewById(R.id.resultText);
    resultText2 = (TextView) findViewById(R.id.resultText2);
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Not in use
  }

  @Override
  public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    xText.setText("X: " + x);
    float y = event.values[1];
    yText.setText("Y: " + y);
    float z = event.values[2];
    zText.setText("Z: " + z);

    double mag = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));

    if (pos < array.length) {
      array[pos] = 9.3;
    }
    array[pos] = mag;

    pos++;
  }

  private void stopSensor() {
    SM.unregisterListener(this);
    File path = getApplicationContext().getExternalFilesDir(null);
    File file = new File(path, "my_file-name.txt");
    // String filename = "my_file";
    FileOutputStream outputStream;

    try {
      outputStream = new FileOutputStream(file);  //openFileOutput(file, Context.MODE_APPEND);
      for (double d : array) {
        String s = Double.toString(d) + ",";
        outputStream.write(s.getBytes());
      }

      String newline = "/n";
      outputStream.write(newline.getBytes());

      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      double peak1, peak2;
      int peaklocation1, peaklocation2;

      if (array.length >= 2) {
        peak1 = array[0];
        peak2 = array[1];
        peaklocation1 = 0;
        peaklocation2 = 1;
      } else { // not enough elements
        return;
      }
      for (int i = 0; i < array.length; i++) {
        double d = array[i];
        // peak2 is greater, leave it;
        // save new value to peak1 ?
        if (peak1 < peak2 && d > peak1) {
          peak1 = d;
          peaklocation1 = i;
        } else if (d > peak2) { // peak1 is greater or d is less
          peak2 = d;
          peaklocation2 = i;
        }
      }
      int size = peaklocation1;
        size = peaklocation1 - peaklocation2 ;

      resultText.setText("Result:" + peaklocation1 );
      resultText2.setText("Result:" + peaklocation2);

    } catch (Exception e) {
      // System.out.println("Error: " + e);
      //e.printStackTrace();
    }
  }
}

0 个答案:

没有答案