accurate sampling in c++

早过忘川 提交于 2019-12-19 04:13:24

问题


I want to sample values I get from a gpio 4000 times per second, currently I do something like that:

std::vector<int> sample_a_chunk(unsigned int rate, unsigned int block_size_in_seconds) {
    std::vector<std::int> data;
    constexpr unsigned int times = rate * block_size_in_seconds;
    constexpr unsigned int delay = 1000000 / rate; // microseconds
    for (int j=0; j<times; j++) {
      data.emplace_back(/* read the value from the gpio */);
      std::this_thread::sleep_for(std::chrono::microseconds(delay));
    }
    return data;
}

yet according to the reference sleep_for is guaranteed to wait for at least the specified amount of time.

How can I have my system wait for the exact amount of time, or at least achieve the best possible accuracy? How can I be sure of the time resolution of my system?


回答1:


I think the best you can probably achieve is to use absolute timing so as to avoid drift.

Something like this:

std::vector<int> sample_a_chunk(unsigned int rate,
    unsigned int block_size_in_seconds)
{
    using clock = std::chrono::steady_clock;

    std::vector<int> data;

    const auto times = rate * block_size_in_seconds;
    const auto delay = std::chrono::microseconds{1000000 / rate};

    auto next_sample = clock::now() + delay;

    for(int j = 0; j < times; j++)
    {
        data.emplace_back(/* read the value from the gpio */);

        std::this_thread::sleep_until(next_sample);

        next_sample += delay; // don't refer back to clock, stay absolute
    }
    return data;
}



回答2:


I would use boost::asio::deadline_timer.

#include <vector>

#define BOOST_ERROR_CODE_HEADER_ONLY 1
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

std::vector<int> sample_a_chunk(unsigned int rate, unsigned int block_size_in_seconds) {
  std::vector<int> data;
  const unsigned int times = rate * block_size_in_seconds;
  auto expiration_time = boost::posix_time::microsec_clock::local_time();
  const auto delay = boost::posix_time::microseconds(1000000/rate);
  boost::asio::io_service io;
  boost::asio::deadline_timer t(io);

  for (unsigned int j=0; j < times; j++) {
    expiration_time += delay;
    data.emplace_back(/* read the value from the gpio */);
    t.expires_at(expiration_time);
    t.wait();
  }
  return data;
}


来源:https://stackoverflow.com/questions/39987806/accurate-sampling-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!