0001 function [ res ] = invertIfNecessary( rawEcg, fs, windowInSeconds )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 rawEcgSample = rawEcg(1:windowInSeconds*fs);
0017 windowSize = 200;
0018
0019
0020 [ sig, detrended ] = preprocessingNew(rawEcgSample, fs);
0021 [ R_locs ] = kota(sig, detrended, fs);
0022 [ peakVal, meanVal, stdVal ] = getEnsembleInfo(detrended, R_locs, windowSize);
0023
0024
0025
0026 invEcg = - rawEcgSample;
0027 [ sig, detrended ] = preprocessingNew(invEcg, fs);
0028 [ R_locs ] = kota(sig, detrended, fs);
0029 [ invPeakVal, invMeanVal, invStdVal ] = getEnsembleInfo(detrended, R_locs, windowSize);
0030
0031
0032 heightNormal = abs(peakVal - meanVal) / stdVal;
0033 heightInv = abs(invPeakVal - invMeanVal) / invStdVal;
0034
0035 if(heightNormal > heightInv)
0036 res = rawEcg;
0037 return;
0038 else
0039 res = -rawEcg;
0040 return;
0041 end
0042
0043 function [ ensemble_peak, ensemble_mean, ensemble_std ] = getEnsembleInfo(detrended_sig, R_locs, windowSize)
0044 avg = zeros(1,(2*windowSize+1));
0045 left = R_locs-windowSize;
0046 right = R_locs+windowSize;
0047 totalLength = length(R_locs);
0048
0049 parfor i=1:length(R_locs)
0050 if(R_locs(i) < windowSize + 1)
0051 continue;
0052 elseif(R_locs(i) > totalLength-windowSize)
0053 continue;
0054 end
0055 complex = detrended_sig(left(i):right(i));
0056 avg = avg + complex;
0057 end
0058 ensemble_peak = max(avg);
0059 ensemble_mean = mean(avg);
0060 ensemble_std = std(avg);
0061 end
0062
0063 end
0064