Example #1
0
        public static void Main()
        {
            string            path         = "data/allSamples/";
            List <BirdSample> trainingData = buildTrainingData(path);
            List <BirdSample> testingData  = buildTestingData(path);

            BirdSample b = createSampleFromFile(1, path + "mal1_song_8.wav");

            using (StreamWriter writer = new StreamWriter("out.csv"))
            {
                for (int i = 0; i < b.features.Count; i++)
                {
                    writer.Write(b.features[i]);
                    if (i < b.features.Count - 1)
                    {
                        writer.WriteLine(",");
                    }
                }
            }

            int k = getIdealK(trainingData);

            Console.WriteLine("1NN: " + KNN(trainingData, testingData, 1) * 100 + "% Accuracy");
            Console.WriteLine(k + "NN: " + KNN(trainingData, testingData, k) * 100 + "% Accuracy");
        }
Example #2
0
        public static double tSumDist(BirdSample a, BirdSample b)
        {
            double distance = 0;

            for (int i = 0; i < 100; i++)
            {
                distance += Math.Abs(a.tSums[i] - b.tSums[i]);
            }
            return(distance);
        }
Example #3
0
        public static double KLDivergence(BirdSample p, BirdSample q)
        {
            double divergence = 0;
            int    limit      = (p.features.Count < q.features.Count) ? p.features.Count : q.features.Count;

            for (int i = 0; i < limit; i++)
            {
                divergence += p.features[i] * Math.Log(p.features[i] / q.features[i], 2);
            }
            return(divergence);
        }
Example #4
0
        public static double sumDist(BirdSample a, BirdSample b)
        {
            double distance = 0;

            for (int i = 0; i < 100; i++)
            {
                double d = a.sums[i] - b.sums[i];
                distance += Math.Abs(d);
            }
            return(distance);
        }
Example #5
0
        public static double birdDist(BirdSample a, BirdSample b)
        {
            double distance = 0;
            int    limit    = (a.features.Count < b.features.Count) ? a.features.Count : b.features.Count;

            for (int i = 0; i < limit; i++)
            {
                distance += Math.Abs(a.features[i] - b.features[i]);
            }
            //Average limit, if it's absolute the shortest sample will always be predicted.
            return(distance / limit);
        }
Example #6
0
        public static BirdSample createSampleFromFile(int label, string filename)
        {
            BirdSample    bs    = new BirdSample();
            List <double> bytes = convertToDecimal(filename);

            bs.label    = label;
            bs.features = bytes;
            //bs.normalizeFeatures();
            bs.isolatePeaks(0);
            //bs.normalizeZeroToOne();
            bs.calculateSums();
            bs.calculateTSums();
            return(bs);
        }
Example #7
0
        public static double crossCorrelation(BirdSample a, BirdSample b)
        {
            double similarity_a_b = 0;
            double distance       = 0;
            int    limit          = (a.features.Count < b.features.Count) ? a.features.Count : b.features.Count;

            for (int i = 0; i < limit; i++)
            {
                //distance += Math.Pow(a.features[i], 2) - a.features[i]*b.features[i];
                similarity_a_b = checked (similarity_a_b + a.features[i] * b.features[i]);
            }
            //Console.WriteLine("mal5_song1 vs mal5_song2: {0}", similarity_a_b);

            return(similarity_a_b);
        }