/// <summary>
        /// Extracts features of the song. Saves it to database if the features do not exist there yet
        /// </summary>
        /// <param name="data">Song of which features will be extracted</param>
        /// <param name="meanVector">Extracted mean vector</param>
        /// <param name="mfccCoefficients">Extracted MFCC coefficients</param>
        /// <param name="covarianceMatrix">Extracted covariance matrix</param>
        public void Extract(DataElement data, out double[] meanVector, out double[][] mfccCoefficients, out double[,] covarianceMatrix)
        {
            string songName = data.FilePath.Split(new[] { '\\' }).Last();

            using (var db = new Database())
            {
                bool songInDatabase = db.Exists(songName);
                if (!songInDatabase)
                {
                    db.AddTestSong(data.FilePath);
                }

                meanVector       = db.GetMeanVector(songName);
                covarianceMatrix = db.GetCovarianceMatrix(songName);
                mfccCoefficients = db.GetMFCCCoefficients(songName);

                if (!songInDatabase)
                {
                    db.DeleteTestFiles();
                }
            }
        }
Beispiel #2
0
        public override SongGenre GetClass(DataElement testedData)
        {
            base.GetClass(testedData);
            if (!testedData.HasFeaturesExtracted())
            {
                testedData.GetFeatures();
            }

            for (int i = 0; i < TrainingData.Count; i++)
            {
                TrainingData[i].GetDistance(testedData);
            }
            TrainingData.Sort((data1, data2) => data1.NeighbourDistance.CompareTo(data2.NeighbourDistance));

            List <int>         labelHistogram    = new int[Enum.GetNames(typeof(SongGenre)).Length].ToList();
            List <DataElement> nearestNeighbours = TrainingData.GetRange(0, K);

            foreach (DataElement neighbour in nearestNeighbours)
            {
                SongGenre genre = neighbour.GetClass();
                labelHistogram[(int)genre]++;
            }

            int        max          = labelHistogram.Max();
            List <int> nearestGenre = new List <int>();

            for (int i = 0; i < labelHistogram.Count; i++)
            {
                if (labelHistogram[i] == max)
                {
                    nearestGenre.Add(i);
                }
            }
            nearestNeighbours.RemoveAll(data => !nearestGenre.Contains((int)data.GetClass()));
            Random r = new Random();

            testedData.Probability = 1.0;
            return(nearestNeighbours[r.Next(nearestNeighbours.Count)].GetClass());
        }
        private int findClosestMean(DataElement data, List <DataElement> means)
        {
            double minDistance   = double.MaxValue;
            int    bestMeanIndex = -1;
            int    i             = 0;

            foreach (var m in means)
            {
                // zwykła odl. miedzy wektorami
                //data.GetDistance(m);
                //double distance = data.NeighbourDistance;
                double distance = MathHelper.EuclideanDistance(data.MeanVector, m.MeanVector);
                if (distance < minDistance)
                {
                    minDistance   = distance;
                    bestMeanIndex = i;
                }
                i++;
            }

            return(bestMeanIndex);
        }
Beispiel #4
0
 public override double GetProbability(DataElement data)
 {
     GetClass(data);
     return(data.Probability);
 }
 /// <summary>
 /// Gets probability thad specified song belongs to classificator (important in GMM implementation)
 /// </summary>
 /// <returns>Value in range [0, 1]</returns>
 public abstract double GetProbability(DataElement data);
Beispiel #6
0
 /// <summary>
 /// Computes KL distance to the other song
 /// </summary>
 /// <param name="data">Song to which distance will be computed</param>
 public void GetDistance(DataElement data)
 {
     NeighbourDistance = MathHelper.KLdistance(MeanVector, CovarianceMatrix, data.MeanVector, data.CovarianceMatrix);
 }
        /// <summary>
        /// Computes genre of specified song using currently chosen classificator
        /// </summary>
        /// <param name="testData">Song</param>
        /// <returns>Song genre converted to string</returns>
        public string ComputeGenre(DataElement testData)
        {
            SongGenre genre = classificator.GetClass(testData);

            return(DataElement.ToString(genre));
        }
 public override SongGenre GetClass(DataElement data)
 {
     throw new NotImplementedException();
     //TODO:: wyznaczenie gatunku testowanego utworu
     // TODO:: ustawienie data.Probability = <prawdopodobieństwo_przynależenia_do_znalezionego_gatunku>
 }
 public override double?GetProbability(DataElement data)
 {
     return(data.Probability);
 }