SMOOTH

Syntax | Return Value | Arguments | Keywords | Examples | Version History | See Also

The SMOOTH function returns a copy of Array smoothed with a boxcar average of the specified width. The result has the same type and dimensions as Array. The algorithm used by SMOOTH is:

SMOOTH-31.jpg

where N is the number of elements in A.

Syntax

Result = SMOOTH( Array, Width [, /EDGE_TRUNCATE] [, MISSING=value] [, /NAN] )

Return Value

Returns the smoothed array, which has the same dimensions as the input array.

Arguments

Array

The array to be smoothed. Array can have any number of dimensions.

See Note on Smoothing Over Large Data Ranges if Array contains data from a very wide range of data values.

Width

The width of the smoothing window. Width can either be a scalar or a vector with length equal to the number of dimensions of Array. If Width is a scalar then the same width is applied for each dimension that has length greater than 1 (dimensions of length 1 are skipped). If Width is a vector, then each element of Width is used to specify the smoothing width for each dimension of Array. Values for Width must be smaller than the corresponding Array dimension. If a Width value is even, then Width+1 will be used instead. The value of Width does not affect the running time of SMOOTH to a great extent.

Note
A Width value of zero or 1 implies no smoothing. However, if the NAN keyword is set, then any NaN values within the Array will be treated as missing data and will be replaced.

Tip
For a multi-dimensional array, set widths to 1 within the Width vector for dimensions that you don't want smoothed.

Keywords

EDGE_TRUNCATE

Set this keyword to apply the smoothing function to all points. If the neighborhood around a point includes a point outside the array, the nearest edge point is used to compute the smoothed result. If EDGE_TRUNCATE is not set, the end points are copied from the original array to the result with no smoothing.

For example, when smoothing an n-element vector with a three point wide smoothing window, the first point of the result R0 is equal to A0 if EDGE_TRUNCATE is not set, but is equal to (A0+A0+A1)/3 if the keyword is set. In the same manner, point Rn-1 is set to An-1 if EDGE_TRUNCATE is not set, or to (An-2+An-1+An-1)/3 if it is.

Note
Normally, two-dimensional floating-point arrays are smoothed in one pass. If the EDGE_TRUNCATE keyword is specified for a two-dimensional floating-point array, the result is obtained in two passes, first for all of the rows, and second for all of the columns. Therefore, the results for points in the interior of the array may differ slightly when the EDGE_TRUNCATE keyword is set. This difference will be most pronounced if the array contains NaN values.

MISSING

The value to return for elements that contain no valid points within the kernel. The default is the IEEE floating-point value NaN. This keyword is only used if the NAN keyword is set.

NAN

Set this keyword to cause the routine to check for occurrences of the IEEE floating-point values NaN or Infinity in the input data. Elements with the value NaN or Infinity are treated as missing data. (See Special Floating-Point Values (Application Programming) for more information on IEEE floating-point values.) In the Result, missing elements are replaced by the smoothed value of all other valid points within the smoothing window. If all points within the window are missing, then the result at that point is given by the MISSING keyword.

Note
SMOOTH should never be called without the NAN keyword if the input array may possibly contain NaN values.

Note on Smoothing Over Large Data Ranges

The smallest resolvable difference between two double-precision numbers is about 1015. If your data include numbers that span a large range, such as 1 and 1015 or greater, numerical roundoff effects will degrade the accuracy of the computation.

For efficiency, SMOOTH computes a running total using the difference between neighboring values, and then divides by the smooth width. Because of this, if the input data contain data representing a range of values larger than 1015, the results may not appear symmetric following the large values. For example:

a = [1.0, 1.0, 2.0, 3.0, 4.0, 1.d18, 4.0, 3.0, 2.0, 1.0, 1.0]
PRINT, SMOOTH(a, 3)

IDL Prints:

 1.0000000       1.3333333       2.0000000       3.0000000 
 3.3333333e+017  3.3333333e+017  3.3333333e+017  0.00000000 
-1.0000000      -1.6666667       1.0000000 

A different way to compute the smoothed array is to use the SHIFT operation, which won't pollute the downstream values. For example:

a = [1.0, 1.0, 2.0, 3.0, 4.0, 1.d18, 4.0, 3.0, 2.0, 1.0, 1.0]
PRINT, (a+SHIFT(a,-1)+SHIFT(a,1))/3

IDL Prints:

1.0000000       1.3333333       2.0000000       3.0000000 
3.3333333e+017  3.3333333e+017  3.3333333e+017  3.0000000 
2.0000000       1.3333333       1.0000000 

This method has the same roundoff errors as using SMOOTH, and uses more memory, but does give symmetric results.

Examples

Note
Also see Smoothing an Image (Image Processing in IDL).

Create and display a simple image by entering:

WINDOW, XSIZE=800, YSIZE=400
D = SIN(DIST(256)/3)
TVSCL, D

Now display the same dataset smoothed with a width of 9 in each dimension by entering:

TVSCL, SMOOTH(D, 9), 256, 0

Now smooth only in the vertical direction with a width of 15:

TVSCL, SMOOTH(D, [1, 15]), 512, 0

This example shows the use of SMOOTH with the multidimensional width argument on an RGB image.

; Determine the path to the file.
file = FILEPATH('rose.jpg', $
   SUBDIRECTORY = ['examples', 'data'])
; Import in the RGB image from the file.
image = READ_IMAGE(file)
; Initialize the image size parameter.
imageSize = SIZE(image, /DIMENSIONS)
; Initialize the display.
DEVICE, DECOMPOSED = 1
WINDOW, 0, XSIZE = imageSize[1], YSIZE = imageSize[2], $
   TITLE = 'Original Rose Image'
; Display the original image on the left side.
TV, image, TRUE = 1
; Initialize another display.
WINDOW, 1, XSIZE = 3*imageSize[1], YSIZE = imageSize[2], $
   TITLE = 'Vertically Smoothed (left), Horizontally ' + $
   'Smoothed (middle), and Both (right)'
; Smooth the RGB image in just the width dimension.
smoothed = SMOOTH(image, [1, 1, 21])
; Display the results.
TV, smoothed, 0, TRUE = 1
; Smooth the RGB image in just the height dimension.
smoothed = SMOOTH(image, [1, 21, 1])
; Display the results.
TV, smoothed, 1, TRUE = 1
; Smooth the RGB image in just the width and height dimensions.
smoothed = SMOOTH(image, [1, 5, 5])
; Display the results.
TV, smoothed, 2, TRUE = 1

Version History

Original

Introduced

See Also

DIGITAL_FILTER, LEEFILT, MEDIAN, TS_DIFF, TS_FCAST, TS_SMOOTH