Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Removing peaks from raw data
Replies: 3   Last Post: Sep 24, 2012 5:10 AM

 Messages: [ Previous | Next ]
 Greg Heath Posts: 214 Registered: 12/13/04
Re: Removing peaks from raw data
Posted: Sep 24, 2012 5:10 AM

On Sep 19, 7:50 am, "Chridert " <chrisvdpl...@upcmail.nl> wrote:
> I am currently working on a script that removes the noise (peaks) from a set of raw data, so say you have a matrix:
>
> 1  2  3
> 4 15 6
> 7  8  9
>
> The script will detect a peak at [2,2] (which is 15) and will replace it with a value which is the mean of the surrounding values (2,6,4 and 8)
>
> The script looks like this:
>
> for i=1:360
> for j=1:180
>     %% Correct corners
>     if i<2 && j<2
>            if data_5(i,j)>mean([data_5(i+1,j) data_5(i,j+1)])+(0.05*mean([data_5(i+1,j) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i+1,j) data_5(i,j+1)]);
>            end
>            if data_5(i,j)<mean([data_5(i+1,j) data_5(i,j+1)])-(0.05*mean([data_5(i+1,j) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i+1,j) data_5(i,j+1)]);
>            end
>     end
>
>     if i<2 && j>179
>            if data_5(i,j)>mean([data_5(i+1,j) data_5(i,j-1)])+(0.05*mean([data_5(i+1,j) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i+1,j) data_5(i,j-1)]);
>            end
>            if data_5(i,j)<mean([data_5(i+1,j) data_5(i,j-1)])-(0.05*mean([data_5(i+1,j) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i+1,j) data_5(i,j-1)]);
>            end
>     end
>
>     if i>359 && j<2
>            if data_5(i,j)>mean([data_5(i-1,j) data_5(i,j+1)])+(0.05*mean([data_5(i-1,j) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i,j+1)]);
>            end
>            if data_5(i,j)<mean([data_5(i-1,j) data_5(i,j+1)])-(0.05*mean([data_5(i-1,j) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i,j+1)]);
>            end
>     end
>
>     if i>359 && j>179
>            if data_5(i,j)>mean([data_5(i-1,j) data_5(i,j-1)])+(0.05*mean([data_5(i-1,j) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i,j-1)]);
>            end
>            if data_5(i,j)<mean([data_5(i-1,j) data_5(i,j-1)])-(0.05*mean([data_5(i-1,j) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i,j-1)]);
>            end
>     end
>     %% Correct sides of matrix
>
>     if i<2 && j>1 && j<180
>            if data_5(i,j)>mean([data_5(i+1,j) data_5(i,j+1) data_5(i,j-1)])+(0.05*mean([data_5(i+1,j) data_5(i,j+1) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i+1,j) data_5(i,j+1) data_5(i,j-1)]);
>            end
>            if data_5(i,j)<mean([data_5(i+1,j) data_5(i,j+1) data_5(i,j-1)])-(0.05*mean([data_5(i+1,j) data_5(i,j+1) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i+1,j) data_5(i,j+1) data_5(i,j-1)]);
>            end
>     end
>
>     if i>359 && j>1 && j<180
>            if data_5(i,j)>mean([data_5(i-1,j) data_5(i,j+1) data_5(i,j-1)])+(0.05*mean([data_5(i-1,j) data_5(i,j+1) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i,j+1) data_5(i,j-1)]);
>            end
>            if data_5(i,j)<mean([data_5(i-1,j) data_5(i,j+1) data_5(i,j-1)])-(0.05*mean([data_5(i-1,j) data_5(i,j+1) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i,j+1) data_5(i,j-1)]);
>            end
>     end
>
>     if i>1 && i<360 && j<2
>            if data_5(i,j)>mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j+1)])+(0.05*mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j+1)]);
>            end
>            if data_5(i,j)<mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j+1)])-(0.05*mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j+1)]);
>            end
>     end
>
>     if i>1 && i<360 && j>159
>            if data_5(i,j)>mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1)])+(0.05*mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1)]);
>            end
>            if data_5(i,j)<mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1)])-(0.05*mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1)]);
>            end
>     end
>     %% Correct middle of matrix
>
>     if i>1 && i<360 && j>1 && j<180
>            if data_5(i,j)>mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1) data_5(i,j+1)])+(0.05*mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1) data_5(i,j+1)]);
>            end
>            if data_5(i,j)<mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1) data_5(i,j+1)])-(0.05*mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1) data_5(i,j+1)]))
>            data_5(i,j)=mean([data_5(i-1,j) data_5(i+1,j) data_5(i,j-1) data_5(i,j+1)]);
>            end
>     end
>
> end
> end
>
> This script works fine for peaks which are not adjacent to other peaks, however, when plotting the set of data with surf it will still contain peaks because the peaks adjacent to other peaks will still have a higher mean value because of the value of the other peak. Is there an option to skip the adjacent peaks and find the next value which is not a peak, or is this not possible with this code?
>
> Thanks in forward

I think I'm going blind. You must love to type. Why not use short
variable names and factors 0.95 and 1.05:?

x = data_5;
% Corner Matrix size = [4 4 ]
cx = [ x( 1:2 , (1,2,end-1,end)) ; x( end-1:end , (1,2,end-1,end)) ];
% TopBottom Matrix size = [ 4 180 ]
tbx = ...
% Sides Matrix size = [ 360 4 ].
sx = ...

Just a thought,

Greg

Date Subject Author
9/19/12 Chris
9/19/12 ImageAnalyst
9/22/12 Ruben Hoebee
9/24/12 Greg Heath