简体   繁体   English

具有延时的简单输入输出

[英]simple input-output with time delay

Let's say I have an input signal, could be a random double: 假设我有一个输入信号,可以是一个随机双精度数:

while(true){
  double t; // = current time, let's assume I know that
  double input = rand();
}

I want to generate an output signal that simply applies a 0.5 sec time delay (a 0.5sec dead time in signal processing terms.) 我想生成一个输出信号,该信号仅施加0.5秒的时间延迟(就信号处理而言为0.5秒的停滞时间)。

while(true){
  double input = rand();
  // in pseudocode double output(t) = input(t-0.5)
}

I was thinking about storing the input in a vector, along with a time stamp in another vector, and then look up output = input(0.5sec ago). 我当时正在考虑将输入与一个时间戳一起存储在一个向量中,然后查找output = input(0.5秒前)。 However, that seems very inefficient. 但是,这似乎效率很低。

What's an appropriate data structure for this type of problem? 对于此类问题,合适的数据结构是什么? (A buffer that let's me recall a value that was stored 0.5 sec ago and discards recorded values that are further in the past than the chosen time delay) (让我回忆一下一个存储在0.5秒前的值的缓冲区,该缓冲区丢弃了过去比选定的时间延迟还远的记录值)

The struct you use to store data should have a timestamp (either expiry or the moment it was enqueued) along with the double value. 用于存储数据的结构应带有时间戳记(到期或入队的时间)以及double值。

The data structure to store the structs should be a priority queue (sorted on timestamp). 存储结构的数据结构应为优先级队列(按时间戳排序)。

The consumer thread should sleep for n milliseconds where n is initialized to 500ms. 使用者线程应休眠n毫秒,其中n被初始化为500ms。

When the consumer pops the first item, it can check the second item and calculate n (the amount of time to sleep for the next iteration). 当消费者弹出第一个项目时,它可以检查第二个项目并计算n(下一次迭代的睡眠时间)。 Else it can sleep again for 500 ms. 否则它可以再次睡眠500毫秒。

Let me know if I should write code for it. 让我知道是否应该为此编写代码。

What immediately comes to mind is the Producer-Consumer Pattern . 立即想到的是生产者-消费者模式

Have the producer push the input to a std::queue and every 0.5 seconds (using a std::thread ) have the consumer pop from it. 让生产者将输入推送到std::queue然后每0.5秒(使用std::thread )从其弹出消费者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM