Overview of Curve Fitting Models and Methods in LabVIEW - National Instruments
Overview of Curve Fitting Models and Methods in LabVIEW - National Instruments
com/white-paper/6954/en/
Overview
As the usage of digital measurement instruments during the test and measurement process increases, acquiring large
quantities of data becomes easier. However, the methods of processing and extracting useful information from the acquired
data become a challenge.
During the test and measurement process, you often see a mathematical relationship between observed values and
independent variables, such as the relationship between temperature measurements, an observable value, and measurement
error, an independent variable that results from an inaccurate measuring device. One way to find the mathematical
relationship is curve fitting, which defines an appropriate curve to fit the observed values and uses a curve function to
analyze the relationship between the variables.
You can use curve fitting to perform the following tasks:
Reduce noise and smooth data
Find the mathematical relationship or function among variables and use that function to perform further data processing,
such as error compensation, velocity and acceleration calculation, and so on
Estimate the variable value between data samples
Estimate the variable value outside the data sample range
This document describes the different curve fitting models, methods, and the LabVIEW VIs you can use to perform curve
fitting.
Table of Contents
1. Curve Fitting in LabVIEW
2. Application Examples
3. Summary
1 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
The Bisquare method finds f(x) by using an iterative process, as shown in the following flowchart, and calculates the residual
by using the same formula as in the LS method. The Bisquare method calculates the data starting from iteration k.
2 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
From the formula, you can see that the LAR method is an LS method with changing weights. If the data sample is far from
f(x), the weight is set relatively lower after each iteration so that this data sample has less negative influence on the fitting
result. Therefore, the LAR method is suitable for data with outliers.
Bisquare Method
Like the LAR method, the Bisquare method also uses iteration to modify the weights of data samples. In most cases, the
Bisquare method is less sensitive to outliers than the LAR method.
Comparing the Curve Fitting Methods
If you compare the three curve fitting methods, the LAR and Bisquare methods decrease the influence of outliers by adjusting
the weight of each data sample using an iterative process. Unfortunately, adjusting the weight of each data sample also
decreases the efficiency of the LAR and Bisquare methods.
To better compare the three methods, examine the following experiment. Use the three methods to fit the same data set: a
linear model containing 50 data samples with noise. The following table shows the computation times for each method:
Table 1. Processing Times for Three Fitting Methods
Time(μs) 3.5 30 60
As you can see from the previous table, the LS method has the highest efficiency.
3 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
The following figure shows the influence of outliers on the three methods:
When you use the General Polynomial Fit VI, you first need to set the Polynomial Order input. A high Polynomial Order
does not guarantee a better fitting result and can cause oscillation. A tenth order polynomial or lower can satisfy most
applications. The Polynomial Order default is 2.
This VI has a Coefficient Constraint input. You can set this input if you know the exact values of the polynomial coefficients.
By setting this input, the VI calculates a result closer to the true value.
General Linear Fit
The General Linear Fit VI fits the data set according to the following equation:
y = a0 + a1f1(x) + a2f2(x) + …+ak-1fk-1(x)
where y is a linear combination of the coefficients a0, a1, a2, …, ak-1 and k is the number of coefficients.
The following equations show you how to extend the concept of a linear combination of coefficients so that the multiplier for
a1 is some function of x.
y = a0 + a1sin(ωx)
y = a0 + a1x2
4 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
y = a0 + a1cos(ωx2)
where ω is the angular frequency.
In each of the previous equations, y is a linear combination of the coefficients a0 and a1. For the General Linear Fit VI, y also
can be a linear combination of several coefficients. Each coefficient has a multiplier of some function of x. Therefore, you can
use the General Linear Fit VI to calculate and represent the coefficients of the functional models as linear combinations of the
coefficients.
y = a0 + a1sin(ωx)
y = a0 + a1x2 + a2cos(ωx2)
y = a0 + a1(3sin(ωx)) + a2x3 + (a3/x) + …
In each of the previous equations, y can be both a linear function of the coefficients a0, a1, a2,…, and a nonlinear function of
x.
Building the Observation Matrix
When you use the General Linear Fit VI, you must build the observation matrix H. For example, the following equation
defines a model using data from a transducer.
y = a0 + a1sin(ωx) + a2cos(ωx) + a3x2
The following table shows the multipliers for the coefficients, aj, in the previous equation.
Coefficient Multiplier
ao 1
a1 sin(ωx)
a2 cos(ωx)
a3 x2
To build the observation matrix H, each column value in H equals the independent function, or multiplier, evaluated at each x
value, xi. The following equation defines the observation matrix H for a data set containing 100 x values using the previous
equation.
If the data set contains n data points and k coefficients for the coefficient a0, a1, …, ak– 1, then H is an n × k observation
matrix. Therefore, the number of rows in H equals the number of data points, n. The number of columns in H equals the
number of coefficients, k.
To obtain the coefficients, a0, a1, …, ak – 1, the General Linear Fit VI solves the following linear equation:
Ha=y
where a = [a0 a1 … ak – 1]T and y = [y0 y1 … yn – 1]T.
Cubic Spline Fit
A spline is a piecewise polynomial function for interpolating and smoothing. In curve fitting, splines approximate complex
shapes.
The Cubic Spline Fit VI fits the data set (x,i yi) by minimizing the following function:
5 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
6 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
Some data sets demand a higher degree of preprocessing. A median filter preprocessing tool is useful for both removing the
outliers and smoothing out data.
Postprocessing
LabVIEW offers VIs to evaluate the data results after performing curve fitting. These VIs can determine the accuracy of the
curve fitting results and calculate the confidence and prediction intervals in a series of measurements.
Goodness of Fit
The Goodness of Fit VI evaluates the fitting result and calculates the sum of squares error (SSE), R-square error (R2), and
root mean squared error (RMSE) based on the fitting result. These three statistical parameters describe how well the fitted
model matches the original data set. The following equations describe the SSE and RMSE, respectively.
where SST is the total sum of squares according to the following equation:
R-square is a quantitative representation of the fitting level. A high R-square means a better fit between the fitting model and
the data set. Because R-square is a fractional representation of the SSE and SST, the value must be between 0 and 1.
0 ≤ R-square ≤ 1
When the data samples exactly fit on the fitted curve, SSE equals 0 and R-square equals 1. When some of the data samples
are outside of the fitted curve, SSE is greater than 0 and R-square is less than 1. Because R-square is normalized, the
closer the R-square is to 1, the higher the fitting level and the less smooth the curve.
The following figure shows the fitted curves of a data set with different R-square results.
You can use the function form x = (ATA)-1ATb of the LS method to fit the data according to the following equation.
7 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
y = [y0 y1 … yn-1]T
You can rewrite the covariance matrix of parameters, a0 and a1, as the following equation.
where is the Student’s t inverse cumulative distribution function of n–m degrees of freedom at probability
and is the standard deviation of the parameter ai and equals .
You also can estimate the confidence interval of each data sample at a certain confidence level . For example, a 95%
confidence interval of a sample means that the true value of the sample has a 95% probability of falling within the confidence
interval. The confidence interval of the ith data sample is:
where diagi(A) denotes the ith diagonal element of matrix A. In the above formula, the matrix (JCJ)T represents matrix A.
The prediction interval estimates the uncertainty of the data samples in the subsequent measurement experiment at a certain
confidence level . For example, a 95% prediction interval means that the data sample has a 95% probability of falling within
the prediction interval in the next measurement experiment. Because the prediction interval reflects not only the uncertainty of
the true value, but also the uncertainty of the next measurement, the prediction interval is wider than the confidence interval.
The prediction interval of the ith sample is:
LabVIEW provides VIs to calculate the confidence interval and prediction interval of the common curve fitting models, such
as the linear fit, exponential fit, Gaussian peak fit, logarithm fit, and power fit models. These VIs calculate the upper and lower
bounds of the confidence interval or prediction interval according to the confidence level you set.
The following figure shows examples of the Confidence Interval graph and the Prediction Interval graph, respectively, for
the same data set.
8 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
Ambient Temper Measured Temp Ambient Temper Measured Temp Ambient Temper Measured Temp
ature erature ature erature ature erature
Figure 10. Block Diagram of an Error Function VI Using the General Polynomial Fit VI
The following front panel displays the results of the experiment using the VI in Figure 10.
9 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
Figure 11. Using the General Polynomial Fit VI to Fit the Error Curve
The previous figure shows the original measurement error data set, the fitted curve to the data set, and the compensated
measurement error. After first defining the fitted curve to the data set, the VI uses the fitted curve of the measurement error
data to compensate the original measurement error.
You can see from the graph of the compensated error that using curve fitting improves the results of the measurement
instrument by decreasing the measurement error to about one tenth of the original error value.
Removing Baseline Wandering
During signal acquisition, a signal sometimes mixes with low frequency noise, which results in baseline wandering. Baseline
wandering influences signal quality, therefore affecting subsequent processes. To remove baseline wandering, you can use
curve fitting to obtain and extract the signal trend from the original signal.
As shown in the following figures, you can find baseline wandering in an ECG signal that measures human respiration. You
can obtain the signal trend using the General Polynomial Fit VI and then detrend the signal by finding and removing the
baseline wandering from the original signal. The remaining signal is the subtracted signal.
Figure 12. Using the General Polynomial Fit VI to Remove Baseline Wandering
You can see from the previous graphs that using the General Polynomial Fit VI suppresses baseline wandering. In this
example, using the curve fitting method to remove baseline wandering is faster and simpler than using other methods such as
wavelet analysis.
Edge Extraction
In digital image processing, you often need to determine the shape of an object and then detect and extract the edge of the
shape. This process is called edge extraction. Inferior conditions, such as poor lighting and overexposure, can result in an
edge that is incomplete or blurry. If the edge of an object is a regular curve, then the curve fitting method is useful for
processing the initial edge.
10 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
To extract the edge of an object, you first can use the watershed algorithm. This algorithm separates the object image from
the background image. Then you can use the morphologic algorithm to fill in missing pixels and filter the noise pixels. After
obtaining the shape of the object, use the Laplacian, or the Laplace operator, to obtain the initial edge. The following figure
shows the edge extraction process on an image of an elliptical object with a physical obstruction on part of the object.
Figure 14. Using the Nonlinear Curve Fit VI to Fit an Elliptical Edge
The graph in the previous figure shows the iteration results for calculating the fitted edge. After several iterations, the VI
extracts an edge that is close to the actual shape of the object.
Decomposing Mixed Pixels Using Curve Fitting
The standard of measurement for detecting ground objects in remote sensing images is usually pixel units. Due to spatial
resolution limitations, one pixel often covers hundreds of square meters. The pixel is a mixed pixel if it contains ground
objects of varying compositions. Mixed pixels are complex and difficult to process. One method of processing mixed pixels is
to obtain the exact percentages of the objects of interest, such as water or plants.
The following image shows a Landsat false color image taken by Landsat 7 ETM+ on July 14, 2000. This image displays an
area of Shanghai for experimental data purposes.
11 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
where
You can rewrite the original exponentially modified Gaussian function as the following equation.
LabVIEW can fit this equation using the Nonlinear Curve Fit VI. The following figure shows an exponentially modified
Gaussian model for chromatography data.
12 of 13 7/20/17, 10:22 AM
Overview of Curve Fitting Models and Methods in LabVIEW - National In... http://www.ni.com/white-paper/6954/en/
By using the appropriate VIs, you can create a new VI to fit a curve to a data set whose function is not available in LabVIEW.
3. Summary
Curve fitting not only evaluates the relationship among variables in a data set, but also processes data sets containing noise,
irregularities, errors due to inaccurate testing and measurement devices, and so on. LabVIEW provides basic and advanced
curve fitting VIs that use different fitting methods, such as the LS, LAR, and Bisquare methods, to find the fitting curve. The
fitting model and method you use depends on the data set you want to fit. LabVIEW also provides preprocessing and
evaluation VIs to remove outliers from a data set, evaluate the accuracy of the fitting result, and measure the confidence
interval and prediction interval of the fitted data.
Refer to the LabVIEW Help for more information about curve fitting and LabVIEW curve fitting VIs.
13 of 13 7/20/17, 10:22 AM