12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package com.feemers.android.fftdrawer.SignalProcessing;
-
- class ThresholdCalculator {
- private double[] m_buffer;
- private int m_windowSize;
- private int m_bufferCounter;
- private int m_bufferInitCounter;
- private double m_constantThresholdPart;
- private double m_adaptiveThresholdPart;
-
- public ThresholdCalculator(int windowSize) {
- if (windowSize <= 0) {
- throw new AssertionError("windowSize should be postive");
- }
- m_windowSize = windowSize;
- reset();
- }
-
- public double getThreshold() {
- // This method is meant for the use inside of a detector implementation
- // Should be called every time a new sensor value is available
- // Returns the sum of the constant part of the threshold (determined by the user) and the
- // adaptive part of the threshold (determined by the noise level of the signal)
- return m_constantThresholdPart + m_adaptiveThresholdPart;
- }
-
- public void setConstantThresholdPart(double constantThresholdPart) {
- // This method should be called when the User changes the constant part of the threshold with the GUI
- this.m_constantThresholdPart = constantThresholdPart;
- }
-
- public void onNewSample(double sample) {
- // store sample
- m_buffer[m_bufferCounter] = sample;
- // increase counter
- m_bufferCounter++;
- // check if we are on the end of Array -> go to start
- if (m_bufferCounter >= m_windowSize) {
- m_bufferCounter = 0;
- }
-
- // when we start the application we will have an arry with emty values
- // hence we define how many values are already new
- // after N_sample == windowSize this will have no further effect
- if (m_bufferInitCounter < m_windowSize) {
- m_bufferInitCounter++;
- }
-
- // now calculate Mean
- double absMean = 0;
- for (int i = 0; i < m_bufferInitCounter; i++) {
- double sampleVal = m_buffer[i];
- absMean += Math.sqrt(sampleVal * sampleVal);
- }
- m_adaptiveThresholdPart = absMean / m_bufferInitCounter;
- }
-
- // for reset this class reset
- public void reset() {
- m_bufferCounter = 0;
- m_bufferInitCounter = 0;
- m_buffer = new double[m_windowSize];
- for (int i = 0; i < m_windowSize; i++) {
- m_buffer[i] = 0;
- }
- }
- }
|