Detecting peaks in sensor values

Hey guys!
Ive got a mind blank that I figure someone could maybe help me out with:

Im working on a project that detects rapid air pressure changes using a BME280 and would "trigger" an external device to "do something" for a film prop whenever it detects a spike. Ive posted a screenshot and some descriptors that would describe when I want it to trigger.
I cant work out a good way of filtering this to get a "fire now" case. If I just go "fire after the sensor detects higher than this number" it would be unstable with any drift (and i'm getting a bit over time). If I also said just detect the next highest value in a time array of the graph, then I would get 2 firing points from the peak rising and falling. Also this has to be real-time, or close too.
I might have over explained it a bit, my apologies.

Any ideas? It doesn't have to happen immediately at the peak, but somewhere close.
Here is my current code ("val" in the loop being the number I'm sampling to get the spikes):

#include <Wire.h>

#include "BlueDot_BME280.h"
BlueDot_BME280 bme280 = BlueDot_BME280();
//

//

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


  //0:        Set to 0 for I2C (default value)
  //1:        Set to 1 for Software SPI
  //2:        Set to 2 for Hardware SPI
    bme280.parameter.communication = 0;                  //Choose communication protocol
    bme280.parameter.I2CAddress = 0x76;                  //Choose I2C Address
 
  //Set the pins for SPI Communication
  //Or ignore this, if you're using I2C Communication instead
    bme280.parameter.SPI_cs = 10;                          //Are you using either Software SPI or Hardware SPI? Then you need to define the Chip Select Pin.
    bme280.parameter.SPI_mosi = 13;                       //If you are using Software SPI, then you need to define the MOSI line. For Hardware SPI you can leave this line commented.
    bme280.parameter.SPI_miso = 11;                       //If you are using Software SPI, then you need to define the MISO line. Just comment this out for Hardware SPI.
    bme280.parameter.SPI_sck = 12;                        //If you are using Software SPI, then you need to define the SCK line. Same as before. For Hardware SPI, just comment this out.
 
  //0b00:     In sleep mode no measurements are performed, but power consumption is at a minimum
  //0b01:     In forced mode a single measured is performed and the device returns automatically to sleep mode
  //0b11:     In normal mode the sensor measures continually (default value)
    bme280.parameter.sensorMode = 0b11;                   //Choose sensor mode
    
  //0b000:      factor 0 (filter off)
  //0b001:      factor 2
  //0b010:      factor 4
  //0b011:      factor 8
  //0b100:      factor 16 (default value)
    bme280.parameter.IIRfilter = 0b000;                    //Setup for IIR Filter

  //0b000:      factor 0 (Disable humidity measurement)
  //0b001:      factor 1
  //0b010:      factor 2
  //0b011:      factor 4
  //0b100:      factor 8
  //0b101:      factor 16 (default value)
    bme280.parameter.humidOversampling = 0b000;            //Setup Humidity Oversampling

  //0b000:      factor 0 (Disable temperature measurement)
  //0b001:      factor 1
  //0b010:      factor 2
  //0b011:      factor 4
  //0b100:      factor 8
  //0b101:      factor 16 (default value)
    bme280.parameter.tempOversampling = 0b000;             //Setup Temperature Ovesampling

  //0b000:      factor 0 (Disable pressure measurement)
  //0b001:      factor 1
  //0b010:      factor 2
  //0b011:      factor 4
  //0b100:      factor 8
  //0b101:      factor 16 (default value)
    bme280.parameter.pressOversampling = 0b001;            //Setup Pressure Oversampling 


  
  if (bme280.init() != 0x60)
    while(1);
  }



void loop() {


float mesure_2 = bme280.readPressure();
delayMicroseconds(500);
float mesure_1 = bme280.readPressure();

float val = 0;
val = (mesure_2 * mesure_1);

Serial.println(val);

}

A screenshot of the spikes id like it to fire on attached.

Cheers!
Tim

OP's pic: Image posting guide

Google "peak detection algorithms" (or code) for several ideas.

To take drift into account, you need to subtract some sort of moving average background. A median filter works well to ignore occasional large spikes in the data; a simple moving average may not work.

If the difference between the last and the current value is bigger than a specific amount, fire the trigger. To not fire to often, you might deactivate the trigger for some time after it actually triggered.

This algorithm would trigger on the smaller peak in front of the third peak. If that's not acceptable, you have to define in much more detail what actually should trigger and how you think you can detect that the peak is just a smaller peak in front of a bigger peak.

Hey guys thanks for your replies.
Peak detection or falling/rising edge detection were my first ideas. I have yet to find any example that works with this waveform, for one reason or another.
Im sure there is a simple quick and dirty way of doing it, Im just not sure what.
I need to make some sort of "noise gate" that moves with the floor of the drift.

That little peak will be alright. The amount of time my "action" takes would overlap and not bother it at all.

Is anyone aware of any peak detection examples that you may think would work with the sample?

Is anyone aware of any peak detection examples that you may think would work with the sample?

Yes, there are several posted on the web. It is a very common problem with lots of good solutions. Yours is easy.

jremington:
Yes, there are several posted on the web. It is a very common problem with lots of good solutions. Yours is easy.

Are you able to throw a link up? I rarely ever ask for help, but I've honestly not been able to find one that would work with my waveform

The code in your first post obviously will not do anything useful.

Post code (using code tags) for other algorithms that you have tried, and explain what goes wrong. Should be easy to fix.

Generally, forum members do not want to write original code to solve posted problems. We expect you to do some research and show some understanding of the basic problem. If you do want to have someone write code, post in the Gigs and Collaborations forum section. You may be asked to pay for the help.

Thanks for the answers guys but I managed to get an algorithm running. Turns out my issues were with Float vs Int. Dont rum and code :slight_smile: