private void timer1_Tick(object sender, EventArgs e) { LineItem kurvaMagnitude = zedGraphControl1.GraphPane.CurveList[0] as LineItem; IPointListEdit listMagnitude = kurvaMagnitude.Points as IPointListEdit; dataMagnitude = new double[1000]; try { for (int i = 0; i < 1000; i++) { dataMagnitude[i] = double.Parse(uno.ReadLine(), CultureInfo.InvariantCulture.NumberFormat); } } catch (Exception) { //uno.Close(); return; } OnlineFilter bandpass = OnlineFilter.CreateBandpass(MathNet.Filtering.ImpulseResponse.Finite, 1000, 20, 500); OnlineFilter bandstop = OnlineFilter.CreateBandstop(MathNet.Filtering.ImpulseResponse.Finite, 1000, 48.5, 51.5); OnlineFilter denoise = OnlineFilter.CreateDenoise(); dataFilter = new double[1000]; dataFilter = bandpass.ProcessSamples(dataMagnitude); dataFilter = bandstop.ProcessSamples(dataFilter); dataFilter = denoise.ProcessSamples(dataFilter); var N = Enumerable.Range(1, dataMagnitude.Length).ToArray(); waktu = Array.ConvertAll <int, double>(N, Convert.ToDouble); // double waktu = (Environment.TickCount - waktustart) / 1000.0; listMagnitude.Clear(); for (int i = 0; i < 1000; i++) { listMagnitude.Add(waktu[i], dataFilter[i]); } label1.Text = Convert.ToString(dataFilter[500]); //uno.Close(); zedGraphControl1.AxisChange(); zedGraphControl1.Refresh(); }
/// <summary> /// Samples the input stream and applies a band-stop filter (using MathNet.Filtering) over the sampled input stream. Removes middle frequencies. /// </summary> /// <param name="input">The input source of doubles.</param> /// <param name="sampleRate">The desired sample rate for the filter.</param> /// <param name="cutoffLowRate">The desired low frequency cutoff for the filter.</param> /// <param name="cutoffHighRate">The desired high frequency cutoff for the filter.</param> /// <param name="impulseResponse">Specifies how the filter will respond to an impulse input (Finite or Infinite). Default is Finite.</param> /// <param name="sampleInterpolator">Optional sampling interpolator over the input. Default is a reproducible linear interpolation.</param> /// <param name="alignmentDateTime">If non-null, this parameter specifies a time to align the sampled messages with, and the sampled messages /// will have originating times that align with (i.e., are an integral number of intervals away from) the specified alignment time.</param> /// <param name="deliveryPolicy">An optional delivery policy for the input stream.</param> /// <returns>A band-stop filtered version of the input.</returns> public static IProducer <double> BandstopFilter( this IProducer <double> input, double sampleRate, double cutoffLowRate, double cutoffHighRate, ImpulseResponse impulseResponse = ImpulseResponse.Finite, Interpolator <double, double> sampleInterpolator = null, DateTime?alignmentDateTime = null, DeliveryPolicy <double> deliveryPolicy = null) { var filter = OnlineFilter.CreateBandstop(impulseResponse, sampleRate, cutoffLowRate, cutoffHighRate); return(MathNetFilter(input, sampleRate, sampleInterpolator ?? Reproducible.Linear(), alignmentDateTime, filter, deliveryPolicy)); }
private void button4_Click(object sender, EventArgs e) { openFileDialog1.Multiselect = true; openFileDialog1.Title = "Open Files..."; openFileDialog1.FileName = ""; openFileDialog1.Filter = "CSV (comma delimited)|*.csv|All Files|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.Cancel) { MessageBox.Show("Choice Cancelled"); } else { folderBrowserDialog1.Description = "Choose Folder to Save..."; if (folderBrowserDialog1.ShowDialog() == DialogResult.Cancel) { MessageBox.Show("Choice Cancelled"); } else { var saveto = folderBrowserDialog1.SelectedPath; for (int i = 0; i < openFileDialog1.FileNames.Length; i++) { data = File.ReadAllLines(openFileDialog1.FileNames[i]); dataMagnitude = data.Select(x => double.Parse(x)).ToArray(); dataFilter = new double[dataMagnitude.Length]; OnlineFilter bandstop = OnlineFilter.CreateBandstop(MathNet.Filtering.ImpulseResponse.Finite, 80, 48.5, 51.5); // OnlineFilter denoise = OnlineFilter.CreateDenoise(); dataFilter = bandstop.ProcessSamples(dataMagnitude); //dataFilter = denoise.ProcessSamples(dataFilter); var path = Path.Combine(saveto, "Datafilter" + Convert.ToString(i) + ".csv"); File.WriteAllLines(path, Array.ConvertAll <double, string>(dataFilter, Convert.ToString), Encoding.Default); } MessageBox.Show("Done"); } } }
private void button2_Click(object sender, EventArgs e) { dataFilter = new double[dataMagnitude.Length]; OnlineFilter bandpass = OnlineFilter.CreateBandpass(ImpulseResponse.Finite, 1000, 20, 500); OnlineFilter bandstop = OnlineFilter.CreateBandstop(MathNet.Filtering.ImpulseResponse.Finite, 1000, 49, 51); OnlineFilter denoise = OnlineFilter.CreateDenoise(); dataFilter = bandpass.ProcessSamples(dataMagnitude); dataFilter = bandstop.ProcessSamples(dataFilter); dataFilter = denoise.ProcessSamples(dataFilter); LineItem kurvaMagnitude = zedGraphControl1.GraphPane.CurveList[1] as LineItem; IPointListEdit listFilter = kurvaMagnitude.Points as IPointListEdit; listFilter.Clear(); for (int i = 1; i < dataFilter.Length; i++) { listFilter.Add(dataN[i], dataFilter[i]); } zedGraphControl1.AxisChange(); zedGraphControl1.Invalidate(); button2.Enabled = false; button3.Enabled = true; }
private void InitPlots(CallModel callModel) { BatCall call = callModel.Call; PlotModel pmPower = new PlotModel(); LineSeries pwrLineSeries = new LineSeries(); pwrLineSeries.LineJoin = LineJoin.Round; pmPower.Series.Add(pwrLineSeries); pmPower.Axes.Add(new LinearAxis { Maximum = 260, Minimum = 0, Position = AxisPosition.Left, Title = "Intensität" }); pmPower.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Dauer [ms]" }); if (call.PowerData != null) { pwrLineSeries.Points.AddRange(call.PowerData.Select((b, i) => new DataPoint(i * 0.251, b))); } //if (call.OriginalCalls.Count > 1) //{ // int leftSum = call.OriginalCalls[0].PowerData.Length; // for (int i = 0; i < call.OriginalCalls.Count-1; i++) // { // int width = (int)Math.Round((call.OriginalCalls[i + 1].StartTimeMs - call.OriginalCalls[i].EndTimeMs) / 0.251); // double left = leftSum* 0.251; // double right = left + (width* 0.251); // RectangleAnnotation annotation = new RectangleAnnotation { Fill = OxyColors.LightGray, Layer = AnnotationLayer.BelowAxes, MinimumX = left, MaximumX = right, MinimumY = 0, MaximumY = 260 }; // //LineAnnotation lineAnnotation = new LineAnnotation { Color = OxyColors.Red, StrokeThickness = 2, LineStyle = LineStyle.Dash, Type = LineAnnotationType.Vertical, X = linePos }; // pmPower.Annotations.Add(annotation); // leftSum = leftSum + width + call.OriginalCalls[i].PowerData.Length; // } //} OnlineFilter filter = OnlineFilter.CreateBandstop(ImpulseResponse.Infinite, 20, 20, 150); LineSeries pwrLineSeriesAvg = new LineSeries(); pwrLineSeriesAvg.LineJoin = LineJoin.Round; pwrLineSeriesAvg.Color = OxyColors.Blue; pmPower.Series.Add(pwrLineSeriesAvg); //double[] input = call.PowerData.Select(d => (double)d).ToArray(); //double[] data = filter.ProcessSamples(input); //pwrLineSeriesAvg.Points.AddRange(data.Select((d, i) => new DataPoint(i * 0.251, d))); int[] window = new int[] { 2, 4, 8, 4, 2 }; int divisor = window.Sum(); byte[] input = call.PowerData; for (int i = 0; i < input.Length; i++) { double sum = 0; for (int j = 0; j < 5; j++) { int x = i + (j - 2); double q; if (x < 0) { q = input[0]; } else if (x >= input.Length) { q = input[input.Length - 1]; } else { q = input[x]; } sum += q * window[j]; } pwrLineSeriesAvg.Points.Add(new DataPoint(i * 0.251, sum / divisor)); } LineSeries pwrLineSeriesAvg2 = new LineSeries(); pwrLineSeriesAvg2.LineJoin = LineJoin.Round; pwrLineSeriesAvg2.Color = OxyColors.Crimson; pmPower.Series.Add(pwrLineSeriesAvg2); int avgCount = 7; int[] ringBuffer = Enumerable.Repeat(-1, avgCount).ToArray(); int bufferIndex = 0; for (int i = 0; i < call.PowerData.Length; i++) { ringBuffer[bufferIndex++] = call.PowerData[i]; if (bufferIndex >= ringBuffer.Length) { bufferIndex = 0; } if (i > 4) { int c = 0; double mAvg = 0; for (int j = 0; j < ringBuffer.Length; j++) { if (ringBuffer[j] >= 0) { c++; mAvg += ringBuffer[j]; } } pwrLineSeriesAvg2.Points.Add(new DataPoint((i - 4) * 0.251, mAvg / c)); } } LineAnnotation lineAnnotation = new LineAnnotation { Color = OxyColors.Red, StrokeThickness = 2, LineStyle = LineStyle.Dash, Type = LineAnnotationType.Horizontal, Y = call.AveragePower }; pmPower.Annotations.Add(lineAnnotation); Power = pmPower.AddStyles(); PlotModel pmFreq = new PlotModel(); ColumnSeries freqSeries = new ColumnSeries(); pmFreq.Series.Add(freqSeries); CategoryAxis item = new CategoryAxis(); item.Position = AxisPosition.Bottom; item.Title = "Frequenz [kHz]"; item.GapWidth = 0.1; item.IsTickCentered = true; item.LabelFormatter = i => LogAnalyzer.GetFrequencyFromFftBin((int)i).ToString(CultureInfo.CurrentCulture); item.MajorGridlineThickness = 0; pmFreq.Axes.Add(item); if (call.FftData != null) { freqSeries.Items.AddRange(call.FftData.Select((f, i) => { ColumnItem columnItem = new ColumnItem(f, i); columnItem.Color = i == call.MaxFrequencyBin ? OxyColors.Red : OxyColors.Green; return(columnItem); })); } Frequency = pmFreq.AddStyles(); }