Beispiel #1
0
        public override ISignal CreateSignal()
        {
            var fi = new FileInfo(this.FileName);

            if (!fi.Exists)
            {
                return(null);
            }

            var file = File.ReadLines(this.FileName);

            var frequencies = new List <double>();
            var values      = new List <Complex>();

            double frequency;
            double mag;
            double phase;

            foreach (var line in file)
            {
                var fields = line.Split(',');

                if (fields.Length < 2)
                {
                    continue;
                }

                if (!double.TryParse(fields[0], NumberStyles.Any, CultureInfo.InvariantCulture, out frequency))
                {
                    continue;
                }

                if (!double.TryParse(fields[1], NumberStyles.Any, CultureInfo.InvariantCulture, out mag))
                {
                    continue;
                }

                if (fields.Length == 2)
                {
                    frequencies.Add(frequency);
                    values.Add(mag);
                }
                else if (fields.Length == 3)
                {
                    if (!double.TryParse(fields[1], NumberStyles.Any, CultureInfo.InvariantCulture, out phase))
                    {
                        continue;
                    }

                    frequencies.Add(frequency);
                    values.Add(Complex.FromPolarCoordinates(mag, phase));
                }
            }

            var series = new FftSeries(this.SampleRate, this.SignalLength);
            var ret    = Dsp.AdaptiveInterpolation(frequencies, values, series.Values.ToReadOnlyList(), false).ToReadOnlyList();

            return(new FiniteSignal(new FftSpectrum(series, ret), this.TimeOffset));
        }
Beispiel #2
0
        public void TestAdaptiveInterpolationComplex()
        {
            var x = new[] { 0.0, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 3.0, 20.0 };
            var m = new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };

            double[] p =
            {
                0,
                Math.PI * 0.25,
                Math.PI * 0.5,
                Math.PI * 0.75,
                Math.PI,
                Math.PI * 1.25,
                Math.PI * 1.5,
                Math.PI * 1.75,
                Math.PI * 2,
                Math.PI * 2.25
            };

            var x2 = new[] { 0.0, 1.0, 2.0, 3, 4, 5, 6, 7, 8, 9, 10 };

            var y = m.Zip(p, Complex.FromPolarCoordinates).ToReadOnlyList();

            var y2 = Dsp.AdaptiveInterpolation(x, y, x2).ToReadOnlyList();

            Assert.That(y2.Count == x2.Length);
            Assert.That(y2[0] == y[0]);
            FilterAssert.ListIsMonotonouslyRising(y2.Select(c => c.Magnitude));
            Assert.Less(y2[y2.Count - 1].Magnitude, 9.0);
            Assert.Greater(y2[y2.Count - 1].Magnitude, 8.0);

            Assert.That(Dsp.AdaptiveInterpolation(x, y, new List <double>()).ToReadOnlyList().Count == 0);

            Assert.Throws <ArgumentNullException>(() => Dsp.AdaptiveInterpolation(x, y, null).ToReadOnlyList());
            Assert.Throws <ArgumentNullException>(() => Dsp.AdaptiveInterpolation(x, (IReadOnlyList <Complex>)null, x2).ToReadOnlyList());
            Assert.Throws <ArgumentNullException>(() => Dsp.AdaptiveInterpolation(null, y, x2).ToReadOnlyList());
            Assert.Throws <ArgumentException>(() => Dsp.AdaptiveInterpolation(x2, y, x2).ToReadOnlyList());
            Assert.Throws <ArgumentException>(() => Dsp.AdaptiveInterpolation(new List <double>(), new List <Complex>(), x2).ToReadOnlyList());
        }
Beispiel #3
0
        public void TestAdaptiveInterpolation()
        {
            var x  = new[] { 0.0, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 3.0, 20.0 };
            var y  = new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
            var x2 = new[] { 0.0, 1.0, 2.0, 3, 4, 5, 6, 7, 8, 9, 10 };

            var y2 = Dsp.AdaptiveInterpolation(x, y, x2).ToReadOnlyList();

            Assert.That(y2.Count == x2.Length);
            Assert.That(y2[0] == y[0]);
            FilterAssert.ListIsMonotonouslyRising(y2);
            Assert.Less(y2[y2.Count - 1], 9.0);
            Assert.Greater(y2[y2.Count - 1], 8.0);

            Assert.That(Dsp.AdaptiveInterpolation(x, y, new List <double>()).ToReadOnlyList().Count == 0);

            Assert.Throws <ArgumentNullException>(() => Dsp.AdaptiveInterpolation(x, y, null).ToReadOnlyList());
            Assert.Throws <ArgumentNullException>(() => Dsp.AdaptiveInterpolation(x, (IReadOnlyList <double>)null, x2).ToReadOnlyList());
            Assert.Throws <ArgumentNullException>(() => Dsp.AdaptiveInterpolation(null, y, x2).ToReadOnlyList());
            Assert.Throws <ArgumentException>(() => Dsp.AdaptiveInterpolation(x2, y, x2).ToReadOnlyList());
            Assert.Throws <ArgumentException>(() => Dsp.AdaptiveInterpolation(new List <double>(), new List <double>(), x2).ToReadOnlyList());
        }
