public void ThrowsOnNullData() { double[] data = null; // ReSharper disable InvokeAsExtensionMethod Assert.That(() => Statistics.Minimum(data), Throws.Exception); Assert.That(() => Statistics.Maximum(data), Throws.Exception); Assert.That(() => Statistics.Mean(data), Throws.Exception); Assert.That(() => Statistics.Median(data), Throws.Exception); Assert.That(() => Statistics.Quantile(data, 0.3), Throws.Exception); Assert.That(() => Statistics.Variance(data), Throws.Exception); Assert.That(() => Statistics.StandardDeviation(data), Throws.Exception); Assert.That(() => Statistics.PopulationVariance(data), Throws.Exception); Assert.That(() => Statistics.PopulationStandardDeviation(data), Throws.Exception); Assert.That(() => Statistics.Covariance(data, data), Throws.Exception); Assert.That(() => Statistics.PopulationCovariance(data, data), Throws.Exception); // ReSharper restore InvokeAsExtensionMethod Assert.That(() => SortedArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.OrderStatistic(data, 1), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.Median(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.LowerQuartile(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.UpperQuartile(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.Percentile(data, 30), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.Quantile(data, 0.3), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.InterquartileRange(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => SortedArrayStatistics.FiveNumberSummary(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.OrderStatisticInplace(data, 1), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.Mean(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.Variance(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.StandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.PopulationVariance(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.PopulationStandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.Covariance(data, data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.PopulationCovariance(data, data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.MedianInplace(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => ArrayStatistics.QuantileInplace(data, 0.3), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.Mean(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.Variance(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.StandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.PopulationVariance(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.PopulationStandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.Covariance(data, data), Throws.Exception.TypeOf <NullReferenceException>()); Assert.That(() => StreamingStatistics.PopulationCovariance(data, data), Throws.Exception.TypeOf <NullReferenceException>()); }
public void MaximumOfEmptyMustBeNaN() { Assert.That(Statistics.Maximum(new double[0]), Is.NaN); Assert.That(Statistics.Maximum(new[] { 2d }), Is.Not.NaN); Assert.That(ArrayStatistics.Maximum(new double[0]), Is.NaN); Assert.That(ArrayStatistics.Maximum(new[] { 2d }), Is.Not.NaN); Assert.That(SortedArrayStatistics.Maximum(new double[0]), Is.NaN); Assert.That(SortedArrayStatistics.Maximum(new[] { 2d }), Is.Not.NaN); Assert.That(StreamingStatistics.Maximum(new double[0]), Is.NaN); Assert.That(StreamingStatistics.Maximum(new[] { 2d }), Is.Not.NaN); }
private static List <StatResult> NumericStat(List <string> data) { List <StatResult> results = new List <StatResult>(); try { var numData = data.Select(x => double.Parse(x)).ToList(); double min = ArrayStatistics.Minimum(numData.ToArray()); double max = ArrayStatistics.Maximum(numData.ToArray()); results.Add(new StatResult { Label = "Moyenne", Result = ArrayStatistics.Mean(numData.ToArray()).ToString(CultureInfo.InvariantCulture) }); results.Add(new StatResult { Label = "Médiane", Result = ArrayStatistics.MedianInplace(numData.ToArray()).ToString(CultureInfo.InvariantCulture) }); results.Add(new StatResult { Label = "Maximum", Result = max.ToString(CultureInfo.InvariantCulture) }); results.Add(new StatResult { Label = "Minimum", Result = min.ToString(CultureInfo.InvariantCulture) }); results.Add(new StatResult { Label = "Étendue", Result = (max - min).ToString(CultureInfo.InvariantCulture) }); results.Add(new StatResult { Label = "Écart-type", Result = ArrayStatistics.StandardDeviation(numData.ToArray()).ToString(CultureInfo.InvariantCulture) }); results.Add(new StatResult { Label = "Variance", Result = ArrayStatistics.Variance(numData.ToArray()).ToString(CultureInfo.InvariantCulture) }); } catch (Exception ex) { Logger.LogError(ex.Message); } return(results); }
public void ThrowsOnNullData() { double[] data = null; Assert.Throws <ArgumentNullException>(() => Statistics.Minimum(data)); Assert.Throws <ArgumentNullException>(() => Statistics.Maximum(data)); Assert.Throws <ArgumentNullException>(() => Statistics.Mean(data)); Assert.Throws <ArgumentNullException>(() => Statistics.Median(data)); Assert.Throws <ArgumentNullException>(() => Statistics.Quantile(data, 0.3)); Assert.Throws <ArgumentNullException>(() => Statistics.Variance(data)); Assert.Throws <ArgumentNullException>(() => Statistics.StandardDeviation(data)); Assert.Throws <ArgumentNullException>(() => Statistics.PopulationVariance(data)); Assert.Throws <ArgumentNullException>(() => Statistics.PopulationStandardDeviation(data)); Assert.Throws <ArgumentNullException>(() => Statistics.Covariance(data, data)); Assert.Throws <ArgumentNullException>(() => Statistics.PopulationCovariance(data, data)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Minimum(data)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Maximum(data)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.OrderStatistic(data, 1)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Median(data)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.LowerQuartile(data)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.UpperQuartile(data)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Percentile(data, 30)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Quantile(data, 0.3)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.InterquartileRange(data)); Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.FiveNumberSummary(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Minimum(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Maximum(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.OrderStatisticInplace(data, 1)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Mean(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Variance(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.StandardDeviation(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationVariance(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationStandardDeviation(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Covariance(data, data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationCovariance(data, data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.MedianInplace(data)); Assert.Throws <ArgumentNullException>(() => ArrayStatistics.QuantileInplace(data, 0.3)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Minimum(data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Maximum(data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Mean(data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Variance(data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.StandardDeviation(data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationVariance(data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationStandardDeviation(data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Covariance(data, data)); Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationCovariance(data, data)); }
public void MinimumMaximumOnShortSequence() { var samples = new[] { -1.0, 5, 0, -3, 10, -0.5, 4 }; Assert.That(Statistics.Minimum(samples), Is.EqualTo(-3), "Min"); Assert.That(Statistics.Maximum(samples), Is.EqualTo(10), "Max"); Assert.That(ArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min"); Assert.That(ArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max"); Assert.That(StreamingStatistics.Minimum(samples), Is.EqualTo(-3), "Min"); Assert.That(StreamingStatistics.Maximum(samples), Is.EqualTo(10), "Max"); Array.Sort(samples); Assert.That(SortedArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min"); Assert.That(SortedArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max"); }
public void DoesNotThrowOnEmptyData() { double[] data = new double[0]; Assert.DoesNotThrow(() => Statistics.Minimum(data)); Assert.DoesNotThrow(() => Statistics.Maximum(data)); Assert.DoesNotThrow(() => Statistics.Mean(data)); Assert.DoesNotThrow(() => Statistics.Median(data)); Assert.DoesNotThrow(() => Statistics.Quantile(data, 0.3)); Assert.DoesNotThrow(() => Statistics.Variance(data)); Assert.DoesNotThrow(() => Statistics.StandardDeviation(data)); Assert.DoesNotThrow(() => Statistics.PopulationVariance(data)); Assert.DoesNotThrow(() => Statistics.PopulationStandardDeviation(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.Minimum(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.Maximum(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.OrderStatistic(data, 1)); Assert.DoesNotThrow(() => SortedArrayStatistics.Median(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.LowerQuartile(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.UpperQuartile(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.Percentile(data, 30)); Assert.DoesNotThrow(() => SortedArrayStatistics.Quantile(data, 0.3)); Assert.DoesNotThrow(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0)); Assert.DoesNotThrow(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest)); Assert.DoesNotThrow(() => SortedArrayStatistics.InterquartileRange(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.FiveNumberSummary(data)); Assert.DoesNotThrow(() => ArrayStatistics.Minimum(data)); Assert.DoesNotThrow(() => ArrayStatistics.Maximum(data)); Assert.DoesNotThrow(() => ArrayStatistics.OrderStatisticInplace(data, 1)); Assert.DoesNotThrow(() => ArrayStatistics.Mean(data)); Assert.DoesNotThrow(() => ArrayStatistics.Variance(data)); Assert.DoesNotThrow(() => ArrayStatistics.StandardDeviation(data)); Assert.DoesNotThrow(() => ArrayStatistics.PopulationVariance(data)); Assert.DoesNotThrow(() => ArrayStatistics.PopulationStandardDeviation(data)); Assert.DoesNotThrow(() => ArrayStatistics.MedianInplace(data)); Assert.DoesNotThrow(() => ArrayStatistics.QuantileInplace(data, 0.3)); Assert.DoesNotThrow(() => StreamingStatistics.Minimum(data)); Assert.DoesNotThrow(() => StreamingStatistics.Maximum(data)); Assert.DoesNotThrow(() => StreamingStatistics.Mean(data)); Assert.DoesNotThrow(() => StreamingStatistics.Variance(data)); Assert.DoesNotThrow(() => StreamingStatistics.StandardDeviation(data)); Assert.DoesNotThrow(() => StreamingStatistics.PopulationVariance(data)); Assert.DoesNotThrow(() => StreamingStatistics.PopulationStandardDeviation(data)); }
//[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static double Quartile(double[] array, int quant) { switch (quant) { case 0: return ArrayStatistics.Minimum(array); case 1: return array.QuantileCustom(0.25, QuantileDefinition.Excel); case 2: return array.QuantileCustom(0.5, QuantileDefinition.Excel); case 3: return array.QuantileCustom(0.75, QuantileDefinition.Excel); case 4: return ArrayStatistics.Maximum(array); default: throw new ArgumentOutOfRangeException("quant"); } }
/// <summary> /// Find the Maximum value from the double array (supported platforms: MathNet, Intel IPP) /// </summary> /// <param name="data">input data</param> /// <returns>maximum value</returns> public static double Maximum(double[] data) { try { switch (Engine.Provider) { case ProviderEngine.MathNet: return(ArrayStatistics.Maximum(data)); case ProviderEngine.IntelIPP: return(IPP.Statistics.FindMaxValue(data)); default: return(ArrayStatistics.Maximum(data)); } } catch (Exception ex) { throw new Exception(ex.Message); } }
private double[] ApplyAggregationFunction(AggregationMethod method, string argument, int kernelSize, double[] data, double nanLimit, ILogger logger) { var targetDatasetLength = data.Length / kernelSize; var result = new double[targetDatasetLength]; switch (method) { case AggregationMethod.Mean: Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var isHighQuality = (chunkData.Length / (double)kernelSize) >= nanLimit; if (isHighQuality) { result[x] = ArrayStatistics.Mean(chunkData); } else { result[x] = double.NaN; } }); break; case AggregationMethod.MeanPolar: double[] sin = new double[targetDatasetLength]; double[] cos = new double[targetDatasetLength]; double limit; if (argument.Contains("*PI")) { limit = Double.Parse(argument.Replace("*PI", "")) * Math.PI; } else { limit = Double.Parse(argument); } var factor = 2 * Math.PI / limit; Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var length = chunkData.Length; var isHighQuality = (length / (double)kernelSize) >= nanLimit; if (isHighQuality) { for (int i = 0; i < chunkData.Length; i++) { sin[x] += Math.Sin(chunkData[i] * factor); cos[x] += Math.Cos(chunkData[i] * factor); } result[x] = Math.Atan2(sin[x], cos[x]) / factor; if (result[x] < 0) { result[x] += limit; } } else { result[x] = double.NaN; } }); break; case AggregationMethod.Min: Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var isHighQuality = (chunkData.Length / (double)kernelSize) >= nanLimit; if (isHighQuality) { result[x] = ArrayStatistics.Minimum(chunkData); } else { result[x] = double.NaN; } }); break; case AggregationMethod.Max: Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var isHighQuality = (chunkData.Length / (double)kernelSize) >= nanLimit; if (isHighQuality) { result[x] = ArrayStatistics.Maximum(chunkData); } else { result[x] = double.NaN; } }); break; case AggregationMethod.Std: Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var isHighQuality = (chunkData.Length / (double)kernelSize) >= nanLimit; if (isHighQuality) { result[x] = ArrayStatistics.StandardDeviation(chunkData); } else { result[x] = double.NaN; } }); break; case AggregationMethod.Rms: Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var isHighQuality = (chunkData.Length / (double)kernelSize) >= nanLimit; if (isHighQuality) { result[x] = ArrayStatistics.RootMeanSquare(chunkData); } else { result[x] = double.NaN; } }); break; case AggregationMethod.SampleAndHold: Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var isHighQuality = (chunkData.Length / (double)kernelSize) >= nanLimit; if (isHighQuality) { result[x] = chunkData.First(); } else { result[x] = double.NaN; } }); break; case AggregationMethod.Sum: Parallel.For(0, targetDatasetLength, x => { var chunkData = this.GetNaNFreeData(data, x, kernelSize); var isHighQuality = (chunkData.Length / (double)kernelSize) >= nanLimit; if (isHighQuality) { result[x] = Vector <double> .Build.Dense(chunkData).Sum(); } else { result[x] = double.NaN; } }); break; default: logger.LogWarning($"The aggregation method '{method}' is not known. Skipping period."); break; } return(result); }
static void Main(string[] args) { //ExamplesMain(); //Initialize class level object variables apiWrapper = new APIWrapper(); configuration = LoadConfiguration(); powerArraysByFrequencyDictionary = new SortedDictionary <double, List <double> >(); //Search for devices. int[] devID = null; string[] devSN = null; string[] devType = null; ReturnStatus returnStatus = apiWrapper.DEVICE_Search(ref devID, ref devSN, ref devType); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("DEVICE_Search ERROR: " + returnStatus); } //Connect to the first device detected. returnStatus = apiWrapper.DEVICE_Connect(devID[0]); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("DEVICE_Connect ERROR: " + returnStatus); } else // print the name of the connected device. { Console.WriteLine("\nCONNECTED TO: " + devType[0]); Console.WriteLine("\nSerial Number:" + devSN[0]); } int measurementsCount = configuration.Measurements.Count; Console.WriteLine("measurementsCount: " + measurementsCount); foreach (Measurement measurement in configuration.Measurements) { AssignMeasurementVariables(measurement); //CONFIG SPECTRUM //TODO: Handle when returnStatus != ReturnStatus.noError returnStatus = apiWrapper.ALIGN_RunAlignment(); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ALIGN_RunAlignment ERROR: " + returnStatus); } returnStatus = apiWrapper.CONFIG_Preset(); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("CONFIG_Preset ERROR: " + returnStatus); } returnStatus = apiWrapper.SPECTRUM_SetEnable(true); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("SPECTRUM_SetEnable ERROR: " + returnStatus); } returnStatus = apiWrapper.CONFIG_SetCenterFreq(centerFreq); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("CONFIG_SetCenterFreq ERROR: " + returnStatus); } returnStatus = apiWrapper.CONFIG_SetReferenceLevel(refLevel); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("CONFIG_SetReferenceLevel ERROR: " + returnStatus); } returnStatus = apiWrapper.CONFIG_SetExternalRefEnable(enableExtRef); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("CONFIG_SetExternalRefEnable ERROR: " + returnStatus); } returnStatus = apiWrapper.SPECTRUM_SetTraceType(spectrumTraceNumber, true, detectorType); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("SPECTRUM_SetTraceType ERROR: " + returnStatus); } Spectrum_Limits salimits = new Spectrum_Limits(); returnStatus = apiWrapper.SPECTRUM_GetLimits(ref salimits); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("SPECTRUM_GetLimits ERROR: " + returnStatus); } //Check the limits if (span > salimits.maxSpan) { span = salimits.maxSpan; } // Set and get RSA parameter values. Spectrum_Settings setSettings = new Spectrum_Settings(); //Assign user settings to settings struct. setSettings.span = span; setSettings.rbw = RBW; setSettings.enableVBW = enableVBW; setSettings.vbw = VBW; setSettings.traceLength = traceLength; setSettings.window = windowType; setSettings.verticalUnit = verticalUnits; Spectrum_Settings getSettings = new Spectrum_Settings(); returnStatus = apiWrapper.SPECTRUM_SetEnable(true); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("SPECTRUM_SetEnable ERROR: " + returnStatus); } //returnStatus = apiWrapper.SPECTRUM_SetDefault(); //if (returnStatus != ReturnStatus.noError) { Console.WriteLine("SPECTRUM_SetDefault ERROR: " + returnStatus); } //Register the settings. returnStatus = apiWrapper.SPECTRUM_SetSettings(setSettings); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("SPECTRUM_SetSettings ERROR: " + returnStatus); } //returnStatus = apiWrapper.SPECTRUM_GetSettings(ref getSettings); //if (returnStatus != ReturnStatus.noError) { Console.WriteLine("SPECTRUM_GetSettings ERROR: " + returnStatus); } Console.WriteLine("\nSet Settings: " + setSettings); //Retrieve the settings info. returnStatus = apiWrapper.SPECTRUM_GetSettings(ref getSettings); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ERROR: " + returnStatus); } Console.WriteLine("\nGet Settings: " + getSettings); double actualRBW = getSettings.actualRBW; Console.WriteLine("actualRBW: " + actualRBW); double actualVBW = getSettings.actualVBW; Console.WriteLine("actualVBW: " + actualVBW); double numIQsamples = getSettings.actualNumIQSamples; Console.WriteLine("numIQsamples: " + numIQsamples); int actualTraceLength = getSettings.traceLength; Console.WriteLine("actualTraceLength: " + actualTraceLength); //Set variables used to calculate freqArray items double actualStartFreq = getSettings.actualStartFreq; Console.WriteLine("actualStartFreq: " + actualStartFreq); double actualStopFreq = getSettings.actualStopFreq; Console.WriteLine("actualStopFreq: " + actualStopFreq); double actualFreqStepSize = getSettings.actualFreqStepSize; Console.WriteLine("actualFreqStepSize: " + actualFreqStepSize); //New freqArray length of traceLength //double[] freqArray = new double[traceLength]; //Console.WriteLine("freqArray.Length:" + freqArray.Length); //Allocate memory array for spectrum output vector. float[] powerArray = null; double[] frequencyArray = new double[traceLength]; //Use for loop to create dictionary items for (int arrayIndex = 0; arrayIndex < traceLength; arrayIndex++) { double frequency = (actualFreqStepSize * arrayIndex) + actualStartFreq; frequencyArray[arrayIndex] = frequency; List <double> powerListForThisFrequency = null; if (powerArraysByFrequencyDictionary.TryGetValue(frequency, out powerListForThisFrequency)) { //TryGetValue() call found pre-existing powerListForThisFrequency in powerArraysByFrequencyDictionary //so no need to add a new item at this frequency } else { //Pre-existing powerListForThisFrequency in powerArraysByFrequencyDictionary not found so create a new one. Then add a new item //to the dictionary with the new item's key being the current frequency value we are at in this iteration of the for loop powerArraysByFrequencyDictionary.Add(frequency, new List <double>()); } } //Start the trace capture. returnStatus = apiWrapper.SPECTRUM_SetEnable(true); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ERROR: " + returnStatus); } Console.WriteLine("Trace capture is starting..."); bool traceReady = false; bool isActive = true; bool eventOccured = false; int waitTimeoutMsec = 1000; //Maximum allowable wait time for each data acquistion. int numTimeouts = 3; //Maximum amount of attempts to acquire data if a timeout occurs. //Note: the total wait time to acquire data is waitTimeoutMsec x numTimeouts. int timeoutCount = 0; //Variable to track the timeouts. int traceCount = 0; int outTracePoints = 0; long eventTimestamp = 0; while (isActive) { returnStatus = apiWrapper.SPECTRUM_AcquireTrace(); //Wait for the trace to be ready. returnStatus = apiWrapper.SPECTRUM_WaitForTraceReady(waitTimeoutMsec, ref traceReady); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ERROR: " + returnStatus); } if (traceReady) { Console.WriteLine("================INPUT MEASUREMENT PARAMETERS================="); Console.WriteLine("Start Frequency = {0} MHz", actualStartFreq * hertz2MegaHertz); Console.WriteLine("Stop Frequency = {0} MHz", actualStopFreq * hertz2MegaHertz); Console.WriteLine("Center Frequency = {0} MHz", centerFreq * hertz2MegaHertz); Console.WriteLine("Span = {0} MHz", span * hertz2MegaHertz); Console.WriteLine("RBW = {0} kHz", actualRBW * hertz2KiloHertz); Console.WriteLine("VBW = {0} MHz", actualVBW); Console.WriteLine("Detection = {0}", detectorType); Console.WriteLine("Trace Length = {0}", traceLength); Console.WriteLine("Number of Traces = {0}", numTraces); Console.WriteLine("Reference Level = {0} dB", refLevel); ////*********************************************Get spectrum trace data. returnStatus = apiWrapper.SPECTRUM_GetTrace(spectrumTraceNumber, traceLength, ref powerArray, ref outTracePoints); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ERROR: " + returnStatus); } Console.WriteLine("outTracePoints: " + outTracePoints); //Get traceInfo struct. Spectrum_TraceInfo traceInfo = new Spectrum_TraceInfo(); returnStatus = apiWrapper.SPECTRUM_GetTraceInfo(ref traceInfo); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ERROR: " + returnStatus); } //You can use this information to report any non-zero bits in AcqDataStatus word, for example. if (traceInfo.acqDataStatus != 0) { Console.WriteLine("Trace:" + traceCount + ", AcqDataStatus:" + traceInfo.acqDataStatus, "Timestamp:" + traceInfo.timestamp); Console.WriteLine(powerArray.Max()); } //ADC Overload EventType overloadDetected = EventType.DEVEVENT_OVERRANGE; returnStatus = apiWrapper.DEVICE_GetEventStatus(overloadDetected, ref eventOccured, ref eventTimestamp); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ERROR: " + returnStatus); } if (eventOccured) { Console.WriteLine("ADC OVERLOAD! Adjust Reference Level?:" + eventTimestamp); float maxPwr = powerArray.Max(); Console.WriteLine("Maximum Power Level:" + maxPwr); } //Trigger Detection EventType triggerDetected = EventType.DEVEVENT_TRIGGER; returnStatus = apiWrapper.DEVICE_GetEventStatus(triggerDetected, ref eventOccured, ref eventTimestamp); if (returnStatus != ReturnStatus.noError) { Console.WriteLine("ERROR: " + returnStatus); } if (eventOccured) { Console.WriteLine("Trigger Detected!"); } //Create a list of power vs frequency values for (int arrayIndex = 0; arrayIndex < powerArray.Length; arrayIndex++) { List <double> powerListForThisFrequency = null; double frequency = frequencyArray[arrayIndex]; float power = powerArray[arrayIndex]; if (powerArraysByFrequencyDictionary.TryGetValue(frequency, out powerListForThisFrequency)) { //TryGetValue() call found pre-existing powerListForThisFrequency in powerArraysByFrequencyDictionary //add the current power value to the powerListForThisFrequency if (powerListForThisFrequency.Count < numTraces) { powerListForThisFrequency.Add(power); } } else { //Pre-existing powerListForThisFrequency in powerArraysByFrequencyDictionary not found so create a new one. //Then add the current power value to the new powerListForThisFrequency, then add a new item to the dictionary //with the new item's key being the current frequency value we are at in this iteration of the for loop powerListForThisFrequency = new List <double>(); powerListForThisFrequency.Add(power); powerArraysByFrequencyDictionary.Add(frequency, powerListForThisFrequency); } } traceCount++; Console.WriteLine("Trace Count:" + traceCount); } else { timeoutCount++; Console.WriteLine("Timeout Count:" + timeoutCount); } Console.WriteLine("'numTraces:" + numTraces + "' == " + "'traceCount:" + traceCount + " " + (numTraces == traceCount)); //Stop acquiring traces when the limit is reached or the wait time is exceeded. if (numTraces == traceCount || timeoutCount == numTimeouts) { isActive = false; Console.WriteLine("Is Active?" + isActive); } } } string outputFilePath = @"..\..\..\Output\" + DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss") + "_" + "STARTFREQ" + "-" + "STARTFREQ" + @"_survey.csv"; string statisticsFilePath = @"..\..\..\Output\" + DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss") + "_" + "STARTFREQ" + "-" + "STARTFREQ" + @"_M4.csv"; using (StreamWriter streamWriter = new StreamWriter(outputFilePath, true)) { using (StreamWriter statsStreamWriter = new StreamWriter(statisticsFilePath, true)) { if (numTraces > 1) { //Write CSV File Headings: Frequency,Minimum,Maximum,Mean,Median statsStreamWriter.WriteLine("Frequency,Minimum,Maximum,Mean,Median"); } int dictionaryIndex = 0; foreach (KeyValuePair <double, List <double> > keyValuePair in powerArraysByFrequencyDictionary) { double frequency = keyValuePair.Key; List <double> powerList = keyValuePair.Value; if (powerList.Count > 0) { if (numTraces > 1 && powerList.Count > 1) { double[] powerArrayForStatistics = powerList.ToArray(); double maximumPower = ArrayStatistics.Maximum(powerArrayForStatistics); double minimumPower = ArrayStatistics.Minimum(powerArrayForStatistics); double meanPower = (float)ArrayStatistics.Mean(powerArrayForStatistics); double medianPower = ArrayStatistics.MedianInplace(powerArrayForStatistics); //Write CSV values for this frequency statsStreamWriter.WriteLine("{0},{1},{2},{3},{4}", frequency, minimumPower, maximumPower, meanPower, medianPower); } else //write frequency and power value for single trace when powerList.Count == 1 { streamWriter.WriteLine("{0},{1}", frequency, powerList[0]); // only one power value in List at index 0 } } if (dictionaryIndex == 800) { } dictionaryIndex++; } } } //Disconnect the device and finish up. returnStatus = apiWrapper.SPECTRUM_SetEnable(false); returnStatus = apiWrapper.DEVICE_Stop(); returnStatus = apiWrapper.DEVICE_Disconnect(); Console.WriteLine("\nSpectrum trace acquisition routine complete."); Console.WriteLine("\nPress enter key to exit..."); Console.ReadKey(); }