Beispiel #1
0
        public void Test_PeakDetectionAlgorithm()
        {
            // Arrange
            var input = new List <double> {
                1.0, 1.0, 1.1, 1.0, 0.9, 1.0, 1.0, 1.1, 1.0, 0.9, 1.0,
                1.1, 1.0, 1.0, 0.9, 1.0, 1.0, 1.1, 1.0, 1.0, 1.0, 1.0, 1.1, 0.9, 1.0, 1.1, 1.0, 1.0, 0.9,
                1.0, 1.1, 1.0, 1.0, 1.1, 1.0, 0.8, 0.9, 1.0, 1.2, 0.9, 1.0, 1.0, 1.1, 1.2, 1.0, 1.5, 1.0,
                3.0, 2.0, 5.0, 3.0, 2.0, 1.0, 1.0, 1.0, 0.9, 1.0, 1.0, 3.0, 2.6, 4.0, 3.0, 3.2, 2.0, 1.0,
                1.0, 0.8, 4.0, 4.0, 2.0, 2.5, 1.0, 1.0, 1.0
            };
            var timeStamps    = new List <double>(new double[input.Count]);
            var expectedPeaks = new List <double> {
                45, 49, 60, 68
            };
            List <Complex> inputSignal = ToComplex(input.ToArray()).ToList();

            for (int i = 0; i < timeStamps.Count; i++)
            {
                timeStamps[i] = i;
            }
            int    lag       = 30;
            double threshold = 5;
            double influence = 0;

            // Act
            PeakDetectionOutput detector = PeakDetection.PeakDetectionAlgorithm(inputSignal, timeStamps, lag, threshold, influence);

            // Assert
            Assert.Equal(expectedPeaks.Count, detector.peakIndexes.Count);
            for (int i = 0; i < expectedPeaks.Count; i++)
            {
                Assert.Equal(expectedPeaks.ElementAt(i), detector.peakIndexes.ElementAt(i));
            }
        }
Beispiel #2
0
        /// <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);
            }
        }