Exemple #1
0
        /// <summary>
        /// Computes a Scms model from the MFCC representation of a song.
        /// </summary>
        /// <param name="mfcc">Comirva.Audio.Util.Maths.Matrix mfcc</param>
        /// <returns></returns>
        public static Scms GetScmsNoInverse(Comirva.Audio.Util.Maths.Matrix mfccs, string name)
        {
            DbgTimer t = new DbgTimer();

            t.Start();

            Comirva.Audio.Util.Maths.Matrix mean = mfccs.Mean(2);

                        #if DEBUG
            if (Analyzer.DEBUG_INFO_VERBOSE)
            {
                if (Analyzer.DEBUG_OUTPUT_TEXT)
                {
                    mean.WriteText(name + "_mean.txt");
                }
                mean.DrawMatrixGraph(name + "_mean.png");
            }
                        #endif

            // Covariance
            Comirva.Audio.Util.Maths.Matrix covarMatrix = mfccs.Cov(mean);
                        #if DEBUG
            if (Analyzer.DEBUG_INFO_VERBOSE)
            {
                if (Analyzer.DEBUG_OUTPUT_TEXT)
                {
                    covarMatrix.WriteText(name + "_covariance.txt");
                }
                covarMatrix.DrawMatrixGraph(name + "_covariance.png");
            }
                        #endif

            Comirva.Audio.Util.Maths.Matrix covarMatrixInv = new Comirva.Audio.Util.Maths.Matrix(covarMatrix.Rows, covarMatrix.Columns);

            // Store the Mean, Covariance, Inverse Covariance in an optimal format.
            int  dim = mean.Rows;
            Scms s   = new Scms(dim);
            int  l   = 0;
            for (int i = 0; i < dim; i++)
            {
                s.mean[i] = (float)mean.MatrixData[i][0];
                for (int j = i; j < dim; j++)
                {
                    s.cov[l]  = (float)covarMatrix.MatrixData[i][j];
                    s.icov[l] = (float)covarMatrixInv.MatrixData[i][j];
                    l++;
                }
            }

            Dbg.WriteLine("GetScmsNoInverse - Execution Time: {0} ms", t.Stop().TotalMilliseconds);
            return(s);
        }
		public AudioFeature Calculate(double[] input)
		{
			//pack the mfccs into a pointlist
			double[][] mfccCoefficients = mfcc.Process(input);

			//check if element 0 exists
			if(mfccCoefficients.Length == 0)
				throw new ArgumentException("The input stream is to short to process;");

			//create mfcc matrix
			Matrix mfccs = new Matrix(mfccCoefficients);
			#if DEBUG
			mfccs.WriteText("mfccdata-mandelellis.txt");
			mfccs.DrawMatrixGraph("mfccdata-mandelellis.png");
			#endif

			// compute mean
			//Matrix mean = mfccs.Mean(1).Transpose();
			Matrix mean = mfccs.Mean(2);
			#if DEBUG
			mean.WriteText("mean-mandelellis.txt");
			mean.DrawMatrixGraph("mean-mandelellis.png");
			#endif
			
			// create covariance matrix
			Matrix covarMatrix = mfccs.Cov();
			#if DEBUG
			covarMatrix.WriteText("covariance-mandelellis.txt");
			covarMatrix.DrawMatrixGraph("covariance-mandelellis.png");
			#endif
			
			// Inverse Covariance
			Matrix covarMatrixInv;
			try {
				//covarMatrixInv = covarMatrix.Inverse();
				covarMatrixInv = covarMatrix.InverseGausJordan();
			} catch (Exception) {
				Console.Error.WriteLine("Mandel Ellis Extraction Failed!");
				return null;
			}
			#if DEBUG
			covarMatrixInv.WriteText("inverse_covariance-mandelellis.txt");
			covarMatrixInv.DrawMatrixGraph("inverse_covariance-mandelellis.png");
			#endif

			MandelEllis.GmmMe gmmMe = new MandelEllis.GmmMe(mean, covarMatrix, covarMatrixInv);
			MandelEllis mandelEllis = new MandelEllis(gmmMe);
			return mandelEllis;
		}
