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

2 个评论

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.
well it's a task so I have to build that function...

请先登录,再进行评论。

 采纳的回答

Image Analyst
Image Analyst 2016-12-13

0 个投票

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 个评论

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
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.
Did my suggestions work?
yes ! thank you very much
Well, can you then go ahead and "Accept this Answer"? Thanks in advance.
sure! I'm the one to thank you.

请先登录,再进行评论。

更多回答(0 个)

类别

标签

Community Treasure Hunt

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

Start Hunting!

Translated by