Example #1
0
        /// <summary>
        /// Make Slaney's filter bank
        /// </summary>
        /// <param name="filterbankSize"></param>
        /// <param name="samplingRate"></param>
        /// <param name="fftSize"></param>
        /// <param name="frameDuration"></param>
        /// <param name="lowFreq"></param>
        /// <param name="highFreq"></param>
        /// <param name="normalize"></param>
        /// <returns></returns>
        private static float[][] MakeFilterbank(int filterbankSize,
                                                int samplingRate,
                                                int fftSize,
                                                double frameDuration,
                                                double lowFreq  = 0,
                                                double highFreq = 0,
                                                bool normalize  = true)
        {
            var frameSize = (int)(frameDuration * samplingRate);

            fftSize = fftSize > frameSize ? fftSize : MathUtils.NextPowerOfTwo(frameSize);

            return(FilterBanks.MelBankSlaney(filterbankSize, fftSize, samplingRate, lowFreq, highFreq, normalize));
        }
Example #2
0
        public MfccSlaneyOptions(int samplingRate,
                                 int featureCount,
                                 double frameDuration,
                                 double lowFrequency  = 0,
                                 double highFrequency = 0,
                                 int filterbankSize   = 40,
                                 int fftSize          = 0,
                                 bool normalize       = true)
        {
            var frameSize = (int)(frameDuration * samplingRate);

            fftSize = fftSize > frameSize ? fftSize : MathUtils.NextPowerOfTwo(frameSize);

            FilterBank     = FilterBanks.MelBankSlaney(filterbankSize, fftSize, samplingRate, lowFrequency, highFrequency, normalize);
            FilterBankSize = filterbankSize;
            FeatureCount   = featureCount;
            FftSize        = fftSize;
            SamplingRate   = samplingRate;
            LowFrequency   = lowFrequency;
            HighFrequency  = highFrequency;
            NonLinearity   = NonLinearityType.LogE;
        }
Example #3
0
        private void buttonCompute_Click(object sender, EventArgs e)
        {
            var filterCount  = int.Parse(textBoxSize.Text);
            var samplingRate = _signal.SamplingRate;
            var fftSize      = int.Parse(textBoxFftSize.Text);
            var lowFreq      = float.Parse(textBoxLowFreq.Text);
            var highFreq     = float.Parse(textBoxHighFreq.Text);

            Tuple <double, double, double>[] bands;
            float[][]  filterbank = null;
            VtlnWarper vtln       = null;

            if (checkBoxVtln.Checked)
            {
                var alpha    = float.Parse(textBoxVtlnAlpha.Text);
                var vtlnLow  = float.Parse(textBoxVtlnLow.Text);
                var vtlnHigh = float.Parse(textBoxVtlnHigh.Text);

                vtln = new VtlnWarper(alpha, lowFreq, highFreq, vtlnLow, vtlnHigh);
            }

            switch (comboBoxFilterbank.Text)
            {
            case "Mel":
                bands = FilterBanks.MelBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;

            case "Mel Slaney":
                bands      = FilterBanks.MelBandsSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                filterbank = FilterBanks.MelBankSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxNormalize.Checked, vtln);
                break;

            case "Bark":
                bands = FilterBanks.BarkBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;

            case "Bark Slaney":
                bands      = FilterBanks.BarkBandsSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                filterbank = FilterBanks.BarkBankSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq);
                break;

            case "Critical bands":
                bands = FilterBanks.CriticalBands(filterCount, fftSize, samplingRate, lowFreq, highFreq);
                break;

            case "Octave bands":
                bands = FilterBanks.OctaveBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;

            case "ERB":
                bands      = null;
                filterbank = FilterBanks.Erb(filterCount, fftSize, samplingRate, lowFreq, highFreq);
                break;

            default:
                bands = FilterBanks.HerzBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;
            }

            if (bands != null && filterbank == null)
            {
                switch (comboBoxShape.Text)
                {
                case "Triangular":
                    filterbank = FilterBanks.Triangular(fftSize, samplingRate, bands, vtln, Utils.Scale.HerzToMel);
                    break;

                case "Trapezoidal":
                    filterbank = FilterBanks.Trapezoidal(fftSize, samplingRate, bands, vtln);
                    break;

                case "BiQuad":
                    filterbank = FilterBanks.BiQuad(fftSize, samplingRate, bands);
                    break;

                default:
                    filterbank = FilterBanks.Rectangular(fftSize, samplingRate, bands, vtln);
                    break;
                }

                if (checkBoxNormalize.Checked)
                {
                    FilterBanks.Normalize(filterCount, bands, filterbank);
                }
            }


            var spectrumType = (SpectrumType)comboBoxSpectrum.SelectedIndex;
            var nonLinearity = (NonLinearityType)comboBoxNonLinearity.SelectedIndex;
            var logFloor     = float.Parse(textBoxLogFloor.Text);

            var mfccExtractor = new MfccExtractor(//samplingRate, 13, 0.025, 0.01,
                samplingRate, 13, 512.0 / samplingRate, 0.01,
                filterbank: filterbank,
                //filterbankSize: 26,
                //highFreq: 8000,
                //preEmphasis: 0.97,
                //lifterSize: 22,
                //includeEnergy: true,
                spectrumType: spectrumType,
                nonLinearity: nonLinearity,
                dctType: comboBoxDct.Text,
                window: WindowTypes.Hamming,
                logFloor: logFloor);

            _mfccVectors = mfccExtractor.ComputeFrom(_signal);


            //_mfccVectors = mfccExtractor.ComputeFrom(_signal * 32768);
            //var mfccVectorsP = mfccExtractor.ParallelComputeFrom(_signal * 32768);

            //for (var i = 0; i < _mfccVectors.Count; i++)
            //{
            //    for (var j = 0; j < _mfccVectors[i].Features.Length; j++)
            //    {
            //        if (Math.Abs(_mfccVectors[i].Features[j] - mfccVectorsP[i].Features[j]) > 1e-32f)
            //        {
            //            MessageBox.Show($"Nope: {i} - {j}");
            //            return;
            //        }

            //        if (Math.Abs(_mfccVectors[i].TimePosition - mfccVectorsP[i].TimePosition) > 1e-32f)
            //        {
            //            MessageBox.Show($"Time: {i} - {j}");
            //            return;
            //        }
            //    }
            //}

            //FeaturePostProcessing.NormalizeMean(_mfccVectors);        // optional (but REQUIRED for PNCC!)
            //FeaturePostProcessing.AddDeltas(_mfccVectors);

            var header = mfccExtractor.FeatureDescriptions;

            //.Concat(mfccExtractor.DeltaFeatureDescriptions)
            //.Concat(mfccExtractor.DeltaDeltaFeatureDescriptions);

            FillFeaturesList(_mfccVectors, header);
            mfccListView.Items[0].Selected = true;

            melFilterBankPanel.Groups = mfccExtractor.FilterBank;

            mfccPanel.Line = _mfccVectors[0].Features;
        }