public static Image <Rgba32> GenerateImage(CollectionBuffer cb, string type = "ACC1", string field = "AccX", string timeus = "TimeUS") { var bins = 10; int N = 1 << bins; var fft = new FFT2(); var acc1data = cb.GetEnumeratorType(type).ToArray(); log.Debug(type); var firstsample = acc1data.Take(N); var samplemin = double.Parse(firstsample.Min(a => a[timeus])); var samplemax = double.Parse(firstsample.Max(a => a[timeus])); log.Debug("samplemin " + samplemin + " samplemax " + samplemax); var timedelta = samplemin - samplemax; log.Debug(" timedelta " + timedelta); double[] freqt = fft.FreqTable(N, (int)(1000 / (N / timedelta))); // time , freq , [color] &freqcount int totalsamples = acc1data.Count(); int count = totalsamples / N; int done = 0; // 50% overlap int divisor = 2; count *= divisor; var img = new Image <Rgba32>(count, freqt.Length); log.Debug("done and count "); while (count > 1) // skip last part { var fftdata = acc1data.Skip((N * (done / divisor))).Take(N); var time = fftdata.Skip(N / 2).First().time.ToString("o"); var fftanswerz = fft.rin(fftdata.Select(a => (double)(float)a.GetRaw(field)).ToArray(), (uint)bins); //plotlydata.root.z.Add(fftanswerz.Select(a => a > 2 ? 0 : a).ToArray()); freqt.Select((y, i) => img[done, (freqt.Length - 1) - i] = GetColor(fftanswerz[i])).ToArray(); count--; done++; } return(img); }
public long GetLineNoFromTime(CollectionBuffer logdata, DateTime p1) { DateTime last = DateTime.MaxValue; foreach (var dfItem in logdata.GetEnumeratorType(new string[] { "GPS", "GPS2" })) { // always forwards if (dfItem.time >= p1) { return(dfItem.lineno); } last = dfItem.time; } if (last != DateTime.MaxValue) { return(long.MaxValue); } return(0); }
public static void Create(string filein, string fileout, List <string> fmtList = null) { using (StreamReader tr = new StreamReader(filein)) using (CollectionBuffer logdata = new CollectionBuffer(tr.BaseStream)) { List <string> colList = new List <string>(); Dictionary <string, int> colStart = new Dictionary <string, int>(); colStart.Add("GLOBAL", colList.Count); colList.Add("GLOBAL_TimeMS"); foreach (var logformatValue in logdata.dflog.logformat.Values) { if (fmtList != null && !fmtList.Contains(logformatValue.Name)) { continue; } colStart.Add(logformatValue.Name, colList.Count); foreach (var field in logformatValue.FieldNames) { colList.Add(logformatValue.Name + "_" + field); } } using (StreamWriter sr = new StreamWriter(fileout)) { // header foreach (var item in colList) { sr.Write(item + ","); } sr.WriteLine(); // lines foreach (var dfitem in logdata.GetEnumeratorType(colStart.Keys.ToArray())) { if (dfitem.msgtype == "FMT") { continue; } var idx = 0; StringBuilder sb = new StringBuilder(); sb.Append(dfitem.timems); idx++; sb.Append(','); var start = colStart[dfitem.msgtype]; while (idx < start) { idx++; sb.Append(','); } foreach (var item in dfitem.items.Skip(1)) { sb.Append(item?.Trim()); idx++; sb.Append(','); } sr.WriteLine(sb); } } } }
private void but_ISBH_Click(object sender, EventArgs e) { Utilities.FFT2 fft = new FFT2(); using ( OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = "*.log;*.bin|*.log;*.bin"; ofd.ShowDialog(); if (!File.Exists(ofd.FileName)) { return; } var file = new CollectionBuffer(File.OpenRead(ofd.FileName)); int bins = (int)NUM_bins.Value; int N = 1 << bins; Color[] color = new Color[] { Color.Red, Color.Green, Color.Blue, Color.Black, Color.Violet, Color.Orange }; ZedGraphControl[] ctls = new ZedGraphControl[] { zedGraphControl1, zedGraphControl2, zedGraphControl3, zedGraphControl4, zedGraphControl5, zedGraphControl6 }; // 3 imus * 2 sets of measurements(gyr/acc) datastate[] alldata = new datastate[3 * 2]; for (int a = 0; a < alldata.Length; a++) { alldata[a] = new datastate(); } // state cache int Ns = 0; int type = 0; int instance = 0; int sensorno = 0; double smp_rate = 0; foreach (var item in file.GetEnumeratorType(new string[] { "ISBH", "ISBD" })) { if (item.msgtype == null) { continue; } if (item.msgtype.StartsWith("ISBH")) { Ns = int.Parse(item.items[file.dflog.FindMessageOffset(item.msgtype, "N")], CultureInfo.InvariantCulture); type = int.Parse(item.items[file.dflog.FindMessageOffset(item.msgtype, "type")], CultureInfo.InvariantCulture); instance = int.Parse(item.items[file.dflog.FindMessageOffset(item.msgtype, "instance")], CultureInfo.InvariantCulture); sensorno = type * 3 + instance; alldata[sensorno].sample_rate = double.Parse(item.items[file.dflog.FindMessageOffset(item.msgtype, "smp_rate")], CultureInfo.InvariantCulture); if (type == 0) { alldata[sensorno].type = "ACC" + instance.ToString(); } if (type == 1) { alldata[sensorno].type = "GYR" + instance.ToString(); } } else if (item.msgtype.StartsWith("ISBD")) { var Nsdata = int.Parse(item.items[file.dflog.FindMessageOffset(item.msgtype, "N")], CultureInfo.InvariantCulture); if (Ns != Nsdata) { continue; } int offsetX = file.dflog.FindMessageOffset(item.msgtype, "x"); int offsetY = file.dflog.FindMessageOffset(item.msgtype, "y"); int offsetZ = file.dflog.FindMessageOffset(item.msgtype, "z"); int offsetTime = file.dflog.FindMessageOffset(item.msgtype, "TimeUS"); double time = double.Parse(item.items[offsetTime], CultureInfo.InvariantCulture) / 1000.0; if (time < alldata[sensorno].lasttime) { continue; } if (time != alldata[sensorno].lasttime) { alldata[sensorno].timedelta = alldata[sensorno].timedelta * 0.99 + (time - alldata[sensorno].lasttime) * 0.01; } alldata[sensorno].lasttime = time; item.items[offsetX].Split(new[] { ' ', '[', ']' }, StringSplitOptions.RemoveEmptyEntries).ForEach(aa => { alldata[sensorno].datax.Add(double.Parse(aa, CultureInfo.InvariantCulture)); }); item.items[offsetY].Split(new[] { ' ', '[', ']' }, StringSplitOptions.RemoveEmptyEntries).ForEach(aa => { alldata[sensorno].datay.Add(double.Parse(aa, CultureInfo.InvariantCulture)); }); item.items[offsetZ].Split(new[] { ' ', '[', ']' }, StringSplitOptions.RemoveEmptyEntries).ForEach(aa => { alldata[sensorno].dataz.Add(double.Parse(aa, CultureInfo.InvariantCulture)); }); } } int controlindex = 0; foreach (var sensordata in alldata) { if (sensordata.datax.Count <= N) { continue; } double samplerate = 0; samplerate = sensordata.sample_rate;// Math.Round(1000 / sensordata.timedelta, 1); double[] freqt = fft.FreqTable(N, (int)samplerate); double[] avgx = new double[N / 2]; double[] avgy = new double[N / 2]; double[] avgz = new double[N / 2]; int totalsamples = sensordata.datax.Count; int count = totalsamples / N; int done = 0; while (count > 1) // skip last part { var fftanswerx = fft.rin(sensordata.datax.Skip(N * done).Take(N).ToArray(), (uint)bins); var fftanswery = fft.rin(sensordata.datay.Skip(N * done).Take(N).ToArray(), (uint)bins); var fftanswerz = fft.rin(sensordata.dataz.Skip(N * done).Take(N).ToArray(), (uint)bins); for (int b = 0; b < N / 2; b++) { if (freqt[b] < (double)NUM_startfreq.Value) { continue; } avgx[b] += fftanswerx[b] / (done + count); avgy[b] += fftanswery[b] / (done + count); avgz[b] += fftanswerz[b] / (done + count); } count--; done++; } ZedGraph.PointPairList pplx = new ZedGraph.PointPairList(freqt, avgx); ZedGraph.PointPairList pply = new ZedGraph.PointPairList(freqt, avgy); ZedGraph.PointPairList pplz = new ZedGraph.PointPairList(freqt, avgz); var curvex = new LineItem(sensordata.type + " x", pplx, color[0], SymbolType.None); var curvey = new LineItem(sensordata.type + " y", pply, color[1], SymbolType.None); var curvez = new LineItem(sensordata.type + " z", pplz, color[2], SymbolType.None); ctls[controlindex].GraphPane.Legend.IsVisible = true; ctls[controlindex].GraphPane.XAxis.Title.Text = "Freq Hz"; ctls[controlindex].GraphPane.YAxis.Title.Text = "Amplitude"; ctls[controlindex].GraphPane.Title.Text = "FFT " + sensordata.type + " - " + Path.GetFileName(ofd.FileName) + " - " + samplerate + "hz input"; ctls[controlindex].GraphPane.CurveList.Clear(); ctls[controlindex].GraphPane.CurveList.Add(curvex); ctls[controlindex].GraphPane.CurveList.Add(curvey); ctls[controlindex].GraphPane.CurveList.Add(curvez); ctls[controlindex].Invalidate(); ctls[controlindex].AxisChange(); ctls[controlindex].GraphPane.XAxis.Scale.Max = samplerate / 2; ctls[controlindex].Refresh(); controlindex++; } SetScale(ctls); } }
//FMT, 131, 43, IMU, IffffffIIf, TimeMS,GyrX,GyrY,GyrZ,AccX,AccY,AccZ,ErrG,ErrA,Temp //FMT, 135, 43, IMU2, IffffffIIf, TimeMS,GyrX,GyrY,GyrZ,AccX,AccY,AccZ,ErrG,ErrA,Temp //FMT, 149, 43, IMU3, IffffffIIf, TimeMS,GyrX,GyrY,GyrZ,AccX,AccY,AccZ,ErrG,ErrA,Temp //FMT, 172, 23, ACC1, IIfff, TimeMS,TimeUS,AccX,AccY,AccZ //FMT, 173, 23, ACC2, IIfff, TimeMS,TimeUS,AccX,AccY,AccZ //FMT, 174, 23, ACC3, IIfff, TimeMS,TimeUS,AccX,AccY,AccZ //FMT, 175, 23, GYR1, IIfff, TimeMS,TimeUS,GyrX,GyrY,GyrZ //FMT, 176, 23, GYR2, IIfff, TimeMS,TimeUS,GyrX,GyrY,GyrZ //FMT, 177, 23, GYR3, IIfff, TimeMS,TimeUS,GyrX,GyrY,GyrZ private void myButton1_Click(object sender, EventArgs e) { Utilities.FFT2 fft = new FFT2(); using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = "*.log;*.bin|*.log;*.bin"; ofd.ShowDialog(); if (!File.Exists(ofd.FileName)) { return; } var file = new CollectionBuffer(File.OpenRead(ofd.FileName)); int bins = (int)NUM_bins.Value; int N = 1 << bins; double[] datainGX = new double[N]; double[] datainGY = new double[N]; double[] datainGZ = new double[N]; double[] datainAX = new double[N]; double[] datainAY = new double[N]; double[] datainAZ = new double[N]; List <double[]> avg = new List <double[]>(); // 6 avg.Add(new double[N / 2]); avg.Add(new double[N / 2]); avg.Add(new double[N / 2]); avg.Add(new double[N / 2]); avg.Add(new double[N / 2]); avg.Add(new double[N / 2]); object[] datas = new object[] { datainGX, datainGY, datainGZ, datainAX, datainAY, datainAZ }; string[] datashead = new string[] { "GYR1-GyrX", "GYR1-GyrY", "GYR1-GyrZ", "ACC1-AccX", "ACC1-AccY", "ACC1-AccZ" }; Color[] color = new Color[] { Color.Red, Color.Green, Color.Black, Color.Violet, Color.Blue, Color.Orange }; ZedGraphControl[] ctls = new ZedGraphControl[] { zedGraphControl1, zedGraphControl2, zedGraphControl3, zedGraphControl4, zedGraphControl5, zedGraphControl6 }; int samplecounta = 0; int samplecountg = 0; double lasttime = 0; double timedelta = 0; double[] freqt = null; double samplerate = 0; foreach (var item in file.GetEnumeratorType(new string[] { "ACC1", "GYR1" })) { if (item.msgtype == "ACC1") { int offsetAX = file.dflog.FindMessageOffset("ACC1", "AccX"); int offsetAY = file.dflog.FindMessageOffset("ACC1", "AccY"); int offsetAZ = file.dflog.FindMessageOffset("ACC1", "AccZ"); int offsetTime = file.dflog.FindMessageOffset("ACC1", "TimeUS"); double time = double.Parse(item.items[offsetTime], CultureInfo.InvariantCulture) / 1000.0; timedelta = timedelta * 0.99 + (time - lasttime) * 0.01; // we missed gyro data if (samplecounta >= N) { continue; } datainAX[samplecounta] = double.Parse(item.items[offsetAX], CultureInfo.InvariantCulture); datainAY[samplecounta] = double.Parse(item.items[offsetAY], CultureInfo.InvariantCulture); datainAZ[samplecounta] = double.Parse(item.items[offsetAZ], CultureInfo.InvariantCulture); samplecounta++; lasttime = time; } else if (item.msgtype == "GYR1") { int offsetGX = file.dflog.FindMessageOffset("GYR1", "GyrX"); int offsetGY = file.dflog.FindMessageOffset("GYR1", "GyrY"); int offsetGZ = file.dflog.FindMessageOffset("GYR1", "GyrZ"); int offsetTime = file.dflog.FindMessageOffset("ACC1", "TimeUS"); double time = double.Parse(item.items[offsetTime], CultureInfo.InvariantCulture) / 1000.0; // we missed accel data if (samplecountg >= N) { continue; } datainGX[samplecountg] = double.Parse(item.items[offsetGX], CultureInfo.InvariantCulture); datainGY[samplecountg] = double.Parse(item.items[offsetGY], CultureInfo.InvariantCulture); datainGZ[samplecountg] = double.Parse(item.items[offsetGZ], CultureInfo.InvariantCulture); samplecountg++; } if (samplecounta >= N && samplecountg >= N) { int inputdataindex = 0; foreach (var itemlist in datas) { var fftanswer = fft.rin((double[])itemlist, (uint)bins); for (int b = 0; b < N / 2; b++) { avg[inputdataindex][b] += fftanswer[b] * (1.0 / (N / 2.0)); } samplecounta = 0; samplecountg = 0; inputdataindex++; } } } if (freqt == null) { samplerate = Math.Round(1000 / timedelta, 1); freqt = fft.FreqTable(N, (int)samplerate); } // 0 out all data befor cutoff for (int inputdataindex = 0; inputdataindex < 6; inputdataindex++) { for (int b = 0; b < N / 2; b++) { if (freqt[b] < (double)NUM_startfreq.Value) { avg[inputdataindex][b] = 0; continue; } break; } } int controlindex = 0; foreach (var item in avg) { ZedGraph.PointPairList ppl = new ZedGraph.PointPairList(freqt, item); //double xMin, xMax, yMin, yMax; var curve = new LineItem(datashead[controlindex], ppl, color[controlindex], SymbolType.None); //curve.GetRange(out xMin, out xMax, out yMin, out yMax, true, false, ctls[c].GraphPane); ctls[controlindex].GraphPane.Legend.IsVisible = false; ctls[controlindex].GraphPane.XAxis.Title.Text = "Freq Hz"; ctls[controlindex].GraphPane.YAxis.Title.Text = "Amplitude"; ctls[controlindex].GraphPane.Title.Text = "FFT " + datashead[controlindex] + " - " + Path.GetFileName(ofd.FileName) + " - " + samplerate + "hz input"; ctls[controlindex].GraphPane.CurveList.Clear(); ctls[controlindex].GraphPane.CurveList.Add(curve); ctls[controlindex].Invalidate(); ctls[controlindex].AxisChange(); ctls[controlindex].Refresh(); controlindex++; } SetScale(ctls); } }
private void but_fftimu_Click(object sender, EventArgs e) { Utilities.FFT2 fft = new FFT2(); using ( OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = "*.log;*.bin|*.log;*.bin"; ofd.ShowDialog(); if (!File.Exists(ofd.FileName)) { return; } var file = new CollectionBuffer(File.OpenRead(ofd.FileName)); int bins = (int)NUM_bins.Value; int N = 1 << bins; Color[] color = new Color[] { Color.Red, Color.Green, Color.Blue, Color.Black, Color.Violet, Color.Orange }; ZedGraphControl[] ctls = new ZedGraphControl[] { zedGraphControl1, zedGraphControl2, zedGraphControl3, zedGraphControl4, zedGraphControl5, zedGraphControl6 }; // 3 imus * 2 sets of measurements(gyr/acc) datastate[] alldata = new datastate[3 * 2]; for (int a = 0; a < alldata.Length; a++) { alldata[a] = new datastate(); } Log.DFLog dflog = new Log.DFLog(); foreach (var item in file.GetEnumeratorType(new string[] { "FMT", "IMU", "IMU2", "IMU3" })) { if (item.msgtype == null) { continue; } if (item.msgtype == "FMT") { dflog.FMTLine(file[item.lineno]); continue; } if (item.msgtype.StartsWith("IMU")) { int sensorno = 0; if (item.msgtype == "IMU") { sensorno = 0; } if (item.msgtype == "IMU2") { sensorno = 1; } if (item.msgtype == "IMU3") { sensorno = 2; } alldata[sensorno + 3].type = item.msgtype + " A"; int offsetAX = dflog.FindMessageOffset(item.msgtype, "AccX"); int offsetAY = dflog.FindMessageOffset(item.msgtype, "AccY"); int offsetAZ = dflog.FindMessageOffset(item.msgtype, "AccZ"); int offsetTime = dflog.FindMessageOffset(item.msgtype, "TimeUS"); double time = double.Parse(item.items[offsetTime]) / 1000.0; if (time != alldata[sensorno + 3].lasttime) { alldata[sensorno + 3].timedelta = alldata[sensorno + 3].timedelta * 0.99 + (time - alldata[sensorno + 3].lasttime) * 0.01; } alldata[sensorno + 3].lasttime = time; alldata[sensorno + 3].datax.Add(double.Parse(item.items[offsetAX])); alldata[sensorno + 3].datay.Add(double.Parse(item.items[offsetAY])); alldata[sensorno + 3].dataz.Add(double.Parse(item.items[offsetAZ])); //gyro alldata[sensorno].type = item.msgtype + " G"; int offsetGX = dflog.FindMessageOffset(item.msgtype, "GyrX"); int offsetGY = dflog.FindMessageOffset(item.msgtype, "GyrY"); int offsetGZ = dflog.FindMessageOffset(item.msgtype, "GyrZ"); if (time != alldata[sensorno].lasttime) { alldata[sensorno].timedelta = alldata[sensorno].timedelta * 0.99 + (time - alldata[sensorno].lasttime) * 0.01; } alldata[sensorno].lasttime = time; alldata[sensorno].datax.Add(double.Parse(item.items[offsetGX])); alldata[sensorno].datay.Add(double.Parse(item.items[offsetGY])); alldata[sensorno].dataz.Add(double.Parse(item.items[offsetGZ])); } } int controlindex = 0; foreach (var sensordata in alldata) { if (sensordata.datax.Count <= N) { continue; } double samplerate = 0; samplerate = Math.Round(1000 / sensordata.timedelta, 1); double[] freqt = fft.FreqTable(N, (int)samplerate); double[] avgx = new double[N / 2]; double[] avgy = new double[N / 2]; double[] avgz = new double[N / 2]; int totalsamples = sensordata.datax.Count; int count = totalsamples / N; int done = 0; while (count > 1) // skip last part { var fftanswerx = fft.rin(sensordata.datax.Skip(N * done).Take(N).ToArray(), (uint)bins); var fftanswery = fft.rin(sensordata.datay.Skip(N * done).Take(N).ToArray(), (uint)bins); var fftanswerz = fft.rin(sensordata.dataz.Skip(N * done).Take(N).ToArray(), (uint)bins); for (int b = 0; b < N / 2; b++) { if (freqt[b] < (double)NUM_startfreq.Value) { continue; } avgx[b] += fftanswerx[b] / (N / 2); avgy[b] += fftanswery[b] / (N / 2); avgz[b] += fftanswerz[b] / (N / 2); } count--; done++; } ZedGraph.PointPairList pplx = new ZedGraph.PointPairList(freqt, avgx); ZedGraph.PointPairList pply = new ZedGraph.PointPairList(freqt, avgy); ZedGraph.PointPairList pplz = new ZedGraph.PointPairList(freqt, avgz); var curvex = new LineItem(sensordata.type + " x", pplx, color[0], SymbolType.None); var curvey = new LineItem(sensordata.type + " y", pply, color[1], SymbolType.None); var curvez = new LineItem(sensordata.type + " z", pplz, color[2], SymbolType.None); ctls[controlindex].GraphPane.Legend.IsVisible = true; ctls[controlindex].GraphPane.XAxis.Title.Text = "Freq Hz"; ctls[controlindex].GraphPane.YAxis.Title.Text = "Amplitude"; ctls[controlindex].GraphPane.Title.Text = "FFT " + sensordata.type + " - " + Path.GetFileName(ofd.FileName) + " - " + samplerate + "hz input"; ctls[controlindex].GraphPane.CurveList.Clear(); ctls[controlindex].GraphPane.CurveList.Add(curvex); ctls[controlindex].GraphPane.CurveList.Add(curvey); ctls[controlindex].GraphPane.CurveList.Add(curvez); ctls[controlindex].Invalidate(); ctls[controlindex].AxisChange(); ctls[controlindex].GraphPane.XAxis.Scale.Max = samplerate / 2; ctls[controlindex].Refresh(); controlindex++; } } }
public static void getdata(string file) { DFLog.DFItem? IMU = null; DFLog.DFItem? ATT = null; List <Vector3> gaccel = new List <Vector3>(); List <Vector3> vel = new List <Vector3>(); List <DateTime> timestamps = new List <DateTime>(); List <int> accel_indexes = new List <int>(); var dtsum = 0.0; var dtcount = 0; var cb = new CollectionBuffer(File.OpenRead(file)); foreach (var item in cb.GetEnumeratorType(new string[] { "GPS", "GPS2", "IMU", "ATT" })) { switch (item.msgtype) { case "GPS": case "GPS2": if ((Byte)item.GetRaw("Status") >= 3) { var v = new Vector3( (Single)item.GetRaw("Spd") * Math.Cos(radians((Single)item.GetRaw("GCrs"))), (Single)item.GetRaw("Spd") * Math.Sin(radians((Single)item.GetRaw("GCrs"))), (Single)item.GetRaw("VZ")); vel.Add(v); timestamps.Add(item.time); accel_indexes.Add(Math.Max(gaccel.Count - 1, 0)); } break; case "IMU": if (ATT.HasValue) { gaccel.Add(earth_accel_df(item, ATT.Value)); if (IMU.HasValue) { var dt = item.time - IMU.Value.time; dtsum += dt.TotalSeconds; dtcount += 1; } IMU = item; } break; case "ATT": ATT = item; break; } } var imu_dt = dtsum / dtcount; Console.WriteLine("{0} samples at dt {1}", vel.Count, imu_dt); var besti = -1; var besterr = 0.0; List <double> delays = new List <double>(); List <double> errors = new List <double>(); foreach (var i in Enumerable.Range(0, 100)) { var err = velocity_error(timestamps, vel, gaccel, accel_indexes, imu_dt, i); if (err == null) { break; } errors.Add(err); delays.Add(i * imu_dt); if (besti == -1 || err < besterr) { besti = i; besterr = err; } } Console.WriteLine("Best {0} ({1}s) {2}", besti, besti * imu_dt, besterr); }