Setting limit size to vector and overwriting the oldest value when limit reached
-
Hello. In my project, I am getting sensor readings every 1 second and would like to calculate 1 minute / 30 minute / 60 minute average.
For now, I focus on getting 1 minute average.
My initial idea was to create a vector and update it every second. Once it fills up to 60 (since I take samples every 1 second and 1 minute average will contain 60 samples), I would then start to overwrite oldest data and recalculate the average.
std::vector< float > current_1_min_avg;
//sensor data received and put into variable "value" current_1_min_avg.push_back(value);
With this method, I could not find a way to detect that I have already received 60 samples. 61'th sample should overwrite oldest data at position 0 and recalculate average. Then 62'th sample should overwrite data at position 1 and so on...
What is the most efficient way to do this?
-
Hello. In my project, I am getting sensor readings every 1 second and would like to calculate 1 minute / 30 minute / 60 minute average.
For now, I focus on getting 1 minute average.
My initial idea was to create a vector and update it every second. Once it fills up to 60 (since I take samples every 1 second and 1 minute average will contain 60 samples), I would then start to overwrite oldest data and recalculate the average.
std::vector< float > current_1_min_avg;
//sensor data received and put into variable "value" current_1_min_avg.push_back(value);
With this method, I could not find a way to detect that I have already received 60 samples. 61'th sample should overwrite oldest data at position 0 and recalculate average. Then 62'th sample should overwrite data at position 1 and so on...
What is the most efficient way to do this?
@lukutis222 said in Setting limit size to vector and overwriting the oldest value when limit reached:
I could not find a way to detect that I have already received 60 samples
What about calling https://en.cppreference.com/w/cpp/container/vector/size ?
"What is the most efficient way to do this?" - keep a growing counter for incoming samples. Then, to access your vector calculate index as modulo 60:
int index = sampleNumber % 60; current_1_min_avg[index] = sample;
Of course you need to resize the vector at the beginning, so it can hold 60 elements (https://en.cppreference.com/w/cpp/container/vector/resize).
-
Hello. In my project, I am getting sensor readings every 1 second and would like to calculate 1 minute / 30 minute / 60 minute average.
For now, I focus on getting 1 minute average.
My initial idea was to create a vector and update it every second. Once it fills up to 60 (since I take samples every 1 second and 1 minute average will contain 60 samples), I would then start to overwrite oldest data and recalculate the average.
std::vector< float > current_1_min_avg;
//sensor data received and put into variable "value" current_1_min_avg.push_back(value);
With this method, I could not find a way to detect that I have already received 60 samples. 61'th sample should overwrite oldest data at position 0 and recalculate average. Then 62'th sample should overwrite data at position 1 and so on...
What is the most efficient way to do this?
@lukutis222 said in Setting limit size to vector and overwriting the oldest value when limit reached:
and recalculate the average.
What is the most efficient way to do this?
How do you recalculate the average when a new sample arrives? Do you do it by recalculating from scratch visiting the 60 (latest) samples?
If you wish to be most efficient there are better ways which do not revisit the samples. Won't matter hugely on only 60 samples and every minute, but theoretically as the recalculations become more frequent. Do you care about that?
-
Well, the first thing that comes to mind is you're using the wrong container for that purpose. Use list<> instead and limit the length of the list when it gets to 60 entries.
-
Well, the first thing that comes to mind is you're using the wrong container for that purpose. Use list<> instead and limit the length of the list when it gets to 60 entries.
Hi,
As @Kent-Dorfman wrote, not the best container.
Even simpler, use a QQueue so you just have to call dequeue once it reaches the limit size.
If you want to stay with the stl, there's std::queue.
-
Hi,
As @Kent-Dorfman wrote, not the best container.
Even simpler, use a QQueue so you just have to call dequeue once it reaches the limit size.
If you want to stay with the stl, there's std::queue.
@SGaist said in Setting limit size to vector and overwriting the oldest value when limit reached:
If you want to stay with the stl, there's std::queue.
Reasons I suggest list is that there is a need to iterate over the list on each addition of an element to recalculate. I don't believe queue supports such.
But this could open a discussion of other more optimized methods when container size is kept sane...which I'd like to avoid. LOL
-
@SGaist said in Setting limit size to vector and overwriting the oldest value when limit reached:
If you want to stay with the stl, there's std::queue.
Reasons I suggest list is that there is a need to iterate over the list on each addition of an element to recalculate. I don't believe queue supports such.
But this could open a discussion of other more optimized methods when container size is kept sane...which I'd like to avoid. LOL
@Kent-Dorfman QQueue is a glorified QList so it's all good :-)
-
@SGaist said in Setting limit size to vector and overwriting the oldest value when limit reached:
If you want to stay with the stl, there's std::queue.
Reasons I suggest list is that there is a need to iterate over the list on each addition of an element to recalculate. I don't believe queue supports such.
But this could open a discussion of other more optimized methods when container size is kept sane...which I'd like to avoid. LOL
@Kent-Dorfman said in Setting limit size to vector and overwriting the oldest value when limit reached:
Reasons I suggest list is that there is a need to iterate over the list on each addition of an element to recalculate
I suggested earlier you can avoid this if you wish to be efficient. You would need to be able to read the oldest sample you are overwriting/discarding.
-
@Kent-Dorfman said in Setting limit size to vector and overwriting the oldest value when limit reached:
Reasons I suggest list is that there is a need to iterate over the list on each addition of an element to recalculate
I suggested earlier you can avoid this if you wish to be efficient. You would need to be able to read the oldest sample you are overwriting/discarding.
@JonB said in Setting limit size to vector and overwriting the oldest value when limit reached:
I suggested earlier you can avoid this if you wish to be efficient. You would need to be able to read the oldest sample you are overwriting/discarding.
MUST...FIGHT...URGE...TO...RESPOND!...but you read my mind. LOL