Ejemplo n.º 1
0
        public void BasicWnd()
        {
            // Same Input Signal as Example 1
            double amplitude = 1.0; double frequency = 20000;
            UInt32 length       = 1000;
            double samplingRate = 100000;

            double[] inputSignal = mdsplib.DSP.Generate.ToneSampling(amplitude, frequency, samplingRate, length);

            // Apply window to the Input Data & calculate Scale Factor
            double[] wCoefs       = mdsplib.DSP.Window.Coefficients(mdsplib.DSP.Window.Type.Hamming, length);
            double[] wInputData   = inputSignal.Multiply(wCoefs);
            double   wScaleFactor = mdsplib.DSP.Window.ScaleFactor.Signal(wCoefs);

            // Instantiate & Initialize a new DFT
            DFT dft = new DFT();

            dft.Initialize(length);

            // Call the DFT and get the scaled spectrum back
            Complex[] cSpectrum = dft.Execute(wInputData);

            // Convert the complex spectrum to note: Magnitude Format
            double[] lmSpectrum = cSpectrum.Magnitude();

            // Properly scale the spectrum for the added window
            lmSpectrum = lmSpectrum.Multiply(wScaleFactor);

            // For plotting on an XY Scatter plot generate the X Axis frequency Span
            double[] freqSpan = Util.FFT.FrequencySpan(samplingRate, length);

            // At this point a XY Scatter plot can be generated from,
            // X axis => freqSpan
            // Y axis => lmSpectrum
        }
Ejemplo n.º 2
0
        public void PhaseAnalysis()
        {
            // Generate a Phase Ramp between two signals
            double[] resultPhase = new double[360];

            // Instantiate & Initialize a new DFT
            DFT dft = new DFT();

            dft.Initialize(2048);

            for (Int32 phase = 0; phase < 360; phase++)
            {
                double[] inputSignalRef   = mdsplib.DSP.Generate.ToneCycles(7.0, 128, 2048);
                double[] inputSignalPhase = mdsplib.DSP.Generate.ToneCycles(7.0, 128, 2048, phaseDeg: phase);

                // Call the DFT and get the scaled spectrum back of a reference and a phase shifted signal.
                Complex[] cSpectrumRef   = dft.Execute(inputSignalRef);
                Complex[] cSpectrumPhase = dft.Execute(inputSignalPhase);

                // Magnitude Format - Just as a test point
                //double[] lmSpectrumTest = DSPLib.DSP.ConvertComplex.ToMagnitude(cSpectrumRef);
                //double[] lmSpectrumTestPhase = DSPLib.DSP.ConvertComplex.ToMagnitude(cSpectrumPhase);

                // Extract the phase of bin 128
                double[] resultArrayRef   = cSpectrumRef.PhaseDegrees();
                double[] resultArrayPhase = cSpectrumPhase.PhaseDegrees();
                resultPhase[phase] = resultArrayPhase[128] - resultArrayRef[128];
            }

            // resultPhase has a linear phase incrementing signal at this point.
            // Use UnwrapPhase() to remove phase jumps in output data.
        }
Ejemplo n.º 3
0
        public void NoiseAnalysis()
        {
            // Setup parameters to generate noise test signal of 5 nVrms / rt-Hz
            double amplitude = 5.0e-9;
            UInt32 length = 1000; double samplingRate = 2000;

            // Generate window & calculate Scale Factor for NOISE!
            double[] wCoefs       = mdsplib.DSP.Window.Coefficients(mdsplib.DSP.Window.Type.Hamming, length);
            double   wScaleFactor = mdsplib.DSP.Window.ScaleFactor.Noise(wCoefs, samplingRate);

            // Instantiate & Initialize a new DFT
            DFT dft = new DFT();

            dft.Initialize(length);

            // Average the noise 'N' times
            Int32 N = 1000;

            double[] noiseSum = new double[(length / 2) + 1];
            for (Int32 i = 0; i < N; i++)
            {
                // Generate the noise signal & apply window
                double[] inputSignal = mdsplib.DSP.Generate.NoisePsd(amplitude, samplingRate, length);
                inputSignal = inputSignal.Multiply(wCoefs);

                // DFT the noise -> Convert -> Sum
                Complex[] cSpectrum = dft.Execute(inputSignal);
                double[]  mag2      = cSpectrum.MagnitudeSquared();
                noiseSum = (N == 0) ? noiseSum : noiseSum = noiseSum.Add(mag2);
            }

            // Calculate Average, convert to magnitude format
            // See text for the reasons to use Mag^2 format.
            double[] averageNoise = noiseSum.Divide(N);
            double[] lmSpectrum   = averageNoise.Magnitude();

            // Properly scale the spectrum for the added window
            lmSpectrum = lmSpectrum.Multiply(wScaleFactor);

            // For plotting on an XY Scatter plot generate the X Axis frequency Span
            double[] freqSpan = Util.FFT.FrequencySpan(samplingRate, length);

            // At this point a XY Scatter plot can be generated from,
            // X axis => freqSpan
            // Y axis => lmSpectrum as a Power Spectral Density Value

            // Extra Credit - Analyze Plot Data Ignoring the first and last 20 Bins
            // Average value should be what we generated = 5.0e-9 Vrms / rt-Hz
            double averageValue = mdsplib.DSP.Analyze.FindMean(lmSpectrum, 20, 20);
        }
