%% ECG baseline wandering removal
function y = ECGbaselineRemoving(val)
%the signal should be 15000 samples or you need to modify the code
%extend the signal to be 15360 samples
for i =1:360
val(15000+i) = 0;
end
% get the length of the signal
l_s = length(val);
%% Decompose the signal using discrete stationary wavelet transform (SWT)
[swa,swd] = swt(val,7,'db15');
swd = swd *0;
%% Reconstraction of the ECG signal using SWT
a = iswt(swa,swd,'db15');
%% subtract the exrtracted wander baseline from The original ECG signal
x = val - a;
y = x(1:15000);
%% plot the ECG signal before and after baseline wander removing
figure;
% plot unfiltered signal
subplot(2,1,1)
plot(val(1:15000))
% plot the extracted baseline wander
hold on
plot(a(1:15000))
% plot filtered segnal
subplot(2,1,2)
plot(x(1:15000))
% https://www.mathworks.com/help/wavelet/ug/one-dimensional-discrete-stationary-wavelet-analysis.html