Exemple #1
0
        public static void PreprocessForWordCount(string filePath)
        {
            Console.WriteLine($"Pre-processing for word count of file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var            waveFile = new WaveFile(afr);
                DiscreteSignal signal   = waveFile[Channels.Left];

                // smooth signal via moving average filter
                var            maFilter       = new MovingAverageFilter(19);
                DiscreteSignal smoothedSignal = maFilter.ApplyTo(signal);

                // instance of SimpleGate
                SimpleGate sg = new SimpleGate(30, 30, smoothedSignal.SamplingRate);

                for (int i = 0; i < smoothedSignal.Length; i++)
                {
                    double inValue = smoothedSignal.Samples[i];

                    sg.Process(ref inValue);

                    smoothedSignal.Samples[i] = (float)inValue;
                }

                using (var stream = new FileStream($"{DATA_PATH}/preprocessed.wav", FileMode.Create))
                {
                    var signalFile = new WaveFile(smoothedSignal);
                    signalFile.SaveTo(stream);
                }
            }
        }
        public static DiscreteSignal PreprocessAudio(DiscreteSignal signal)
        {
            // smooth signal via moving average filter
            var maFilter = new MovingAverageFilter(19);
            DiscreteSignal smoothedSignal = maFilter.ApplyTo(signal);



            using (var stream = new FileStream("smoothedSignal.wav", FileMode.Create))
            {
                var signalFile = new WaveFile(smoothedSignal);
                signalFile.SaveTo(stream);
            }



            // pre-process signal with SimpleGate
            SimpleGate sg = new SimpleGate(30, 30, smoothedSignal.SamplingRate);

            // apply for each measured sample
            for (int i = 0; i < smoothedSignal.Length; i++)
            {
                double inValue = smoothedSignal.Samples[i];

                sg.Process(ref inValue);

                smoothedSignal.Samples[i] = (float)inValue;
            }

            // apply envelope operation
            DiscreteSignal envelopeSignal = Operation.Envelope(smoothedSignal);

            using (var stream = new FileStream("envelopeSignal.wav", FileMode.Create))
            {
                var signalFile = new WaveFile(envelopeSignal);
                signalFile.SaveTo(stream);
            }

            return envelopeSignal;
        }
Exemple #3
0
        public static void TestMovingAverageFilter(string filePath)
        {
            Console.WriteLine($"Testing 'MovingAverage' filters on file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var            waveFile = new WaveFile(afr);
                DiscreteSignal signal   = waveFile[Channels.Left];

                int[] wSizes = new int[] { 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51 };

                for (int i = 0; i < wSizes.Length; i++)
                {
                    // build moving average filter of specified window size
                    int wSize    = wSizes[i];
                    var maFilter = new MovingAverageFilter(wSize);

                    // apply moving average filter
                    DiscreteSignal smoothed = maFilter.ApplyTo(signal);

                    // compute signal differences
                    DiscreteSignal diff = Operation.SpectralSubtract(signal, smoothed);

                    using (var stream = new FileStream($"{DATA_PATH}/moving-average-{wSize}.wav", FileMode.Create))
                    {
                        var smoothedFile = new WaveFile(smoothed);
                        smoothedFile.SaveTo(stream);
                    }

                    using (var stream = new FileStream($"{DATA_PATH}/diff-original-smoothed-{wSize}.wav", FileMode.Create))
                    {
                        var diffFile = new WaveFile(diff);
                        diffFile.SaveTo(stream);
                    }
                }
            }
        }
