Guest User

Untitled

a guest
Jan 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Linq;
  6. using System.Text;
  7. using System.IO;
  8. using ZedGraph;
  9. using FileHelpers;
  10.  
  11.  
  12. namespace DataAnalysisGUI
  13. {
  14.     class mathStuff
  15.     {
  16.  
  17.         // Set up initial variables
  18.         DataVars[] fileData;
  19.         double[][] jerkData = new double[2][];
  20.  
  21.         public void analyseData(string filename, ZedGraphControl zgc)
  22.         {
  23.             readDataFile(filename);
  24.            
  25.             double[] xAccel = new double[this.fileData.Length];
  26.             double[] yAccel = new double[this.fileData.Length];
  27.             for (int i = 0; i < this.fileData.Length; i++)
  28.             {
  29.                 xAccel[i] = this.fileData[i].xAccel;
  30.                 yAccel[i] = this.fileData[i].yAccel;
  31.             }
  32.             double[] xAccelc = calibData(xAccel, 1.3);
  33.             double[] yAccelc = calibData(yAccel, -1.3);
  34.  
  35.             this.jerkData[0] = this.numericalDiff(xAccelc);
  36.             this.jerkData[1] = this.numericalDiff(yAccelc);
  37.  
  38.             CreateChart(zgc);
  39.         }
  40.  
  41.        
  42.         public void CreateChart(ZedGraphControl zgc)
  43.         {
  44.             GraphPane myPane = zgc.GraphPane;
  45.  
  46.             // Set the titles
  47.             myPane.Title.Text = "Test Plot of Data";
  48.             myPane.XAxis.Title.Text = "X Accel";
  49.             myPane.YAxis.Title.Text = "X Jerk";
  50.  
  51.             // Fill the background of the chart rect and pane
  52.             myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
  53.             myPane.Fill = new Fill(Color.White, Color.SlateGray, 45.0f);
  54.            
  55.             // Populate a PointPairList
  56.             PointPairList list = new PointPairList();
  57.  
  58.             for (int i = 0; i < this.fileData.Length; i++)
  59.             {
  60.                 list.Add(this.fileData[i].xAccel, this.jerkData[0][i]);
  61.             }
  62.  
  63.             // Add the curve
  64.             LineItem myCurve = myPane.AddCurve("Data Points", list, Color.Black, SymbolType.Circle);
  65.             // Don't display the line (This makes a scatter plot)
  66.             myCurve.Line.IsVisible = false;
  67.             // Hide the symbol outline
  68.             myCurve.Symbol.Border.IsVisible = false;
  69.             // Fill the symbol interior with color
  70.             myCurve.Symbol.Fill = new Fill(Color.Firebrick);
  71.  
  72.  
  73.             zgc.AxisChange();
  74.             zgc.Refresh();
  75.         }
  76.  
  77.         [DelimitedRecord(",")]
  78.         private class DataVars
  79.         {
  80.             public double time;
  81.  
  82.             public double xAccel;
  83.  
  84.             public double yAccel;
  85.  
  86.             public double zAccel;
  87.  
  88.             public double alt;
  89.  
  90.             public double gpsLat;
  91.  
  92.             public double gpsLong;
  93.  
  94.             public double speed;
  95.  
  96.         }
  97.  
  98.         private class dataCoefs
  99.         {
  100.             public double[] alpha_PPAL = new double[3] {2, 3, 5};
  101.             public double alpha_DTV = 1;
  102.             public double[] atl = new double[3] { .0910, .159, .227 };
  103.             public double[] dtl = new double[3] { -.141, -.235, -.329 };
  104.             public double[] jtl = new double[3] { 1, 2, 3 };
  105.             public double phi = 2.88;
  106.             public double tau = 2.5;
  107.             public double vt = 125.5;
  108.  
  109.         }
  110.  
  111.  
  112.         private double[] numericalDiff(double[] values)
  113.         {
  114.             double[] diff = new double[values.Length];
  115.             for (int i=1; i < values.Length; i++)
  116.             {
  117.                 diff[i] = values[i] - values[i - 1];
  118.             }
  119.             diff[0] = diff[1];
  120.  
  121.             return diff;
  122.         }
  123.  
  124.         private double[] calibData(double[] data, double offset)
  125.         {
  126.             double[] calibValues = new double[data.Length];
  127.             for (int i = 0; i < data.Length; i++)
  128.             {
  129.                 calibValues[i] = data[i] + offset;
  130.             }
  131.             return calibValues;
  132.  
  133.         }
  134.         /* Under Construction Still
  135.         private double[][] createLimitPlots()
  136.         {
  137.             dataCoefs data = new dataCoefs();
  138.             double[] ra = new double[2];
  139.             double[] rj = new double[2];
  140.             double[] ca = new double[2];
  141.             double[] cj = new double[2];
  142.  
  143.             for (int i=0; i<2; i++)
  144.             {
  145.                 ra[i] = .5 * (data.atl[i] - data.dtl[i]);
  146.                 rj[i] = data.jtl[i];
  147.                 ca[i] = data.atl[i] - ra[i];
  148.                 cj[i] = data.jtl[i] - rj[i];
  149.                 for (int theta=0; theta<360; theta++)
  150.                 {
  151.                     theta_rad = deg2rad[theta];
  152.                     plots[1][i][theta+1] = ra[i]*cos[phi_rad]*cos[theta_rad]-rj[i]*sin[phi_rad]*sin[theta_rad]+ca[i];
  153.                     plots[2][i][theta+1] = rj[i]*cos[phi_rad]*sin[theta_rad]+ra[i]*sin[phi_rad]*cos[theta_rad]+cj[i];
  154.                 }
  155.             }
  156.             return 3.0;
  157.         }
  158.  
  159.         private double[] deg2rad(double[] rad)
  160.         {
  161.  
  162.             return 5;
  163.         }
  164.         */
  165.         private void readDataFile(string filename)
  166.         {
  167.             FileHelperEngine engine = new FileHelperEngine(typeof(DataVars));
  168.             engine.Options.IgnoreFirstLines = 1;
  169.             try
  170.             {
  171.                 DataVars[] result = engine.ReadFile(filename.Trim()) as DataVars[];
  172.                 this.fileData = result;
  173.             }
  174.             catch (Exception ex)
  175.             {
  176.                 MessageBox.Show(ex.ToString());
  177.             }
  178.         }
  179.  
  180.     }
  181. }
Add Comment
Please, Sign In to add comment