Example #1
0
        private void FourierRecord_Click(object sender, EventArgs e)
        {
            if (!File.Exists("./Sounds/temp.wav"))
            {
                return;
            }


            ComplexSignal sComplex = FFTComplex("temp");

            Complex[] channel = sComplex.GetChannel(0);

            chart2.Series.Clear();
            chart2.Series.Add("FFT");
            chart2.Series["FFT"].ChartType = SeriesChartType.FastLine;
            chart2.Series["FFT"].ChartArea = "ChartArea1";


            double[] power = FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(channel));
            double[] freqv = FilterVoiceHz(Accord.Audio.Tools.GetFrequencyVector(sComplex.Length, sComplex.SampleRate));

            for (int i = 1; i < power.Length; i++)
            {
                chart2.Series["FFT"].Points.AddXY(freqv[i], power[i]);
            }
        }
Example #2
0
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        protected override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            if (sourceData.Status != ComplexSignalStatus.Analytic)
            {
                throw new ArgumentException("Signal must be in analytic form.", "sourceData");
            }

            SampleFormat format   = sourceData.SampleFormat;
            int          channels = sourceData.Channels;
            int          length   = sourceData.Length;
            int          samples  = sourceData.Samples;

            Complex d = new Complex();

            unsafe
            {
                Complex *src = (Complex *)sourceData.Data.ToPointer();
                Complex *dst = (Complex *)destinationData.Data.ToPointer();

                for (int i = 0; i < samples; i++, src++, dst++)
                {
                    d.Re = (*dst).Magnitude;
                    *dst = d;
                }
            }
        }
Example #3
0
        /// <summary>
        ///   This method will be called whenever there is a new audio
        ///   frame to be processed.
        /// </summary>
        ///
        void source_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // We can start by converting the audio frame to a complex signal
            ComplexSignal signal = ComplexSignal.FromSignal(eventArgs.Signal);

            // If its needed,
            if (window != null)
            {
                // Apply the chosen audio window
                signal = window.Apply(signal, 0);
            }

            // Transform to the complex domain
            signal.ForwardFourierTransform();

            // Now we can get the power spectrum output and its
            // related frequency vector to plot our spectrometer.

            Complex[] channel = signal.GetChannel(0);
            double[]  power   = Accord.Audio.Tools.GetPowerSpectrum(channel);
            double[]  freqv   = Accord.Audio.Tools.GetFrequencyVector(signal.Length, signal.SampleRate);

            power[0] = 0; // zero DC
            float[] g = new float[power.Length];
            for (int i = 0; i < power.Length; i++)
            {
                g[i] = (float)power[i];
            }

            // Adjust the zoom according to the horizontal and vertical scrollbars.
            chart1.RangeX = new DoubleRange(freqv[0], freqv[freqv.Length - 1] / hScrollBar1.Value);
            chart1.RangeY = new DoubleRange(0f, Math.Pow(10, -vScrollBar1.Value));

            chart1.UpdateWaveform("fft", g);
        }
Example #4
0
        public void createFrequencyArray(ComplexSignal[] tempFrequency, ComplexSignal curComplex, out double[] power, out double[] magnitudes, out double[] freq, out double[] meanPower, out double[] meanMagnitudes)
        {
            int amountOfSignals = 0;
            var length          = curComplex.Length / (2 + 1);

            double[] tempMeanPower  = new double[length];
            double[] tempMeanMag    = new double[length];
            double[] tempPower      = { };
            double[] tempMagnitudes = { };
            double[] tempFreq       = { };

            foreach (ComplexSignal curSig in tempFrequency)
            {
                frequencyData(curSig, out tempPower, out tempMagnitudes, out tempFreq);
                for (int j = 0; j < length; j++)
                {
                    tempMeanPower[j] += tempPower[j];
                    tempMeanMag[j]   += tempPower[j];
                }
                amountOfSignals++;
            }
            for (int j = 0; j < length; j++)
            {
                tempMeanPower[j] /= amountOfSignals;
                tempMeanMag[j]   /= amountOfSignals;
            }
            meanPower      = tempMeanPower;
            meanMagnitudes = tempMeanMag;
            power          = tempPower;
            magnitudes     = tempMagnitudes;
            freq           = tempFreq;
        }
