public static double[] getFFTResult(int[] data, int dx, int len = 1024) { double[] result = new double[len]; double[] resulti = new double[len]; for (int i = 0; i < len; i++) { int index = i + dx; if (index >= 0 && index < data.Length) { result[i] = data[index]; } else { result[i] = 0; } resulti[i] = 0; } result = addHanning(result); TWFFT.FFT(result, resulti); double[] result_len = new double[len]; for (int i = 0; i < len; i++) { result_len[i] = Math.Sqrt(result[i] * result[i] + resulti[i] * resulti[i]); } return(result); }
/// <summary> /// 来自iDAU的计算请求 /// </summary> public static int CalcAndSave(int structid, int module, int channel, float fs, DateTime acqTime, float[] orgdata, out string err) { int affectCnt = 0; double sensitivity = 1; // 灵敏度系数 int formulaid = 0; // 计算公式 err = ""; var sensorid = DataAccessHelper.GetSensorId(structid, module, channel); if (sensorid == -1) { err = string.Format("传感器 {0}-{1}-{2} 不存在", structid, module, channel); return(0); } if (!DataAccessHelper.GetSensorParameter1(sensorid, out sensitivity, out formulaid)) { err = string.Format("传感器 {0}-{1}-{2} 参数不存在", structid, module, channel); return(0); } double[] freq, ampl; var org = new double[orgdata.Length]; for (int i = 0; i < org.Length; i++) { org[i] = orgdata[i]; } PhisicalTrans(ref org, sensitivity); if (formulaid == 21) // 包含滤波 { org = RemoveDcAndFilterWave(org); TWFFT.FFTData(org, fs, 1024, out ampl, out freq);//带平均的FFT } else { var tmp = (double[])org.Clone(); TWFFT.FFTData1(tmp, null, fs, out ampl, out freq); ampl[0] = 0; // 消除频谱中的直流分量 } CheckRedundancy(sensorid, acqTime); #if USE_BULK_COPY affectCnt = InsertDataBulk(sensorid, (int)fs, acqTime, org, freq, ampl); #else affectCnt = InsertData(sensorid, (int)fs, acqTime, org, freq, ampl); #endif return(affectCnt); }
/// <summary> /// 振动数据FFT计算/物理量转换 /// -- 来自数据接收的计算请求 /// </summary> /// <param name="sensorid">传感器ID</param> /// <param name="acqTime">采集时间</param> /// <param name="filepath">文件路径</param> /// <returns>共插入的振动数据个数(原始数据和FFT数据) -1:false </returns> public static int CalcAndSave(int sensorid, DateTime acqTime, string filepath) { int affectCnt = 0; double sensitivity = 1; // 灵敏度系数 int formulaid = 0; // 计算公式 var structId = DataAccessHelper.GetSensorStruct(sensorid); if (!DataAccessHelper.GetSensorParameter1(sensorid, out sensitivity, out formulaid)) { _logger.Warn(string.Format("计算振动数据时发生错误,传感器{0}未能找到计算系数", sensorid)); return(-1); } double[] freq, ampl; double fs = ReadParam(filepath).diSampleFreq; double[] org = ReadDatum(filepath); PhisicalTrans(ref org, sensitivity); // --FILTER BEGIN-- if (structId > 0) { var filter = ConfigHelper.GetAccFilter(structId); string err = ""; if (filter != null && !filter.IsValid(org, out err)) { throw new Exception(err); } } // --FILTER END-- if (formulaid == 21) // 包含滤波 { org = RemoveDcAndFilterWave(org); TWFFT.FFTData(org, fs, 1024, out ampl, out freq);//带平均的FFT } else { var tmp = (double[])org.Clone(); TWFFT.FFTData1(tmp, null, fs, out ampl, out freq); ampl[0] = 0; // 消除频谱中的直流分量 } CheckRedundancy(sensorid, acqTime); // 检查数据库中是否有重复数据 #if USE_BULK_COPY affectCnt = InsertDataBulk(sensorid, (int)fs, acqTime, org, freq, ampl); #else affectCnt = InsertData(sensorid, (int)fs, acqTime, org, freq, ampl); #endif return(affectCnt); }
/// <summary> /// 用倒谱获得频谱包络 /// </summary> /// <param name="data"></param> /// <returns></returns> private static double[] GetEnvelopeFD(double[] data) { int n = data.Length; double[] data1 = new double[n]; double[] data1i = new double[n]; for (int i = 0; i < n; i++) { data1[i] = Math.Log(data[i]); } for (int i = 0; i < n; i++) { data1i[i] = 0; } TWFFT.FFT(data1, data1i); double therehold = 100; double[] data2 = new double[n]; double[] data2i = new double[n]; // 低通滤波 for (int i = 0; i < n; i++) { if (i < therehold || i > n - therehold) { data2[i] = data1[i]; data2i[i] = data1i[i]; } else { data2[i] = 0; data2i[i] = 0; } } //for (int i = 0; i < n; i++) data2i[i] = 0; TWFFT.IFFT(data2, data2i); var res = FToneAnalysis.MiddleFilter(data2, 3); for (int i = 0; i < n; i++) { res[i] = Math.Pow(Math.E, res[i]); } return(res); }
private ArrayList FFTList(ArrayList lfft) { float[] resource = new float[max]; int x = 0; foreach (string fft in lfft) { if (x == max) { break; } float f = float.Parse(fft); resource[x] = f; x++; } ArrayList time_out = TWFFT.getArrayWithDivision(512.0f, 0.0f, 1024.0f); ArrayList result = TWFFT.FFTMagic(time_out, TWFFT.FFTAbs(TWFFT.FFT(resource, getZero())), 1025); return(result); }
public static Image getFFTImage(int[] data) { int w = 2000; int h = 1000; Bitmap bitmap = new Bitmap(w, h); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //draw fft int n = 4096; double[] datas = new double[n]; double[] datas2 = new double[n]; double[] datamod = new double[n]; //if (data.Length <= n) return null; for (int i = 0; i < n; i++) { datas2[i] = 0; if (i > data.Length - 1) { datas[i] = 0; } else { datas[i] = data[i]; } } TWFFT.FFT(datas, datas2); for (int i = 0; i < n; i++) { datamod[i] = Math.Sqrt(datas[i] * datas[i] + datas2[i] * datas2[i]); } int time = 2; for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Black, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datamod[i - 1] / w)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datamod[i] / w))); } double[] datad = new double[n]; double[] datad2 = new double[n]; for (int i = 0; i < n; i++) { datad2[i] = 0; } for (int i = 0; i < n; i++) { datad[i] = Math.Log(datamod[i]); } for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Red, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datad[i - 1] * 50)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datad[i] * 50))); } TWFFT.FFT(datad, datad2); for (int i = 0; i < n; i++) { datamod[i] = Math.Log(Math.Sqrt(datad[i] * datad[i] + datad2[i] * datad2[i])); } double therehold = 100; double[] rdata = new double[n]; double[] rdata2 = new double[n]; for (int i = 0; i < n; i++) { if (i < therehold || i > n - therehold) { rdata[i] = datad[i]; } else { rdata[i] = 0; } } for (int i = 0; i < n; i++) { rdata2[i] = datad2[i]; } TWFFT.IFFT(rdata, rdata2); for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Blue, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - rdata[i - 1] * 50)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - rdata[i] * 50))); } return(bitmap); }
public static Image getDFFTImage(int[] data, int beginx, int len, double pitch = 1) { int w = 2000; int h = 1000; Bitmap bitmap = new Bitmap(w, h); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //draw fft int n = 4096; double[] datas = new double[n]; double[] datasi = new double[n]; double[] datamod = new double[n]; //if (data.Length <= n) return null; for (int i = 0; i < n; i++) { datasi[i] = 0; if (i > data.Length - 1) { datas[i] = 0; } else { datas[i] = data[i]; } } TWFFT.FFT(datas, datasi); for (int i = 0; i < n; i++) { datamod[i] = Math.Sqrt(datas[i] * datas[i] + datasi[i] * datasi[i]); } var ddataenv = GetEnvelopeFD(datamod); int time = 2; for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Black, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datamod[i - 1] / w)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datamod[i] / w))); } double[] ddatabase = new double[n]; double[] ddataibase = new double[n]; for (int i = 0; i < n; i++) { ddatabase[i] = datas[i] / ddataenv[i]; } for (int i = 0; i < n; i++) { ddataibase[i] = datasi[i] / ddataenv[i]; } double[] ddatabaser = new double[n]; double[] ddataibaser = new double[n]; // 对称共轭,所以分两半分别压缩 ddatabaser[0] = datas[0]; ddataibaser[0] = datasi[0]; pitch = 2.4; for (int i = 1; i <= n / 2; i++) { ddatabaser[i] = getResampleValue(ddatabase, (double)i / pitch); ddatabaser[n - i] = ddatabaser[i]; ddataibaser[i] = getResampleValue(ddataibase, (double)i / pitch); ddataibaser[n - i] = -ddataibaser[i]; } for (int i = 0; i < n; i++) { ddatabaser[i] = ddatabaser[i] * ddataenv[i]; } for (int i = 0; i < n; i++) { ddataibaser[i] = ddataibaser[i] * ddataenv[i]; } for (int i = 0; i < n; i++) { datamod[i] = Math.Sqrt(ddatabaser[i] * ddatabaser[i] + ddataibaser[i] * ddataibaser[i]); } for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Red, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datamod[i - 1] / w)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datamod[i] / w))); } return(bitmap); }
private void ThreadReadExample1() { //float[] Qc = new float[graphDt.Rows.Count]; //float[] Qh = new float[graphDt.Rows.Count]; float[] Qm = new float[graphDt.Rows.Count]; //float[] Tc = new float[graphDt.Rows.Count]; //float[] Th = new float[graphDt.Rows.Count]; float[] Tm = new float[graphDt.Rows.Count]; //float[] Tm2 = new float[graphDt.Rows.Count]; //float[] Pc = new float[graphDt.Rows.Count]; //float[] Ph = new float[graphDt.Rows.Count]; //float[] Pm = new float[graphDt.Rows.Count]; //float[] Qm5 = new float[graphDt.Rows.Count]; //float[] Wh = new float[graphDt.Rows.Count]; DateTime[] dateTime = new DateTime[graphDt.Rows.Count]; DateTimeFormatInfo dtFormat = new DateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy-MM-dd hh:mm:ss:fff"; //加载流量数据 for (int i = 0; i < graphDt.Rows.Count; i++) { try { //Qc[i] = (float)Convert.ToDouble(graphDt.Rows[i][1]); //Qh[i] = (float)Convert.ToDouble(graphDt.Rows[i][2]); Qm[i] = (float)Convert.ToDouble(graphDt.Rows[i][3]); //Tc[i] = (float)Convert.ToDouble(graphDt.Rows[i][4]); //Th[i] = (float)Convert.ToDouble(graphDt.Rows[i][5]); Tm[i] = (float)Convert.ToDouble(graphDt.Rows[i][6]); //Tm2[i] = (float)Convert.ToDouble(graphDt.Rows[i][7]); //Pc[i] = (float)Convert.ToDouble(graphDt.Rows[i][7]); //Ph[i] = (float)Convert.ToDouble(graphDt.Rows[i][8]); //Pm[i] = (float)Convert.ToDouble(graphDt.Rows[i][9]); //Qm5[i] = (float)Convert.ToDouble(graphDt.Rows[i][10]); //Wh[i] = (float)Convert.ToDouble(graphDt.Rows[i][11]); // dateTime[i] = Convert.ToDateTime(dt.Rows[i][0],dtFormat); dateTime[i] = DateTime.ParseExact((string)graphDt.Rows[i][0], "yyyy-MM-dd hh:mm:ss:fff", dtFormat); } catch (Exception ex) { Log.Error(ex.ToString()); return; } } //快速傅里叶变化滤波 Console.WriteLine("1:" + Qm.Length); Qm = TWFFT.FFT_filter(Qm); Tm = TWFFT.FFT_filter(Tm); Console.WriteLine("2:" + Qm.Length); for (int i = 0; i < graphDt.Rows.Count; i++) { } DateTime start = dateTime[0]; DateTime PointA = dateTime[0].AddSeconds(2); TimeSpan ts = new TimeSpan(1); // 显示出数据信息来 Invoke(new Action(() => { //总流量达到6L加载数据 if (false) { hslCurveHistory1.Text = "累计流量未达到6L..."; hslCurveHistory1.RemoveAllCurve(); } else { hslCurveHistory1.Text = "正在加载数据..."; hslCurveHistory1.RemoveAllCurve(); hslCurveHistory1.SetLeftCurve("出水流量Qm", Qm, Color.DodgerBlue, true, "{0:F2} L/min");//布尔变量:是否开启曲线平滑 //hslCurveHistory1.SetLeftCurve("冷水流量Qc", Qc, Color.Tomato, true, "{0:F2} L/min"); //hslCurveHistory1.SetLeftCurve("热水流量Qh", Qh, Color.GreenYellow, true, "{0:F2} L/min"); //hslCurveHistory1.SetLeftCurve("Qm5", Qm5, Color.Purple, true, "{0:F2} L/min"); //hslCurveHistory1.SetLeftCurve("出水压力Pm", Pm, Color.Red, true, "{0:F2} Bar"); //hslCurveHistory1.SetLeftCurve("热水压力Ph", Ph, Color.Orange, true, "{0:F2} Bar"); //hslCurveHistory1.SetLeftCurve("冷水压力Pc", Pc, Color.Yellow, true, "{0:F2} Bar"); hslCurveHistory1.SetLeftCurve("出水温度Tm", Tm, Color.Red, true, "{0:F2} ℃"); //hslCurveHistory1.SetLeftCurve("出水温度Tm2", Tm2, Color.Yellow, true, "{0:F2} ℃"); //hslCurveHistory1.SetRightCurve("热水温度Th", Th, Color.Honeydew, true, "{0:F2} ℃"); //hslCurveHistory1.SetRightCurve("冷水温度Tc", Tc, Color.Pink, true, "{0:F2} ℃"); //hslCurveHistory1.SetRightCurve("液面高度Wh", Tc, Color.Blue, true, "{0:F2} mm"); hslCurveHistory1.SetDateTimes(dateTime); // 增加一个三角形的线段标记示例 Points的每个点的X是数据索引,Y是数据值(需要选对参考坐标轴,默认为左坐标轴) // 添加一个活动的标记 HslControls.HslMarkForeSection active = new HslControls.HslMarkForeSection() { StartIndex = 1000, EndIndex = 1500, Height = 0.9f, }; //active.CursorTexts.Add("条码", "A123123124ashdiahsd是的iahsidasd"); //active.CursorTexts.Add("工号", "asd2sd123dasf"); //hslCurveHistory1.AddMarkActiveSection(active); //hslCurveHistory1.SetCurveVisible("步序", false); // 步序不是曲线信息,不用显示出来 //hslCurveHistory1.ValueMaxLeft = 10; //hslCurveHistory1.ValueMinLeft = 0; SetTempLight(28); SetTempLight(29); SetTempLight(30); SetRemark(29f, 28f, Tm); hslCurveHistory1.SetScaleByXAxis((float)0.1); hslCurveHistory1.RenderCurveUI(); } } )); }