Пример #1
0
        public void SetFilterParameters(int LoHi, double samplingRate, double[] cornerFrequency, int nTaps)
        {
            this.resetFilterBuffer();

            if (nTaps % 2 != 0)
            {
                if (nTaps == 1)
                {
                    nTaps++;
                }
                else
                {
                    nTaps--;
                }
                System.Console.WriteLine("Warning. nTaps is not an even number. nTaps will be rounded to " + nTaps);
            }

            if (LoHi == LOW_PASS || LoHi == HIGH_PASS)
            {
                this.samplingRate    = samplingRate;
                this.cornerFrequency = cornerFrequency;
                this.nTaps           = nTaps;

                double fc = (cornerFrequency[0] / samplingRate);

                coefficients         = new double[nTaps];
                coefficients         = calculateCoefficients(fc, LoHi, nTaps);
                this.validparameters = true;
            }
            else if (LoHi == BAND_PASS || LoHi == BAND_STOP)
            {
                if (cornerFrequency.Length != 2)
                {
                    throw new System.ArgumentException("Error. Bandpass or bandstop filter requires two corner frequencies to be specified");
                }
                this.samplingRate = samplingRate;
                this.nTaps        = nTaps;

                // filter parameters: corners at +/- 1Hz from notch
                // center frequency
                double fcHigh;
                double fcLow;

                fcHigh = cornerFrequency.Max() / samplingRate;
                fcLow  = cornerFrequency.Min() / samplingRate;

                // calculate filter coefficients
                double[] coefficientHighPass = calculateCoefficients(fcHigh, HIGH_PASS, nTaps);
                double[] coefficientLowPass  = calculateCoefficients(fcLow, LOW_PASS, nTaps);
                coefficients = new double[coefficientHighPass.Length];

                for (int i = 0; i < coefficientHighPass.Length; i++)
                {
                    if (LoHi == BAND_PASS)
                    {
                        coefficients[i] = -(coefficientHighPass[i] + coefficientLowPass[i]); //sum of HPF and LPF for bandstop filter, spectral inversion for bandpass filter
                    }
                    else
                    {
                        coefficients[i] = coefficientHighPass[i] + coefficientLowPass[i]; //sum of HPF and LPF for bandstop filter
                    }
                }

                if (LoHi == BAND_PASS)
                {
                    coefficients[(nTaps / 2) + 1] = coefficients[(nTaps / 2) + 1] + 1;
                }

                this.validparameters = true;
                fastFilter           = new MathNet.Filtering.FIR.OnlineFirFilter(coefficients);
            }
            else
            {
                throw new System.ArgumentException("Error. Undefined filter type: use 0 - lowpass, 1 - highpass, 2- bandpass, or 3- bandstop", LoHi.ToString());
            }
        }
