Beispiel #1
0
        public FrequencyChart AnalyzeSound(double[] sound)
        {
            var sample = sound;
            var amp    = Math.Sqrt(sample.Average(k => k * k));
            var ampMax = sample.Max(k => Math.Abs(k));
            var l      = AnalyzeUtils.FindMinMorePow(sound.Length);

            Array.Resize(ref sample, l);
            double[] real;
            double[] img;

            trans.TransformForward(sample, out real, out img);
            //	var chart = new FrequencyChart (ampMax, amp,
            //			                             real.Zip (img, (r, i) => Math.Sqrt (r * r + i * i))
            //                          .Zip (frqs, (a, f) => new FrequencyPair (f, a))
            //                           .Where (k => k.Frequency > 0)
            //                           .ToArray ());

            var chart = new FrequencyChart(ampMax, amp,
                                           real.Zip(frqs, (a, f) => new FrequencyPair(f, Math.Abs(a)))
                                           .Where(k => k.Frequency > 0)
                                           .ToArray());

            return(chart);
        }
Beispiel #2
0
        public FrequencyChart CutOffLows(FrequencyChart chart, double cutoffLevel)
        {
            var t = new FrequencyChart(chart.TotalLevel, chart.AverageLevel, chart);

            for (int i = 0, tCount = t.Count; i < tCount; i++)
            {
                var e = t [i];
                e.Level = cutoffLevel > 0?(e.Level > cutoffLevel ? e.Level : 0):(e.DBLevel < cutoffLevel ? e.Level : 0);
            }
            return(t);
        }
Beispiel #3
0
        public FrequencyChart Zoom(FrequencyChart chart)
        {
            var l = new List <FrequencyPair> ();

            for (int i = 0; i < chart.Count; i++)
            {
                if (GetFromArray(chart, i).Level < GetFromArray(chart, i + 1).Level&& GetFromArray(chart, i).Level < GetFromArray(chart, i - 1).Level ||
                    GetFromArray(chart, i).Level > GetFromArray(chart, i + 1).Level&& GetFromArray(chart, i).Level > GetFromArray(chart, i - 1).Level)
                {
                    l.Add(GetFromArray(chart, i));
                }
            }
            return(new FrequencyChart(l));
        }
Beispiel #4
0
        public FrequencyChart CutEdges(FrequencyChart chart, double lowBound, double highBound)
        {
            var t = chart.Where(k => k.Frequency >= lowBound && k.Frequency <= highBound);

            if (!t.Any())
            {
                return(new FrequencyChart(0, 0, new List <FrequencyPair> ()));
            }
            return(new FrequencyChart(
                       t.Max(k => k.Level),
                       t.Average(k => k.Level),
                       t
                       ));
        }
Beispiel #5
0
        public FrequencyChart ApplyWindow(FrequencyChart chart)
        {
            if (chart.Count != window.Count())
            {
                throw new Exception();
            }
            var e = new FrequencyChart(chart.TotalLevel, chart.AverageLevel, chart);

            for (int i = 0; i < chart.Count; i++)
            {
                var d = e [i].Level * window [i];
                e [i].Level = d;
            }
            return(e);
        }
Beispiel #6
0
        public FrequencyChart AnalyzeSound(Complex[] sound, int offset)
        {
            var sample = new Complex[window.Length];

            Array.Copy(sound, offset, sample, 0, window.Length);

            Fourier.Radix2Forward(sample, FourierOptions.Default);
            var chart = new FrequencyChart(
                sample.Select(k => k.Magnitude * 2 / (sample.Length))
                .Zip(frqs, (a, f) => new FrequencyPair(f, a))
                .Where(k => k.Frequency > 0)
                .ToArray());

            return(chart);
        }
Beispiel #7
0
        public FrequencyChart BandGrouping(Bands bands, FrequencyChart chart, double threshHold)
        {
            var freqs      = bands.Frequencies;
            var workCharts = chart.Where(k => k.Level > threshHold).ToArray();
            var newChart   = new FrequencyChart(chart.TotalLevel, chart.AverageLevel, new List <FrequencyPair>());
            var curBand    = 0;
            var curUpBound = freqs[curBand];

            int curFreqInBand = 0;
            var curPair       = new FrequencyPair(curUpBound, 0);

            for (int i = 0; i < workCharts.Count; i++)
            {
                var sample = workCharts [i];

                if (sample.Frequency < curUpBound)
                {
                    curPair.Level += sample.Level;
                }
                else if (freqs.Count > curBand)
                {
                    curPair.Level /= curFreqInBand;
                    curFreqInBand  = 0;
                    while (sample.Frequency > curUpBound)
                    {
                        curBand++;
                        if (freqs.Count <= curBand)
                        {
                            curUpBound = freqs[freqs.Count - 1] * 10;
                        }
                        else
                        {
                            curUpBound = freqs[curBand];
                        }
                    }
                    newChart.Add(curPair);
                    curPair = new FrequencyPair(curUpBound, sample.Level);
                }
                curFreqInBand++;
            }


            return(newChart);
        }
