using CommonLib.LOG; using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using NWaves.FeatureExtractors.Options; using NWaves.FeatureExtractors; using NWaves.FeatureExtractors.Base; using NWaves.FeatureExtractors.Multi; namespace CommonLib.DataStruct { public class ErrorArgs:EventArgs { public string ErrorMessage { get; private set; } public ErrorArgs(string ErrorMessage) { this.ErrorMessage = ErrorMessage; } } public class FFTRingArray : RingArray { public Object Tag = 0; public EventHandler ErrorNotify; /// /// 能量计算点 /// int[] powerPoints = { 1720, 3840 }; /// /// 能量计算范围 /// int[] powerPointsSize = { 10, 10 }; /// /// RMS计算起始点 /// int[] RMSPointBeginPoint = { 3800, 2500, 3250, 4400 }; /// /// RMS计算结束点 /// int[] RMSPointEndPoint = { 4000, 3000, 3750, 4900 }; public FFTRingArray(int capacity) : base(capacity) { } public double[] AddData(double[] node) { int len1 = powerPoints.Length; int len2 = RMSPointEndPoint.Length; double[] Results = new double[len1 + len2]; Boolean bResult = false; Stopwatch watch = new Stopwatch(); watch.Start(); try { for (int i = 0; i < len1; i++) { Results[i] = calPower(node, powerPoints[i], i); } for (int i = 0; i < len2; i++) { Results[i + len1] = calRMS(node, i); } bResult = base.EnQueue(Results); if (IsFull()) { Boolean[] result = checkError(); for (int i = 0; i < result.Length; i++) { if (!result[i]) { ErrorArgs e = new ErrorArgs("出现故障!"); if(ErrorNotify != null) { ErrorNotify.Invoke(this, e); } // ErrorNotify?.Invoke(this, e); } } } } catch (Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message + ex.StackTrace); } double t = watch.ElapsedMilliseconds; string info = string.Format("计算一次消耗的时间为{0}ms", t.ToString("F3"));//$"计算一次消耗的时间为{t.ToString("F3")}ms"; // LogStorage.Logs.Add(LogLevel.Trace, info); return Results; } /// /// 求均方值 /// /// /// /// /// private double calRMS(double[] node, int index) { //int[] RMSPoints = { 151, 701, 1001, 2001 }; //int[] RMSPointEndPoint = { 750, 1400, 2000, 3000 }; int min = RMSPointBeginPoint[index] - 1; int max = RMSPointEndPoint[index] - 1; double result = 0; for (int i = min; i < max; i++) { result += Math.Pow(node[i], 2); } int size = max - min + 1; result /= size; result = Math.Sqrt(result); return result; } /// /// 计算能量点, /// /// 输入数据源 /// 此点的下标从1开始 /// 计算点位 private double calPower(double[] node, int Point, int index) { int min = Point - powerPointsSize[index] - 1; int max = Point + powerPointsSize[index]; double result = 0; for (int i = min; i < max; i++) { result += Math.Pow(node[i], 2); } return result; } public double[] Threshold = new double[6]{ 1,1,1,1,1,1}; private Boolean[] checkError() { int MatrixLen, MatrixDimension; MatrixLen = this.capacity; MatrixDimension = queue[0]!=null? queue[0].Length:0; //6个 Boolean[] result = new Boolean[MatrixDimension]; int i=0, j=0; double[] avr = new double[MatrixDimension]; try { for (i = 0; i < MatrixDimension; i++) { for (j = 0; j < MatrixLen; j++) { avr[i] += queue[j][i]; //转置 } avr[i] /= MatrixLen; result[i] = avr[i] < Threshold[i]; } } catch (Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message + ex.StackTrace); } return result; } /* private void checkError() { int MatrixLen, MatrixDimension; MatrixLen = this.capacity; // MatrixDimension = queue[0]!=null? queue[0].Length:0; MatrixDimension = 2; //抽取了两个 double[,] Maxtrix = new double[MatrixLen, MatrixDimension]; int i, j, pos; try { for ( i = 0; i < MatrixLen; i++) { pos = (i + head) % capacity; for ( j = 0; j < MatrixDimension; j++) { // Maxtrix[i, j] = queue[pos][j]; Maxtrix[i, j] = queue[pos][j==0?0:3]; } } checkError(Maxtrix); } catch(Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message+ex.StackTrace); } } */ } }