Beispiel #1
0
        private void Add(float value)
        {
            if (PerformFFT && FftCalculated != null)
            {
                fftBuffer[fftPos].X = /*value;//*/ (float)(value * FastFourierTransform.BlackmannHarrisWindow(fftPos, fftLength));
                fftBuffer[fftPos].Y = 0;
                fftPos++;
                if (fftPos >= fftBuffer.Length)
                {
                    fftPos = 0;
                    // 1024 = 2^10
                    FastFourierTransform.FFT(true, m, fftBuffer);
                    for (int i = 0; i < fftBuffer.Length; i++)
                    {
                        Complex c           = fftBuffer[i];
                        double  intensityDB = (4 + Math.Log(Math.Sqrt(c.X * c.X + c.Y * c.Y), 16)) / 4;
                        //double minDB = -32;
                        //if (intensityDB < minDB) intensityDB = minDB;
                        //double percent =- (intensityDB - minDB) / minDB;
                        double percent = Math.Max(0, intensityDB);
                        _spectrum[i] = (float)Math.Max(_spectrum[i] - downspeed, percent);
                    }
                    FftCalculated(this, fftArgs);
                }
            }

            maxValue = Math.Max(maxValue, value);
            minValue = Math.Min(minValue, value);
            count++;
            if (count >= NotificationCount && NotificationCount > 0)
            {
                MaximumCalculated?.Invoke(this, new MaxSampleEventArgs(minValue, maxValue));
                Reset();
            }
        }
Beispiel #2
0
    private void FFTWindowChange(Complex[] channelData, FFTWindow window)
    {
        switch (window)
        {
        case FFTWindow.HannWindow:
            for (int i = 0; i < channelData.Length; i++)
            {
                channelData[i].X = (float)(channelData[i].X * FastFourierTransform.HannWindow(i, bufferSize));
            }
            break;

        case FFTWindow.HammingWindow:
            for (int i = 0; i < channelData.Length; i++)
            {
                channelData[i].X = (float)(channelData[i].X * FastFourierTransform.HammingWindow(i, bufferSize));
            }
            break;

        case FFTWindow.BlackmannHarrisWindow:
            for (int i = 0; i < channelData.Length; i++)
            {
                channelData[i].X = (float)(channelData[i].X * FastFourierTransform.BlackmannHarrisWindow(i, bufferSize));
            }
            break;
        }
        FastFourierTransform.FFT(false, binaryExponentitation, channelData);
    }
Beispiel #3
0
        public static void AddSample(float value)
        {
            switch (Window)
            {
            case FFTWindow.BlackmannHarris:
                _fftBuffer[_fftPos].X = (float)(value * FastFourierTransform.BlackmannHarrisWindow(_fftPos, _fftLength));
                break;

            case FFTWindow.Hamming:
                _fftBuffer[_fftPos].X = (float)(value * FastFourierTransform.HammingWindow(_fftPos, _fftLength));
                break;

            case FFTWindow.Hann:
                _fftBuffer[_fftPos].X = (float)(value * FastFourierTransform.HannWindow(_fftPos, _fftLength));
                break;
            }
            _fftBuffer[_fftPos].Y = 0;
            _fftPos++;
            if (_fftPos >= _fftLength)
            {
                _fftPos = 0;
                FastFourierTransform.FFT(true, _m, _fftBuffer);
                FftCalculated();
            }
        }
 public void Add(float value)
 {
     if (PerformFFT && FftCalculated != null)
     {
         // Remember the window function! There are many others as well.
         fftBuffer[fftPos].X = (float)(value * FastFourierTransform.BlackmannHarrisWindow(fftPos, fftLength));
         fftBuffer[fftPos].Y = 0; // This is always zero with audio.
         fftPos++;
         if (fftPos >= fftLength)
         {
             fftPos = 0;
             FastFourierTransform.FFT(true, m, fftBuffer);
             FftCalculated(this, fftArgs);
         }
     }
 }
Beispiel #5
0
        private void GenerateAudioData()
        {
            // Q: make sure that fft_data can be 'filled in' by values[]
            // Ideally buffer should have exactly as much data as we need for fft
            // Possibly tweak latency or the thread sleep duration? Alternatively increase FFT reso.
            Complex[] fft_data = new Complex[fftSize];
            int       i        = 0;

            foreach (var value in this.unanalyzedValues.GetRange(0, fftSize))
            {
                fft_data[i].X = (float)(value * FastFourierTransform.BlackmannHarrisWindow(i, fftSize));
                fft_data[i].Y = 0;
                i++;
            }
            FastFourierTransform.FFT(true, (int)Math.Log(fftSize, 2.0), fft_data);

            // FFT results are Complex
            // Now we want the magnitude of each band
            // Note: cumulative power after transformation is slightly higher than original. Precision errors?
            // In any case we're scaling so that total power is 1.
            // TODO: scaling is all over the place in terms of where and when it's done. this should be fixed:
            // Possible places where we could or already do re-scale:
            // - Scaling on input (ex. "auto-gain")
            // - Scaling on FFT results (numerical precision concern?)
            //   This one is required by FFT theory - cumulative power 'in' and 'out' of the transform is preserved
            // - Scaling on processing (statistical methods might require values in a specific domain)

            float[] fft_results = new float[fftSize];

            float maxBinValue = 0;

            for (int j = 0; j < fftSize; j++)
            {
                fft_results[j] = Magnitude(fft_data[j].X, fft_data[j].Y) / fftSize;
                if (fft_results[j] > maxBinValue)
                {
                    maxBinValue = fft_results[j];
                }
            }

            // Here we arbitrarily re-scale so that the max is 1
            for (int j = 0; j < fftSize; j++)
            {
                fft_results[j] = fft_results[j] / maxBinValue;
            }
            this.AudioData = fft_results;
        }
Beispiel #6
0
        public FFTProcessor(int exponentOfTwo, bool magnitude = true, bool phase = false)
        {
            exponent   = exponentOfTwo;
            WindowSize = 1 << exponentOfTwo;

            fftBuffer      = new Complex[WindowSize];
            windowFunction = new float[WindowSize];
            if (magnitude)
            {
                Magnitudes = new float[WindowSize];
            }
            if (phase)
            {
                Phases = new float[WindowSize];
            }

            for (int i = 0; i < WindowSize; ++i)
            {
                windowFunction[i] = (float)FastFourierTransform.BlackmannHarrisWindow(i, WindowSize);
            }
        }