c++qttimerqelapsedtimer

Algorithm for masking time when the signal is above a threshold


I have a realtime signal from a sensor. I need logic to implement a masking time once the signal is above a threshold. As shown below:

enter image description here

Here the signal (in blue) crosses a threshold. And I need to mask for any checks for the threshold for a period (masking time). (In this way I can detect only the positive pulse, similarly, I have another check for negative pulse)

See the code below:

static QTime time(QTime::currentTime());
// calculate two new data points:
double key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
static double lastKey;

if(a_vertical> onThreshold && key-lastKey >0.2) // is this check correct for masking time?
    {
        ui->rdo_btn_vertical->show();
        ui->rdo_btn_vertical->setStyleSheet(StyleSheetOn1);
        lastKey = key;
    }
    else
    {
        ui->rdo_btn_vertical->setStyleSheet(StyleSheetOff1);
    }


I'm not sure if the expression inside IF statement is a correct way for implementing a masking time. Any thoughts/suggestions are welcome.

EDIT

Masking time: it's a time period masked for any threshold checking. This to differentiate positive and negative pulse. See below, during a postive pulse there is a negative-going side but it should not detect as a "negative pulse". That's why I implemented a masking time.

enter image description here


Solution

  • you can define 2 signal-slots that take care of starting, stopping the elapsed timer, fortunately, Qt has a class doing this for you, read the doc here and be aware of possible overflows and if required set QElapsedTimer::MonotonicClock

    QElapsedTimer timer;
    timer.start();
    slowOperation1();
    qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";
    

    and to trigger the signals

    if(sensorSignal>THRESHOLD_K)
        emit startTimer();
    else
        emit stopTimer();
        
    void startTimer()
    {   
        timer.start();
    }
    
    void stopTimer()
    {
    
        timer.elapsed();
    }