View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Fast 'n easy smoothing

4.9 | 34 ratings Rate this file 90 Downloads (last 30 days) File Size: 27.4 KB File ID: #25634 Version: 2.1.1
image thumbnail

Fast 'n easy smoothing


Damien Garcia (view profile)


22 Oct 2009 (Updated )

SMOOTHN allows automatized and robust smoothing in arbitrary dimension w/wo missing values

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

SMOOTHN provides a fast, unsupervised and robust discretized spline smoother for data of arbitrary dimension.
SMOOTHN(Y) automatically smoothes the uniformly-sampled array Y. Y can be any N-D multicomponent noisy array (e.g. time series, images, 3D data, 3D vector fields, tensors...).
To smooth a vector field or multi-component data, Y must be a cell array. For example, if you need to smooth a 3-D vectorial flow (Vx,Vy,Vz), use Y = {Vx,Vy,Vz}. The output Z is also a cell array which contains the smoothed components.
SMOOTHN can deal with missing (NaN) values (see screenshot and examples).
SMOOTHN(...,'robust') carries out a robust smoothing that minimizes the influence of outlying data (see screenshot and examples).
SMOOTHN is made automated by the minimization of the generalized cross-validation score.
Enter "help smoothn" in the Matlab command window for complete instructions and 1-D to 3-D examples.
When using this algorithm, please refer to these 2 papers:
1) Garcia D. Robust smoothing of gridded data in one and higher dimensions with missing values.
Comput Statist Data Anal, 2010;54:1167-1178
2) Garcia D. A fast all-in-one method for automated post-processing of PIV data.
Exp Fluids, 2011;50:1247-1259.


This file inspired Pi Vsuite, Pi Vlab Time Resolved Particle Image Velocimetry (Piv) Tool, Robust Smoothing For 1 D To N D Data (Easy Version Of Smoothn), Quick & Easy Smoothing, and Pcg Unwrap 2 D(Ph Nii, Ph File, Mask Nii, Max Iter, Epsi Con, N).

Required Products MATLAB
MATLAB release MATLAB 9.0 (R2016a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (40)
03 Jan 2017 Parker Sorenson

30 Dec 2016 Dang Le Van

Just used a part of the code. Very clear instruction. Well done! Thanks for sharing.

14 Dec 2016 Dipankar Kumar

Thanks to Damien Garcia for your robust and fast function for smooting.

Comment only
24 Nov 2016 Sathish Sanjeevi

This function is gold! Would be largely helpful for the scientific community across different disciplines.

22 Sep 2016 Michal Kvasnicka

Perfect ... !!! Robust and fast function for smoothing.

06 Aug 2016 yuseff12

works great and very smoothly :)

29 Jul 2016 Ned Gulley

Ned Gulley (view profile)

I featured this file in a blog post about how to write good help for your function. Thanks and well done!

05 Jul 2016 Brian McFall

Works great and it's easy to adjust the robustness of the smoothing function.

25 Jun 2016 Kevin Miller

18 Apr 2016 Stephan Koehler

I'm trying to run this on large data sets, which crashes my computer. Is there any way to chunk it, i.e. break up say a 1000x1000x1000 array into pieces and merge the smoothened pieces?

05 Feb 2016 Zhouxin Xi

20 Dec 2015 David MERCIER

17 Jul 2014 ogadol

ogadol (view profile)

Hi Damien

Im applying smoothn in order to smooth bathmetric data sets and it works perfectly. I'm using a smooth factor of 10^9 on a 1601X1601 grid. The spacing of the points in realty is 12.5 m. can you please tell me what is the spatial meaning of the smooth factor in my case ?



28 May 2014 Rogier Westerhoff

Works fine for me!

30 Apr 2014 John Booker

29 Apr 2014 Matteo Niccoli

24 Mar 2014 Jospeh Chang

28 Feb 2014 arnold

arnold (view profile)

hey Damien,

is there a way to suppress filling of missing values (nans to be specific)? it'd be really useful if this were an option.


Comment only
19 Dec 2013 Tete Tevi

14 Oct 2013 francisco fuentes

Really nice tool, thank you.

21 Jul 2013 Ben

Ben (view profile)

Hi Damien,
What if I have non-uniformly sampled array? Could it be possible to us this function? Currently, I found the smoothed curve did not maintain the start and end points in 3D. The smoothed curve looks being shrunk.

Comment only
24 May 2013 Martin Lechner

Super tool for smoothing!

04 May 2013 Ben

Ben (view profile)

Thanks for sharing!! This is really a nice pice of code.

08 Nov 2012 Michael

What is with the "DCTN and iDCTN are required"? I was able to access them outside the MatLab browser, but not from within it.

Comment only
07 Jun 2012 Matlab2010

09 May 2012 Jakub Wosyka

Hello Damien,
very useful and nice function.
I tried to add the non-uniform grid (x values) according to your article but then it only interpolates the data. When I raise the value of smooting parameter, new the function will fall to zero (zero mean). (...probably my fault...). Do you plan to extend this nice function for non-uniform grids? Thank you

23 Feb 2012 Erdal Bizkevelci

18 May 2011 Erik

Erik (view profile)

Really nice method!

I does seem to me though that there is a bug when using this in combination with your pppiv-function.

In the function RobustWeights, there is a line:
MAD = median(abs(r(I)-median(r(I))));

