Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lag threshold influence values #6

Open
nervusvagus opened this issue Sep 11, 2023 · 2 comments
Open

lag threshold influence values #6

nervusvagus opened this issue Sep 11, 2023 · 2 comments

Comments

@nervusvagus
Copy link

nervusvagus commented Sep 11, 2023

I have a sinewave from mpu9250 sensor which I need to calculate the max of during live sensor readings. I'm trying to understand how lag threshold influence values work in your library and what i should set them to. is there any other sources? In example they are peakDetection.begin(48, 3, 0.6); and have no idea why.

@frankcohen
Copy link

Just another user here... The library source is 142 lines long. I'd look there for how it works. And surprisingly ChatGPT answered my question "Thanks. A sample of the calculated magnitude values is: "99
305
-487
-125
112
-2457
-1204
638
861
-1032
-61
162
9
443
150
-361
-305
-216
50
-675
568
35
-348
6". What values should I use for lag, threshold, influence?" with this answer:

To determine the optimal values for lag, threshold, and influence, we need to analyze the provided data. Here's a breakdown of how to choose these parameters:

  1. Parameter Descriptions
    lag: Number of previous samples used for smoothing and calculating averages and standard deviations. It determines the window size for trend detection.
    threshold: The sensitivity multiplier for detecting peaks. Higher values make peak detection stricter, reducing false positives.
    influence: The weight given to new data when a peak is detected. Lower values make the algorithm less influenced by peaks and stabilize future trend calculations.
  2. **Analyzing the Data
    Given your sample values, let’s use the following guidelines:

Lag:
Based on your data, where peaks occur relatively quickly but some noise exists, start with a small window (e.g., 10 samples) to balance responsiveness and smoothing.
T
To determine the optimal values for lag, threshold, and influence based on the sample values you provided, let's analyze them step by step.

  1. Understanding the Parameters
    lag: The size of the sliding window used for smoothing. It defines how many previous samples are considered to compute the moving average and standard deviation.
    threshold: Controls how significant a deviation must be to qualify as a peak. A higher value reduces false positives.
    influence: Determines how much detected peaks influence the trend calculation. A lower value reduces the impact of peaks on subsequent calculations.
  2. Analyzing Your Data
    Your data contains large variations, with positive and negative values ranging from -2457 to 861. This suggests:

Lag: Peaks occur relatively quickly, so a lag of around 10 samples should work well.
Threshold: Given the wide range, a threshold of 2 to 3 (2-3 times the standard deviation) can help detect significant deviations.
Influence: To stabilize the trend while still adapting to peaks, set the influence to around 0.5.
3. Recommended Starting Values
Based on your data:

lag = 10: A small window for responsive peak detection.
threshold = 2.5: Moderate sensitivity to reduce noise-induced peaks.
influence = 0.5: Balanced impact of peaks on future calculations.
4. Code Implementation
Here’s how to initialize and test the algorithm with your data:

cpp
Copy code
#include
#include
#include
#include "PeakDetection.h"

std::vector magnitudes = {
99, 305, -487, -125, 112, -2457, -1204, 638, 861,
-1032, -61, 162, 9, 443, 150, -361, -305, -216,
50, -675, 568, 35, -348, 6
};

void setup() {
Serial.begin(115200);

// Initialize peak detection with chosen parameters
PeakDetection peakDetector;
peakDetector.begin(10, 2.5, 0.5); // lag=10, threshold=2.5, influence=0.5

for (double magnitude : magnitudes) {
    double filteredValue = peakDetector.add(magnitude);
    int peak = peakDetector.getPeak();

    Serial.print("Magnitude: ");
    Serial.print(magnitude);
    Serial.print(" | Filtered: ");
    Serial.print(filteredValue, 2);
    Serial.print(" | Peak: ");
    if (peak == 1) {
        Serial.println("Positive Peak Detected!");
    } else if (peak == -1) {
        Serial.println("Negative Peak Detected!");
    } else {
        Serial.println("No Peak");
    }
}

}

void loop() {
// Do nothing
}
5. Fine-Tuning
If the algorithm detects too many false positives or misses peaks:

Increase threshold to reduce false positives.
Decrease lag for quicker responsiveness.
Adjust influence to balance the impact of peaks.
These values should provide a good starting point for detecting peaks in your magnitude data. Let me know if you'd like help tuning further or analyzing the results!

@frankcohen
Copy link

By the way, ChatGPT's answer says to use "peakDetector.begin(10, 2.5, 0.5);" which is wrong, since the threshold is an int. ChatGPT is helpful and not perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants