How to remove ECG artifacts from EMG data?

I'm new to signal processing and try to analyze EMG data from pectoralis major and other respiratory muscles. Unfortunately, there are strong ECG artifacts in all recordings and I don't know how to remove them from my data.

My data looks like this:

In my experiment, there are two expected EMG-events, that should be at around second 2 and 8 of the recorded data:

I'm working with python and matlab and couldn't find code-examples for this specific problem.

My sampling rate is 4000 Hz. Here is an example of the EMG-data.

Thanks a lot for your help!

Here is principal component analysis of the data you supplied. PCA will extract the features within the signal as is done in picture 1.

Note that the signal will be broken down as it is presented in picture 2 from the paper in 3 (page 15). To remove, just note that most of the principal components look like they come from the ecg data, so lets just take all the eigvectors after 10 and project them onto our signal and add them up to get emg data (see picture 4). I'm not entirely sure what the emg data is suppose to look like, but this looks close to diaphragm emg data I see on google images. Try excluding even more eigenvectors to see if you get what you are looking for.

close all clear all H = csvread('\example.csv');

%regroup data into snapshots of the signal
%to treat as a group of random processes
l = 2710

Z = (H(1:l*10));
X = reshape(Z,[l, 10]);[![enter image description here][5]][5]
RV = X*X'/length(X)
%eigen decomposition
[U,D,V] = svd(RV);

figure
stem(diag(D))
title('eigenvalues')

%projection
figure
for i  = 1:12
xx0 = V(:,i).*Z(1:l);
subplot(4,3,i)
plot(-xx0)
strr = sprintf('principal component: %d', i);
title(strr)
end

sig = 0;
for i = 11:length(V)
sig = sig + V(:,i).*Z(1:l);
end

figure
plot(sig)
title('Emg data?')

figure
subplot(121)
plot(V(:,11).*Z(1:l))
title('Principal component 11, emg?')
subplot(122)
plot(V(:,12).*Z(1:l))
title('Principal component 12, emg?')

• Thanks a lot for your detailed explanation – the emg data looks perfect! However, trying to use the provided code in Matlab (R2016b) I receive the following error: >> EMG_Filter Error: File: EMG_Filter.m Line: 32 Column: 12 Illegal use of reserved keyword "end". – sarahp Oct 5 '18 at 22:15
• woops I edited the code to fix it. the 'end' in line 32 should have been length(V) – spet Oct 5 '18 at 22:22
• also added figures of principal component 11 & 12, you should check these out. They are probably what you are looking for instead of the signal I constructed as the addition of all eigenvectors after 10. – spet Oct 5 '18 at 22:42
• Thanks for your last adjustments. I took a look at the data and don't understand the resulting amount of data points (the sampling rate of the resulting signal). The resulting signal has 2709 values, the original signal 40000. Maybe this is a stupid question, but which timeframe does the resulting signal represent? In my experiment, there are two expected emg-events, that should be at around second 2 and 8 of the recorded data (see added image in my question). – sarahp Oct 6 '18 at 8:45
• I basically took 'snapshots' as they did in the figure from the paper I linked. 2710 samples encompassed 1 ecg signal from your data. I then cleaned only one of these up Z(1:l) by projecting it onto the principal component, V(:,11).*Z(1:l). You can extend the principal component over your entire signal by looping over all l-length (2710) snapshots of H the code. i.e. V(:,11).*H(1:l), V(:,11).*H(l+1, 2*l+1), etc... – spet Oct 6 '18 at 17:50