/// <summary> /// Main entry point /// </summary> /// <param name="args">= Input program arguments</param> static void Main(string[] args) { try { Dictionary <string, string> progArgs = Parse(args); // Program arguments double fs = Convert.ToDouble(progArgs["sampling"]); double Ts = 1.0 / fs; double fc = Convert.ToDouble(progArgs["carrier"]); double passBand = Convert.ToDouble(progArgs["passband"]); double stopBand = Convert.ToDouble(progArgs["cutoffband"]); double gainDeviation = Convert.ToDouble(progArgs["gaindev"]); int peakLag = Convert.ToInt32(progArgs["peaklag"]); double peakThreshold = Convert.ToDouble(progArgs["peakthreshold"]); double peakInfluence = Convert.ToDouble(progArgs["peakinfluence"]); double tiThreshold = Convert.ToDouble(progArgs["tithreshold"]); Dsp Signal = new Dsp(); // Low pass FIR filter List <Complex> firImpulseResponse = Signal.KaiserWindowFir(gainDeviation, passBand, stopBand, fs); // FIR filter length int M = firImpulseResponse.Count; // Block length int N = Signal.MinBlockLength(M); // FFT input sequence length int L = N - M + 1; Complex[] appendSequence = new Complex[L - 1]; Array.Clear(appendSequence, 0, L - 1); // FIR filter frequency response Complex[] firFFT = Signal.ComputePaddedFFT(ref firImpulseResponse, N, appendSequence.ToList()); // Main class output values List <double> timeStamps = new List <double>(); List <double> peakTimeStamps = new List <double>(); List <double> peakTimeIntervals = new List <double>(); List <double> timeIntervalsMovingAverage = new List <double>(); List <int> peakIndexes = new List <int>(); List <int> outliersTimeIntervals = null; // Read data from input file using StreamReader inputFile = new StreamReader(progArgs["inputfile"]); WriteToFile outFile = new WriteToFile(progArgs["outputfile"]); WriteToFile timeIntervalFile = new WriteToFile(progArgs["tifile"]); inputFile.BaseStream.Seek(0, SeekOrigin.Begin); string str = inputFile.ReadLine(); double dLSBValue = -1; double dVoltageValue = -1; double dValue = 0.0; double dAverage = 0.0; double dTimeStamp = 0.0; double dPhase = 0.0; double timeIntervalsMoveAverage = 0.0; double timeIntervalsMoveVariance = 0.0; double lastPeakTimeStamp = 0.0; int iNumSamples = 1; int numDetections = 0; int numMovingAverageSamples = 0; while (str != null) { string[] sDataValues = str.Split(','); try { dTimeStamp = ((double)iNumSamples - 1.0) * Ts; dLSBValue = Convert.ToDouble(sDataValues[0]); dVoltageValue = Convert.ToDouble(sDataValues[1]); dAverage = ((double)iNumSamples - 1.0) / ((double)iNumSamples) * dAverage + dVoltageValue / ((double)iNumSamples); // Remove DC offset dValue = dVoltageValue - dAverage; // Extract I and Q from signal and get base band signal bool bPeakDetection = Signal.AcquireSignal(dValue, dPhase, dTimeStamp, fc, L, ref firFFT); // Perform peak detection every L samples of the input sequence found from FFT transform if (bPeakDetection) { PeakDetectionOutput detector = PeakDetection.PeakDetectionAlgorithm( Signal.baseBandSignal.GetRange(numDetections * L, L), Signal.timeStamp.GetRange(numDetections * L, L), peakLag, peakThreshold, peakInfluence, lastPeakTimeStamp, numDetections * L ); // Shift by L samples numDetections += 1; // Compute moving average of time intervals numMovingAverageSamples += detector.peakTimeIntervals.Count; outliersTimeIntervals = PeakDetection.TimeIntervalOutlierDetection( detector.peakTimeIntervals, numMovingAverageSamples, timeIntervalsMoveAverage, ref timeIntervalsMoveVariance, tiThreshold ); double currentWindowSum = detector.peakTimeIntervals.Sum(); timeIntervalsMoveAverage = (numMovingAverageSamples - detector.peakTimeIntervals.Count) * timeIntervalsMoveAverage / numMovingAverageSamples + currentWindowSum / numMovingAverageSamples; // Time stamp for the next block of data to find the time intervals if (detector.peakTimeStamps.Count > 0) { lastPeakTimeStamp = detector.peakTimeStamps.Last(); } // Store outputs either list or write to a file timeIntervalFile.SummaryOutliers( detector, outliersTimeIntervals, timeIntervalsMoveAverage, Math.Sqrt(timeIntervalsMoveVariance), tiThreshold ); timeIntervalsMovingAverage.Add(timeIntervalsMoveAverage); peakTimeStamps.AddRange(detector.peakTimeStamps); peakIndexes.AddRange(detector.peakIndexes); peakTimeIntervals.AddRange(detector.peakTimeIntervals); } timeStamps.Add(dTimeStamp); iNumSamples += 1; } catch (FormatException) { } catch (IndexOutOfRangeException) { } str = inputFile.ReadLine(); } Signal.LastSamplesConvolution(ref firFFT, L); SignalOutput(outFile, Signal.baseBandSignal); outFile.outFileHandler.Close(); timeIntervalFile.outFileHandler.Close(); Console.WriteLine("End of Test"); } catch (Exception e) { Console.WriteLine(e); } }