Example #5
0
        void FourierBTClick(object sender, EventArgs e)
        {
            if (soundList.SelectedItems.Count == 0)
            {
                return;
            }
            if (waveWriter != null)
            {
                return;
            }

            string sFile = soundList.SelectedItem.ToString();

            ComplexSignal sComplex = FFTComplex(sFile);

            Complex[] channel = sComplex.GetChannel(0);

            chart2.Series.Clear();
            chart2.Series.Add("FFT");
            chart2.Series["FFT"].ChartType = SeriesChartType.FastLine;
            chart2.Series["FFT"].ChartArea = "ChartArea1";


            double[] power = FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(channel));
            double[] freqv = FilterVoiceHz(Accord.Audio.Tools.GetFrequencyVector(sComplex.Length, sComplex.SampleRate));

            for (int i = 1; i < power.Length; i++)
            {
                chart2.Series["FFT"].Points.AddXY(freqv[i], power[i]);
            }
        }
Example #6
0
        void source_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            ComplexSignal signal = ComplexSignal.FromSignal(eventArgs.Signal);

            if (hammingWindowToolStripMenuItem.Checked)
            {
                ComplexSignal c = window.Apply(signal, 0);
                signal = c;
            }


            signal.ForwardFourierTransform();

            Complex[] channel = signal.GetChannel(0);
            double[]  power   = Accord.Audio.Tools.GetPowerSpectrum(channel);
            double[]  freqv   = Accord.Audio.Tools.GetFrequencyVector(signal.Length, signal.SampleRate);

            power[0] = 0; // zero DC

            float[] g = new float[power.Length];

            for (int i = 0; i < power.Length; i++)
            {
                // g[i, 0] = freqv[i];
                g[i] = (float)power[i];
            }

            chart1.RangeX = new DoubleRange(freqv[0], freqv[freqv.Length - 1] / hScrollBar1.Value);
            chart1.RangeY = new DoubleRange(0f, Math.Pow(10, -vScrollBar1.Value));

            chart1.UpdateWaveform("fft", g);
        }
Example #7
0
 /// <summary>
 ///   Applies the filter to a windowed signal.
 /// </summary>
 /// 
 public ComplexSignal[] Apply(params ComplexSignal[] complexSignal)
 {
     ComplexSignal[] s = new ComplexSignal[complexSignal.Length];
     for (int i = 0; i < complexSignal.Length; i++)
         s[i] = Apply(complexSignal[i]);
     return s;
 }
Example #8
0
        void source_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // We can start by converting the audio frame to a complex signal
            var signal = ComplexSignal.FromSignal(eventArgs.Signal);

            // If its needed,
            if (window != null)
            {
                // Apply the chosen audio window
                signal = window.Apply(signal, 0);
            }

            // Transform to the complex domain
            signal.ForwardFourierTransform();

            // Now we can get the power spectrum output and its
            // related frequency vector to plot our spectrometer.
            Complex[] channel = signal.GetChannel(0);


            double[] power = Accord.Audio.Tools.GetPowerSpectrum(channel);


            ListOfPowerSpectrum.Add(power);

            foreach (double value in power)
            {
                if (value != 0.0 && value > 1.0E-10)
                {
                    //Console.WriteLine("Index to {0}", ListOfPowerSpectrum.IndexOf(power));
                    soundsDetectedIndexes.Add(ListOfPowerSpectrum.IndexOf(power));
                    break;
                }
            }
        }
Example #9
0
    public float[] EEGToSound(double[] inputData)
    {
        ComplexSignal cs = Signal.FromArray(inputData, SAMPLE_RATE).ToComplex();

        cs.ForwardFourierTransform();
        powerSpectrum = Tools.GetPowerSpectrum(cs.GetChannel(0));

        Array.ConstrainedCopy(powerSpectrum, 0, shortenedPowerSpectrum, 0, MAX_FREQ);

        double maxSignal = shortenedPowerSpectrum.Max();

        shortenedPowerSpectrum = shortenedPowerSpectrum.Select(x => x / maxSignal).ToArray();

        Array.Sort(shortenedPowerSpectrum, frequencyRange);
        Array.ConstrainedCopy(shortenedPowerSpectrum, 0, highestPowers, 0, NUM_MAX_FREQS);
        Array.ConstrainedCopy(frequencyRange, 0, highestFrequencies, 0, NUM_MAX_FREQS);

        for (int i = 0; i < NUM_MAX_FREQS; i++)
        {
            double[] sineWave = Generate.Sinusoidal(RECORDING_RATE * SAMPLE_DURATION, RECORDING_RATE, highestFrequencies [i], highestPowers [i]);
            outputData = outputData.Zip(sineWave, (x, y) => x + y).ToArray();
        }

        float[] audioData = outputData.Select <double, float> (x => (float)x).ToArray();

        return(audioData);
    }
