Apply median filter to an ECG without native MATLAB medfilt

조회 수: 8 (최근 30일)
José  Burgo
José Burgo 2022년 12월 18일
댓글: Image Analyst 2022년 12월 22일
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
José  Burgo
José Burgo 2022년 12월 18일
Yes, when selecting sucessive lead elements to establish neighbourhoods of the ECG I have problems with the first or last elements, because the are no more elements to form the new neighbourhodd, for example:
with A=[ 1 2 3 4 5] with k(neighbourhood) = 3 ,
it´s ease to obtain median of [ 1 2 3] and [2 3 4] and [3 4 5],
but that would leave me with only 3 elements, and to perform the subtraction I need 5 elements ( same as the ECG)
Is there any way to correct this?
DGM
DGM 2022년 12월 18일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2022년 12월 18일
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
José  Burgo
José Burgo 2022년 12월 22일
Cheers! Adapting it to my introduced ECG and with an handmade median worked just great. Thanks a lot for your help :)
Image Analyst
Image Analyst 2022년 12월 22일
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. 🙂

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

DGM
DGM 2022년 12월 18일
편집: DGM 2022년 12월 18일
It's a simple sliding-window filter. Given a vector V:
  1. define a window width W (an odd number is convenient)
  2. pad V by padwidth = floor(W/2) elements on each end. (replicating the end values is convenient)
  3. preallocate output vector to the same size as the original unpadded input vector
  4. loop through the elements of V from padwidth+1:end-padwidth (the original part of the vector)
  5. at each point k, sample a region around the current element V(k-padwidth:k+padwidth)
  6. find the sample median (and difference if desired). The result should be a scalar.
  7. the result at point k goes in the corresponding element of the output vector (i.e. k-padwidth)

카테고리

Help CenterFile Exchange에서 Signal Generation and Preprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by