SMOOTHDATA1D Function
Smooths one-dimensional data by error detection.
Usage
result = SMOOTHDATA1D(x, y)
Input Parameters
x—One-dimensional array containing the abscissas of the data points.
y—One-dimensional array containing the ordinates of the data points.
Returned Value
result—One-dimensional array containing the smoothed data.
Input Keywords
Double—If present and nonzero, double precision is used.
Itmax—The maximum number of iterations allowed. Default: Itmax = 500
Sc—The stopping criterion. Sc should be greater than or equal to zero. Default: Sc = 0.0
Distance—Proportion of the distance the ordinate in error is moved to its interpolating curve. It must be in the range 0.0 to 1.0. Default: Distance = 1.0
Algorithm
The function SMOOTHDATA1D is designed to smooth a data set that is mildly contaminated with isolated errors. In general, the routine will not work well if more than 25% of the data points are in error. The routine SMOOTHDATA1D is based on an algorithm of Guerra and Tapia (1974).
Setting N_ELEMENTS(x) = n, Y = f, result = s and X= x, the algorithm proceeds as follows. Although the user need not an ordered x sequence, we will assume that x is increasing for simplicity. The algorithm first sorts the x values into an increasing sequence and then continues. A cubic spline interpolant is computed for each of the 6-point data sets (initially setting s = f ):
(xj, sj)      j = i – 3, ..., i + 3     j i
where i = 4, ... , n – 3. For each i the interpolant, which we will call Si, is compared with the current value of si, and a ‘point energy’ is computed as:
pei = Si(xi) – si
Setting sc = Sc, the algorithm terminates either if Itmax iterations have taken place or if:
If the above inequality is violated for any i, then we update the ith element of s by setting si = si + d(pei), where d = Distance. Note that neither the first three nor the last three data points are changed. Thus, if these points are inaccurate, care must be taken to interpret the results.
The choice of the parameters Distance, Sc and Itmax are crucial to the successful usage of this subroutine. If the user has specific information about the extent of the contamination, then he should choose the parameters as follows: Distance = 1, Sc = 0 and Itmax to be the number of data points in error. On the other hand, if no such specific information is available, then choose Distance = 0.5, Itmax 2n, and:
In any case, we would encourage the user to experiment with these values.
Example
We take 91 uniform samples from the function 5 + (5 + t2 sin t )/t on the interval [1, 10]. Then, we contaminate 10 of the samples and try to recover the original function values.
FUNCTION F, xdata
   RETURN, (xdata*xdata*SIN(xdata) + 5)/xdata + 5
END
isub=[5, 16, 25, 33, 41, 48, 55, 61, 74, 82]
rnoise=[2.5, -3.0, -2.0, 2.5, 3.0, -2.0, -2.5, 2.0, -2.0, 3.0]
 
; Example 1: No specific information available.
dis = 0.5
sc = 0.56
itmax = 182
; Set values for xdata and fdata.     
xdata = 1 + 0.1*FINDGEN(91)
fdata = f(xdata)
; Contaminate the data.
fdata(isub) = fdata(isub) + rnoise
; Smooth the data.
sdata = SMOOTHDATA1D(xdata, fdata, Itmax = itmax, $
   Distance = dis, Sc = sc)
; Output the results.
PM, [[f(xdata(isub))], [fdata(isub)], [sdata(isub)]], $
   Title  =  '        F(X)    F(X) + noise     sdata'      
 
   ; PV-WAVE prints the following:
;       F(X)    F(X) + noise     sdata
;     9.82958      12.3296      9.87030
;     8.26338      5.26338      8.21537
;     5.20083      3.20083      5.16823
;     2.22328      4.72328      2.26399
;     1.25874      4.25874      1.30825
;     3.16738      1.16738      3.13830
;     7.16751      4.66751      7.13076
;     10.8799      12.8799      10.9092
;     12.7739      10.7739      12.7075
;     7.59407      10.5941      7.63885
 
 
; Example 2: Specific information available.
dis = 1.0
sc = 0.0
itmax = 10.0
; A warning message is produce because the maximum number 
; of iterations is reached.
; Maximum number of iterations limit 'ITMAX' = 10 exceeded. 
; The best answer found is returned. Output the results.
sdata = SMOOTHDATA1D(xdata, fdata, Itmax = itmax, $
   Distance = dis, Sc = sc)
% SMOOTHDATA1D: Warning: MATH_ITMAX_EXCEEDED
PM, [[f(xdata(isub))], [fdata(isub)], [sdata(isub)]], $
   Title  =  '        F(X)    F(X) + noise     sdata'      
 
   ; PV-WAVE prints the following:
   ;       F(X)    F(X) + noise     sdata
   ;     9.82958      12.3296      9.83127
   ;     8.26338      5.26338      8.26223
   ;     5.20083      3.20083      5.19946
   ;     2.22328      4.72328      2.22495
   ;     1.25874      4.25874      1.26142
   ;     3.16738      1.16738      3.16958
   ;     7.16751      4.66751      7.16986
   ;     10.8799      12.8799      10.8779
   ;     12.7739      10.7739      12.7699
   ;     7.59407      10.5941      7.59194