Apply median filter to an ECG without native MATLAB medfilt
3 views (last 30 days)
Show older comments
José Burgo
on 18 Dec 2022
Commented: Image Analyst
on 22 Dec 2022
I need to apply a median filter to an ECG(the ECG data is anexed bellow) to remove low frequency noise, so that a new filtered signal is generated.
However, I cannot use the native matlab function medilft (a new median filter function must me written without it) and it should allow the input of a variable s- the size of the neighbourhood used by the filter.
So, to obtain the filtered singnal, the is a final subtraction between the original signal and the filter:
signal_filtered(i) = signal(i) - signal_median(i)
Any help is greatly appreciated!
Cheers!
3 Comments
DGM
on 18 Dec 2022
Edge conditions can be handled in various ways, but normally I just pad the array. In order to hang the window off the end of the vector, pad the ends of the vector by half the window width. You could do this by adding zeros, but replicating the end elements avoids adding a step discontinuity.
Accepted Answer
Image Analyst
on 18 Dec 2022
If you don't have medfilt1 because you're missing the Signal Processing Toolbox, you can use a for loop and the median, which is part of base MATLAB.
signal = rand(1, 30);
signal_median = signal; % Initialize output signal
subplot(2, 1, 1);
plot(signal, 'b-', 'LineWidth', 2)
windowWidth = 3;
for k = ceil(windowWidth/2) : numel(signal) - floor(windowWidth/2)
signal_median(k) = median(signal(k - floor(windowWidth/2) : k + floor(windowWidth/2)));
end
hold on
plot(signal_median, 'r-', 'LineWidth', 2)
grid on;
title('Original and Median Filtered Signals')
legend('Original', 'Median Filtered')
% Subtract
signal_filtered = signal - signal_median;
subplot(2, 1, 2);
plot(signal_filtered, 'b-', 'LineWidth', 2)
yline(0, 'Color', 'k', 'LineWidth',2); % Draw black line along y=0 (x axis).
title('Difference Signal')
grid on;
2 Comments
Image Analyst
on 22 Dec 2022
A hand made median would require the use of sort(), unless you also did the sorting yourself. Not sure what functions you can't use, or why you can't use them, but glad you got it working. Thanks for accepting the answer. 🙂
More Answers (1)
DGM
on 18 Dec 2022
Edited: DGM
on 18 Dec 2022
It's a simple sliding-window filter. Given a vector V:
- define a window width W (an odd number is convenient)
- pad V by padwidth = floor(W/2) elements on each end. (replicating the end values is convenient)
- preallocate output vector to the same size as the original unpadded input vector
- loop through the elements of V from padwidth+1:end-padwidth (the original part of the vector)
- at each point k, sample a region around the current element V(k-padwidth:k+padwidth)
- find the sample median (and difference if desired). The result should be a scalar.
- the result at point k goes in the corresponding element of the output vector (i.e. k-padwidth)
See Also
Categories
Find more on Bartlett in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!