Beispiel #4
0
        protected override Series CreateGraph(ISignal signal)
        {
            var ret     = new LineSeries();
            var fsignal = signal as IFiniteSignal;
            var esignal = signal as IEnumerableSignal;
            var ssignal = signal as ISyntheticSignal;

            int         winstart;
            int         winlength;
            WindowModes winmode;
            WindowTypes wintype;

            if (fsignal != null)
            {
                winmode = this.FiniteWindowMode;
                wintype = this.FiniteWindowType;

                if (this.FiniteMode == FiniteModes.Fixed)
                {
                    winstart  = this.FiniteFixedStart;
                    winlength = this.FiniteFixedLength;
                }
                else if (this.FiniteMode == FiniteModes.Regular)
                {
                    winstart  = fsignal.Start;
                    winlength = fsignal.Length;
                }
                else if (this.FiniteMode == FiniteModes.Oversampled)
                {
                    winstart  = fsignal.Start;
                    winlength = fsignal.Length * this.FiniteOversampling;
                }
                else if (this.FiniteMode == FiniteModes.View)
                {
                    winstart  = this.ViewStart;
                    winlength = this.ViewLength;
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            else if (esignal != null)
            {
                winmode = this.EnumerableWindowMode;
                wintype = this.EnumerableWindowType;

                if (this.EnumerableMode == EnumerableModes.Fixed)
                {
                    winstart  = this.EnumerableFixedStart;
                    winlength = this.EnumerableFixedLength;
                }
                else if (this.EnumerableMode == EnumerableModes.View)
                {
                    winstart  = this.ViewStart;
                    winlength = this.ViewLength;
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            else if (ssignal != null)
            {
                winmode = this.SyntheticWindowMode;
                wintype = this.SyntheticWindowType;

                if (this.SyntheticMode == SyntheticModes.Ideal)
                {
                    ret.Points.AddRange(this.GetYValues(ssignal.Spectrum).Zip(ssignal.Spectrum.Frequencies.Values, (m, f) => new DataPoint(f, m)));
                    return(ret);
                }
                if (this.SyntheticMode == SyntheticModes.Fixed)
                {
                    winstart  = this.SyntheticFixedStart;
                    winlength = this.SyntheticFixedLength;
                }
                else if (this.SyntheticMode == SyntheticModes.View)
                {
                    winstart  = this.ViewStart;
                    winlength = this.ViewLength;
                }
                else if (this.SyntheticMode == SyntheticModes.FixedSymmetric)
                {
                    winstart  = -(this.SyntheticFixedSymmetricLength >> 1);
                    winlength = this.SyntheticFixedSymmetricLength;
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                winmode = this.InfiniteWindowMode;
                wintype = this.InfiniteWindowType;

                if (this.InfiniteMode == InfiniteModes.Fixed)
                {
                    winstart  = this.InfiniteFixedStart;
                    winlength = this.InfiniteFixedLength;
                }
                else if (this.InfiniteMode == InfiniteModes.View)
                {
                    winstart  = this.ViewStart;
                    winlength = this.ViewLength;
                }
                else if (this.InfiniteMode == InfiniteModes.FixedSymmetric)
                {
                    winstart  = -(this.InfiniteFixedSymmetricLength >> 1);
                    winlength = this.InfiniteFixedSymmetricLength;
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }

            var win = new Window(wintype, winstart, winlength, signal.SampleRate, winmode);

            var winsignal = signal.Multiply(win);

            var frequencies = winsignal.Spectrum.Frequencies.Values.ToReadOnlyList();
            var values      = this.GetYValues(winsignal.Spectrum).ToReadOnlyList();

            if (this.CustomResulutionEnabled)
            {
                if (this.CustomFrequencies == null)
                {
                    this.CustomFrequencies = new UniformSeries(this.StartFrequency, this.StopFrequency, this.NumberOfPoints, this.Logarithmic);
                }

                var fnew = this.CustomFrequencies.Values.ToReadOnlyList();

                values      = Dsp.AdaptiveInterpolation(frequencies, values, fnew, this.Logarithmic).ToReadOnlyList();
                frequencies = fnew;
            }

            if (this.Smoothing > 0)
            {
                values = Dsp.Smooth(frequencies, values, this.Smoothing, this.Logarithmic).ToReadOnlyList();
            }

            ret.Points.AddRange(values.Zip(frequencies, (m, f) => new DataPoint(f, m)));
            return(ret);
        }