Example #1
0
        private readonly double STOP_FLOW_THRESHOLD = 0.01; // 停止检测流量阈值,当启动测试后如果流量对值小于阈值,则开始检测停止条件

        public FlowValidator(double sampleRate, double calVolume = 1.0)
        {
            SAMPLE_RATE = sampleRate;
            SAMPLE_TIME = 1000 / sampleRate;

            m_waveAnalyzer = new WaveAnalyzer(sampleRate,
                                              START_SAMPLE_COUNT, START_FLOW_DELTA,
                                              STOP_SAMPLE_COUNT, STOP_FLOW_THRESHOLD);

            CalVolume = calVolume;

            m_waveAnalyzer.SampleStarted += (uint dataIndex, WaveSampleDirection direction) =>
            {
                /* 转换为呼吸方向 */
                RespireDirection respireDir = ToRespireDirection(direction);
                SampleStarted?.Invoke(dataIndex, respireDir);
            };

            m_waveAnalyzer.SampleStoped += (uint dataIndex, WaveSampleDirection direction, uint sampleIndex) =>
            {
                /* 转换为呼吸方向 */
                RespireDirection respireDir = ToRespireDirection(direction);
                SampleStoped?.Invoke(dataIndex, respireDir, sampleIndex);
            };
        }
 public async Task When(SampleStarted e)
 {
     await _writer.Add(e.Id, new SampleView
     {
         Id     = e.Id,
         Name   = e.Name,
         Status = SampleStatus.Draft
     });
 }
Example #3
0
 protected override void Given()
 {
     base.Given();
     _sampleId = Guid.NewGuid();
     _ev       = new SampleStarted
     {
         Id   = _sampleId,
         Name = "Foo"
     };
 }
Example #4
0
        /* 输入数据 */
        public void Input(double data)
        {
            /* 最新数据索引值 */
            uint dataIndex = (uint)m_listData.Count;

            /* 记录数据到队列 */
            m_listData.Add(data);

            /* 如果是正在采样状态 */
            if ((m_state > State.WaitStartSample) &&
                (m_state < State.SampleStop))
            {
                /* 统计和值 */
                m_dataSum += data;

                /* 检测采样停止条件 */
                if (Math.Abs(data) < STOP_THRESHOLD)
                {
                    if (m_waveStatistician.SampleCount >= STOP_SAMPLE_COUNT)
                    {
                        /* 检测采样停止条件 */
                        double delta = m_waveStatistician.Delta(data);
                        if (Math.Abs(delta) < START_DELTA)
                        {
                            /* 重置波动统计器 */
                            m_waveStatistician.Reset();

                            /* 记录结束采样点Index */
                            m_endIndex = dataIndex;

                            /* 统计最大/最小值点(索引) */
                            if (data < m_listData[(int)m_minDataIndex])
                            {
                                m_minDataIndex = dataIndex;
                            }
                            if (data > m_listData[(int)m_maxDataIndex])
                            {
                                m_maxDataIndex = dataIndex;
                            }

                            /* 样本方向 */
                            WaveSampleDirection direction = (m_state == State.PositiveSample) ?
                                                            WaveSampleDirection.PositiveSample : WaveSampleDirection.NegativeSample;

                            /* 进入[采样停止]状态 */
                            SetState(State.SampleStop);

                            /* 记录当前样本信息 */
                            WaveSampleInfo info = new WaveSampleInfo()
                            {
                                startIndex = m_startIndex,
                                endIndex   = m_endIndex,
                                minIndex   = m_minDataIndex,
                                maxIndex   = m_maxDataIndex,
                                sum        = m_dataSum
                            };
                            m_sampleInfoList.Add(info);

                            /* 触发采样结束事件 */
                            uint sampleIndex = (uint)(m_sampleInfoList.Count - 1);
                            SampleStoped?.Invoke(m_endIndex, direction, sampleIndex);
                        }
                        else
                        {
                            /* 统计波动范围 */
                            m_waveStatistician.Input(data);
                        }
                    }
                    else
                    {
                        /* 统计波动范围 */
                        m_waveStatistician.Input(data);
                    }
                }
                else
                {
                    /* 重置波动统计器 */
                    m_waveStatistician.Reset();
                }
            }

            switch (m_state)
            {
            case State.Reset:     // [复位]状态
            {
                /* 进入[等待启动采样]状态 */
                SetState(State.WaitStartSample);

                /* 开始统计波动范围 */
                m_waveStatistician.Input(data);
                break;
            }

            case State.WaitStartSample:     // [等待启动采样]状态
            {
                if (m_waveStatistician.SampleCount >= START_SAMPLE_COUNT)
                {
                    /* 检测启动条件 */
                    double delta = m_waveStatistician.Delta(data);
                    if (Math.Abs(delta) > START_DELTA)
                    {
                        /* 记录启动采样点Index */
                        m_startIndex = dataIndex - 1;

                        /* 初始化最小/最大值点 */
                        m_minDataIndex = m_startIndex;
                        m_maxDataIndex = m_startIndex;
                        if (data < m_listData[(int)m_minDataIndex])
                        {
                            m_minDataIndex = dataIndex;
                        }
                        if (data > m_listData[(int)m_maxDataIndex])
                        {
                            m_maxDataIndex = dataIndex;
                        }

                        /* 初始化和值 */
                        m_dataSum = (m_listData[(int)m_startIndex] + data);

                        /* 重置波动统计器 */
                        m_waveStatistician.Reset();

                        /* 记录启动类型(是否正方向启动) */
                        if (delta > START_DELTA)
                        {         //正方向采样开始
                            /* 进入[正方向采样]状态 */
                            SetState(State.PositiveSample);

                            /* 方向: 正方向采样 */
                            WaveSampleDirection direction = WaveSampleDirection.PositiveSample;

                            /* 触发(正方向)采样开始事件 */
                            SampleStarted?.Invoke(m_startIndex, direction);
                        }
                        else
                        {         // 负方向采样开始
                            /* 进入[负方向采样]状态 */
                            SetState(State.NegativeSample);

                            /* 方向: 负方向采样 */
                            WaveSampleDirection direction = WaveSampleDirection.NegativeSample;

                            /* 触发(负方向)采样开始事件 */
                            SampleStarted?.Invoke(m_startIndex, direction);
                        }
                    }
                    else
                    {
                        /* 统计波动范围 */
                        m_waveStatistician.Input(data);
                    }
                }
                else
                {
                    /* 统计波动范围 */
                    m_waveStatistician.Input(data);
                }
                break;
            }

            case State.PositiveSample:     // [正方向采样]状态
            {
                /* 统计最大/最小值点 */
                if (data < m_listData[(int)m_minDataIndex])
                {
                    m_minDataIndex = dataIndex;
                }
                if (data > m_listData[(int)m_maxDataIndex])
                {
                    m_maxDataIndex = dataIndex;
                }
                break;
            }

            case State.NegativeSample:     // [负方向采样]状态
            {
                /* 统计最大/最小值点 */
                if (data < m_listData[(int)m_minDataIndex])
                {
                    m_minDataIndex = dataIndex;
                }
                if (data > m_listData[(int)m_maxDataIndex])
                {
                    m_maxDataIndex = dataIndex;
                }
                break;
            }

            case State.SampleStop:     // [采样停止]状态
            {
                // do nothing
                break;
            }
            }
        }
Example #5
0
 private void When(SampleStarted e)
 {
     Id     = e.Id;
     Status = SampleStatus.Draft;
 }