Ejemplo n.º 4
0
        public void GetSpectre(List <double> values, double samplingRate, out double[] spectrum, out double[] freqSpan)
        {
            // Instantiate a new DFT
            DFT dft = new DFT();

            // Initialize the DFT
            // You only need to do this once or if you change any of the DFT parameters.
            dft.Initialize((uint)values.Count);

            // Call the DFT and get the scaled spectrum back
            Complex[] cSpectrum = dft.Execute(values.ToArray());

            // Convert the complex spectrum to magnitude
            spectrum = DSP.ConvertComplex.ToMagnitude(cSpectrum);
            freqSpan = dft.FrequencySpan(samplingRate);
        }
Ejemplo n.º 5
0
        public void Basic()
        {
            // Generate a test signal,
            //      1 Vrms at 20,000 Hz
            //      Sampling Rate = 100,000 Hz
            //      DFT Length is 1000 Points
            double amplitude    = 1.0;
            double frequency    = 20000;
            UInt32 length       = 1000;
            double samplingRate = 100000;

            double[] inputSignal = mdsplib.DSP.Generate.ToneSampling(amplitude, frequency, samplingRate, length);

            // Instantiate a new DFT
            DFT dft = new DFT();

            // Initialize the DFT
            // You only need to do this once or if you change any of the DFT parameters.
            dft.Initialize(length);

            // Call the DFT and get the scaled spectrum back
            Complex[] cSpectrum = dft.Execute(inputSignal);

            // Convert the complex spectrum to magnitude
            double[] lmSpectrum = cSpectrum.Magnitude();

            // Note: At this point lmSpectrum is a 501 byte array that
            // contains a properly scaled Spectrum from 0 - 50,000 Hz

            // For plotting on an XY Scatter plot generate the X Axis frequency Span
            double[] freqSpan = Util.FFT.FrequencySpan(samplingRate, length);

            // At this point a XY Scatter plot can be generated from,
            // X axis => freqSpan
            // Y axis => lmSpectrum

            // In this example the maximum value of 1 Vrms is located at bin 200 (20,000 Hz)
        }
        public void GenerateFFTData()
        {
            var vector = this.sourceFunction.ToYVector();

            var array = vector.Select(s => (double)s).ToArray();
            var dft   = new DFT();

            dft.Initialize((uint)array.Length);
            Complex[] cSpectrum = dft.Execute(array);

            var An = DSP.ConvertComplex.ToMagnitude(cSpectrum);
            var Pn = DSP.ConvertComplex.ToPhaseRadians(cSpectrum);

            LogTool.AssertIsTrue(An.Length == Pn.Length);

            this.cosData.Clear();
            for (var i = 0; i < An.Length; ++i)
            {
                this.cosData.Add(new CosData()
                {
                    amplitude = (float)An[i], frequency = i, phase = (float)Pn[i]
                });
            }
        }
        async private Task CalcXYAsync(bool isDFT, double zeroOffset)
        {
            // Ensure row was clicked and not empty space
            try
            {
                //SensorReadingsModel s = (SensorReadingsModel)dataGrid.SelectedItem;
                if (_lastSensorReadingsModel.Reading_Type.Equals(Keys.VIBRATION_KEY))
                {
                    List <double> y = JsonConvert.DeserializeObject <List <double> >(_lastSensorReadingsModel.Reading);

                    for (int i = 0; i < y.Count; i++)
                    {
                        y[i] = y[i] - zeroOffset;
                    }

                    if (isDFT)
                    {
                        //use a lower number if the PC doesn't have the CPU to handle large zero padding
                        UInt32 zeroPadding = 12000; // NOTE: Zero Padding

                        // Apply window to the Input Data & calculate Scale Factor
                        double[] wCoefs       = DSP.Window.Coefficients(DSP.Window.Type.Hamming, (UInt32)y.Count);
                        double[] wInputData   = DSP.Math.Multiply(y.ToArray(), wCoefs);
                        double   wScaleFactor = DSP.Window.ScaleFactor.Signal(wCoefs);

                        DFT dft = new DFT();
                        dft.Initialize((UInt32)y.Count, zeroPadding);
                        // Call the DFT and get the scaled spectrum back
                        Complex[] cSpectrum = dft.Execute(y.ToArray());

                        // Convert the complex spectrum to magnitude
                        double[] lmSpectrum = DSP.ConvertComplex.ToMagnitude(cSpectrum);

                        // Properly scale the spectrum for the added window
                        lmSpectrum = DSP.Math.Multiply(lmSpectrum, wScaleFactor);

                        // Note: At this point lmSpectrum is a 501 byte array that
                        // contains a properly scaled Spectrum from 0 - 50,000 Hz (1/2 the Sampling Frequency)

                        // For plotting on an XY Scatter plot generate the X Axis frequency Span
                        double[] freqSpan = dft.FrequencySpan(_lastSensorReadingsModel.Vib_Sample_Rate);

                        // At this point a XY Scatter plot can be generated from,
                        // X axis => freqSpan
                        // Y axis => lmSpectrum

                        _x = freqSpan;
                        _y = lmSpectrum;
                    }
                    else
                    {
                        double[] x = new double[y.Count];
                        for (int i = 0; i < x.Length; i++)
                        {
                            x[i] = (1 / _lastSensorReadingsModel.Vib_Sample_Rate) * i;
                        }

                        _x = x;
                        _y = y.ToArray();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }

            progressBar.Visibility = Visibility.Hidden;
            dftText.Visibility     = Visibility.Hidden;
        }
Ejemplo n.º 8
0
        void ComputeTransformation()
        {
            // Generate a test signal,
            //  1 Vrms at 20,000 Hz
            //  Sampling Rate = 100,000 Hz
            //  DFT Length is 1000 Points
            double frequency    = double.Parse(Frequency, cultureUS);
            UInt32 lengthSample = UInt32.Parse(Lenght, cultureUS);
            UInt32 zeros        = UInt32.Parse(ZerosNumber, cultureUS);
            double samplingRate = double.Parse(SamplingFrequency);

            Argument timeArg, frequencyArg;

            org.mariuszgromada.math.mxparser.Expression expression;

            FttHelper.GetExpressionFromString(FunctionToFFT, out timeArg, out frequencyArg, out expression);

            double[] inputSignal = FttHelper.GetInputData(frequency, lengthSample, samplingRate, timeArg, frequencyArg, expression);


            // Apply window to the time series data
            double[] wc = DSP.Window.Coefficients((DSP.Window.Type)Enum.Parse(typeof(DSP.Window.Type), WindowFunction), lengthSample);

            double windowScaleFactor = DSP.Window.ScaleFactor.Signal(wc);

            double[] windowedTimeSeries = DSP.Math.Multiply(inputSignal, wc);


            _oldShiftValue = IsShift;
            if (IsFFT)
            {
                FFT fft = new();
                fft.Initialize(lengthSample, zeros, fullFrequencyData: IsShift);
                _cSpectrum = fft.Execute(windowedTimeSeries);
                _cSpectrum = fft.ShiftData(_cSpectrum, IsShift);

                _freqSpan = fft.FrequencySpan(samplingRate);
            }
            else
            {
                // Instantiate a new DFT
                DFT dft = new DFT();
                // Initialize the DFT
                // You only need to do this once or if you change any of the DFT parameters.

                dft.Initialize(lengthSample, zeros, fullFrequency: IsShift);
                // Call the DFT and get the scaled spectrum back
                _cSpectrum = dft.Execute(windowedTimeSeries);
                _cSpectrum = dft.ShiftData(_cSpectrum, IsShift);
                // For plotting on an XY Scatter plot, generate the X Axis frequency Span
                _freqSpan = dft.FrequencySpan(samplingRate);
            }



            // Note: At this point, lmSpectrum is a 501 byte array that
            // contains a properly scaled Spectrum from 0 - 50,000 Hz (1/2 the Sampling Frequency)


            // Convert the complex spectrum to magnitude

            double[] lmSpectrum = DSP.ConvertComplex.ToMagnitude(_cSpectrum);
            IsMagnitude = true;
            // double[] lmSpectrum = _cSpectrum.ToList().Select(a => a.Magnitude).ToArray();

            // At this point a XY Scatter plot can be generated from,
            // X axis => freqSpan
            // Y axis => lmSpectrum

            // In this example, the maximum value of 1 Vrms is located at bin 200 (20,000 Hz)


            // XAxe = ChartHelper.GetXAxisLabelForFrequencyCart(freqSpan);
            SeriesFrequency = ChartHelper.GetSeriesFrequency(lmSpectrum, _freqSpan, IsShift);

            // ((LineSeries<DataModel>)SeriesFrequency[0]).GeometryFill.StrokeThickness = 0.5f;


            SeriesTime = ChartHelper.GetSeriesTime(windowedTimeSeries, samplingRate);
        }