コード例 #1
0
ファイル: MFCC.cs プロジェクト: remy22/AudioVSTToolbox
        private void Initialize(float sampleRate, int windowSize, int numberCoefficients, bool useFirstCoefficient, double minFreq, double maxFreq, int numberFilters)
        {
            //check for correct window size
            if(windowSize < 32)
            {
                throw new Exception("window size must be at least 32");
            }
            else
            {
                int i = 32;
                while(i < windowSize && i < Int32.MaxValue)
                    i = i << 1;

                if(i != windowSize)
                    throw new Exception("window size must be 2^n");
            }

            //check sample rate
            sampleRate = (float)Math.Round(sampleRate);
            if(sampleRate < 1)
                throw new Exception("sample rate must be at least 1");

            //check numberFilters
            if(numberFilters < 2 || numberFilters > (windowSize/2) + 1)
                throw new Exception("number filters must be at least 2 and smaller than the nyquist frequency");

            //check numberCoefficients
            if(numberCoefficients < 1 || numberCoefficients >= numberFilters)
                throw new Exception("the number of coefficients must be greater or equal to 1 and samller than the number of filters");

            //check minFreq/maxFreq
            if(minFreq <= 0 || minFreq > maxFreq || maxFreq > 88200.0f)
                throw new Exception("the min. frequency must be greater 0 smaller than the max. frequency, which must be smaller than 88200.0");;

            this.sampleRate = sampleRate;
            this.windowSize = windowSize;
            this.hopSize = windowSize/2; //50% Overleap
            this.baseFreq = sampleRate/windowSize;

            this.numberCoefficients = numberCoefficients;
            this.useFirstCoefficient = useFirstCoefficient;

            this.minFreq = minFreq;
            this.maxFreq = maxFreq;
            this.numberFilters = numberFilters;

            //create buffers
            inputData = new double[windowSize];
            buffer = new double[windowSize];

            //store filter weights and DCT matrix due to performance reason
            melFilterBanks = GetMelFilterBanks();
            dctMatrix = GetDCTMatrix();

            //create power fft object
            normalizedPowerFFT = new FFT(FFT.FFT_NORMALIZED_POWER, windowSize, FFT.WND_HANNING);
        }