Example #10
0
        private void testWindow(int length, IWindow window)
        {
            Complex[,] data = (Complex[, ]) this.data.Clone();
            ComplexSignal target = ComplexSignal.FromArray(data, 8000);

            Complex[,] samples = target.ToArray();

            Assert.IsTrue(data.IsEqual(samples));

            ComplexSignal[] windows = target.Split(window, 1);

            for (int i = 0; i < windows.Length; i++)
            {
                int min = System.Math.Min(i + length, samples.Length / 2);

                Complex[] segment = windows[i].ToArray().Reshape(1);
                Complex[] sub     = samples.Submatrix(i, min - 1, null).Reshape(1);

                var expected = new Complex[length * 2];
                for (int j = 0; j < sub.Length; j++)
                {
                    expected[j] = sub[j];
                }

                Assert.IsTrue(segment.IsEqual(expected));
            }
        }
Example #11
0
    void ProcessBaselinePowerSpectrum()
    {
        ComplexSignal cs = Signal.FromArray(baselineArray, SAMPLE_RATE).ToComplex();

        cs.ForwardFourierTransform();
        baselinePowerSpectrum = Tools.GetPowerSpectrum(cs.GetChannel(0));
        CubeStateManager.baselineEEG[(int)channelToRecordFrom] = baselineArray.Average();
    }
Example #12
0
        public void GetEnergyTest()
        {
            ComplexSignal target   = ComplexSignal.FromArray(data, 8000);
            double        expected = 0.5444;
            double        actual   = target.GetEnergy();

            Assert.AreEqual(expected, actual, 1e-4);
        }
Example #13
0
 /// <summary>
 ///   Applies the filter to a windowed signal.
 /// </summary>
 ///
 public ComplexSignal[] Apply(params ComplexSignal[] complexSignal)
 {
     ComplexSignal[] s = new ComplexSignal[complexSignal.Length];
     for (int i = 0; i < complexSignal.Length; i++)
     {
         s[i] = Apply(complexSignal[i]);
     }
     return(s);
 }
Example #14
0
        public void ComplexSignalConstructorTest()
        {
            ComplexSignal target = ComplexSignal.FromArray(data, 8000);

            Assert.AreEqual(target.Channels, 2);
            Assert.AreEqual(target.Length, 8);
            Assert.AreEqual(target.Samples, 16);
            Assert.AreEqual(target.SampleRate, 8000);
            Assert.IsNotNull(target.RawData);
        }
Example #15
0
        /// <summary>
        ///   Splits a signal using a window
        /// </summary>
        /// 
        public static ComplexSignal[] Split(this ComplexSignal signal, IWindow window, int step)
        {
            int n = (int)System.Math.Floor(signal.Length / (double)step);

            ComplexSignal[] windows = new ComplexSignal[n];

            for (int i = 0; i < n; i++)
            {
                windows[i] = window.Apply(signal, i * step);
            }

            return windows;
        }
Example #16
0
        public void ComplexSignalConstructorTest()
        {
            ComplexSignal target = ComplexSignal.FromArray(data, 8000);

            Assert.AreEqual(target.Channels, 2);
            Assert.AreEqual(target.Length, 8);
            Assert.AreEqual(target.Samples, 16);
            Assert.AreEqual(target.SampleRate, 8000);
            Assert.AreEqual(256, target.NumberOfBytes);
            Assert.AreEqual(16, target.InnerData.Length);
            Assert.AreEqual(16, target.SampleSize);
            Assert.IsNotNull(target.RawData);
        }
        private static double FindFrequencyCount(int sampleRate, Signal signal, double frequency)
        {
            ComplexSignal c = signal.ToComplex();

            c.ForwardFourierTransform();

            double[] freq = Accord.Audio.Tools.GetFrequencyVector(c.Length, sampleRate);
            double[] ms   = Accord.Audio.Tools.GetMagnitudeSpectrum(c.GetChannel(0));

            int idx = freq.Subtract(frequency).Abs().ArgMin();

            return(ms[idx]);
        }
Example #18
0
        /// <summary>
        ///   Splits a signal using a window
        /// </summary>
        ///
        public static ComplexSignal[] Split(this ComplexSignal signal, IWindow window, int step)
        {
            int n = (int)System.Math.Floor(signal.Length / (double)step);

            ComplexSignal[] windows = new ComplexSignal[n];

            for (int i = 0; i < n; i++)
            {
                windows[i] = window.Apply(signal, i * step);
            }

            return(windows);
        }
Example #19
0
        public ComplexSignal FFTComplex(string sFile)
        {
            WaveDecoder    signalwav    = new WaveDecoder("./Sounds/" + sFile + ".wav");
            Signal         sourceSignal = signalwav.Decode();
            BlackmanWindow window       = new BlackmanWindow(fs);

            Signal[]      windows  = sourceSignal.Split(window, fs);
            Signal        wSignal  = window.Apply(sourceSignal, fs);
            ComplexSignal sComplex = wSignal.ToComplex();

            sComplex.ForwardFourierTransform();
            signalwav.Close();
            return(sComplex);
        }
Example #20
0
        /// <summary>
        ///   Computes (populates) a Spectrogram mapping with values from a signal.
        /// </summary>
        ///
        /// <param name="signal">The signal to be windowed in the spectrogram.</param>
        ///
        public void Compute(Signal signal)
        {
            this.frequencies = FourierTransform2.GetFrequencyVector(windowSize, signal.SampleRate);

            Signal[] signals = signal.Split(window: window, stepSize: windowSize);

            this.magnitudes = new double[signals.Length][];
            for (int i = 0; i < signals.Length; i++)
            {
                ComplexSignal c = signals[i].ToComplex();
                c.ForwardFourierTransform();
                this.magnitudes[i] = FourierTransform2.GetMagnitudeSpectrum(c.GetChannel(channel));
            }
        }
Example #21
0
        public void waveInput()
        {
            writeHeaderForARFF();
            for (int i = 0; i < audioFileName.Length; i++)
            {
                string type;
                string name;
                setTypeAndName(audioFileName[i], out type, out name);


                Accord.Audio.Formats.WaveDecoder currentWav = new Accord.Audio.Formats.WaveDecoder(audioFileName[i]);
                //Used for time Domain
                Signal timeDomain = currentWav.Decode();


                //Creates an array of time domain samples for use in calculation of RootMeanSquare aka AverageEnergy
                float[] energyArray = new float[timeDomain.Samples];
                timeDomain.CopyTo(energyArray);

                //average energy for the current wav file
                double averageEnergy = Accord.Audio.Tools.RootMeanSquare(energyArray);
                //ZCR for the current wav file
                double zeroCrossingRate = zeroCrossingRateMethod(timeDomain);

                Accord.Audio.Windows.RaisedCosineWindow window = Accord.Audio.Windows.RaisedCosineWindow.Hamming(1024);
                Signal[] windows = timeDomain.Split(window, 512);
                //Used for Frequency Domain
                ComplexSignal[] tempFrequency = windows.Apply(ComplexSignal.FromSignal);
                tempFrequency.ForwardFourierTransform();
                ComplexSignal curComplex = tempFrequency[0];

                double[] power          = { };
                double[] magnitudes     = { };
                double[] freq           = { };
                var      length         = curComplex.Length / (2 + 1);
                double[] meanPower      = new double[length];
                double[] meanMagnitudes = new double[length];

                createFrequencyArray(tempFrequency, curComplex, out power, out magnitudes, out freq, out meanPower, out meanMagnitudes);


                //Spectral Centrois for the current wav file
                double spectralCentroid = meanMagnitudes.Zip(freq, (m, f) => m * f).Sum() / meanMagnitudes.Sum();
                //double spectralCentroid = spectralCentroidMethod(tempFrequency);

                //Writes data to arff file
                writeARFF(name, averageEnergy, zeroCrossingRate, spectralCentroid, type);
            }
        }
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        ///
        protected unsafe override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            int length = sourceData.Length;

            Complex *src = (Complex *)sourceData.Data.ToPointer();
            Complex *dst = (Complex *)destinationData.Data.ToPointer();

            for (int i = 0; i < length - 1; i++, src++, dst++)
            {
                double re = src[i + 1].Real - src[i].Real;

                // Retain only if difference is positive
                *dst = (re > 0) ? new Complex(re, 0) : Complex.Zero;
            }
        }
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        /// 
        protected unsafe override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            int length = sourceData.Length;

            Complex* src = (Complex*)sourceData.Data.ToPointer();
            Complex* dst = (Complex*)destinationData.Data.ToPointer();

            for (int i = 0; i < length - 1; i++, src++, dst++)
            {
                double re = src[i + 1].Real - src[i].Real;

                // Retain only if difference is positive
                *dst = (re > 0) ? new Complex(re, 0) : Complex.Zero;
            }
        }
