/**************************************************************************** * GetfittingMetrics * calculate the fitting parameters that the user is interested in ****************************************************************************/ public FittingMetrics GetFittingMetrics(LorentzParams lp, FFTSettings fftSettings) { FittingMetrics fm = new FittingMetrics { amplitude = lp.A / lp.gamma / lp.gamma + lp.up, f0 = lp.f0 }; double ampPrime = 0; switch (fftSettings.Fftstyle) { case 0: //Log (dB) ampPrime = fm.amplitude - 3; break; case 1: // Log - Complex Conjugate (dB) ampPrime = fm.amplitude - 3; break; case 2: // Magnitude (V) ampPrime = fm.amplitude * 0.5; break; case 3: // Magnitude - Complex Conjugate (V) ampPrime = fm.amplitude * 0.5; break; } try { fm.f1 = fm.f0 + Math.Sqrt((lp.A + lp.gamma * lp.gamma * (lp.up - ampPrime)) / (ampPrime - lp.up)); fm.f2 = fm.f0 - Math.Sqrt((lp.A + lp.gamma * lp.gamma * (lp.up - ampPrime)) / (ampPrime - lp.up)); } catch (Exception) { fm.f1 = 0; fm.f2 = 0; } if (fm.f1 - fm.f2 > 0) { fm.Q = fm.f0 / (fm.f1 - fm.f2); } else { fm.Q = 0; } return(fm); }
// 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; } }