Esempio n. 1
0
        public double[][] CreateSpectrogram(string pathToFilename, IWindowFunction windowFunction, int sampleRate, int overlap, int wdftSize)
        {
            // read 5512 Hz, Mono, PCM, with a specific proxy
            float[] samples = ReadMonoFromFile(pathToFilename, sampleRate, 0, 0);

            NormalizeInPlace(samples);

            int width = (samples.Length - wdftSize) / overlap;             /*width of the image*/

            double[][] frames        = new double[width][];
            double[]   complexSignal = new double[2 * wdftSize];           /*even - Re, odd - Img, thats how Exocortex works*/
            double[]   window        = windowFunction.GetWindow();
            for (int i = 0; i < width; i++)
            {
                // take 371 ms each 11.6 ms (2048 samples each 64 samples)
                for (int j = 0; j < wdftSize; j++)
                {
                    // Weight by Hann Window
                    complexSignal[2 * j] = window[j] * samples[(i * overlap) + j];

                    // need to clear out as fft modifies buffer (phase)
                    complexSignal[(2 * j) + 1] = 0;
                }

                lomonFFT.TableFFT(complexSignal, true);

                // When the input is purely real, its transform is Hermitian,
                // i.e., the component at frequency f_k is the complex conjugate of the component
                // at frequency -f_k, which means that for real inputs there is no information
                // in the negative frequency components that is not already available from the
                // positive frequency components.
                // Thus, n input points produce n/2+1 complex output points.
                // The inverses of this family assumes the same symmetry of its input,
                // and for an output of n points uses n/2+1 input points.

                // Transform output contains, for a transform of size N,
                // N/2+1 complex numbers, i.e. 2*(N/2+1) real numbers
                // our transform is of size N+1, because the histogram has n+1 bins
                double[] band = new double[(wdftSize / 2)];                 // Don't add te last band, i.e. + 1 is removed
                for (int j = 0; j < (wdftSize / 2); j++)                    // Don't add te last band, i.e. + 1 is removed
                {
                    double re  = complexSignal[2 * j];
                    double img = complexSignal[(2 * j) + 1];

                    band[j] = Math.Sqrt(((re * re) + (img * img)) * wdftSize);
                }

                frames[i] = band;
            }

            return(frames);
        }
        public DerivedNoteData FindNote(double[] audioBuffer)
        {
            if (audioBuffer.Length != _fftSize)
            {
                throw new InvalidOperationException(
                          $"{nameof(audioBuffer)} must be of length {_fftSize}");
            }

            _soFilter.Process(audioBuffer);
            _hanWindow.ApplyWindow(audioBuffer);

            {
                for (var i = 0; i < _fftSize; ++i)
                {
                    _fftBufferTmp[i * 2]     = audioBuffer[i];
                    _fftBufferTmp[i * 2 + 1] = 0;
                }
                _fft.TableFFT(new Span <double>(_fftBufferTmp), true);
                for (var i = 0; i < _fftSize; ++i)
                {
                    audioBuffer[i] = _fftBufferTmp[i * 2];
                }
            }

            return(_frequencyNotes.DeriveNoteData(audioBuffer));
        }
Esempio n. 3
0
        public void ComputeMatrixUsingLomontTableFFT(ref Matrix m, int column, float[] audiodata, int pos)
        {
            // apply the window method (e.g HammingWindow, HannWindow etc)
            win.Apply(ref data, audiodata, pos);

            double[] complexSignal = FFTUtils.FloatToComplexDouble(data);
            lomonFFT.TableFFT(complexSignal, true);

            int row = 0;

            for (int i = 0; i < complexSignal.Length / 4; i += 2)
            {
                double re  = complexSignal[2 * i];
                double img = complexSignal[2 * i + 1];
                m.MatrixData[row][column] = Math.Sqrt((re * re + img * img) * complexSignal.Length / 2);
                row++;
            }
        }
Esempio n. 4
0
        public void Run()
        {
            if (!ChEnable.Checked)
            {
                return;
            }

            if (AudioCapturer == null)
            {
                return;
            }


            //for (int j = 0; j < 92; j++)
            {
                // reset mem
                for (int i = 0; i < 256; i++)
                {
                    audioBuffer[i] = 0;
                    fftData[i]     = 0;
                    fft[i]         = 0;
                }

                AudioCapturer.ReadSamples(audioBuffer, 256);

                for (int i = 0; i < 256; i++)
                {
                    fft[i] = (audioBuffer[i] - 128) * amplitude;
                }

                fftTransoformer.TableFFT(fft, true);

                for (int i = 0; i < 256; i += 2)
                {
                    double fftmag = Math.Sqrt((fft[i] * fft[i]) + (fft[i + 1] * fft[i + 1]));
                    fftavg        += fftmag;
                    fftData[i]     = (byte)fftmag;
                    fftData[i + 1] = fftData[i];
                }

                //fftavg /= 10;

                Main.Keyboard.WriteAudio(fft);
            }
        }
Esempio n. 5
0
    public void TimetoFreq(ref double[] audioSamples, int fftType = 0)
    {
        switch (fftType)
        {
        case 0:
        {
            _fft.FFT(audioSamples, true);
        }
        break;

        case 1:
        {
            _fft.RealFFT(ref audioSamples, true);
        }
        break;

        case 2:
        {
            _fft.TableFFT(audioSamples, true);
        }
        break;
        }
    }