Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting limit size to vector and overwriting the oldest value when limit reached
Forum Updated to NodeBB v4.3 + New Features

Setting limit size to vector and overwriting the oldest value when limit reached

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 681 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lukutis222
    wrote on last edited by lukutis222
    #1

    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?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • L lukutis222

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • L lukutis222

        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?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          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.

          SGaistS 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            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.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Kent-DorfmanK 1 Reply Last reply
            1
            • SGaistS SGaist

              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.

              Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #6

              @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

              SGaistS JonBJ 2 Replies Last reply
              0
              • Kent-DorfmanK Kent-Dorfman

                @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

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Kent-Dorfman QQueue is a glorified QList so it's all good :-)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • Kent-DorfmanK Kent-Dorfman

                  @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

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #8

                  @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-DorfmanK 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @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-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved