looking for strictly recurrent and fast moving median implementation
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am looking for any suitable trick how to effectively compute moving window (length w) median. I know of course the movmedian function, but I need strictly recurrent native MATLAB function working sample by sample.
My naive solution, which is equivalent to the
output_median = movmedian(input_x,[w-1,0])
is as follows:
rng('default')
% number of samples
N = 25;
% moving median windows length
w = 5;
% init history buffer and median
x_hist = rand;
med_new = x_hist;
% init input x vector
input_x = zeros(1,N);
input_x(1) = x_hist;
% init output median vector of length N
output_median = zeros(1,N);
output_median(1) = med_new;
for i = 2:N
x_new = rand;
[med_new,x_hist] = moving_median(x_hist,x_new,w);
input_x(i) = x_new;
output_median(i) = med_new;
end
where function moving_median is here:
function [med_new,x_hist] = moving_median(x_hist,x_new,w)
% Old length of history
wo = length(x_hist);
% Update history
x_hist = [x_hist(max(1,wo-w+2):wo),x_new]; % Grow history until size w, then append new x and remove oldest x
med_new = median(x_hist);
end
Any idea how to make this algorithm more effective (faster) a still strictly recurrent?
Target use case should works with window length:
w ~ 1e3 - 1e4 (!!!)
Additional notes:
- fast moving median computing (including MATLAB movmedian function) is always based on advanced data structures use like Heap or Queues, etc.
- some sort-structure information could be stored in these structures and used at the next sample step to significant speed-up of median computing
- similar approach is used in movmedian function, but this function is not directly applicable on running-data stream!
3 Kommentare
Image Analyst
am 26 Jun. 2025
I still don't understand. Sounds like recurrent is just a regular mdeian filter using a sliding window. Just pass each individual sample vector into movmedian. Not sure why you think that does not do the job for you.
How long does your processing take and how fast do you need it to be? There are other denoising methods other than the median filter, which is non-linear and slower than linear filters.
Antworten (2)
Steven Lord
am 26 Jun. 2025
If I understand what you're trying to do, you have a process that generates data (like streaming a video, where you don't necessarily have to download the whole video before you start playing) and as each new data point is entered, you want to take the moving median including that new data point. Is that correct? If so, the simplest solution is probably to call median on the subset of the vector containing your stream of data that is in the window. As a quick and dirty example:
x = (1:5).^2;
m = median(x); % moving median: 2 elements before and 2 elements after
for k = 6:10
x(k) = k.^2; % If I were making this a full example I'd try to preallocate x and m
m(k-4) = median(x(k-4:k));
end
m
Compare with taking the moving median of the entire x vector, discarding any windows that are not contained entirely inside the vector. If you want other endpoint handling, you should be able to adjust the for loop or the initial creation of the m vector.
m2 = movmedian(x, [2 2], Endpoints = "discard")
5 Kommentare
Steven Lord
am 26 Jun. 2025
Could you combine the 500-1000 individual 1-D streams into a 2-D matrix and call median with a dimension input?
A = randi([-10 10], 4, 5)
median(A)
median(A, 1)
median(A, 2)
Siehe auch
Kategorien
Mehr zu Array and Matrix Mathematics finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!