Beispiel #8
0
        public FrequencyChart MaxFrequenciesAnalyze(FrequencyChart chart, double cutoffLevel)
        {
            var frClusters = chart.Where(k3 => k3.Level > cutoffLevel).ToArray();

            if (!frClusters.Any())
            {
                return(new FrequencyChart(0, 0, new List <FrequencyPair> ()));
            }
            frClusters = frClusters
                         .ClusterBy(k => k.Frequency, 0.01)
                         .Select(k => new FrequencyPair(k.Average(k3 => k3.Frequency), k.Average(k3 => k3.Level))).ToArray();

            var newChart = new FrequencyChart(chart.TotalLevel, chart.AverageLevel,
                                              frClusters.OrderBy(k => k.Frequency)
                                              );


            return(newChart);
        }
Beispiel #9
0
        public FrequencyChart BandGrouping(Bands bands, FrequencyChart chart)
        {
            Debug.Assert(bands.Notes.Count >= 2);
            var freqs = bands.Notes.Select(k => k.Frequency).ToArray();


            var curBand = 0;


            var t        = new List <FrequencyPair> ();
            var lowPair  = new FrequencyPair(bands.Notes[0].Frequency, 0);
            var highPair = new FrequencyPair(bands.Notes[1].Frequency, 0);

            t.Add(highPair);
            t.Add(lowPair);

            for (int i = 0; i < chart.Count; i++)
            {
                var sample = chart [i];

                while (sample.Frequency > highPair.Frequency)
                {
                    if (curBand == bands.Notes.Count - 1)
                    {
                        goto C1;
                    }
                    lowPair = highPair;
                    curBand++;
                    highPair = new FrequencyPair(bands.Notes [curBand].Frequency, 0);
                    t.Add(highPair);
                }


                lowPair.Level  += sample.Level * Math.Round((sample.Frequency - lowPair.Frequency) / (highPair.Frequency - lowPair.Frequency));
                highPair.Level += sample.Level * Math.Round((highPair.Frequency - sample.Frequency) / (highPair.Frequency - lowPair.Frequency));
            }


            C1 :         return(new FrequencyChart(t));
        }
Beispiel #10
0
        public FrequencyChart LocalizeMaximums(FrequencyChart chart, int gapCount)
        {
            if (!chart.Any())
            {
                return(chart);
            }
            var           list          = new List <FrequencyPair> ();
            bool          raising       = true;
            FrequencyPair localMax      = chart[0];
            FrequencyPair lastMeaninful = chart[0];

            for (var i = 0; i < chart.Count; i++)
            {
                double derivation;
                var    cur = GetFromArray(chart, i);
                derivation = cur.Level - lastMeaninful.Level;

                if (raising)
                {
                    if (derivation >= 0)
                    {
                        localMax      = cur;
                        lastMeaninful = localMax;
                    }
                    else
                    {
                        bool shouldContinueRaising = false;

                        for (int j = i + 1; j <= i + gapCount; j++)
                        {
                            if (GetFromArray(chart, j).Level - lastMeaninful.Level >= 0)
                            {
                                lastMeaninful         = GetFromArray(chart, j);
                                shouldContinueRaising = true;
                                break;
                            }
                        }
                        if (!shouldContinueRaising)
                        {
                            list.Add(localMax);
                            lastMeaninful = cur;
                            raising       = false;
                        }
                    }
                }
                else
                {
                    if (derivation <= 0)
                    {
                        lastMeaninful = cur;
                    }
                    else
                    {
                        bool shouldStartRaising = true;

                        for (int j = i + 1; j <= i + gapCount; j++)
                        {
                            if (GetFromArray(chart, j).Level - lastMeaninful.Level <= 0)
                            {
                                lastMeaninful      = GetFromArray(chart, j);
                                shouldStartRaising = false;
                                break;
                            }
                        }
                        if (shouldStartRaising)
                        {
                            localMax      = cur;
                            lastMeaninful = cur;
                            raising       = true;
                        }
                    }
                }
            }
            if (raising)
            {
                list.Add(chart[chart.Count - 1]);
            }
            return(new FrequencyChart(list));
        }