Example #1
0
        public void Test_AcquireSignal()
        {
            // Arrange
            double[] h        = { 1, 1, 1 };
            double[] x        = { 3, -1, 0, 1, 3, 2, 0, 1, 2, 1 };
            double[] expected = { 3, 2, 2, 0, 4, 6, 5, 3, 3, 4, 3, 1 };
            int      M        = h.Length;
            int      N        = this.Signal.MinBlockLength(h.Length);
            int      L        = N - M + 1;

            Complex[] appendSequence = new Complex[L - 1];
            Array.Clear(appendSequence, 0, L - 1);
            // FIR filter frequency response
            List <Complex> hComplex = this.ToComplex(h).ToList();

            Complex[] hFFT  = Signal.ComputePaddedFFT(ref hComplex, N, appendSequence.ToList());
            double    phase = 0;

            // Act
            foreach (double dataPoint in x)
            {
                Signal.AcquireSignal(dataPoint, phase, 0, 0, L, ref hFFT);
            }
            Signal.LastSamplesConvolution(ref hFFT, L);
            double[] xhConvRe  = this.RealPart(Signal.baseBandSignal.ToArray());
            double[] xhConvImg = this.ImaginaryPart(Signal.baseBandSignal.ToArray());

            // Assert
            for (int i = 0; i < xhConvRe.Length; i++)
            {
                Assert.True(Math.Abs(xhConvRe[i] - expected[i]) < 1e-12);
                Assert.True(Math.Abs(xhConvImg[i]) < 1e-12);
            }
        }
Example #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);
            }
        }