rf_fit#

Fit data to sine/cosine, circle, ellipse or Gaussian functions.

Functions

fit_Gaussian(X, Y)

Fit a Guassian distribution function.

fit_circle(X, Y)

Fit a circle.

fit_ellipse(X, Y)

Fit an ellipse.

fit_sincos(X_rad, Y[, target])

Fit the sine or cosine function

fit_Gaussian(X, Y)[source]#

Fit a Guassian distribution function.

See the webpage: https://pythonguides.com/scipy-normal-distribution/.

Parameters:
  • X – numpy array, x-coordinate of the points

  • Y – numpy array, y-coordinate of the points

Returns:
  • status – boolean, success (True) or fail (False)

  • a – float, magnitude scale of the un-normalized distribution

  • mu – float, mean value

  • sigma – float, standard deviation

fit_circle(X, Y)[source]#

Fit a circle. Given x, y data points and find x0, y0 and r following (x - x0)^2 + (y - y0)^2 = r^2.

Parameters:
  • X – numpy array, x-coordinate of the points

  • Y – numpy array, y-coordinate of the points

Returns:
  • status – boolean, success (True) or fail (False)

  • x0, y0 – float, center coordinate of the circle

  • r – float, radius of the circle

fit_ellipse(X, Y)[source]#

Fit an ellipse. Get the general ellipse function and its characteristics, including semi-major axis a, semi-minor axis b, center coordinates (x0, y0) and rotation angle sita (the angle from the positive horizontal axis to the ellipse’s major axis). The general ellipse equation is A*X^2 + B*X*Y + C*Y^2 + D*X + E*Y + F = 0. When making the fitting, we divide the equation by C to normalize the coefficient of Y^2 to 1 and move it to the right side of the fitting equation. The ellipse is derived with the following steps:

  1. define a canonical ellipse:
    • X1 = a * cos(phi)

    • Y1 = b * sin(phi)

    where phi is a phase vector covering from 0 to 2*pi

  2. rotate the canonical ellipse:
    • X2 = X1 * cos(sita) - Y1 * sin(sita)

    • Y2 = X1 * sin(sita) + Y1 * cos(sita)

  3. add offset to the rotated ellipse:
    • X = X2 + x0

    • Y = Y2 + y0

See the webpage: https://en.wikipedia.org/wiki/Ellipse.

Parameters:
  • X – numpy array, x-coordinate of the points

  • Y – numpy array, y-coordinate of the points

Returns:
  • status – boolean, success (True) or fail (False)

  • Coef – list, coefficiets derived from the least-square fitting

  • a – float, semi-major

  • b – float, semi-minor

  • x0, y0 – float, center of the ellipse

  • sita – float, angle of the ellipse (see above), rad

fit_sincos(X_rad, Y, target='cos')[source]#
Fit the sine or cosine function
  • y = A * cos(x + phi) + c = (A*cos(phi)) * cos(x) - (A*sin(phi)) * sin(x) + c

  • y = A * sin(x + phi) + c = (A*cos(phi)) * sin(x) + (A*sin(phi)) * cos(x) + c

Parameters:
  • X_rad – numpy array, phase array in radian

  • Y – numpy array, value of the sine or cosine function

  • target – ‘sin’ or ‘cos’, determine which function to fit to

Returns:
  • status – boolean, success (True) or fail (False)

  • A – float, amplitude of the function

  • phi_rad – float, phase of the function, rad

  • c – float, offset of the function