/// <summary>
        ///     Gets the charge state (determined by AutoCorrelation algorithm) for a peak in some data.
        /// </summary>
        /// <param name="peak">is the peak whose charge we want to detect.</param>
        /// <param name="peakData">is the PeakData object containing raw data, peaks, etc which are used in the process.</param>
        /// <param name="debug"></param>
        /// <returns>Returns the charge of the feature.</returns>
        public static int GetChargeState(ThrashV1Peak peak, PeakData peakData, bool debug)
        {
            var minus      = 0.1;
            var plus       = 1.1; // right direction to look
            var startIndex = PeakIndex.GetNearest(peakData.MzList, peak.Mz - peak.FWHM - minus, peak.DataIndex);
            var stopIndex  = PeakIndex.GetNearest(peakData.MzList, peak.Mz + peak.FWHM + plus, peak.DataIndex);
            var numPts     = stopIndex - startIndex;
            var numL       = numPts;

            if (numPts < 5)
            {
                return(-1);
            }

            if (numPts < 256)
            {
                numL = 10 * numPts;
            }

            // TODO: PattersonChargeStateCalculator does a lot of funny stuff around here.
            // variable to help us perform spline interpolation.
            // count is stopIndex - startIndex + 1 because we need to include the values at stopIndex as well
            // NOTE: This output is different from what the DeconEngineV2 code outputs; there are notes in that code
            //    wondering if there was a bug in the previous implementation imported from VB, of starting at startIndex + 1.
            //    That code performed interpolation on the range from startIndex + 1 to stopIndex, inclusive, and minMz set to PeakData.MzList[startIndex + 1].
            //    This code can produce output that more closely matches the DeconEngineV2 output by using
            //    "startIndex, stopIndex - startIndex + 2" as the parameters to "GetRange()", and setting minMz to PeakData.MzList[startIndex + 1].
            //    Since using startIndex and stopIndex directly produces output that mostly differs on fit score by a small amount,
            //    we are changing this code to use them.
            var interpolator = CubicSpline.InterpolateNaturalSorted(
                peakData.MzList.GetRange(startIndex, stopIndex - startIndex + 1).ToArray(),
                peakData.IntensityList.GetRange(startIndex, stopIndex - startIndex + 1).ToArray());

            var minMz = peakData.MzList[startIndex];
            var maxMz = peakData.MzList[stopIndex];

            // List to store the interpolated intensities of the region on which we performed the cubic spline interpolation.
            var iv = new List <double>(numL);

            for (var i = 0; i < numL; i++)
            {
                var xVal = minMz + (maxMz - minMz) * i / numL;
                var fVal = interpolator.Interpolate(xVal);
                iv.Add(fVal);
            }

            if (debug)
            {
                Console.Error.WriteLine("mz,intensity");
                for (var i = 0; i < numL; i++)
                {
                    var xVal = minMz + (maxMz - minMz) * i / numL;
                    Console.Error.WriteLine(xVal + "," + iv[i]);
                }
            }

            // List to store the auto correlation values at the points in the region.
            var autoCorrelationScores = ACss(iv.ToArray());

            if (debug)
            {
                Console.Error.WriteLine("AutoCorrelation values");
                for (var i = 0; i < autoCorrelationScores.Count; i++)
                {
                    var score = autoCorrelationScores[i];
                    Console.Error.WriteLine((maxMz - minMz) * i / numL + "," + score);
                }
            }

            var minN = 0;

            while (minN < numL - 1 && autoCorrelationScores[minN] > autoCorrelationScores[minN + 1])
            {
                minN++;
            }
            var success = HighestChargeStatePeak(minMz, maxMz, minN, autoCorrelationScores, MaxCharge, out var bestAcScore, out _);

            if (!success)
            {
                return(-1); // Didn't find anything
            }
            // List to temporarily store charge list. These charges are calculated at peak values of auto correlation.
            // Now go back through the CS peaks and make a list of all CS that are at least 10% of the highest
            var charges = GenerateChargeStates(minMz, maxMz, minN, autoCorrelationScores, MaxCharge, bestAcScore);

            // Get the final CS value to be returned
            var returnChargeStateVal = -1;
            // TODO: PattersonChargeStateCalculator really doesn't match the following code.
            var fwhm = peak.FWHM; // Store a copy of the FWHM to avoid modifying the actual value

            if (fwhm > 0.1)
            {
                fwhm = 0.1;
            }

            for (var i = 0; i < charges.Count; i++)
            {
                // no point retesting previous charge.
                var tempChargeState = charges[i];
                var skip            = false;
                for (var j = 0; j < i; j++)
                {
                    if (charges[j] == tempChargeState)
                    {
                        skip = true;
                        break;
                    }
                }
                if (skip)
                {
                    continue;
                }
                if (tempChargeState > 0)
                {
                    var peakA = peak.Mz + 1.0 / tempChargeState;
                    var found = peakData.GetPeakFromAllOriginalIntensity(peakA - fwhm, peakA + fwhm, out var isoPeak);
                    if (found)
                    {
                        returnChargeStateVal = tempChargeState;
                        if (isoPeak.Mz * tempChargeState < 3000)
                        {
                            break;
                        }
                        // if the mass is greater than 3000, lets make sure that multiple isotopes exist.
                        peakA = peak.Mz - 1.03 / tempChargeState;
                        found = peakData.GetPeakFromAllOriginalIntensity(peakA - fwhm, peakA + fwhm, out isoPeak);
                        if (found)
                        {
                            return(tempChargeState);
                        }
                    }
                    else
                    {
                        peakA = peak.Mz - 1.0 / tempChargeState;
                        found = peakData.GetPeakFromAllOriginalIntensity(peakA - fwhm, peakA + fwhm, out isoPeak);
                        if (found && isoPeak.Mz * tempChargeState < 3000)
                        {
                            return(tempChargeState);
                        }
                    }
                }
            }
            return(returnChargeStateVal);
        }