However, the values of r are complex when using it together with pppiv. That causes the median function to sort the values by the real component and then take the middle value. I think you probably want to add
r = abs(r);
before taking the median to sort the residuals by magnitude instead. Is that correct?

08 Dec 2010 Richard

Well done! well documented. very easy to use.

09 Aug 2010 Balengi

I like this function very much...I do have a question though. I am smoothing a 50x50x3 n-d array but after smoothing my 3-d object is much flatter than the original. it almost seems distorted...has this ever happened to you before?

05 Jul 2010 Juha-Matti Alakoskela

13 Jun 2010 Warwick

Very good!. Minor effort to obtain dctn and idctn but this is an easy way to smooth out "zigzag" contours (much better than going with INTERP2( Array, ntimes)).
thank you.

20 Apr 2010 Damien Garcia

Damien Garcia (view profile)

to Matthew: you should have read the File description:
MATLAB release: MATLAB 7.5 (R2007b)
BSXFUN is nonexistent in versions prior to 7.4!
Regards. DG

Comment only
20 Apr 2010 Matthew Souders

Code does not work out of the box for MATLAB 7.1 - at least I was unable to get it to function. Attempted a test case and got this error:

??? Undefined function or method 'bsxfun' for input argument of type 'function_handle'.

Error in ==> smoothn at line 266
Lambda = bsxfun(@plus,Lambda...

If some special set-up is required to make this code run successfully, this needs to be carefully documented.

21 Feb 2010 Mark Shore

18 Jan 2010 Mark Shore

Definitely very interesting looking. Well referenced with a range of 1, 2 and 3D examples, which work quite well. I'll rate this once I've had a chance to look at it in more detail.

However the author should correct the required products listing since the image processing toolbox is NOT a requirement per se. All of the provided test examples work perfectly without it. A call to bwdist to replace NaNs in supplied data defaults gracefully to another option if the image processing toolbox is not installed. And rgb2gray and imnoise are required only to _create_ the example of the noisy Saturn image given above, not to denoise it.

It would also be convenient for the author to include his subroutines dctn.m and idctn.m in the FEX file if possible, rather than requiring a separate download from an external web site.

Comment only
15 Jan 2010 Rob Campbell

Rob Campbell (view profile)

This is nice!

31 Oct 2009 Jan Simon

Jan Simon (view profile)

H1 line, really descriptive help, examples, date and author mentioned, comments in the code, referenced method, and finally it works well.

30 Oct 2009 Xianmin

Good job!

23 Oct 2009 Carlos Adrian Vargas Aguilera

Welcome back!

30 Oct 2009 1.1

An example has been added

17 Nov 2009 1.2

The "upsample" option has been removed

07 Dec 2009 1.3

Very much faster now with weighted/missing data:
1) SOR
2) Initial guess from nearest-neighbor interpolation
3) GCV score is minimized every 2^k iterations (k = 0...)

17 Dec 2009 1.5

A bug has been reported with a smoothness parameter input. Thank you B.S.

15 Jan 2010 1.6

The link to the manuscript has been modified.

19 Jan 2010 1.7

Gamma was undefined when "isauto" variable was false.

27 Jan 2010 1.8

A minor bug in error(nargchk(...))

25 Feb 2010 1.9

Minor modification in the relaxation factor

26 Apr 2010 1.10

fix a minor bug with the warning messages

10 Jun 2010 1.11

Some options have been removed/added

08 Nov 2010 1.12

minor modifications: mlint messages removed

23 Nov 2011 1.19

3 optional weighting functions

25 Nov 2011 1.20

3 optional weighting functions

11 Apr 2012 1.21

some typos

30 Apr 2013 1.22

DCTN and IDCTN are now included in SMOOTHN

31 May 2013 1.23

SMOOTHN now works with multicomponent data (e.g. 3D vector fields) by using cell arrays. See the help text for details.

27 Jun 2013 1.24

One bug has been fixed.

27 Aug 2013 1.25

A small bug was present in the subfunction InitialGuess (Thank you Jonathan!)

11 Sep 2013 1.26

A bug was present when dealing with complex numbers

04 Nov 2013 1.27

A minor bug in the "Initial Guess" criterion has been removed

11 Nov 2013 1.28

Update of the help text

20 Nov 2013 1.30

There was a non negligible bug in the new SMOOTHN function:
d = ndims(y) => d = ndims(y{1})
Many thanks to Gianni Pedrizzetti.

19 Dec 2013 1.31

SMOOTHN, by default, assumes that the spacing increments are constant and equal in all the directions (i.e. dx = dy = dz = ...). Spacing increments that differ from one direction to the other can now be used (enter "help smoothn" for details).

20 Dec 2013 1.32

Numerical adjustment in the leverage (for robust smoothing) in the case of unequal spacing increments.

10 Feb 2014 1.33

To simplify the syntax, the termination parameters are now in a structure (OPTIONS).

12 Oct 2014 1.34

Some modifications in the help text

12 Oct 2014 1.35


12 Oct 2014 1.36


21 Jan 2015 1.37

I noticed a minor error in one comment

25 Jul 2016 2.0

minor update: 1) use narginchk, 2) some warning messages slightly modified

25 Jul 2016 2.1

minor updates: 1) use narginchk, 2) two warning messages slightly modified

25 Jul 2016 2.1.1


Contact us