Exemple #4
0
        public void Run()
        {
            var output2  = new float[_signal.Length];
            var output4  = new float[_signal.Length];
            var output5  = new float[_signal.Length];
            var outputZi = new float[_signal.Length];

            var samples = _signal.Samples;

            for (var i = 0; i < samples.Length; i++)
            {
                output4[i]  = _filterV4BiQuad.Process(samples[i]);
                output5[i]  = _filterV5BiQuad.Process(samples[i]);
                outputZi[i] = _filterZiBiQuad.Process(samples[i]);
            }

            var diffAverageV4 = output5.Zip(output4, (o5, o4) => Math.Abs(o5 - o4)).Average();
            var diffAverageZi = output5.Zip(outputZi, (o5, zi) => Math.Abs(o5 - zi)).Average();

            Console.WriteLine($"Average difference Ver.0.9.5 vs. Ver.0.9.4 : {diffAverageV4}");
            Console.WriteLine($"Average difference IirFilter vs. ZiFilter : {diffAverageZi}");

            for (var i = 0; i < samples.Length; i++)
            {
                output4[i]  = _filterV4Butterworth6.Process(samples[i]);
                output5[i]  = _filterV5Butterworth6.Process(samples[i]);
                outputZi[i] = _filterZiButterworth6.Process(samples[i]);
            }

            diffAverageV4 = output5.Zip(output4, (o5, o4) => Math.Abs(o5 - o4)).Average();
            diffAverageZi = output5.Zip(outputZi, (o5, zi) => Math.Abs(o5 - zi)).Average();

            Console.WriteLine($"Average difference Ver.0.9.5 vs. Ver.0.9.4 : {diffAverageV4}");
            Console.WriteLine($"Average difference IirFilter vs. ZiFilter : {diffAverageZi}");


            // === MISC ====

            var med  = new MedianFilter();
            var med2 = new MedianFilter2();

            var medOut  = med.ApplyTo(_signal).Samples;
            var medOut2 = med2.ApplyTo(_signal).Samples;

            var diffAverageMed = medOut.Zip(medOut, (m1, m2) => Math.Abs(m1 - m2)).Average();

            Console.WriteLine($"Average difference MedianFilter vs. MedianFilter2 : {diffAverageMed}");


            var ma    = new MovingAverageFilter();
            var maRec = new MovingAverageRecursiveFilter();

            var maOut    = ma.ApplyTo(_signal).Samples;
            var maRecOut = maRec.ApplyTo(_signal).Samples;

            var diffAverageMa = maOut.Zip(maRecOut, (m1, m2) => Math.Abs(m1 - m2)).Average();

            Console.WriteLine($"Average difference MovingAverageFilter vs. MovingAverageRecursiveFilter : {diffAverageMa}");


            // 32bit vs. 64bit

            var fir32 = new FirFilter(DesignFilter.FirWinLp(7, 0.1));
            var fir64 = new FirFilter64(DesignFilter.FirWinLp(7, 0.1));

            var fir32Out = fir32.ApplyTo(_signal).Samples;
            var fir64Out = fir64.ApplyTo(_signal.Samples.ToDoubles());

            var diffAverageFir = fir64Out.Zip(fir32Out, (m1, m2) => Math.Abs(m1 - m2)).Average();

            Console.WriteLine($"Average difference FirFilter vs. FirFilter64 : {diffAverageFir}");


            var iir32 = new IirFilter(_filterV5Butterworth6.Tf);
            var iir64 = new IirFilter64(_filterV5Butterworth6.Tf);

            var iir32Out = iir32.ApplyTo(_signal).Samples;
            var iir64Out = iir64.ApplyTo(_signal.Samples.ToDoubles());

            var diffAverageIir = iir64Out.Zip(iir32Out, (m1, m2) => Math.Abs(m1 - m2)).Average();

            Console.WriteLine($"Average difference IirFilter vs. IirFilter64 : {diffAverageIir}");


            var zi32 = new ZiFilter(_filterV5Butterworth6.Tf);
            var zi64 = new ZiFilter64(_filterV5Butterworth6.Tf);

            var zi32Out = zi32.ApplyTo(_signal).Samples;
            var zi64Out = zi64.ApplyTo(_signal.Samples.ToDoubles());

            var diffAverageZis = zi64Out.Zip(zi32Out, (m1, m2) => Math.Abs(m1 - m2)).Average();

            Console.WriteLine($"Average difference ZiFilter vs. ZiFilter64 : {diffAverageZis}");

            zi32Out = zi32.ZeroPhase(_signal).Samples;
            zi64Out = zi64.ZeroPhase(_signal.Samples.ToDoubles());

            var diffAverageZiZeroPhase = zi64Out.Zip(zi32Out, (m1, m2) => Math.Abs(m1 - m2)).Average();

            Console.WriteLine($"Average difference ZiFilter vs. ZiFilter64 (zero-phase): {diffAverageZiZeroPhase}");
        }
Exemple #5
0
 public void MovingAverageFilter()
 {
     var output = _filter.ApplyTo(_signal);
 }