Пример #2
0
        // load the file, parse the data out, run any filtering operations, then run charting ops
        private void button1_Click(object sender, EventArgs e)
        {
            if (!File.Exists(textBox1.Text))
            {
                MessageBox.Show("Did not find file " + textBox1.Text);
                return;
            }
            try {
                List <Int64> listA = new List <Int64>();
                List <int>   listB = new List <int>();
                using (var reader = new StreamReader(textBox1.Text)) {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();

                        if (line == "")
                        {
                            continue;
                        }

                        var values = line.Split(',');

                        listA.Add(Int64.Parse(values[0]));
                        raw.Add((double)int.Parse(values[1]));
                    }
                }
                // do some signal filtering on the values:
                MathNet.Filtering.OnlineFilter lowpass = MathNet.Filtering.OnlineFilter.CreateLowpass(MathNet.Filtering.ImpulseResponse.Finite, 20, 2, 1);
                MathNet.Filtering.OnlineFilter denoise = MathNet.Filtering.OnlineFilter.CreateDenoise(3);
                output1 = denoise.ProcessSamples(raw.Select(a => (double)a).ToArray()).ToList();
                output2 = lowpass.ProcessSamples(output1.Select(a => (double)a).ToArray()).ToList();

                chart1.Series.Clear();

                // graph the data
                var series1 = new System.Windows.Forms.DataVisualization.Charting.Series {
                    Name              = "Series1",
                    Color             = System.Drawing.Color.Green,
                    IsVisibleInLegend = false,
                    IsXValueIndexed   = false,
                    ChartType         = SeriesChartType.Line
                };

                this.chart1.Series.Add(series1);

                int i = 0;
                foreach (var point in raw.ToArray())
                {
                    series1.Points.AddXY(i, point);
                    i++;
                }

                var series2 = new System.Windows.Forms.DataVisualization.Charting.Series {
                    Name              = "Series2",
                    Color             = System.Drawing.Color.Red,
                    IsVisibleInLegend = false,
                    IsXValueIndexed   = false,
                    ChartType         = SeriesChartType.Line
                };

                this.chart1.Series.Add(series2);

                int i2 = 0;
                foreach (var point in output1.ToArray())
                {
                    series2.Points.AddXY(i2, point);
                    i2++;
                    if (i2 > i - 1)
                    {
                        break;
                    }
                }


                var series3 = new System.Windows.Forms.DataVisualization.Charting.Series {
                    Name              = "Series3",
                    Color             = System.Drawing.Color.Blue,
                    IsVisibleInLegend = false,
                    IsXValueIndexed   = false,
                    ChartType         = SeriesChartType.Line
                };

                this.chart1.Series.Add(series3);

                int i3 = 0;
                foreach (var point in output2.ToArray())
                {
                    series3.Points.AddXY(i3, point);
                    i3++;
                    if (i3 > i - 1)
                    {
                        break;
                    }
                }


                chart1.Invalidate();
                Application.DoEvents();
            }
            catch {
                // fall through
            }
        }
        void ConvertData(IProgressReporterDialogue sender)
        {
            string inFilePath = TXT_InData.Text;

            string[] lines = new string[1];
            if (File.Exists(inFilePath))
            {
                if (formProgressReporter != null)
                {
                    this.formProgressReporter.UpdateProgressAndStatus(1, "Reading input file.");
                }
                try
                {
                    lines = File.ReadAllLines(inFilePath);
                }
                catch (Exception e)
                {
                    if (formProgressReporter != null)
                    {
                        formProgressReporter.doWorkArgs.ErrorMessage = e.Message;
                    }
                    return;
                }
            }
            else
            {
                if (formProgressReporter != null)
                {
                    formProgressReporter.doWorkArgs.ErrorMessage = "Input file don't exist.";
                }
                return;
            }

            List <Tuple <string, double, double> > timeLatLonList = new List <Tuple <string, double, double> >();
            List <double> depthList = new List <double>();

            bool   dbt_OK        = false;
            bool   gga_OK        = false;
            double lat           = 0;
            double lon           = 0;
            string time          = "";
            double depth         = 0;
            int    progressIndex = 0;

            foreach (string line in lines)
            {
                if (progressIndex % 200 == 0)
                {
                    Thread.Sleep(1);
                }

                progressIndex++;
                //Si el usuario cancela el dialogo
                if (formProgressReporter != null && formProgressReporter.doWorkArgs.CancelRequested)
                {
                    formProgressReporter.doWorkArgs.CancelAcknowledged = true;
                    formProgressReporter.doWorkArgs.ErrorMessage       = "User Canceled.";
                    return;
                }

                //Reportar progreso
                if (formProgressReporter != null)
                {
                    this.formProgressReporter.UpdateProgressAndStatus((progressIndex * 70) / lines.Count(), "Extracting NMEA sentences.");
                }

                if (!string.IsNullOrEmpty(line))
                {
                    if (line.Contains("$GPDBT"))
                    {
                        string[] sp = line.Split(',');
                        depth  = double.Parse(sp[3], CultureInfo.InvariantCulture);
                        dbt_OK = true;
                    }

                    if (line.Contains("$GPGGA") || line.Contains("$GNGGA"))
                    {
                        string[] sp = line.Split(',');
                        time   = sp[1];
                        lat    = ToDecimalDegrees(sp[2] + sp[3]);
                        lon    = ToDecimalDegrees(sp[4] + sp[5]);
                        gga_OK = true;
                    }

                    continue;
                }

                //Cuando se llega a este punto, se ha completado un bloque.
                if (dbt_OK && gga_OK)
                {
                    timeLatLonList.Add(time, lat, lon);
                    depthList.Add(depth);
                }
                dbt_OK = false;
                gga_OK = false;
            }

            //Crear el Filtro - orden 30, determinado experimental
            MathNet.Filtering.OnlineFilter filter = MathNet.Filtering.OnlineFilter.CreateDenoise(30);

            //Reportar progreso
            if (formProgressReporter != null)
            {
                this.formProgressReporter.UpdateProgressAndStatus(80, "Applying filtering forward-backward.");
            }
            Thread.Sleep(1000);

            //Filtrar Forward y Backward
            double[] depthForward  = filter.ProcessSamples(depthList.ToArray());
            double[] depthBackward = filter.ProcessSamples(depthForward.Reverse().ToArray());
            depthBackward = depthBackward.Reverse().ToArray();

            //Reportar progreso
            if (formProgressReporter != null)
            {
                this.formProgressReporter.UpdateProgressAndStatus(85, "Preparing text structure.");
            }
            Thread.Sleep(1000);

            //Crear la lista string Lat,Lon,Depth
            List <string>    listOut = new List <string>();
            NumberFormatInfo nfi     = new NumberFormatInfo();
            NumberFormatInfo nfi2    = new NumberFormatInfo();

            nfi.NumberDecimalSeparator  = ".";
            nfi2.NumberDecimalSeparator = ".";
            nfi2.NumberDecimalDigits    = 1;

            for (int i = 0; i < timeLatLonList.Count(); i++)
            {
                DateTime dateTime = DateTime.ParseExact(timeLatLonList[i].Item1, "HHmmss.ff", null);
                string   st       = dateTime.TimeOfDay.TotalSeconds.ToString(nfi2);

                listOut.Add(st + "," +                                    // Time
                            timeLatLonList[i].Item2.ToString(nfi) + "," + // Lat
                            timeLatLonList[i].Item3.ToString(nfi) + "," + // Lon
                            depthList[i].ToString(nfi) + "," +
                            depthBackward[i].ToString(nfi));              // Depth
            }

            //Crear la lista string fotmato HYPACK
            List <string> listOutHYPACK = new List <string>();

            listOutHYPACK.Add("FTP NEW 2");
            listOutHYPACK.Add("TND 12:00:26 05/01/2019");
            listOutHYPACK.Add("DEV 0 32784 \"ECHOSOUNDER\" 0");
            listOutHYPACK.Add("DEV 1 32837 \"GPS\" 57348");
            listOutHYPACK.Add("USR \"GEOSUPPORT\" \"HYPACK\"");
            listOutHYPACK.Add("EOH");
            for (int i = 0; i < timeLatLonList.Count(); i++)
            {
                //EC2 0 1.0 2.8 0.000
                //POS 1 1.1 307916.529 6461349.538
                double[] east_north = new PointLatLngAlt(timeLatLonList[i].Item2, timeLatLonList[i].Item3).ToUTM();
                DateTime dateTime   = DateTime.ParseExact(timeLatLonList[i].Item1, "HHmmss.ff", null);
                string   st         = dateTime.TimeOfDay.TotalSeconds.ToString(nfi2);

                listOutHYPACK.Add("EC2 0 " + st + " " + depthBackward[i].ToString(nfi) + " 0.0");
                listOutHYPACK.Add("POS 1 " + st + " " + east_north[0].ToString(nfi) + " " + east_north[1].ToString(nfi));
            }

            //Reportar progreso
            if (formProgressReporter != null)
            {
                this.formProgressReporter.UpdateProgressAndStatus(90, "Writing the output file.");
            }
            Thread.Sleep(1000);

            //Escribir el archivo XYZ final
            try
            {
                File.WriteAllLines(TXT_OutData.Text + "\\" + Path.GetFileNameWithoutExtension(TXT_InData.Text) + ".XYZ", listOut.ToArray());
            }
            catch (Exception e)
            {
                if (formProgressReporter != null)
                {
                    formProgressReporter.doWorkArgs.ErrorMessage = e.Message;
                }
                return;
            }

            //Escribir el archivo RAW final
            try
            {
                File.WriteAllLines(TXT_OutData.Text + "\\" + Path.GetFileNameWithoutExtension(TXT_InData.Text) + ".RAW", listOutHYPACK.ToArray());
            }
            catch (Exception e)
            {
                if (formProgressReporter != null)
                {
                    formProgressReporter.doWorkArgs.ErrorMessage = e.Message;
                }
                return;
            }

            //Reportar progreso
            if (formProgressReporter != null)
            {
                this.formProgressReporter.UpdateProgressAndStatus(100, "Done.");
            }
            Thread.Sleep(500);
        }