static void run(string[] args) { /* * var t0 = 103.6374; * using (var writer = new StreamWriter(@"C:\Users\mhorowitzgelb\Desktop\basicAsymetricGaussian.csv")) * { * for (double x = -3; x <= 3.1; x += 0.01) * { * var _x = x + t0; * var y = function.GetY(_x, new[] {56564, t0, 0.00001, 1}); * writer.WriteLine(_x+","+y); * } * }*/ var unalteredData = loadCsv(@"C:\Users\mhorowitzgelb\Desktop\multiplePeaks.csv", 6); unalteredData = new[] { unalteredData[0], unalteredData[1] }; var function = new SummedAlteredGaussian(3, 1, 10, unalteredData); //var function = new MultiTransitionAlteredGaussian(5,10,unalteredData); //{14000,5,5,5,5,5,43.9,0.01,0.15,1000,1300,2100,1900,900,500,44.9,0.01,0.15,100,5,4000,5,5,5,46,0.01,0.15} var lma = new LMA(function, new [] { 14000, 43.9, 0.01, 0.15, 1000, 44.9, 0.01, 0.15, 100, 46, 0.01, 0.15 }, function.AlteredData, null, new DotNetMatrix.GeneralMatrix(12, 12), 1, 1000); lma.Fit(); writeCSV(lma, function, unalteredData, @"C:\Users\mhorowitzgelb\Desktop\multiPeaksFit.csv", 1); }
public void RunSin() { //produce test data - a is a vecor with exact solution double[] x = { 0.0, 0.1, 0.2, 0.3, 0.5, 0.7 }; double[] a = { 2.2, 0.4 }; LMAFunction f = new SinFunction(); double[][] dataPoints = f.GenerateData(a, x); LMA algorithm = new LMA(f, new double[] { 0.1, 10 }, dataPoints, null, new GeneralMatrix(2, 2), 1d - 20, 100); algorithm.Fit(); for (int i = 0; i < a.Length; i++) { Assert.IsTrue(System.Math.Abs(algorithm.Parameters[i] - a[i]) < 0.0001); Trace.WriteLine("Parameter" + i.ToString() + " " + algorithm.Parameters[i].ToString()); } Trace.WriteLine("# of iterations =" + algorithm.Iterations.ToString()); }
public void RunLine() { double[] x = { 0, 2, 6, 8, 9 }; double[] a = { 2, 0.51 }; LMAFunction f = new LineFunction(); double[][] dataPoints = f.GenerateData(a, x); LMA algorithm = new LMA(f, new double[] { 100, -100 }, dataPoints, null, new GeneralMatrix(2, 2), 1d - 20, 100); algorithm.Fit(); for (int i = 0; i < a.Length; i++) { Assert.IsTrue(System.Math.Abs(algorithm.Parameters[i] - a[i]) < 0.0001); Trace.WriteLine("Parameter" + i.ToString() + " " + algorithm.Parameters[i].ToString()); } Trace.WriteLine("# of iterations =" + algorithm.Iterations.ToString()); }
public static void writeCSV(LMA lma, SummedAlteredGaussian function, double[][] originalData, string path, int transitions) { using (var writer = new StreamWriter(path)) { var xList = originalData[0]; for (int i = 0; i < xList.Length; i++) { var x = xList[i]; writer.Write("" + x); for (int j = 0; j < transitions; j++) { var y = originalData[1 + j][i]; writer.Write("," + y); } for (int j = 0; j < transitions; j++) { var fitValue = function.GetY(x, j, lma.Parameters); writer.Write("," + fitValue); } writer.WriteLine(); } } }
// LORENTZIAN FITTING // /**************************************************************************** * GetLorentzParams * Get lorentzian fitting parameters ****************************************************************************/ public void GetLorentzParams(FFTData fftData, LorentzSettings lorentzSettings, ref LorentzParams lorentzParams) { double[][] dataPoints = new double[][] { fftData.freq, fftData.fft }; LMA algorithm; if (lorentzSettings.isYShiftLorentz) { LMAFunction lorentzShift = new LorenzianFunctionShift(); double[] startPoint = new double[4]; try { startPoint[0] = lorentzSettings.startPointLP.A; } catch (Exception) { startPoint[0] = 10; } try { startPoint[1] = lorentzSettings.startPointLP.f0; } catch (Exception) { startPoint[1] = 25; } try { startPoint[2] = lorentzSettings.startPointLP.gamma; } catch (Exception) { startPoint[2] = 1; } try { startPoint[3] = lorentzSettings.startPointLP.up; } catch (Exception) { startPoint[3] = 1; } algorithm = new LMA(lorentzShift, startPoint, dataPoints, null, new GeneralMatrix(4, 4), 1d - 20, lorentzSettings.nIter); algorithm.Fit(); lorentzParams.A = algorithm.Parameters[0]; lorentzParams.gamma = algorithm.Parameters[2]; lorentzParams.f0 = algorithm.Parameters[1]; lorentzParams.up = algorithm.Parameters[3]; } else { LMAFunction lorentz = new LorenzianFunction(); double[] startPoint = new double[3]; try { startPoint[0] = lorentzSettings.startPointLP.A; } catch (Exception) { startPoint[0] = 10; } try { startPoint[1] = lorentzSettings.startPointLP.f0; } catch (Exception) { startPoint[1] = 25; } try { startPoint[2] = lorentzSettings.startPointLP.gamma; } catch (Exception) { startPoint[2] = 1; } algorithm = new LMA(lorentz, startPoint, dataPoints, null, new GeneralMatrix(3, 3), 1d - 20, lorentzSettings.nIter); algorithm.Fit(); lorentzParams.A = algorithm.Parameters[0]; lorentzParams.gamma = algorithm.Parameters[2]; lorentzParams.f0 = algorithm.Parameters[1]; lorentzParams.up = 0; } }