예제 #1
0
        private static void Main()
        {
            // const string file = "music\\Afrojack - Unstoppable (Official Video).mp3";
            const string file  = @"C:\Users\Ale\Licenta-master\Licenta-master\PopulateDatabase\music\genres\blues\blues.00002.wav";
            var          watch = Stopwatch.StartNew();

            var sound = SoundReader.ReadFromFile(file);
            var ft    = Fft.CalculateFft(sound);

            watch.Stop();

            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine($"Time: {elapsedMs} ms");
            Console.ReadKey();
        }
예제 #2
0
파일: FFT.cs 프로젝트: alexvasiu/Licenta
        public static Fft CalculateFft(ISound sound, bool isFromServer = true, bool keepAll = false)
        {
            var res = new Fft {
                Result = new List <DataPoint>()
            };

            if (isFromServer)
            {
                if (sound.NumChannels == 2)
                {
                    ConvertStereoToMono(ref sound);
                }
                LowPassFilter(ref sound);
                DownSampling(ref sound);
            }

            HammingWindowFunction(ref sound);

            var list             = sound.Data;
            var chunkSize        = 1024;
            var sampledChunkSize = list.Count / chunkSize;
            var result           = new Complex[sampledChunkSize][];

            for (var i = 0; i < sampledChunkSize; i++)
            {
                result[i] = new Complex[chunkSize];
                for (var j = 0; j < chunkSize; j++)
                {
                    result[i][j] = new Complex(list[i * chunkSize + j], 0);
                }
                result[i] = CalculateFft(result[i]);
            }

            var timeForChunk = sound.Duration / result.Length;

            var mean = 0.0;

            var pointsGlobal     = Enumerable.Repeat(0, Range.Length + 1).ToList();
            var highScoresGlobal = Enumerable.Repeat(0.0, Range.Length + 1).ToList();

            for (var i = 0; i < result.Length; i++)
            {
                var points     = Enumerable.Repeat(0, Range.Length + 1).ToList();
                var highScores = Enumerable.Repeat(0.0, Range.Length + 1).ToList();

                for (var freq = 0; freq < 512; freq++)
                {
                    var mag   = Math.Log(result[i][freq].Magnitude + 1);
                    var index = GetIndex(freq);

                    if (mag >= highScoresGlobal[index])
                    {
                        highScoresGlobal[index] = mag;
                        pointsGlobal[index]     = freq;
                    }

                    if (!(mag >= highScores[index]))
                    {
                        continue;
                    }
                    points[index]     = freq;
                    highScores[index] = mag;
                }

                // var filteredPoints = Filter(points, highScores);

                mean += highScores.Sum();

                res.Result.Add(new DataPoint
                {
                    Hash       = Hash(points),
                    Points     = points,
                    Time       = timeForChunk * i,
                    Duration   = timeForChunk,
                    HighScores = !keepAll ? highScores : points.Select(x => (double)x).ToList()
                });
            }

            if (!keepAll)
            {
                var avg = highScoresGlobal.Average();

                for (var i = 0; i < res.Result.Count; i++)
                {
                    var dataPoint   = res.Result[i];
                    var aPoints     = dataPoint.Points;
                    var aHighScores = dataPoint.HighScores;
                    Filter(ref aPoints, ref aHighScores, avg);
                    dataPoint.HighScores = dataPoint.Points.Select(x => (double)x).ToList();
                    dataPoint.Points     = aPoints;
                    dataPoint.Hash       = Hash(aPoints);
                    res.Result[i]        = dataPoint;
                }
            }

            return(res);
        }