File Exchange

image thumbnail

smooth2

version 1.0 (2.23 KB) by

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

2.77778
9 Ratings

6 Downloads

Updated

No License

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.

Comments and Ratings (14)

I have a problem - your code takes a matrix of mine and after smoothing there are horizontal and vertical 'bars' composed of 0.00444 values. The background is NaN's so these numbers obviously stand out... any ideas why it does this?

its not working properly. Its showing noise along y axis.

Moritz

Moritz (view profile)

Jakob

Jakob (view profile)

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.

Nathan

Nathan (view profile)

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;

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

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;

j bowling

Output matrix has value smaller than expected? Needs some improvement

Paul Griffin

Does exactly what it says on the package.

C Manoj

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

j bowling

This works great

Mark Stoelinga

Didn't work. Produced discontinuites.

Phil Archer

it works for me

Tim Carlton

This function does not work properly

MATLAB Release
MATLAB 7 (R14)
Acknowledgements

Inspired: smoothc.mat, smooth2a

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

» Watch video