Example #24
0
        /// <summary>
        /// Processes the filter.
        /// </summary>
        ///
        protected override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            int samples = sourceData.Samples;

            unsafe
            {
                Complex *src  = (Complex *)sourceData.Data.ToPointer();
                Complex *dst  = (Complex *)destinationData.Data.ToPointer();
                Complex *comb = (Complex *)combSignal.Data.ToPointer();

                for (int i = 0; i < samples; i++, src++, dst++, comb++)
                {
                    *dst = new Complex((src[0] * comb[0]).Magnitude, 0);
                }
            }
        }
Example #25
0
        public void ComplexSignalConstructor()
        {
            UnmanagedMemoryStream sourceStream      = Properties.Resources.Grand_Piano___Fazioli___major_A_middle;
            MemoryStream          destinationStream = new MemoryStream();

            // Create a decoder for the source stream
            WaveDecoder sourceDecoder = new WaveDecoder(sourceStream);

            // Decode the signal in the source stream
            Signal sourceSignal = sourceDecoder.Decode();

            int length = (int)Math.Pow(2, 12);

            RaisedCosineWindow window = RaisedCosineWindow.Hamming(length);

            Assert.AreEqual(length, window.Length);

            Signal[] windows = sourceSignal.Split(window, 1024);

            Assert.AreEqual(windows.Length, 172);

            foreach (var w in windows)
            {
                Assert.AreEqual(length, w.Length);
            }

            ComplexSignal[] complex = windows.Apply(ComplexSignal.FromSignal);

            for (int i = 0; i < complex.Length - 1; i++)
            {
                ComplexSignal c = complex[i];
                Assert.AreEqual(2, c.Channels);
                Assert.AreEqual(92, c.Duration);
                Assert.AreEqual(4096, c.Length);
                Assert.AreEqual(SampleFormat.Format128BitComplex, c.SampleFormat);
                Assert.AreEqual(44100, c.SampleRate);
                Assert.AreEqual(ComplexSignalStatus.Normal, c.Status);
            }

            complex.ForwardFourierTransform();

            for (int i = 0; i < complex.Length - 1; i++)
            {
                ComplexSignal c = complex[i];
                Assert.AreEqual(ComplexSignalStatus.FourierTransformed, c.Status);
            }
        }
Example #26
0
        public void ComplexSignalConstructor()
        {
            var          sourceStream      = SignalTest.GetSignal("a.wav");
            MemoryStream destinationStream = new MemoryStream();

            // Create a decoder for the source stream
            WaveDecoder sourceDecoder = new WaveDecoder(sourceStream);

            // Decode the signal in the source stream
            Signal sourceSignal = sourceDecoder.Decode();

            int length = (int)Math.Pow(2, 12);

            RaisedCosineWindow window = RaisedCosineWindow.Hamming(length);

            Assert.AreEqual(length, window.Length);

            Signal[] windows = sourceSignal.Split(window, 1024);

            Assert.AreEqual(windows.Length, 172);

            foreach (var w in windows)
            {
                Assert.AreEqual(length, w.Length);
            }

            ComplexSignal[] complex = windows.Apply(ComplexSignal.FromSignal);

            for (int i = 0; i < complex.Length - 1; i++)
            {
                ComplexSignal c = complex[i];
                Assert.AreEqual(2, c.Channels);
                Assert.AreEqual(93, c.Duration.TotalMilliseconds);
                Assert.AreEqual(4096, c.Length);
                Assert.AreEqual(SampleFormat.Format128BitComplex, c.SampleFormat);
                Assert.AreEqual(44100, c.SampleRate);
                Assert.AreEqual(ComplexSignalStatus.Normal, c.Status);
            }

            complex.ForwardFourierTransform();

            for (int i = 0; i < complex.Length - 1; i++)
            {
                ComplexSignal c = complex[i];
                Assert.AreEqual(ComplexSignalStatus.FourierTransformed, c.Status);
            }
        }
