1

I'm using QCustomPlot to read and display realtime value from an IMU. This is how I set the realtimeDataSlot:

void Settings::realtimeDataSlot(double x_acceleration_g, double y_acceleration_g, double z_acceleration_g, double z_acceleration_gnew)
{
    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 lastPointKey = 0;
    if (key-lastPointKey > 0.02) // at most add point every 20 ms
    {
      // add data to lines:
        ui->customPlot->graph(0)->addData(key, x_acceleration_g); // X axis
        ui->customPlot->graph(1)->addData(key, y_acceleration_g); // Y axis
        ui->customPlot->graph(2)->addData(key, z_acceleration_g); // Z axis
        ui->customPlot->graph(3)->addData(key, z_acceleration_gnew);

      lastPointKey = key;
    }
    // make key axis range scroll with the data (at a constant range size of 8):
    ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
    ui->customPlot->replot();

    // calculate frames per second:
    static double lastFpsKey;
    static int frameCount;
    ++frameCount;
    if (key-lastFpsKey >2) // average fps over 2 seconds
    {
      ui->statusbar->showMessage(
            QString("%1 FPS, Total Data points: %2")
            .arg(frameCount/(key-lastFpsKey), 0, 'f', 0)
            .arg(ui->customPlot->graph(0)->data()->size()+ui->customPlot->graph(1)->data()->size())
            , 0);
      lastFpsKey = key;
      frameCount = 0;
    }
}

which shows me as follows:

enter image description here

As a next step, I need to detect the peaks in any axis, say for example in the above figure in the Y axis there are peak values which I need to detect and count. Can somebody show me a way to do this?

Thanks in advance

EDIT

I marked in the peaks following figure:

I define peak as the figure that value (positive values) more than 0.25 g at high rate.

enter image description here

tetra
  • 177
  • 3
  • 11
  • 1
    Define "peak" value please. Local maximum? Global maximum? Do you need to dampen because of input jitter (because otherwise "flat" signals easily get a "peak" there)? – Yunnosch Mar 05 '20 at 11:13
  • Maybe you could put little marks into the picture, identifying the values you consider "peak". – Yunnosch Mar 05 '20 at 11:14
  • Are negative values possible peaks? – Yunnosch Mar 05 '20 at 11:15
  • @Yunnosch Thank you. I just edited the question as per your feedback – tetra Mar 05 '20 at 11:31
  • 1
    " if (key-lastPointKey > 0.02) // at most add point every 2 ms". What units is this... sounds a lot like 20 ms (not 2ms)! – Unapiedra Mar 05 '20 at 11:33
  • @Unapiedra Yes! that was a typo, it is 20ms. Thanks for pointing it out – tetra Mar 05 '20 at 11:38
  • This question could be improved. It is not completely bad but also not great (from my perspective of wanting to answer it for you). You are probably aware of [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). Specifically here, your question is not about QCustomPlot but about finding peaks algorithmically. A simple small main program (with some data) that I can copy and paste would allow me to quickly answer your question. (See next comment about some terms for the algorithms you want.) – Unapiedra Mar 05 '20 at 11:39
  • Please take a look at "non-maxima suppression" algorithms. You also want to read on filters and signal processing although I can't say right now more specifics (these terms are too broad to be useful to you). – Unapiedra Mar 05 '20 at 11:42
  • related: https://dsp.stackexchange.com/questions/tagged/peak-detection – Unapiedra Mar 05 '20 at 12:00
  • @vtc do you have example data? – Unapiedra Mar 05 '20 at 12:22
  • @Unapiedra. Thank you for the feedback. what my requirement is to do a realtime peak detection. for ex. , `acceleration_g[1]` gives me realtime values of `Y` axis. – tetra Mar 05 '20 at 12:48

1 Answers1

1

How to do it for the y-axis:

  1. you define a window size on the x-axis, say 5 x-values of 100
  2. let that window move from start to end of the x-axis, which means: for the first measure, look at the x-values number 0,1,2,3,4 and for the second measure, look at the x-values number 1,2,3,4,5 and so on
  3. for each window measure: determine the maximum y-value in that window, and increase a score counter for the appropriate x-value.
  4. after the complete move of the window from start to end you need to find the x-values with the highest score counters.

The size of your window gives you the number of peaks.

Also do the same for the minimum values to find the negative peaks.

Take care at the start and end of the graph.

falkb
  • 1,294
  • 11
  • 35
  • though I don't know what it actually has to do with Qt and C++, but now you should be able to code it... :) – falkb Mar 05 '20 at 12:58
  • Since the algorithm is clear now, you should be able to code it by yourself, if you are a programmer. – falkb Mar 05 '20 at 15:10
  • @vtc: Look at https://stackoverflow.com/questions/45975523/finding-the-peak-number-of-an-array-in-c which provides several peak finder implementations in C. Hope that helps. Is your question answered now? See you and Good luck! – falkb Mar 06 '20 at 09:05
  • Thanks a lot for the feedback. In my case, the data is real time , for ex. calling `sample->acceleration_g[0]` gives me data at a rate of 100Hz. I'm beginner , not quite understood how to do all the steps you mentioned on a realtime data – tetra Mar 06 '20 at 12:27
  • If you want to process an infinite stream of incoming data, you must process the graph from currently last value back to a number of values. For example, if your current x-value is 1000, you look back to the range of e.g. x-value 800 to 1000 and do the peak algorithm for those 200 values. – falkb Mar 06 '20 at 12:46