No BSD License  

Highlights from
smooth2

2.77778

2.8 | 9 ratings Rate this file 36 Downloads (last 30 days) File Size: 2.23 KB File ID: #6298

smooth2

by Kelly Hilands

 

16 Nov 2004 (Updated 18 Nov 2004)

Smooths the data in a 2D matrix with a user-defined resolution.

| Watch this File

File Information
Description

SMOOTH2.M: Smooths matrix data.

MATRIXOUT=SMOOTH2(MATRIXIN,Nr,Nc) smooths the data in MATRIXIN using a running mean over 2*N+1 successive points, N points on each side of the current point. At the ends of the series skewed or one-sided means are used.

Inputs: matrixIn - original matrix
         Nr - number of points used to smooth rows
         Nc - number of points to smooth columns
Outputs: matrixOut - smoothed version of original matrix

Remark: By default, if Nc is omitted, Nc = Nr.

Acknowledgements
This submission has inspired the following:
smoothc.mat, smooth2a
MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
23 May 2005 Tim Carlton

This function does not work properly

27 Jul 2005 Phil Archer

it works for me

20 Oct 2005 Mark Stoelinga

Didn't work. Produced discontinuites.

26 Oct 2005 j bowling

This works great

29 Dec 2005 C Manoj

The output matrix has values much smaller that expected. Normalization ?

22 Oct 2006 Paul Griffin

Does exactly what it says on the package.

12 Nov 2007 j bowling

Output matrix has value smaller than expected? Needs some improvement

12 Mar 2009 Greg Reeves

I took out the for loops and just used array algebra to do essentially the same thing. Improved speed ~10 fold. I didn't bother checking whether edge behavior is the same. My edge behavior is similar to the default behavior in Matlab's builtin smooth function.

%Initial error statements and definitions
if nargin<2, error('Not enough input arguments!'), end

N(1) = Nr;
if nargin<3
    N(2) = N(1);
else
    N(2) = Nc;
end

if length(N(1))~=1, error('Nr must be a scalar!'), end
if length(N(2))~=1, error('Nc must be a scalar!'), end

[row,col] = size(matrixIn);
eL = spdiags(ones(row,2*N(1)+1),(-N(1):N(1)),row,row);
eL = eL./(repmat(sum(eL,1),row,1));
eR = spdiags(ones(col,2*N(2)+1),(-N(2):N(2)),col,col);
eR = eR./(repmat(sum(eR,2),1,col));

matrixOut = eL*matrixIn*eR;

02 Dec 2009 Joan Pau

Wrong results (e.g., a matrix like [ 1 8 1; 1 1 1; 1 1 1] gives [2.7500 1.0000 2.1667; 2.1667 1.0000 1.7778; 2.1667 1.0000 1.7778]) Don't think this was the result intended

21 Jun 2010 Nathan

Greg Reeves' version indeed runs much faster, but gave me strange results in tests:
A =
1 2 3
4 5 6
7 8 9
smooth2(A,1) =
1.8 3.6 2.6
4.9 8.9 6.2
4.3 7.6 5.1

I switched rows and columns on the eL and eR matrices and was able to obtain results consistent with the documentation of Kelly Hilands' original code (at least for this test):
smooth2(A,1) =
3 3.5 4
4.5 5 5.5
6 6.5 7

Here is the corrected code (two lines are changed from Greg Reeves' code above)

%Initial error statements and definitions
if nargin<2, error('Not enough input arguments!'), end

N(1) = Nr;
if nargin<3
    N(2) = N(1);
else
    N(2) = Nc;
end

if length(N(1))~=1, error('Nr must be a scalar!'), end
if length(N(2))~=1, error('Nc must be a scalar!'), end

[row,col] = size(matrixIn);
eL = spdiags(ones(row,2*N(1)+1),(-N(1):N(1)),row,row);
eL = eL./(repmat(sum(eL,2),1,row)); % THIS LINE CHANGED FROM REEVES' CODE
eR = spdiags(ones(col,2*N(2)+1),(-N(2):N(2)),col,col);
eR = eR./(repmat(sum(eR,1),col,1)); % THIS LINE CHANGED FROM REEVES' CODE

matrixOut = eL*matrixIn*eR;

23 Aug 2010 Jakob

I experienced vertical and horizontal jumps in the smoothing and spent quite a while searching for a better alternative when I came across the smooth2a file, found under acknowledgements on this page. If smooth2 fails for you I recommend checking smooth2a out.

11 Apr 2012 Moritz  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
approximation Kelly Hilands 22 Oct 2008 07:35:51
interpolation Kelly Hilands 22 Oct 2008 07:35:51
2d Kelly Hilands 22 Oct 2008 07:35:51
smooth Kelly Hilands 22 Oct 2008 07:35:51
smoothing Kelly Hilands 22 Oct 2008 07:35:51
matrix Kelly Hilands 22 Oct 2008 07:35:51
mathematics Kelly Hilands 22 Oct 2008 07:35:51

Contact us at files@mathworks.com