代码之家  ›  专栏  ›  技术社区  ›  CircAnalyzer

C++模拟脉冲序列

  •  1
  • CircAnalyzer  · 技术社区  · 6 年前

    我想在我的应用程序中模拟一个脉冲波形 他们的顺序。从下图来看,我想做的是 模拟前三个脉冲(脉冲1-3),然后模拟脉冲4 紧接着脉冲3,并模拟脉冲5紧接着脉冲4。 然后重复整个序列N次。

    第一个脉冲的开始时间(以秒为单位)和持续时间 每个脉冲也以秒为单位。我的应用程序将实时运行 在运行循环中,它将以1Hz的频率执行。

    我的问题是,我如何跟踪所有的脉冲并确保 它们都是相互模拟的?我说的模拟是, 例如,我想在测试期间打印一个简单的语句 前三个脉冲,第四和第五个脉冲也一样。有人能推荐一个伪脉冲吗 至少对这个操作来说是算法?

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  1
  •   Hiroki    6 年前

    定义类 sequence 下面,我们可以简单地通过 sequence::isActive . DEMO is here.

    class sequence
    {    
        int period_;
        int start_;
        int end_;
        std::array<std::pair<int,int>, 5> pulses;
    
    public:
        sequence(int start, int duration, int interval, int repeat) 
            : period_(2*interval+3*duration),
              start_(start),
              end_(start+repeat*period_),
              pulses
              {{
                  {0                    , duration             }, // pulse 1
                  {interval             , interval  +  duration}, // pulse 2
                  {2*interval           , 2*interval+  duration}, // pulse 3
                  {2*interval+  duration, 2*interval+2*duration}, // pulse 4
                  {2*interval+2*duration, period_              }  // pulse 5
              }}
        {
            if(duration <= 0){
                throw std::runtime_error("Duration must be positive integer.");
            }
    
            if(interval < 0){
                throw std::runtime_error("Interval must be non negative integer.");
            }
        }
    
        bool isActive(int time, std::size_t idx) const
        {
            const auto& pulse = pulses[idx];
    
            // 0 for each start time of sequence (pulse 1)
            const auto refTime = (time - start_)%period_;
    
            return (pulse.first <= refTime) && (refTime < pulse.second) && (time < end_);
        }
    
        int getPeriod() const{
            return period_;
        }
    
        int getStartTime() const{
            return start_;
        }
    
        int getEndTime() const{
            return end_;
        }
    
        std::size_t getPulseNum() const{
            return pulses.size();
        }
    };
    
        2
  •  0
  •   Jeremy Friesner    6 年前

    假设我正确地理解了这个问题,我的方法是使用模运算,将每个脉冲序列描述为布尔函数,并将时间戳作为函数的参数,例如:

    // Returns true iff the pulse is present at the specified time
    bool IsPulseActiveAtTime(long int theTime);
    

    这样做的好处是,你可以模拟一个无限系列的脉冲,而只使用一个小的,固定的内存量。如果您需要的话,它还允许您高效地查询每个脉冲序列在过去/将来的任何时间(即不仅仅是当前时间)的预期状态。

    下面是一个简单的演示程序,它在100个模拟“秒”的过程中打印出一个包含4个脉冲的股票行情磁带:

    #include <stdio.h>
    
    class PulseSequence
    {
    public:
       PulseSequence(long int startTime, long int duration, long int interval)
          : _startTime(startTime)
          , _duration(duration)
          , _interval(interval)
       {
          // empty
       }
    
       bool IsPulseActiveAtTime(long int theTime) const
       {
          if (theTime < _startTime) return false;
          return ((theTime-_startTime) % _interval) < _duration;
       }
    
    private:
       const long int _startTime;  // time at which the first pulse starts
       const long int _duration;   // length of each pulse
       const long int _interval;   // difference between the start-time of one pulse and the start-time of the next
    };
    
    // Unit test/example
    int main(int, char **)
    {
       const int NUM_PULSE_SEQUENCES = 4;
    
       const PulseSequence sequences[NUM_PULSE_SEQUENCES] = {
          PulseSequence(0, 3, 5),
          PulseSequence(1, 2, 6),
          PulseSequence(3, 3, 4),
          PulseSequence(5, 1, 3),
       };
    
       for (long int curTime = 0; curTime < 100; curTime++)
       {
          printf("curTime=%02li: [", curTime);
          for (int i=0; i<NUM_PULSE_SEQUENCES; i++) putchar(sequences[i].IsPulseActiveAtTime(curTime)?('0'+i):' ');
          printf("]\n");
       }
       return 0;
    }
    

    $ ./a.out
    curTime=00: [0   ]
    curTime=01: [01  ]
    curTime=02: [01  ]
    curTime=03: [  2 ]
    curTime=04: [  2 ]
    curTime=05: [0 23]
    curTime=06: [0   ]
    curTime=07: [012 ]
    curTime=08: [ 123]
    curTime=09: [  2 ]
    curTime=10: [0   ]
    curTime=11: [0 23]
    curTime=12: [0 2 ]
    curTime=13: [ 12 ]
    [...]