Forum Rule: Always post complete source code & details to reproduce any issue!

# Thread: Store values for a period of time.

1. ## Store values for a period of time.

I need help with a solution to a problem.

I want to store some calculations of sensor values (peaks etc.) for a period of time say 2 milliseconds and when that peak "expires" I want to store it in another variable that saves the value for another 5ms or so.

I need to compare values that ocurred in the last ~2ms to the values that ocurred in the ~5ms before that.

I have no clue how to do it.

I'm still kind of new to programming so in need of guidance.

2. I'd time stamp all the values and store them in a FIFO buffer. Then it's easy to go through the FIFO and do any comparisons you want.

3. As @Jonr stated... you could timestamp...

But it will likely depend on your specific desired requirements...
That is how often are you doing samples? For example suppose you are doing 100 per second.
So you find a peak on the 100th element of your 2 seconds period and save that away.

Do you want to hold onto that specific value or are wanting the peak for the last 2 seconds?
i.e. the last 200 samples? So after another 100 samples that 100th sample is no longer within the last 2 seconds but maybe the 120th one is which may now be the new peak? Or are you wanting to get a peak for two seconds, which hold onto and then do sampling for the next two seconds compute a new peak for those two seconds, which replaces the first one?

The solution for this somewhat subtle difference maybe very different. In first case you may need to hold onto all of the samples for that time periods, such that when the current Peak ages out you can compute the current new peak. Note: there are probably easy ways to remove of the entries as you go... like remember sub-peaks...

The second one is simply remember certain peaks for periods and age those out. Like the peak for each second for the last 5 seconds... And then simply look at the last two for the peak or two second peak ...
sorry for rambling here.

4. Originally Posted by KurtE
As @Jonr stated... you could timestamp...

But it will likely depend on your specific desired requirements...
That is how often are you doing samples? For example suppose you are doing 100 per second.
So you find a peak on the 100th element of your 2 seconds period and save that away.

Do you want to hold onto that specific value or are wanting the peak for the last 2 seconds?
i.e. the last 200 samples? So after another 100 samples that 100th sample is no longer within the last 2 seconds but maybe the 120th one is which may now be the new peak? Or are you wanting to get a peak for two seconds, which hold onto and then do sampling for the next two seconds compute a new peak for those two seconds, which replaces the first one?

The solution for this somewhat subtle difference maybe very different. In first case you may need to hold onto all of the samples for that time periods, such that when the current Peak ages out you can compute the current new peak. Note: there are probably easy ways to remove of the entries as you go... like remember sub-peaks...

The second one is simply remember certain peaks for periods and age those out. Like the peak for each second for the last 5 seconds... And then simply look at the last two for the peak or two second peak ...
sorry for rambling here.
Well I'm making an electronic drum.

What I really want to do is separate the real peaks from aftershocks.

I have a program that tracks peaks fairly accurately and it sets the threshold to decay just above the aftershocks after a peak is triggered. This means I can trigger a new sound maybe 8ms after the previous one even though the aftershocks are 60ms.

However I want to track the aftershocks as well. The aftershocks are an AC signal with peaks and valleys that decay for maybe 50-60ms. I want to track the decaying peaks and the slope of those peaks (how fast they are rising) and compare them to the real peaks to maybe cancel out an occasional aftershock that is mistaken for a peak by the peak tracking.

So what I want to do is capture the aftershock peaks and the slope of those peaks. However they are getting smaller and smaller. I need to make sure I compare the new peaks to the most recent aftershocks.

I have something that works but not something that is adaptable, it only works in my particular situation, if I change the tuning of the drum it may not work for instance.

5. If you can derive the BPM using heuristics (or know it up-front), and if the drums only hit at regular intervals, you could implement a timeout such that when a drum hit is detected it can't be re-detected until some portion of the timing to the next possible hit has elapsed.

If the peak-tracking algorithm gives you very reliable amplitude data (it analyzes every last sample and never gets anything wrong) then you might simply compute the difference between two peaks, and if that goes from "got smaller" to "got bigger" then you assume you have a new drum-hit.

If it is less than 100% accurate, you could average its results. For example, suppose you store the last six values it returns, bumping off any value more than six samples ago:
Code:
```254
222
109
91
188
204```
There is an obvious "bounce" between the first and last three elements. If you see a negative slope in the first three, and a positive slope in the last three, and the magnitude of the last element is substantially less (let's say <90%) of the first, then perhaps you have detected an aftershock. It might be possible to do this with only three values; or it might require many more, and you would have to balance that against how quickly you want to be able to respond to this condition.

As for how to store the values such that the oldest is rotated off, you would do this in a for-loop that overwrites each value with that of the next position in the array (except for the last), and then stores the newest-recorded value in the last position in the array.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•