Example #27
0
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        protected unsafe override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            if (sourceData.Status != ComplexSignalStatus.Analytic)
            {
                throw new ArgumentException("Signal must be in analytic form.", "sourceData");
            }

            int samples = sourceData.Samples;

            Complex *src = (Complex *)sourceData.Data.ToPointer();
            Complex *dst = (Complex *)destinationData.Data.ToPointer();

            for (int i = 0; i < samples; i++, src++, dst++)
            {
                *dst = new Complex((*dst).Magnitude, 0);
            }
        }
Example #28
0
        /// <summary>
        ///   Applies the filter to a signal.
        /// </summary>
        ///
        public ComplexSignal Apply(ComplexSignal complexSignal)
        {
            // get number of channels and samples
            int channels = complexSignal.Channels;
            int samples  = complexSignal.Length;

            // retrieve other information
            int rate = complexSignal.SampleRate;

            // create new signal of required format
            ComplexSignal dstSignal = new ComplexSignal(channels, samples, rate);

            // process the filter
            ProcessFilter(complexSignal, dstSignal);

            // return the processed signal
            return(dstSignal);
        }
Example #29
0
        /// <summary>
        ///   Applies the filter to a signal.
        /// </summary>
        /// 
        public ComplexSignal Apply(ComplexSignal complexSignal)
        {
            // get number of channels and samples
            int channels = complexSignal.Channels;
            int samples = complexSignal.Length;

            // retrieve other information
            int rate = complexSignal.SampleRate;

            // create new signal of required format
            ComplexSignal dstSignal = new ComplexSignal(channels, samples, rate);

            // process the filter
            ProcessFilter(complexSignal, dstSignal);

            // return the processed signal
            return dstSignal;
        }
Example #30
0
        private void EqualsButton_Click(object sender, EventArgs e)
        {
            Dictionary <double, string> icorrelation = new Dictionary <double, string>();

            if (!Directory.Exists("./Sounds"))
            {
                return;
            }
            string[] dir_Sounds = Directory.GetFiles("./Sounds");
            double   max        = 0;

            foreach (var sound in dir_Sounds)
            {
                if (!(sound.Contains("temp.wav")) && (sound.Contains(".wav")))
                {
                    string name = sound.Replace("./Sounds", "").Replace(".wav", "").Replace(((char)92).ToString(), "");

                    ComplexSignal recordSignal  = FFTComplex("temp");
                    Complex[]     recordChannel = recordSignal.GetChannel(0);

                    ComplexSignal bd        = FFTComplex(name);
                    Complex[]     bdchannel = bd.GetChannel(0);

                    double[] rpower  = NormalizeStretch(FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(recordChannel)), 0, 1);
                    double[] bdpower = NormalizeStretch(FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(bdchannel)), 0, 1);
                    double   corr    = ComputeCoeff(bdpower, rpower);
                    Console.WriteLine(name + ":" + corr);
                    icorrelation.Add(corr, name);
                    if (corr >= max)
                    {
                        max = corr;
                    }
                }
            }

            foreach (KeyValuePair <double, string> pair in icorrelation)
            {
                if (pair.Key == max)
                {
                    MessageBox.Show(pair.Value);
                }
            }
        }
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        /// 
        protected override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            int length = sourceData.Length;

            unsafe
            {
                Complex* src = (Complex*)sourceData.Data.ToPointer();
                Complex* dst = (Complex*)destinationData.Data.ToPointer();

                Complex d = new Complex();

                for (int i = 0; i < length - 1; i++, src++, dst++)
                {
                    d.Re = src[i + 1].Re - src[i].Re;

                    // Retain only if difference is positive
                    *dst = (d.Re > 0) ? d : Complex.Zero;
                }
            }
        }
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        ///
        protected override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            int length = sourceData.Length;

            unsafe
            {
                Complex *src = (Complex *)sourceData.Data.ToPointer();
                Complex *dst = (Complex *)destinationData.Data.ToPointer();

                Complex d = new Complex();

                for (int i = 0; i < length - 1; i++, src++, dst++)
                {
                    d.Re = src[i + 1].Re - src[i].Re;

                    // Retain only if difference is positive
                    *dst = (d.Re > 0) ? d : Complex.Zero;
                }
            }
        }
