Example #1
1
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using (var stream = new FileStream(ofd.FileName, FileMode.Open))
            {
                var waveFile = new WaveFile(stream);
                _signal = waveFile[Channels.Left];
            }

            _stft = new Stft(_frameSize, _hopSize);

            var frameDuration = (double)_frameSize / _signal.SamplingRate;
            var hopDuration   = (double)_hopSize / _signal.SamplingRate;

            var freqs = new[] { 300f, 600, 1000, 2000, 4000, 7000 };

            var pitchExtractor = new PitchExtractor(_signal.SamplingRate, frameDuration, hopDuration, high: 900 /*Hz*/);
            var pitchTrack     = pitchExtractor.ParallelComputeFrom(_signal)
                                 .Select(p => p.Features[0])
                                 .ToArray();

            var tdExtractor       = new TimeDomainFeaturesExtractor(_signal.SamplingRate, "all", frameDuration, hopDuration);
            var spectralExtractor = new SpectralFeaturesExtractor(_signal.SamplingRate, "sc+sn", frameDuration, hopDuration, frequencies: freqs);
            var mpeg7Extractor    = new Mpeg7SpectralFeaturesExtractor(_signal.SamplingRate, "all", frameDuration, hopDuration);

            mpeg7Extractor.IncludeHarmonicFeatures("all");
            mpeg7Extractor.SetPitchTrack(pitchTrack);

            tdExtractor.AddFeature("pitch_zcr", (signal, start, end) => { return(Pitch.FromZeroCrossingsSchmitt(signal, start, end)); });
            //spectralExtractor.AddFeature("pitch_hss", (spectrum, fs) => { return Pitch.FromHss(spectrum, _signal.SamplingRate); } );

            var tdVectors       = tdExtractor.ParallelComputeFrom(_signal);
            var spectralVectors = spectralExtractor.ParallelComputeFrom(_signal);
            var mpeg7Vectors    = mpeg7Extractor.ParallelComputeFrom(_signal);

            _vectors = FeaturePostProcessing.Join(tdVectors, spectralVectors, mpeg7Vectors);

            //FeaturePostProcessing.NormalizeMean(_vectors);
            //FeaturePostProcessing.AddDeltas(_vectors);

            var descriptions = tdExtractor.FeatureDescriptions
                               .Concat(spectralExtractor.FeatureDescriptions)
                               .Concat(mpeg7Extractor.FeatureDescriptions);

            FillFeaturesList(_vectors, descriptions);

            spectrogramPlot.ColorMapName      = "afmhot";
            spectrogramPlot.MarklineThickness = 2;
            spectrogramPlot.Spectrogram       = _stft.Spectrogram(_signal);
        }
Example #2
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using (var stream = new FileStream(ofd.FileName, FileMode.Open))
            {
                var waveFile = new WaveFile(stream);
                _signal = waveFile[Channels.Left];
            }

            _fftSize      = int.Parse(fftSizeTextBox.Text);
            _cepstrumSize = int.Parse(cepstrumSizeTextBox.Text);
            _hopSize      = int.Parse(hopSizeTextBox.Text);

            _fft = new RealFft(_fftSize);
            _cepstralTransform = new CepstralTransform(_cepstrumSize, _fftSize);

            var options = new PitchOptions
            {
                SamplingRate  = _signal.SamplingRate,
                FrameDuration = (double)_fftSize / _signal.SamplingRate,
                HopDuration   = (double)_hopSize / _signal.SamplingRate
            };

            var pitchExtractor = new PitchExtractor(options);

            _pitches = pitchExtractor.ParallelComputeFrom(_signal);

            _specNo = 0;
            specNoComboBox.DataSource = Enumerable.Range(1, _pitches.Count).ToArray();

            // obtain spectrogram

            _stft = new Stft(_fftSize, _hopSize, WindowTypes.Rectangular);
            var spectrogram = _stft.Spectrogram(_signal);

            spectrogramPanel.ColorMapName      = "viridis";
            spectrogramPanel.MarklineThickness = 6;
            spectrogramPanel.Spectrogram       = spectrogram.Select(s => s.Take(224).ToArray()).ToList();
            spectrogramPanel.Markline          = _pitches.Select(p => p[0] * _fftSize / _signal.SamplingRate).ToArray();
        }