private RawSpectrum ExponentialFitting(RawSpectrum spectrum) { int n = spectrum.mAbsoluteIntensities.Count; var x = spectrum.mWavelengths; var y = spectrum.mAbsoluteIntensities; double xsum = 0, x2sum = 0, ysum = 0, xysum = 0; for (int i = 0; i < n; ++i) { if (y[i] > 0) { double lny = Math.Log(y[i]); xsum += x[i]; ysum += lny; x2sum += x[i] * x[i]; xysum += x[i] * lny; } } double a = (n * xysum - xsum * ysum) / (n * x2sum - xsum * xsum); double b = (x2sum * ysum - xsum * xysum) / (x2sum * n - xsum * xsum); double c = Math.Exp(b); var fittingCurve = new RawSpectrum(); for (int i = 0; i < n; ++i) { fittingCurve.mWavelengths.Add(x[i]); fittingCurve.mAbsoluteIntensities.Add(c * Math.Exp(a * x[i])); } return(fittingCurve); }
void PlotFourierTransform(RawSpectrum spec, int n, string name) { var series = chrFourierTransform.Series.Add(name); series.ChartType = SeriesChartType.FastLine; for (int i = 0; i < spec.mAbsoluteIntensities.Count; ++i) { series.Points.AddXY(Math.Round(spec.mWavelengths[i], 2), spec.mAbsoluteIntensities[i]); } }
void PlotSpline(RawSpectrum spec, int n) { var name = "Spline " + n; var series = chrMainChart.Series.Add(name); series.ChartType = SeriesChartType.FastLine; for (int i = 0; i < spec.mAbsoluteIntensities.Count; ++i) { series.Points.AddXY(Math.Round(spec.mWavelengths[i], 2), spec.mAbsoluteIntensities[i]); } }
private void btnCalculate_Click(object sender, EventArgs e) { double ka = (double)nudFWHM.Value; // remove all computed curves mDeconvolutedSpectra.Clear(); mFourierTransforms.Clear(); mModifiedFourierTransforms.Clear(); mFittingCurves.Clear(); foreach (var experimentalSpectrum in mSpectra) { // define cutoff position using narrowing factor int cutoffPosition = (int)(experimentalSpectrum.mAbsoluteIntensities.Value.Length / (double)nudNarrowingFactor.Value); // absolute intensities alias var spectrum = experimentalSpectrum.mAbsoluteIntensities.Value; // copy spectrum absolute intensities into array of complex numbers Complex[] cdata = new Complex[spectrum.Length]; for (int i = 0; i < spectrum.Length; ++i) { cdata[i] = spectrum[i]; } // do inverse fourier transform to go to time domain FourierTransform.DFT(cdata, FourierTransform.Direction.Backward); // add inverse fourier transform plot to the fourier chart var fourierTransform = new RawSpectrum(); for (int i = 0; i < spectrum.Length; ++i) { fourierTransform.mAbsoluteIntensities.Add(cdata[i].Real); fourierTransform.mWavelengths.Add(i); } mFourierTransforms.Add(fourierTransform); // deconvolve exponential decay for (int i = 0; i < spectrum.Length; ++i) { cdata[i] /= ExponentialDeconvolution(cutoffPosition * ka, i); } // apply apodization function double constantScaler = 2.0f; for (int i = 0; i < spectrum.Length; ++i) { double a = Apodization(cutoffPosition, i); cdata[i] *= a * a * constantScaler; } // add modified fourier transform plot to the fourier chart var modifiedFourierTransform = new RawSpectrum(); for (int i = 0; i < spectrum.Length; ++i) { modifiedFourierTransform.mAbsoluteIntensities.Add(cdata[i].Real); modifiedFourierTransform.mWavelengths.Add(i); } mModifiedFourierTransforms.Add(modifiedFourierTransform); // do forward fourier transform to go back to frequency domain FourierTransform.DFT(cdata, FourierTransform.Direction.Forward); // add deconvolved spectrum to the chart var deconvolution = new RawSpectrum(); for (int i = 0; i < spectrum.Length; ++i) { deconvolution.mAbsoluteIntensities.Add(cdata[i].Real); deconvolution.mWavelengths.Add(experimentalSpectrum.mWaveLengths.Value[i]); } mDeconvolutedSpectra.Add(deconvolution); } UpdateChart(); }