Exemplo n.º 1
0
        private void Autocorrelation(object o)
        {
            double[] autocorrelation = Models.Autocorrelation.CalculateAutocorrelation(this.Channel);

            List <DataPoint> signal = new List <DataPoint>();

            for (int i = 0; i < autocorrelation.Length; i++)
            {
                signal.Add(new DataPoint(i, autocorrelation[i]));
            }
            this.AutocorrelationData = signal;

            double[][] data = FourierWindows.SplitData(this.Channel, 2048);
            this.AutocorrelationFreq = Models.Autocorrelation.CalculateFrequencies(data, this.SampleRate);
        }
Exemplo n.º 2
0
        private void FourierSpectrumAnalysis(object o)
        {
            try
            {
                double[][] data = FourierWindows.Calculate(FourierWindows.SplitData(this.Channel, 2048), this.SelectedWindowType, float.Parse(this.Sigma));
                int[]      freq = new int[data.Length];
                for (int i = 0; i < freq.Length; i++)
                {
                    double[] data2 = new double[data[i].Length];
                    for (int j = 0; j < data[i].Length; j++)
                    {
                        if (j != 0)
                        {
                            data2[j] = data[i][j] - 0.94 * data[i][j - 1];
                        }
                        else
                        {
                            data2[j] = data[i][j];
                        }
                    }

                    List <Complex> signal = new List <Complex>();
                    for (int j = 0; j < data2.Length; j++)
                    {
                        signal.Add(new Complex(data2[j], 0));
                    }
                    List <Complex> dft = AMath.CalculateFastTransform(signal, AMath.CalculateWCoefficients(data2.Length, false), 0);
                    dft.RemoveRange(dft.Count / 2, dft.Count / 2);

                    double[] spectrum = new double[dft.Count];
                    for (int j = 0; j < spectrum.Length; j++)
                    {
                        spectrum[j] = Math.Sqrt((dft[j].Re * dft[j].Re) + (dft[j].Im * dft[j].Im));
                    }

                    List <int> max = new List <int>();
                    for (int j = 1; j < spectrum.Length - 1; j++)
                    {
                        if (spectrum[j - 1] < spectrum[j] && spectrum[j + 1] < spectrum[j])
                        {
                            max.Add(j);
                        }
                    }

                    int    globalMax    = 0;
                    double globalMaxVal = 0;
                    for (int j = 0; j < max.Count; j++)
                    {
                        int    index = max[j];
                        double value = spectrum[index];
                        if (value > globalMaxVal)
                        {
                            globalMax    = index;
                            globalMaxVal = value;
                        }
                    }

                    double     border   = 0.2 * globalMaxVal;
                    List <int> bordered = new List <int>();
                    for (int j = 0; j < max.Count; j++)
                    {
                        if (spectrum[max[j]] >= border)
                        {
                            bordered.Add(max[j]);
                        }
                    }

                    double median;
                    if (bordered.Count > 1)
                    {
                        List <double> differences = new List <double>();
                        for (int j = 0; j < max.Count - 1; j++)
                        {
                            for (int k = j + 1; k < max.Count; k++)
                            {
                                differences.Add(Math.Abs(max[j] - max[k]));
                            }
                        }
                        differences.Sort();

                        if (differences.Count != 1)
                        {
                            if (differences.Count % 2 == 0)
                            {
                                median = differences[differences.Count / 2];
                            }
                            else
                            {
                                int half = (differences.Count - 1) / 2;
                                median = (differences[half] + differences[half + 1]) / 2;
                            }
                        }
                        else
                        {
                            median = differences[0];
                        }
                    }
                    else
                    {
                        median = bordered[0];
                    }

                    freq[i] = Convert.ToInt32((this.SampleRate / data[i].Length) * median);
                }
                this.FourierFreq = freq;
            }
            catch (Exception e)
            {
                Notify.Error(e.Message);
            }
        }