Mantid
Loading...
Searching...
No Matches
GausOsc.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12#include <cmath>
13
15
16using namespace CurveFitting;
17
18using namespace Kernel;
19
20using namespace API;
21
22DECLARE_FUNCTION(GausOsc)
23
24void GausOsc::init() {
25 declareParameter("A", 0.20, "Amplitude at time 0");
26 declareParameter("Sigma", 0.2, "Decay rate");
27 declareParameter("Frequency", 0.1, "Frequency of oscillation");
28 declareParameter("Phi", 0.0, "Frequency of oscillation");
29}
30
31void GausOsc::function1D(double *out, const double *xValues, const size_t nData) const {
32 const double A = getParameter("A");
33 const double G = getParameter("Sigma");
34 const double gf = getParameter("Frequency");
35 const double gphi = getParameter("Phi");
36
37 for (size_t i = 0; i < nData; i++) {
38 double x = xValues[i];
39 out[i] = A * exp(-G * G * x * x) * cos(2 * M_PI * gf * x + gphi);
40 }
41}
42
43void GausOsc::functionDeriv1D(Jacobian *out, const double *xValues, const size_t nData) {
44 const double A = getParameter("A");
45 const double G = getParameter("Sigma");
46 const double gf = getParameter("Frequency");
47 const double gphi = getParameter("Phi");
48
49 for (size_t i = 0; i < nData; i++) {
50 double x = xValues[i];
51 double g = exp(-G * G * x * x);
52 double c = cos(2 * M_PI * gf * x + gphi);
53 double s = sin(2 * M_PI * gf * x + gphi);
54 out->set(i, 0, g * c);
55 out->set(i, 1, -2 * G * x * x * A * g * c);
56 out->set(i, 2, -A * g * 2 * M_PI * x * s);
57 out->set(i, 3, -A * g * s);
58 }
59}
60
61void GausOsc::setActiveParameter(size_t i, double value) {
62 size_t j = i;
63
64 if (parameterName(j) == "Sigma")
65 setParameter(j, fabs(value), false); // Make sigma positive
66 else if (parameterName(j) == "Phi") {
67 // Put angle in range of (-180 to 180] degrees
68 double a = fmod(value, 2 * M_PI);
69 if (a <= -M_PI)
70 a += 2 * M_PI;
71 if (a > M_PI)
72 a -= 2 * M_PI;
73 setParameter(j, a, false);
74 } else
75 setParameter(j, value, false);
76}
77
78} // namespace Mantid::CurveFitting::Functions
double value
The value of the point.
Definition: FitMW.cpp:51
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
#define fabs(x)
Definition: Matrix.cpp:22
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
void setParameter(size_t, const double &value, bool explicitlySet=true) override
Set i-th parameter.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
double getParameter(size_t i) const override
Get i-th parameter.
Provide gaussian decay function: A*exp(-(sigma.x)^2))
Definition: GausOsc.h:26
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
Definition: GausOsc.cpp:43
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
Definition: GausOsc.cpp:31
void setActiveParameter(size_t i, double value) override
Set new value of i-th active parameter.
Definition: GausOsc.cpp:61