Exemple #3
0
		/// <summary>
		/// Init the MFCC class
		/// </summary>
		/// <param name="sampleRate">float sampes per second, must be greater than zero; not
		///                         whole-numbered values get rounded</param>
		/// <param name="windowSize">int size of window; must be 2^n and at least 32</param>
		/// <param name="numberCoefficients">int must be grate or equal to 1 and smaller than
		///                               the number of filters</param>
		/// <param name="useFirstCoefficient">boolean indicates whether the first coefficient
		///                                    of the dct process should be used in the
		///                                    mfcc feature vector or not</param>
		/// <param name="minFreq">double start of the interval to place the mel-filters in</param>
		/// <param name="maxFreq">double end of the interval to place the mel-filters in</param>
		/// <param name="numberFilters">int number of mel-filters to place in the interval</param>
		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();
			#if DEBUG
			if (Mirage.Analyzer.DEBUG_INFO_VERBOSE) {
				melFilterBanks.DrawMatrixGraph("melfilters-comirva.png");
			}
			#endif
			
			dctMatrix = GetDCTMatrix();
			#if DEBUG
			if (Mirage.Analyzer.DEBUG_INFO_VERBOSE) {
				dctMatrix.DrawMatrixGraph("dct-comirva.png");
			}
			#endif

			//create power fft object
			normalizedPowerFFT = new FFT(FFT.FFT_NORMALIZED_POWER, windowSize, FFT.WND_HANNING);
		}
Exemple #4
0
        /// <summary>
        /// Computes a Scms model from the MFCC representation of a song.
        /// </summary>
        /// <param name="mfcc">Comirva.Audio.Util.Maths.Matrix mfcc</param>
        /// <returns></returns>
        public static Scms GetScms(Comirva.Audio.Util.Maths.Matrix mfccs, string name)
        {
            DbgTimer t = new DbgTimer();

            t.Start();

            Comirva.Audio.Util.Maths.Matrix mean = mfccs.Mean(2);

                        #if DEBUG
            if (Analyzer.DEBUG_INFO_VERBOSE)
            {
                if (Analyzer.DEBUG_OUTPUT_TEXT)
                {
                    mean.WriteText(name + "_mean.txt");
                }
                mean.DrawMatrixGraph(name + "_mean.png");
            }
                        #endif

            // Covariance
            Comirva.Audio.Util.Maths.Matrix covarMatrix = mfccs.Cov(mean);
                        #if DEBUG
            if (Analyzer.DEBUG_INFO_VERBOSE)
            {
                if (Analyzer.DEBUG_OUTPUT_TEXT)
                {
                    covarMatrix.WriteText(name + "_covariance.txt");
                }
                covarMatrix.DrawMatrixGraph(name + "_covariance.png");
            }
                        #endif

            // Inverse Covariance
            Comirva.Audio.Util.Maths.Matrix covarMatrixInv;
            try {
                covarMatrixInv = covarMatrix.InverseGausJordan();
            } catch (Exception) {
                Dbg.WriteLine("MatrixSingularException - Scms failed!");
                return(null);
            }
                        #if DEBUG
            if (Analyzer.DEBUG_INFO_VERBOSE)
            {
                if (Analyzer.DEBUG_OUTPUT_TEXT)
                {
                    covarMatrixInv.WriteAscii(name + "_inverse_covariance.ascii");
                }
                covarMatrixInv.DrawMatrixGraph(name + "_inverse_covariance.png");
            }
                        #endif

            // Store the Mean, Covariance, Inverse Covariance in an optimal format.
            int  dim = mean.Rows;
            Scms s   = new Scms(dim);
            int  l   = 0;
            for (int i = 0; i < dim; i++)
            {
                s.mean[i] = (float)mean.MatrixData[i][0];
                for (int j = i; j < dim; j++)
                {
                    s.cov[l]  = (float)covarMatrix.MatrixData[i][j];
                    s.icov[l] = (float)covarMatrixInv.MatrixData[i][j];
                    l++;
                }
            }

            Dbg.WriteLine("Compute Scms - Execution Time: {0} ms", t.Stop().TotalMilliseconds);
            return(s);
        }