C ++模拟脉冲序列 [英] C++ Simulate sequence of pulse trains

查看:145
本文介绍了C ++模拟脉冲序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to model a pulse waveform in my application and I need a way to keep track of the pulses so that I can repeat their sequence. From the figure shown below, what I'd like to do is simulate the first three pulses (pulse 1-3), then simulate pulse 4 immediately after pulse 3, and simulate pulse 5 immediately after 4. Then repeat the whole sequence N times.

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

As shown in the diagram, I have the interval in seconds, the start time of the first pulse in seconds, and the duration of each pulse also in seconds. My application will be running real time in a run loop where it will be executing at 1 Hz.

我的问题是,如何跟踪所有脉冲并确保 它们都是相互模拟的吗?我所说的模拟是 例如,我想在 第三个脉冲,第4个和第5个脉冲相同.有人可以建议使用伪脉冲吗? 算法至少要用于此操作?

My question is, how do I keep track of all the pulses and make sure they are all simulated with respect to each other? What I mean by simulated is, for example I'd like to print a simple statement during the duration of the 1st three pulses, and the same for pulse 4 and 5. Can someone suggest a pseudo algorithm at the very least for this operation?

推荐答案

如下定义类sequence,我们可以通过sequence::isActive简单地检查每个脉冲的活动. 演示在这里.

Defining the class sequence as follows, we can simply check activity of each pulse by 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();
    }
};

这篇关于C ++模拟脉冲序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