public void TestLowPass16()
        {
            const int    order           = 16;
            const double fs              = 31250d;
            const double cutoffFrequency = 300d;
            const double error           = 1e-4;

            var coeff = new LowPassButterworthCoefficients(order, fs, cutoffFrequency);
            var chain = coeff.Calculate();

            var expected     = Helpers.LoadCsv(Resources.LowPass16).ToList();
            var expectedGain = 3.444463173412853e-25;

            Assert.AreEqual(1, chain.GetTransitionBands(fs).Count());
            foreach (var f in chain.GetTransitionBands(fs))
            {
                Assert.AreEqual(f, cutoffFrequency, 1.0);
            }

            Assert.AreEqual(expected.Count, chain.Count);
            for (int i = 0; i < expected.Count; i++)
            {
                Helpers.ValidateBiquad(expected[i], chain[i], error);
            }
        }
        public void TestLowPass08()
        {
            const int    order           = 8;
            const double fs              = 44100d;
            const double cutoffFrequency = 500d;
            const double error           = 1e-4;

            var coeff = new LowPassButterworthCoefficients(order, fs, cutoffFrequency);
            var chain = coeff.Calculate();

            var expected = new[]
            {
                new Biquad(0.000000, 0.000000, 0.000000, 1.000000, -1.864749, 0.869491),
                new Biquad(1.000000, 2.000000, 1.000000, 1.000000, -1.883460, 0.888249),
                new Biquad(1.000000, 2.000000, 1.000000, 1.000000, -1.919040, 0.923920),
                new Biquad(1.000000, 2.000000, 1.000000, 1.000000, -1.967605, 0.972608),
            };

            Assert.AreEqual(1, chain.GetTransitionBands(fs).Count());
            foreach (var f in chain.GetTransitionBands(fs))
            {
                Assert.AreEqual(f, cutoffFrequency, 2.0);
            }

            Assert.AreEqual(expected.Length, chain.Count);
            for (int i = 0; i < expected.Length; i++)
            {
                Helpers.ValidateBiquad(expected[i], chain[i], error);
            }
        }
        public void TestLowPass2()
        {
            const int    order           = 2;
            const double fs              = 44100d;
            const double cutoffFrequency = 2000d;
            const double error           = 1e-4;

            var coeff = new LowPassButterworthCoefficients(order, fs, cutoffFrequency);
            var chain = coeff.Calculate();

            var expected = new[]
            {
                new Biquad(0.016819150107057118, 0.033638300214114236, 0.016819150107057118, 1, -1.6010923941836190, 0.66836899461184751),
            };

            Assert.AreEqual(1, chain.GetTransitionBands(fs).Count());
            foreach (var f in chain.GetTransitionBands(fs))
            {
                Assert.AreEqual(f, cutoffFrequency, 5.0);
            }

            Assert.AreEqual(expected.Length, chain.Count);
            for (int i = 0; i < expected.Length; i++)
            {
                Helpers.ValidateBiquad(expected[i], chain[i], error);
            }
        }
示例#4
0
        public void TestLowPassButtersworthFilteringWithDC()
        {
            const int    order           = 16;
            const int    fs              = 44100;
            const double cutoffFrequency = 2000d;
            const int    cycles          = 10;

            double[] frequencies      = { 770, 5830 };
            var      validFrequencies = frequencies.TakeWhile(f => f < cutoffFrequency);

            var coeff = new LowPassButterworthCoefficients(order, fs, cutoffFrequency);

            TestFilterWithDC(order, fs, cycles, coeff, frequencies, validFrequencies, 1.0);
        }
示例#5
0
        static void Main(string[] args)
        {
            const double noiseAmp = 1e-7d;

            Console.WriteLine("Noise amplitude: {0}.", noiseAmp);
            const double signalAmp = 0;

            Console.WriteLine("Signal amplitude: {0}.", signalAmp);
            const double signalFreq = 150;

            Console.WriteLine("Signal frequency: {0} Hz.", signalFreq);
            const double fs = 31250d;

            Console.WriteLine("Sampling frequency: {0} Hz.", fs);
            const double time = 0.15d;

            Console.WriteLine("Analysis time: {0} s.", time);
            const double lowCut = 5d;

            Console.WriteLine("Filter low cut: {0} Hz.", lowCut);
            const double highCut = 2000d;

            Console.WriteLine("Filter high cut: {0} Hz.", highCut);

            Console.WriteLine("Generating data...");
            var samples = new float[(int)Math.Ceiling(fs * time)];

            if (signalAmp > 0)
            {
                GenerateSinusoid(signalAmp, signalFreq, fs, samples);
            }
            if (noiseAmp > 0)
            {
                GenerateNoise(noiseAmp, samples);
            }

            Console.WriteLine("Designing filters...");
            var lowPass = new LowPassButterworthCoefficients(5, fs, highCut).Calculate();

            Console.WriteLine("Low pass filter:");
            foreach (var biquad in lowPass)
            {
                Console.WriteLine(biquad);
            }
            var highPass = new HighPassButterworthCoefficients(3, fs, highCut).Calculate();

            Console.WriteLine("High pass filter:");
            foreach (var biquad in highPass)
            {
                Console.WriteLine(biquad);
            }

            Console.WriteLine("Processing data...");
            var result = new float[samples.Length];

            Array.Copy(samples, result, samples.Length);
            lowPass.Filter(result, 0, result, 0, result.Length);
            highPass.Filter(result, 0, result, 0, result.Length);

            Console.WriteLine("Result:");
            var min   = double.MaxValue;
            var max   = double.MinValue;
            var sum   = 0d;
            var sqsum = 0d;

            for (int i = 0; i < result.Length; i++)
            {
                var value = result[i];
                min    = Math.Min(min, value);
                max    = Math.Max(max, value);
                sum   += value;
                sqsum += value * value;
            }
            var mean     = sum / result.Length;
            var variance = (sqsum - result.Length * mean * mean) / (result.Length - 1);

            Console.WriteLine("Minimum: {0}.", min);
            Console.WriteLine("Maximum: {0}.", max);
            Console.WriteLine("Mean value: {0}.", mean);
            Console.WriteLine("Variance: {0}.", variance);
            Console.WriteLine("Standard deviation: {0}.", Math.Sqrt(variance));
        }