/* 样本的数据最大值 */ private double SampleMaxData(WaveSampleInfo info) { if (info.maxIndex >= m_listData.Count) { return(0.0); } return(m_listData[(int)info.maxIndex]); }
/* 样本数据迭代器 */ private IEnumerable <double> SampleDataIterator(WaveSampleInfo info) { uint i = 0; for (i = info.startIndex; i <= info.endIndex; ++i) { yield return(m_listData[(int)i]); } }
/* 样本的平均值 */ private double SampleDataAvg(WaveSampleInfo info) { uint dataNum = SampleDataCount(info); if (dataNum <= 0) { return(0.0); } return(info.sum / dataNum); }
/* 样本时间(ms) */ private double SampleTime(WaveSampleInfo info) { if (info.startIndex >= info.endIndex) { return(0U); } uint dataNum = info.endIndex - info.startIndex + 1; return(SAMPLE_TIME * dataNum); }
/* 样本的数据个数 */ private uint SampleDataCount(WaveSampleInfo info) { if (info.startIndex >= info.endIndex) { return(0U); } uint dataNum = info.endIndex - info.startIndex + 1; return(dataNum); }
/* 检测样本是否有效 */ private bool SampleIsValid(WaveSampleInfo info) { if ((info.startIndex >= info.endIndex) || (info.startIndex >= m_listData.Count) || (info.endIndex >= m_listData.Count) || (info.minIndex >= m_listData.Count) || (info.maxIndex >= m_listData.Count)) { return(false); } return(true); }
/* 样本的方差(代表样本的离散程度,越小表示越集中) */ private double SampleDataVariance(WaveSampleInfo info) { /* 取得样本均值 */ double sampleAvg = SampleDataAvg(info); /* 计算方差 */ double varianceSum = 0.0; uint i = 0; for (i = info.startIndex; i <= info.endIndex; ++i) { double d = m_listData[(int)i] - sampleAvg; varianceSum += (d * d); } double variance = 0.0; if (i > 0) { variance = varianceSum / i; } return(variance); }
/* 输入数据 */ 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; } } }