首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脉冲序列的C++模拟

脉冲序列的C++模拟
EN

Stack Overflow用户
提问于 2018-10-31 04:10:35
回答 2查看 1.3K关注 0票数 1

我试图在我的应用程序中建模一个脉冲波形,我需要一种方法来跟踪脉冲,以便我可以重复它们的序列。从下图中,我想做的是模拟前三个脉冲(脉冲1-3),然后在脉冲3之后立即模拟脉冲4,在4之后立即模拟脉冲5,然后重复整个序列N次。

如图所示,我有以秒为单位的间隔,第一个脉冲的开始时间(以秒为单位),以及每个脉冲的持续时间(以秒为单位)。我的应用程序将在运行循环中实时运行,它将以1Hz的速度执行。

我的问题是,我如何跟踪所有的脉冲,并确保它们都是相互模拟的?我的意思是,例如,我想在前三个脉冲的持续时间内打印一条简单的语句,对于脉冲4和5也是这样。有人能为这个操作提供一个伪算法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-03 02:59:30

定义类sequence如下所示,我们可以通过sequence::isActive简单地检查每个脉冲的活动。DEMO is here.

代码语言:javascript
复制
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();
    }
};
票数 1
EN

Stack Overflow用户

发布于 2018-10-31 04:40:30

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

代码语言:javascript
复制
// Returns true iff the pulse is present at the specified time
bool IsPulseActiveAtTime(long int theTime);

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

下面是一个简单的演示程序,它在100秒的模拟过程中打印出一个由四个脉冲组成的刻录机磁带:

代码语言:javascript
复制
#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;
}

输出如下所示:

代码语言:javascript
复制
$ ./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 ]
[...]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53076170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档