コード例 #1
0
ファイル: PitchForm.cs プロジェクト: zlwind/NWaves
        private void UpdateSpectrumAndCepstrum()
        {
            var fftSize      = int.Parse(fftSizeTextBox.Text);
            var cepstrumSize = int.Parse(cepstrumSizeTextBox.Text);

            _hopSize = int.Parse(hopSizeTextBox.Text);

            if (fftSize != _fftSize)
            {
                _fftSize           = fftSize;
                _fft               = new RealFft(fftSize);
                _cepstralTransform = new CepstralTransform(cepstrumSize, _fftSize);
            }

            if (cepstrumSize != _cepstrumSize)
            {
                _cepstrumSize      = cepstrumSize;
                _cepstralTransform = new CepstralTransform(_cepstrumSize, _fftSize);
            }

            var pos   = _hopSize * _specNo;
            var block = _signal[pos, pos + _fftSize];

            //block.ApplyWindow(WindowTypes.Hamming);

            var cepstrum = new float[_fftSize];

            _cepstralTransform.RealCepstrum(block.Samples, cepstrum);

            // ************************************************************************
            //      just visualize spectrum estimated from cepstral coefficients:
            // ************************************************************************

            var real = new float[_fftSize];
            var imag = new float[_fftSize];

            for (var i = 0; i < 32; i++)
            {
                real[i] = cepstrum[i];
            }

            _fft.Direct(real, real, imag);

            var spectrum = _fft.PowerSpectrum(block, normalize: false).Samples;
            var avg      = spectrum.Average(s => LevelScale.ToDecibel(s));

            var spectrumEstimate = real.Take(_fftSize / 2 + 1)
                                   .Select(s => (float)LevelScale.FromDecibel(s * 40 - avg))
                                   .ToArray();

            spectrumPanel.Line     = spectrum;
            spectrumPanel.Markline = spectrumEstimate;
            spectrumPanel.ToDecibel();

            var pitch = Pitch.FromCepstrum(block);

            cepstrumPanel.Line = cepstrum;
            cepstrumPanel.Mark = (int)(_signal.SamplingRate / pitch);
        }
コード例 #2
0
ファイル: LinePlot.cs プロジェクト: zlwind/NWaves
        public void ToDecibel()
        {
            if (_line == null)
            {
                return;
            }

            if (_logLine == null)
            {
                _logLine = _line.Select(l =>
                {
                    var val = (float)LevelScale.ToDecibel(l);
                    //if (float.IsNaN(val)) val = Height / 2 + 1;
                    if (float.IsNaN(val) || Math.Abs(val) > int.MaxValue)
                    {
                        val = Height / 2 + 1;
                    }
                    return(val / Gain ?? val);
                })
                           .ToArray();
            }
            else
            {
                _logLine = null;
            }

            if (_markline != null)
            {
                if (_logMarkline == null)
                {
                    _logMarkline = _markline.Select(l =>
                    {
                        var val = (float)LevelScale.ToDecibel(l);
                        if (float.IsNaN(val) || Math.Abs(val) > Height)
                        {
                            val = Height / 2 + 1;
                        }
                        return(val / Gain ?? val);
                    })
                                   .ToArray();
                }
                else
                {
                    _logMarkline = null;
                }
            }

            MakeBitmap();
            Invalidate();
        }
コード例 #3
0
        // Update chart of frequency domain
        private void UpdateSpectra()
        {
            var fftSize      = int.Parse(fftSizeTextBox.Text);
            var cepstrumSize = int.Parse(cepstrumSizeTextBox.Text);

            _hopSize = int.Parse(hopSizeTextBox.Text);

            if (fftSize != _fftSize)
            {
                _fftSize           = fftSize;
                _fft               = new Fft(fftSize);
                _cepstralTransform = new CepstralTransform(cepstrumSize, _fftSize);
            }

            if (cepstrumSize != _cepstrumSize)
            {
                _cepstrumSize      = cepstrumSize;
                _cepstralTransform = new CepstralTransform(_cepstrumSize, _fftSize);
            }

            var pos   = _hopSize * _specNo;
            var block = _signal[pos, pos + _fftSize];

            block.ApplyWindow(WindowTypes.Hamming);

            var cepstrum = _cepstralTransform.Direct(block);

            var real = new float[_fftSize];
            var imag = new float[_fftSize];

            for (var i = 0; i < 32; i++)
            {
                real[i] = cepstrum[i];
            }

            _fft.Direct(real, imag);

            var spectrum = _fft.PowerSpectrum(block, normalize: false).Samples;
            var avg      = spectrum.Average(s => LevelScale.ToDecibel(s));

            var spectrumEstimate = real.Take(_fftSize / 2 + 1)
                                   .Select(s => (float)LevelScale.FromDecibel(s * 40 / _fftSize - avg))
                                   .ToArray();

            spectrumPanel.Line     = spectrum;
            spectrumPanel.Markline = spectrumEstimate;
            spectrumPanel.ToDecibel();

            spectrumPanel.max_freq_value = spectrum.Length * _signal.SamplingRate / fftSize;
        }