You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DetectorTimedomain.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.feemers.android.fftdrawer.SignalProcessing;
  2. import android.util.Log;
  3. import android.util.Pair;
  4. import java.util.ArrayList;
  5. public class DetectorTimedomain extends DetectorImpl {
  6. private boolean m_calculate_amplitude;
  7. private int m_threshold_cnt;
  8. private int m_sample_cnt;
  9. public DetectorTimedomain(int windowSize) {
  10. super(windowSize);
  11. Log.d("DetectorTimedomain", "DetectorTimedomain() - Constructor called");
  12. m_calculate_amplitude = false;
  13. m_threshold_cnt = 0;
  14. m_sample_cnt = 0;
  15. }
  16. @Override
  17. public void onNewSample(double sample) {
  18. super.onNewSample(sample);
  19. check_for_earthquake(sample);
  20. }
  21. public void check_for_earthquake(double sample)
  22. {
  23. // This method checks if threshold was exceeded and calculates the amplitude if enough values are available
  24. // Possible combinitions of the return values:
  25. // 1: false, 0.0 -> Threshold wasn't exceeded
  26. // 2: true, 0.0 -> Threshold was exceeded, but no value for amplitude is available yet (because m_buffer isn't filled yet)
  27. // 3: true, amplitude -> Threshold was exceeded and a value for amplitude is available
  28. //TODO: find better solution for returning the results -> temp solution: No return-values but printing to logcat instead. Use Notifier later.
  29. Pair<Boolean, Double> return_pair = new Pair<Boolean, Double>(false, 0.0);
  30. if ((sample > m_threshold) && !(m_calculate_amplitude)) {
  31. Log.d("DetectorTimedomain", "threshold was exceeded. threshold: " + m_threshold); //TODO: send this info with notifier instead
  32. m_threshold_cnt++;
  33. // Later: add Logger entry
  34. m_calculate_amplitude = true;
  35. m_buffer = new ArrayList<>(); //resetting to an empty ArrayList
  36. }
  37. if (m_calculate_amplitude) {
  38. if (m_sample_cnt >= m_buffer_SIZE) {
  39. //Vector sensor_samples is filled -> determine amplitude
  40. double amplitude = calculate_maximum(m_buffer);
  41. m_sample_cnt = 0;
  42. m_calculate_amplitude = false;
  43. Log.d("DetectorTimedomain", "Maximum calculated: " + amplitude); //TODO: send this message with notifier instead
  44. return_pair = new Pair<Boolean, Double>(true, amplitude);
  45. // Later: add Logger entry
  46. } else {
  47. m_buffer.add(sample);
  48. m_sample_cnt++;
  49. return_pair = new Pair<Boolean, Double>(true, 0.0);
  50. }
  51. }
  52. m_Notifier.sendEvent();
  53. //return return_pair;
  54. }
  55. private double calculate_maximum(ArrayList<Double> samples) {
  56. double maximum = 0;
  57. for (double sample : samples) {
  58. if (maximum < sample)
  59. maximum = sample;
  60. }
  61. return maximum;
  62. }
  63. }