private void CalculateResolutionNoiseDensity()

in sensors/Tools/SensorExplorer/Scenario0_Tests.xaml.cs [1236:1326]


        private void CalculateResolutionNoiseDensity()
        {
            string str = string.Empty;
            List<double>[] axisList = new List<double>[3];
            List<double>[] histogramList = new List<double>[3];
            List<double>[] resolutionList = new List<double>[3];
            List<int>[] countList = new List<int>[3];
            int type = SensorType[pivotSensor.SelectedIndex];
            double[] axisResolution = new double[3];
            double[] axisStdDev = new double[3];
            double finalResolution = -1;
            double finalNoiseDensity = -1;
            const double histogramBin = 1e-4;
            const double gTomg = 1e3;
            const double gToug = 1e6;
            const double noiseCoefficient = 1.6;

            if (type == Sensor.ACCELEROMETER)
            {
                if (dataList.Count > 0 && testLength[testType] > 0)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        axisList[i] = new List<double>();
                        for (int j = 0; j < dataList.Count; j++)
                        {
                            axisList[i].Add(dataList[j][i]);
                        }
                        axisList[i].Sort();
                    
                        int histogramCount = 1;
                        histogramList[i] = new List<double>();
                        countList[i] = new List<int>();
                        for (int j = 1; j < axisList[i].Count; j++)
                        {
                            if (Math.Abs(axisList[i][j] - axisList[i][j-1]) < histogramBin)
                            {
                                histogramCount++;
                            }
                            else
                            {
                                histogramList[i].Add(axisList[i][j - 1]);
                                countList[i].Add(histogramCount);
                                histogramCount = 1;
                            }
                        }
                    
                        int maxHistogramIndex = -1;
                        maxHistogramIndex = GetIndexOfMaxValue(countList[i]);
                        if (maxHistogramIndex >= 0)
                        {
                            if ((maxHistogramIndex == 0) || (countList[i][maxHistogramIndex + 1] >= countList[i][maxHistogramIndex - 1]))
                            {
                                axisResolution[i] = Math.Abs(histogramList[i][maxHistogramIndex + 1] - histogramList[i][maxHistogramIndex]);
                            }
                            else
                            {
                                axisResolution[i] = Math.Abs(histogramList[i][maxHistogramIndex] - histogramList[i][maxHistogramIndex - 1]);
                            }
                        }
                        else
                        {
                            axisResolution[i] = -1;
                        }
                    
                        axisStdDev[i] = GetStdDev(axisList[i]);
                    }
                    
                    finalResolution = Math.Max(Math.Max(axisResolution[0], axisResolution[1]), axisResolution[2]) * gTomg;
                    finalNoiseDensity = Math.Max(Math.Max(axisStdDev[0], axisStdDev[1]), axisStdDev[2]);
                    int frequency = dataList.Count / testLength[testType];
                    if (frequency > 0)
                    {
                        finalNoiseDensity = finalNoiseDensity * gToug / Math.Sqrt(noiseCoefficient * frequency);
                    }
                    else
                    {
                        finalNoiseDensity = -1;
                    }                
                }

                str = Constants.SensorName[type] + " " + testType + " Test Result: \n" +
                      "--> Resolution: " + finalResolution + " mg/LSB \n" +
                      "--> NoiseDensity: " + finalNoiseDensity + " ug/(hz)^0.5\n";

                rootPage.LoggingChannelView.LogMessage(str);
                hyperlink.NavigateUri = new Uri("https://aka.ms/sensorexplorerblog");
                run.Text = "https://aka.ms/sensorexplorerblog";
                instruction.Text = str + "For more information, please visit:";
            }
        }