public static void Example2() //================[ Basic DFT + Windowing Example ]================ { // Same Input Signal as Example 1 double amplitude = 1.0; double frequency = 20000; UInt32 length = 1000; double samplingRate = 100000; double[] inputSignal = DSP.Generate.ToneSampling(amplitude, frequency, samplingRate, length); // Apply window to the Input Data & calculate Scale Factor double[] wCoefs = DSP.Window.Coefficients(DSP.Window.Type.Hamming, length); double[] wInputData = DSP.Math.Multiply(inputSignal, wCoefs); double wScaleFactor = DSP.Window.ScaleFactor.Signal(wCoefs); // Instantiate & Initialize a new DFT DSPLib.DFT dft = new DSPLib.DFT(); dft.Inicializar(length); // Call the DFT and get the scaled spectrum back Complex[] cSpectrum = dft.Executar(wInputData); // Convert the complex spectrum to note: Magnitude Format double[] lmSpectrum = DSP.ConvertComplex.ToMagnitude(cSpectrum); // Properly scale the spectrum for the added window lmSpectrum = DSP.Math.Multiply(lmSpectrum, wScaleFactor); // For plotting on an XY Scatter plot generate the X Axis frequency Span double[] freqSpan = dft.FrequencySpan(samplingRate); // At this point a XY Scatter plot can be generated from, // X axis => freqSpan // Y axis => lmSpectrum }
public static void Example9() //================[ Basic DFT Phase Test ]================ { // Generate a Phase Ramp between two signals double[] resultPhase = new double[360]; // Instantiate & Initialize a new DFT DSPLib.DFT dft = new DSPLib.DFT(); dft.Inicializar(2048); for (Int32 phase = 0; phase < 360; phase++) { double[] inputSignalRef = DSP.Generate.ToneCycles(7.0, 128, 2048); double[] inputSignalPhase = 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.Executar(inputSignalRef); Complex[] cSpectrumPhase = dft.Executar(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 = DSP.ConvertComplex.ToPhaseDegrees(cSpectrumRef); double[] resultArrayPhase = DSP.ConvertComplex.ToPhaseDegrees(cSpectrumPhase); 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. }
public static void Example6() //================[ Basic DFT + Noise Analysis Example ]================ { // 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 = DSP.Window.Coefficients(DSP.Window.Type.Hamming, length); double wScaleFactor = DSP.Window.ScaleFactor.Noise(wCoefs, samplingRate); // Instantiate & Initialize a new DFT DSPLib.DFT dft = new DSPLib.DFT(); dft.Inicializar(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 = DSP.Generate.NoisePsd(amplitude, samplingRate, length); inputSignal = DSP.Math.Multiply(inputSignal, wCoefs); // DFT the noise -> Convert -> Sum Complex[] cSpectrum = dft.Executar(inputSignal); double[] mag2 = DSP.ConvertComplex.ToMagnitudeSquared(cSpectrum); if (N == 0) { noiseSum = DSP.Math.Add(noiseSum, 0); } else { noiseSum = DSP.Math.Add(noiseSum, mag2); } } // Calculate Average, convert to magnitude format // See text for the reasons to use Mag^2 format. double[] averageNoise = DSP.Math.Divide(noiseSum, N); double[] lmSpectrum = DSP.ConvertMagnitudeSquared.ToMagnitude(averageNoise); // Properly scale the spectrum for the added window lmSpectrum = DSP.Math.Multiply(lmSpectrum, wScaleFactor); // For plotting on an XY Scatter plot generate the X Axis frequency Span double[] freqSpan = dft.FrequencySpan(samplingRate); // 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 = DSP.Analyze.FindMean(lmSpectrum, 20, 20); }
public static void Example8() //================[ Basic DFT + Signal & Noise + Log Magnitude ]================ { // Same Input Signal as Example 1, except amplitude is 5 Vrms. double amplitude = 5.0; double frequency = 20000; UInt32 length = 1000; double samplingRate = 100000; double[] inputSignal = DSP.Generate.ToneSampling(amplitude, frequency, samplingRate, length); // Add noise that is about 80 dBc from signal level // 80 dBc is about the level of noise from a 14 bit ADC // 1/10,000 down from full scale double[] inputNoise = DSP.Generate.NoiseRms(amplitude / 10000.0, length); // Add noise to the signal double[] compositeInput = DSP.Math.Add(inputSignal, inputNoise); // Use the BH92 type window - this is a very "Spectrum Analyzer" like window. // Apply window to the Input Data & calculate Scale Factor double[] wCoefs = DSP.Window.Coefficients(DSP.Window.Type.BH92, length); double wScaleFactor = DSP.Window.ScaleFactor.Signal(wCoefs); double[] wInputData = DSP.Math.Multiply(compositeInput, wCoefs); // Instantiate & Initialize a new DFT DSPLib.DFT dft = new DSPLib.DFT(); dft.Inicializar(length); // Call the DFT and get the scaled spectrum back Complex[] cSpectrum = dft.Executar(wInputData); // Convert the complex spectrum to note: Magnitude Format double[] lmSpectrum = DSP.ConvertComplex.ToMagnitude(cSpectrum); // Properly scale the spectrum for the added window lmSpectrum = DSP.Math.Multiply(lmSpectrum, wScaleFactor); // Convert from linear magnitude to log magnitude format double[] logMagSpectrum = DSP.ConvertMagnitude.ToMagnitudeDBV(lmSpectrum); // For plotting on an XY Scatter plot generate the X Axis frequency Span double[] freqSpan = dft.FrequencySpan(samplingRate); // At this point a XY Scatter plot can be generated from, // X axis => freqSpan // Y axis => logMagSpectrum // In this example - maximum amplitude of 13.974 dBV is at bin 200 (20,000 Hz) }