Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查双阈值_C++_Qt - Fatal编程技术网

C++ 检查双阈值

C++ 检查双阈值,c++,qt,C++,Qt,我有一个基本问题是如何实现一个算法来检测实时信号的阈值 例如,如果我有一个如下所示的信号,我需要检查信号是否在固定的时间段(比如3秒)内超过两个阈值 我这里的问题是,将设置什么样的IF语句来检查固定时间段内的两个阈值 我已经得到的: 我可以检查单个阈值,如下所示: if(signal > onThreshold) { ui->rdo_btn_vertical->setStyleSheet(StyleSheetOn1); // change appe

我有一个基本问题是如何实现一个算法来检测实时信号的阈值

例如,如果我有一个如下所示的信号,我需要检查信号是否在固定的时间段(比如3秒)内超过两个阈值

我这里的问题是,将设置什么样的IF语句来检查固定时间段内的两个阈值

我已经得到的:

我可以检查单个阈值,如下所示:

if(signal > onThreshold)
   {    
       ui->rdo_btn_vertical->setStyleSheet(StyleSheetOn1); // change appearance of a radio button 
       QSound::play(":/res/beep1.wav"); // make a beep sound as well    
   }
   else
   {
       ui->rdo_btn_vertical->setStyleSheet(StyleSheetOff1);
   }

首先,我们创建一个检测向上交叉点的辅助对象:

class DetectUpwardsCrossing : public QObject {
    public:
        DetectUpwardsCrossing(double threshold, QObject *parent = nullptr) : QObject(parent), threshold(threshold), current(qSNaN()) {}
        signals:
            void crossing();
        public slots:
            void onSignal(double value) {
                if (qIsNaN(current)) {
                    current = value;
                    return;
                }

                if (current == value) {
                    return;
                }

                bool before = current <= threshold;
                current = value;
                bool after = current >= threshold;
                if (before && after) {
                    emit crossing();
                }
            }
    private:
        double threshold, current;
};
最后一块是间隔3秒的
QTimer*crossingTimer
,其
timeout
信号连接到
crossingTimerExpire


通过使用这样的计时器,您可以确保当您突然不再接收信号时,显示器会关闭。

首先,我们创建一个检测向上交叉点的帮助器:

class DetectUpwardsCrossing : public QObject {
    public:
        DetectUpwardsCrossing(double threshold, QObject *parent = nullptr) : QObject(parent), threshold(threshold), current(qSNaN()) {}
        signals:
            void crossing();
        public slots:
            void onSignal(double value) {
                if (qIsNaN(current)) {
                    current = value;
                    return;
                }

                if (current == value) {
                    return;
                }

                bool before = current <= threshold;
                current = value;
                bool after = current >= threshold;
                if (before && after) {
                    emit crossing();
                }
            }
    private:
        double threshold, current;
};
最后一块是间隔3秒的
QTimer*crossingTimer
,其
timeout
信号连接到
crossingTimerExpire


通过使用这样的计时器,您可以确保当突然不再接收信号时,显示器会关闭。

简而言之:当超过阈值时,将时间戳保存到变量。再次交叉时,对照时间戳检查当前时间。将问题分解为检查(我想是向上)阈值交叉的部分,然后按照@Thomas的建议添加一个检查时间戳的部分。@Botje Hi,你能用伪代码向我展示逻辑吗?简单地看一下示例:当阈值交叉时,将时间戳保存到变量。再次交叉时,根据时间戳检查当前时间。将问题分解为检查(我想是向上)阈值交叉的部分,然后添加一个检查时间戳的部分,如@Thomas所建议的那样。@Botje Hi,你能用伪代码向我展示逻辑吗?请看示例