Skip to content

Commit

Permalink
Making mincompare more global and updating RLS to Matlab2017
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Andreotti committed Nov 20, 2017
1 parent db86cad commit e706fbd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 56 deletions.
4 changes: 2 additions & 2 deletions subfunctions/benchmark-tools/bibs/FECGSYN_QRSmincompare.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
fqrs = cell(1,size(data,1));
for j = 1:size(data,1)
fqrstmp = cell(1,ceil(size(data,2)/(60*fs)));
for l = 1:5
fqrstmp{l} = jqrs_mod(data(j,60*fs*(l-1)+1:60*fs*(l)),REFRAC,TH,fs) + 60*fs*(l-1);
for l = 1:length(fqrstmp)
fqrstmp{l} = jqrs_mod(data(j,60*fs*(l-1)+1:min(length(data),60*fs*(l))),REFRAC,TH,fs) + 60*fs*(l-1);
end
fqrs{j} = cell2mat(fqrstmp);
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function residual = FECGESN_lmsrls_canceller(ref_ecg,tar_ecg,fs,metStruct,debug)
% Adaptive Noise cancelling filter for FECG source separation.
%
% This function uses the least mean square (LMS) or the recursive least square
% (RLS) algorithm in order to remove the MECG contribution on the abdominal
% mixture. The LMS can be run 'online' (i.e the weights are left evolving)
% or 'offline' (the weights are optimised on a training set and then constant
% This function uses the least mean square (LMS) or the recursive least square
% (RLS) algorithm in order to remove the MECG contribution on the abdominal
% mixture. The LMS can be run 'online' (i.e the weights are left evolving)
% or 'offline' (the weights are optimised on a training set and then constant
% for the test set). The RLS is run online.
%
% inputs
Expand All @@ -16,14 +16,14 @@
% metStruct.N
% metStruct.learningMode ('online'/'offline')
% metStruct.method ('LMS'/'RLS')
%
%
% tar_ecg
% residual: FECG residual
%
% NOTE: time used for initialising the weights = 30sec (see TIME_INIT constant)
%
%
% References
% References
% 1. B. Widrow, J. Glover, J. McCool, J. Kaunitz, C. Williams, R. Hearn,
% J. Zeidler, E. Dong, and R. Goodlin, Adaptive noise cancelling:
% Principles and applications, Proceedings of the IEEE, vol. 63, no. 12,
Expand Down Expand Up @@ -59,66 +59,61 @@
TIME_INIT = 30;

% == core function
try
% == normalize the data
% assumes that the first 5sec are representative (no huge noise) for normalizaiton purposes.
[input_norm,~] = FECGESN_normalise_ecg(ref_ecg,1,5*fs);
[output_norm,~] = FECGESN_normalise_ecg(tar_ecg,1,5*fs);

if strcmp(metStruct.method,'LMS')
% == normalize the data
% assumes that the first 5sec are representative (no huge noise) for normalizaiton purposes.
[input_norm,~] = FECGESN_normalise_ecg(ref_ecg,1,5*fs);
[output_norm,~] = FECGESN_normalise_ecg(tar_ecg,1,5*fs);

if strcmp(metStruct.method,'LMS')
% == least mean square
if strcmp(metStruct.learningMode,'online')
[y,residual,~] = FECGESN_lms(input_norm,output_norm,metStruct.mu,metStruct.Nunits);
else
% training readout on first 30sec and test on ALL signal using
% constant weights

% split into train and test
train_fraction = TIME_INIT/(length(input_norm)/fs);
[train_in,~] = split_train_test(input_norm,train_fraction);
[train_out,~] = split_train_test(output_norm,train_fraction);

% find lms param using training sequence
[y,~,W] = FECGESN_lms(train_in,train_out,metStruct.mu,metStruct.Nunits);
w = W(:,end);

% now apply trained lms to all the signal
residual = output_norm-filter(w,1,input_norm);

% [~,w] = build_design_matrix_and_whopf(train_in,train_out,metStruct.Nunits);
% chestf = conv(input_norm,w,'valid'); % idem as filtering
% plot(output_norm(1:end)); hold on, plot(chestf(metStruct.Nunits+1:end),'r');
% residual = output_norm-chestf(metStruct.Nunits+1:end);
end
elseif strcmp(metStruct.method,'RLS')
% == recursive least square
p0 = 2*eye(metStruct.Nunits);
ha = adaptfilt.rls(metStruct.Nunits,metStruct.mu,p0);
[y,residual] = filter(ha,input_norm,output_norm);
if strcmp(metStruct.learningMode,'online')
[y,residual,~] = FECGESN_lms(input_norm,output_norm,metStruct.mu,metStruct.Nunits);
else
% training readout on first 30sec and test on ALL signal using
% constant weights

% split into train and test
train_fraction = TIME_INIT/(length(input_norm)/fs);
[train_in,~] = split_train_test(input_norm,train_fraction);
[train_out,~] = split_train_test(output_norm,train_fraction);

% find lms param using training sequence
[y,~,W] = FECGESN_lms(train_in,train_out,metStruct.mu,metStruct.Nunits);
w = W(:,end);

% now apply trained lms to all the signal
residual = output_norm-filter(w,1,input_norm);

% [~,w] = build_design_matrix_and_whopf(train_in,train_out,metStruct.Nunits);
% chestf = conv(input_norm,w,'valid'); % idem as filtering
% plot(output_norm(1:end)); hold on, plot(chestf(metStruct.Nunits+1:end),'r');
% residual = output_norm-chestf(metStruct.Nunits+1:end);
end
catch ME
rethrow(ME);
for enb=1:length(ME.stack); disp(ME.stack(enb)); end;
residual = [];
elseif strcmp(metStruct.method,'RLS')
% == recursive least square
p0 = 2*eye(metStruct.Nunits);
RLSFilt = dsp.RLSFilter(metStruct.Nunits,'ForgettingFactor',metStruct.mu,'InitialInverseCovariance',p0);
[y,residual] = RLSFilt(input_norm,output_norm);
end

% == debug


% == debug
if debug
LINE_WIDTH = 2;
FONT_SIZE = 15;
nb_of_points = length(ref_ecg);
tm = 1/fs:1/fs:nb_of_points/fs;
figure('name','lmsrls canceller');
ax(1) = subplot(311); plot(tm,input_norm,'LineWidth',LINE_WIDTH);
hold on, plot(tm,output_norm,'r','LineWidth',LINE_WIDTH);
legend('normalised reference ecg (chest ECG)','normalised target ecg (abdominal ECG)');
ax(1) = subplot(311); plot(tm,input_norm,'LineWidth',LINE_WIDTH);
hold on, plot(tm,output_norm,'r','LineWidth',LINE_WIDTH);
legend('normalised reference ecg (chest ECG)','normalised target ecg (abdominal ECG)');
ax(2) = subplot(312); plot(tm,output_norm); hold on, plot(tm,y,'r','LineWidth',LINE_WIDTH);
legend('normalised target ecg (abdominal ECG)','predicted target ecg');
set(findall(gcf,'type','text'),'fontSize',FONT_SIZE);
legend('normalised target ecg (abdominal ECG)','predicted target ecg');
set(findall(gcf,'type','text'),'fontSize',FONT_SIZE);
ax(3) = subplot(313); plot(tm,residual,'r','LineWidth',LINE_WIDTH);
legend('residual - FECG');
set(findall(gcf,'type','text'),'fontSize',FONT_SIZE);
legend('residual - FECG');
set(findall(gcf,'type','text'),'fontSize',FONT_SIZE);
linkaxes(ax);
end

Expand Down

0 comments on commit e706fbd

Please sign in to comment.