Can anyone please, help me with the 1D median filter algorithm?

2 Comments

David Barry
David Barry on 13 Dec 2016
Help you do what exactly? You need to be clear. Why can't you use the built-in MATLAB function? It seems strange to build your own when one already exists for you to use straight out of the box.
dunklevision
dunklevision on 17 Dec 2016
well it's a task so I have to build that function...

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 13 Dec 2016

0 votes

M = movmedian(A,k) returns an array of local k-point median values, where each median is calculated over a sliding window of length k across neighboring elements of A. When k is odd, the window is centered about the element in the current position. When k is even, the window is centered about the current and previous elements. The window size is automatically truncated at the endpoints when there are not enough elements to fill the window. When the window is truncated, the median is taken over only the elements that fill the window. M is the same size as A.
I supposed you don't want to use that either? Well at least movmedian() is built into base MATLAB, unlike medfilt1() which is in the Signal Processing Toolbox.

8 Comments

dunklevision
dunklevision on 17 Dec 2016
thank you very much for this new info but still need that function, I mean the algorithm that does the same thing as the "movmedian"
It's rather obvious. Just slide a window across the vector and extract the values and send them into median(). The only slightly tricky part is that in that function the window width shrinks as it bumps against either end. Basically it's like this
for k = 1 : length(vec)
filteredVec = median(vec(k:(k+windowWidth-1)));
end
Now that won't quite work because I haven't done the edge effects yet, but that's not hard. See if you can figure it out.
If you really can't figure it out, and maybe later if I get time I'll do it for you, but give it a shot yourself first.
Since you asked someone who posted a question over 5 and a half years ago here, I guess that you were unable to figure it out. So here it is:
numElements = 21;
signal = randi(9, 1, numElements)
windowSize = 7;
halfWidth = floor(windowSize/2)
for windowCenter = 1 : length(signal)
% Get the left index
i1 = max([1, windowCenter - halfWidth]);
% Get the right index
i2 = min([numElements, windowCenter + halfWidth]);
if i1 < 1 || i2 < 1 || i1 > numElements || i2 > numElements
continue;
end
fprintf('Examining indexes %d to %d.\n', i1, i2);
filteredSignal2(windowCenter) = median(signal(i1:i2));
end
% Now do it via the built-in movmedian():
filteredSignal = movmedian(signal, windowSize)
% Prove it's the same as using movmedian() by showing all the differences are 0
differences = filteredSignal - filteredSignal2
dunklevision
dunklevision on 19 Dec 2016
Thank you very much. I'm going to try to figure out these steps and apply them on my signal. Thank you for your help once again.
Image Analyst
Image Analyst on 20 Dec 2016
Did my suggestions work?
dunklevision
dunklevision on 2 Jan 2017
yes ! thank you very much
Image Analyst
Image Analyst on 3 Jan 2017
Well, can you then go ahead and "Accept this Answer"? Thanks in advance.
dunklevision
dunklevision on 13 Jan 2017
sure! I'm the one to thank you.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!