Example #33
0
        /// <summary>
        ///   Splits a signal using the window.
        /// </summary>
        ///
        public virtual unsafe ComplexSignal Apply(ComplexSignal complexSignal, int sampleIndex)
        {
            Complex[,] resultData = new Complex[Length, complexSignal.Channels];
            ComplexSignal result = ComplexSignal.FromArray(resultData, complexSignal.SampleRate);

            int channels  = result.Channels;
            int minLength = System.Math.Min(complexSignal.Length - sampleIndex, Length);

            for (int c = 0; c < complexSignal.Channels; c++)
            {
                Complex *dst = (Complex *)result.Data.ToPointer() + c;
                Complex *src = (Complex *)complexSignal.Data.ToPointer() + c + channels * sampleIndex;

                for (int i = 0; i < minLength; i++, dst += channels, src += channels)
                {
                    *dst = window[i] * (*src);
                }
            }

            return(result);
        }
Example #34
0
        /// <summary>
        /// Processes the filter.
        /// </summary>
        ///
        protected override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            SampleFormat format   = sourceData.SampleFormat;
            int          channels = sourceData.Channels;
            int          length   = sourceData.Length;
            int          samples  = sourceData.Samples;

            unsafe
            {
                Complex *src  = (Complex *)sourceData.Data.ToPointer();
                Complex *dst  = (Complex *)destinationData.Data.ToPointer();
                Complex *comb = (Complex *)combSignal.Data.ToPointer();

                Complex d = new Complex();

                for (int i = 0; i < samples; i++, src++, dst++, comb++)
                {
                    d.Re = (src[0] * comb[0]).Magnitude;
                    *dst = d;
                }
            }
        }
Example #35
0
        int status = 0;                          ///记录上次情绪状态
        //Signal S2;用作第二通道
        //ComplexSignal signal;

        private void fft_process()
        {
            S1     = Signal.FromArray(channel1, 250, SampleFormat.Format32BitIeeeFloat); //从浮点数组创建一个新的信号
            signal = S1.ToComplex();                                                     //将此信号转换为ComplexSignal信号
            signal.ForwardFourierTransform();                                            //对ComplexSignal信号应用前向快速傅立叶变换

            // Now we can get the power spectrum output and its
            // related frequency vector to plot our spectrometer.
            Complex[] channel = signal.GetChannel(0);                                                    //从信号中提取通道
            double[]  power   = Accord.Audio.Tools.GetPowerSpectrum(channel);                            //计算复杂信号的功率谱
            double[]  freqv   = Accord.Audio.Tools.GetFrequencyVector(signal.Length, signal.SampleRate); //创建均匀间隔的频率向量(假设对称FFT)
            power[0] = 0;                                                                                // zero DC
            float[] g = new float[power.Length];
            for (int i = 0; i < power.Length; i++)
            {
                g[i]       = (float)power[i];
                signal1[i] = g[i] * 1000;
                SetpowerCC(g[i]);     //输出power的值
                SetFrequCC(freqv[i]); //输出freqv的值
            }
            powerCaculate();          //对小于20hz的部分求和
            pictureBox2.Refresh();    //重新绘制Channel1频域图
        }
Example #36
0
 /// <summary>
 ///   Processes the filter.
 /// </summary>
 /// 
 protected abstract void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData);
Example #37
0
        /// <summary>
        ///   Splits a signal using the window.
        /// </summary>
        /// 
        public virtual ComplexSignal Apply(ComplexSignal complexSignal, int sampleIndex)
        {
            Complex[,] resultData = new Complex[Length, complexSignal.Channels];
            ComplexSignal result = ComplexSignal.FromArray(resultData, complexSignal.SampleRate);

            int channels = result.Channels;
            int minLength = System.Math.Min(complexSignal.Length - sampleIndex, Length);

            unsafe
            {
                for (int c = 0; c < complexSignal.Channels; c++)
                {
                    Complex* dst = (Complex*)result.Data.ToPointer() + c;
                    Complex* src = (Complex*)complexSignal.Data.ToPointer() + c + channels * sampleIndex;

                    for (int i = 0; i < minLength; i++, dst += channels, src += channels)
                    {
                        *dst = window[i] * (*src);
                    }
                }
            }

            return result;
        }