function [averageDiff,features,featuresSTD]=findECGpeaksNewFeaturesPlayQRSWenAlot(yp,QRS)
% [averageDiff,~,~]=findPCGpeaksNewFeatures2(yp);
% Diffv=abs(diff(Indexes(:,1)));
averageDiff=median(diff(QRS));
feats1=[];feats2=[];feats3=[];feats4=[];feats5=[];feats6=[];feats7=[];feats8=[];feats9=[];
feats10=[];feats11=[];feats12=[];feats13=[];feats14=[];feats15=[];feats16=[];feats17=[];
feats18=[];feats19=[];feats20=[];feats21=[];feats22=[];feats23=[];feats24=[];feats25=[];feats26=[];
for i=1:length(QRS)-1
    ypp=yp(QRS(i)+1:QRS(i+1));
    ypps=sort(ypp);
    went1=wentropy(ypps(1:round(end/5)),'shannon');
    went2=wentropy(ypps(round(end/5):round(2*end/5)),'shannon');
    went3=wentropy(ypps(round(2*end/5):round(3*end/5)),'shannon');
    went4=wentropy(ypps(round(3*end/5):round(4*end/5)),'shannon');
    went5=wentropy(ypps(round(4*end/5):round(end)),'shannon');
    %     wents=sort([went1,went2,went3,went4]);
    wents=min([went1,went2,went3,went4,went5]);
    wents2=median([went1,went2,went3,went4,went5]);
    wents3=max([went1,went2,went3,went4,went5]);
    wenta1=wentropy(ypp(1:round(end/2)),'shannon');
    wenta2=wentropy(ypp(round(end/2):end),'shannon');
    feats1=[feats1,mean(ypp)];
    feats2=[feats2,std(ypp)];
    [peakLoc1, peakMag1] = peakfinder(ypp);
    val3=std(peakMag1);
    llp=length(peakLoc1);
    val4=std(peakLoc1);
    if isnan(val3)==1
        val3=0;llp=0;val4=0;
    end
    feats3=[feats3,llp];
    feats4=[feats4,moment(ypp,4)];
    feats5=[feats5,kurtosis(ypp)];
    feats6=[feats6,moment(ypp,6)];
    feats7=[feats7,wents];
    feats8=[feats8,val3];
    feats9=[feats9,val4];
%     size(ypp)
%     aaa
    if length(ypp)>10
        za=0;%za=mvsampen_fullA(ypp);
    else
        za=0;
    end
    feats24=[feats24,za];
    feats10=[feats10,wents3];
    feats11=[feats11,wents2];
    feats12=[feats12,wents];
    feats13=[feats13,meanfreq(ypp)];
    feats14=[feats14,median(dutycycle(ypp))];
    z1=statelevels(ypp);
    feats15=[feats15,min(z1)];
    feats16=[feats16,max(z1)];
    feats17=[feats17,median(pulseperiod(ypp))];
    feats18=[feats18,median(falltime(ypp))];
    feats19=[feats19,median(pulsewidth(ypp))];
    feats20=[feats20,median(abs(fft(ypp)))];
    feats21=[feats21,median(angle(fft(ypp)))];
    feats22=[feats22,median(overshoot(ypp))];
    feats23=[feats23,median(histcounts(ypp))];
    %     feats24=[feats24,meanfreq(ypp)];
    feats25=[feats25,wenta1];
    feats26=[feats26,wenta2];
end
len=length(feats1);
feats1=sort(feats1);
feats2=sort(feats2);
feats3=sort(feats3);
feats4=sort(feats4);
feats5=sort(feats5);
feats6=sort(feats6);
feats7=sort(feats7);
feats8=sort(feats8);
feats9=sort(feats9);
feats10=sort(feats10);
feats11=sort(feats11);
feats12=sort(feats12);
feats13=sort(feats13);
feats14=sort(feats14);
feats15=sort(feats15);
feats16=sort(feats16);
feats17=sort(feats17);
feats18=sort(feats18);
feats19=sort(feats19);
feats20=sort(feats20);
feats21=sort(feats21);
feats22=sort(feats22);
feats23=sort(feats23);
feats24=sort(feats24);

featuresMed=[median(feats1),median(feats2),median(feats3),median(feats4),median(feats5),median(feats6),median(feats7),...
    median(feats8), median(feats9), median(feats10), median(feats11), median(feats12), median(feats13), median(feats14),...
    median(feats15), median(feats16), median(feats17), median(feats18), median(feats19), median(feats20), median(feats21),...
    median(feats22), median(feats23), median(feats24) median(feats25) median(feats26)];

featuresSTD=[std(feats1),std(feats2),std(feats3),std(feats4),std(feats5),std(feats6),std(feats7),...
    std(feats8), std(feats9), std(feats10), std(feats11), std(feats12), std(feats13), std(feats14),...
    std(feats15), std(feats16), std(feats17), std(feats18), std(feats19), std(feats20), std(feats21),...
    std(feats22), std(feats23), std(feats24) std(feats25) std(feats26)];


featuresKurt=[kurtosis(feats1),kurtosis(feats2),kurtosis(feats3),kurtosis(feats4),kurtosis(feats5),kurtosis(feats6),kurtosis(feats7),...
    kurtosis(feats8), kurtosis(feats9), kurtosis(feats10), kurtosis(feats11), kurtosis(feats12), kurtosis(feats13), kurtosis(feats14),...
    kurtosis(feats15), kurtosis(feats16), kurtosis(feats17), kurtosis(feats18), kurtosis(feats19), kurtosis(feats20), kurtosis(feats21),...
    kurtosis(feats22), kurtosis(feats23), kurtosis(feats24) kurtosis(feats25) kurtosis(feats26)];

featuresSkew=[skewness(feats1),skewness(feats2),skewness(feats3),skewness(feats4),skewness(feats5),skewness(feats6),skewness(feats7),...
    skewness(feats8), skewness(feats9), skewness(feats10), skewness(feats11), skewness(feats12), skewness(feats13), skewness(feats14),...
    skewness(feats15), skewness(feats16), skewness(feats17), skewness(feats18), skewness(feats19), skewness(feats20), skewness(feats21),...
    skewness(feats22), skewness(feats23), skewness(feats24) skewness(feats25) skewness(feats26)];

features1=[median(feats1(1:round(len/3))),median(feats1(round(len/3):round(2*len/3))),median(feats1(round(len/3):end))];features1=sort(features1);
features1=[features1,abs(features1-median(feats1))];
features1a=[median(abs(feats1(1:round(len/3)))),median(abs(feats1(round(len/3):round(2*len/3)))),median(abs(feats1(round(len/3):end)))];features1a=sort(features1a);
features1a=[features1a,abs(features1a-median(abs(feats1)))];
features2=[median(feats2(1:round(len/3))),median(feats2(round(len/3):round(2*len/3))),median(feats2(round(len/3):end))];features2=sort(features2);
features2=[features2,abs(features2-median(feats2))];
features2a=[median(abs(feats2(1:round(len/3)))),median(abs(feats2(round(len/3):round(2*len/3)))),median(abs(feats2(round(len/3):end)))];features2a=sort(features2a);
features2a=[features2a,abs(features2a-median(abs(feats2)))];
len3=length(feats3);
features3=[median(feats3(1:round(len3/3))),median(feats3(round(len3/3):round(2*len3/3))),median(feats3(round(len3/3):end))];features3=sort(features3);
features3=[features3,abs(features3-median(feats3))];
features3a=[median(abs(feats3(1:round(len3/3)))),median(abs(feats3(round(len3/3):round(2*len3/3)))),median(abs(feats3(round(len3/3):end)))];features3a=sort(features3a);
features3a=[features3a,abs(features3a-median(abs(feats3)))];

features4=[median(feats4(1:round(len/3))),median(feats4(round(len/3):round(2*len/3))),median(feats4(round(len/3):end))];features4=sort(features4);
features4=[features4,abs(features4-median(feats4))];
features4a=[median(abs(feats4(1:round(len/3)))),median(abs(feats4(round(len/3):round(2*len/3)))),median(abs(feats4(round(len/3):end)))];features4a=sort(features4a);
features4a=[features4a,abs(features4a-median(abs(feats4)))];

features5=[median(feats5(1:round(len/3))),median(feats5(round(len/3):round(2*len/3))),median(feats5(round(len/3):end))];features5=sort(features5);
features5=[features5,abs(features5-median(feats5))];
features5a=[median(abs(feats5(1:round(len/3)))),median(abs(feats5(round(len/3):round(2*len/3)))),median(abs(feats5(round(len/3):end)))];features5a=sort(features5a);
features5a=[features5a,abs(features5a-median(abs(feats5)))];
%
features6=[median(feats6(1:round(len/3))),median(feats6(round(len/3):round(2*len/3))),median(feats6(round(len/3):end))];features6=sort(features6);
features6=[features6,abs(features6-median(feats6))];
features6a=[median(abs(feats6(1:round(len/3)))),median(abs(feats6(round(len/3):round(2*len/3)))),median(abs(feats6(round(len/3):end)))];features6a=sort(features6a);
features6a=[features6a,abs(features6a-median(abs(feats6)))];
features7=[median(feats7(1:round(len/3))),median(feats7(round(len/3):round(2*len/3))),median(feats7(round(len/3):end))];features7=sort(features7);
features7=[features7,abs(features7-median(feats7))];
features7a=[median(abs(feats7(1:round(len/3)))),median(abs(feats7(round(len/3):round(2*len/3)))),median(abs(feats7(round(len/3):end)))];features7a=sort(features7a);
features7a=[features7a,abs(features7a-median(abs(feats7)))];

features8=[median(feats8(1:round(len/3))),median(feats8(round(len/3):round(2*len/3))),median(feats8(round(len/3):end))];features8=sort(features8);
features8=[features8,abs(features8-median(feats8))];
features8a=[median(abs(feats8(1:round(len/3)))),median(abs(feats8(round(len/3):round(2*len/3)))),median(abs(feats8(round(len/3):end)))];
features8a=[features8a,abs(features8a-median(abs(feats8)))];

features9=[median(feats9(1:round(len/3))),median(feats9(round(len/3):round(2*len/3))),median(feats9(round(len/3):end))];
features9=[features9,abs(features9-median(feats9))];
features9a=[median(diff(feats9(1:round(len/3)))),median(diff(feats9(round(len/3):round(2*len/3)))),median(diff(feats9(round(len/3):end)))];
features9a=[features9a,abs(features9a-median(diff(feats9)))];
features10=[median(feats10(1:round(len/3))),median(feats10(round(len/3):round(2*len/3))),median(feats10(round(len/3):end))];
features10=[features10,abs(features10-median(feats10))];
features11=[median(feats11(1:round(len/3))),median(feats11(round(len/3):round(2*len/3))),median(feats11(round(len/3):end))];
features11=[features11,abs(features11-median(feats11))];
features12=[median(feats12(1:round(len/3))),median(feats12(round(len/3):round(2*len/3))),median(feats12(round(len/3):end))];
features12=[features12,abs(features12-median(feats12))];
features13=[median(feats13(1:round(len/3))),median(feats13(round(len/3):round(2*len/3))),median(feats13(round(len/3):end))];
features13=[features13,abs(features13-median(feats13))];
features14=[median(feats14(1:round(len/3))),median(feats14(round(len/3):round(2*len/3))),median(feats14(round(len/3):end))];
features14=[features14,abs(features14-median(feats14))];
features15=[median(feats15(1:round(len/3))),median(feats15(round(len/3):round(2*len/3))),median(feats15(round(len/3):end))];
features15=[features15,abs(features15-median(feats15))];
features16=[median(feats16(1:round(len/3))),median(feats16(round(len/3):round(2*len/3))),median(feats16(round(len/3):end))];
features16=[features16,abs(features16-median(feats16))];
features17=[median(feats17(1:round(len/3))),median(feats17(round(len/3):round(2*len/3))),median(feats17(round(len/3):end))];
features17=[features17,abs(features17-median(feats17))];
features18=[median(feats18(1:round(len/3))),median(feats18(round(len/3):round(2*len/3))),median(feats18(round(len/3):end))];
features18=[features18,abs(features18-median(feats18))];
features19=[median(feats19(1:round(len/3))),median(feats19(round(len/3):round(2*len/3))),median(feats19(round(len/3):end))];
features19=[features19,abs(features19-median(feats19))];
features20=[median(feats20(1:round(len/3))),median(feats20(round(len/3):round(2*len/3))),median(feats20(round(len/3):end))];
features20=[features20,abs(features20-median(feats20))];
features21=[median(feats21(1:round(len/3))),median(feats21(round(len/3):round(2*len/3))),median(feats21(round(len/3):end))];
features21=[features21,abs(features21-median(feats21))];
features22=[median(feats22(1:round(len/3))),median(feats22(round(len/3):round(2*len/3))),median(feats22(round(len/3):end))];
features22=[features22,abs(features22-median(feats22))];
features23=[median(feats23(1:round(len/3))),median(feats23(round(len/3):round(2*len/3))),median(feats23(round(len/3):end))];
features23=[features23,abs(features23-median(feats23))];
features24=[median(feats24(1:round(len/3))),median(feats24(round(len/3):round(2*len/3))),median(feats24(round(len/3):end))];
features24=[features24,abs(features24-median(feats24))];
features25=[median(feats25(1:round(len/3))),median(feats25(round(len/3):round(2*len/3))),median(feats25(round(len/3):end))];
features25=[features25,abs(features25-median(feats25))];
features26=[median(feats26(1:round(len/3))),median(feats26(round(len/3):round(2*len/3))),median(feats26(round(len/3):end))];
features26=[features26,abs(features26-median(feats26))];

msent=feats24;
msent=sort(feats24);
msent=[msent(1) msent(2) msent(end) msent(end-1) mean(msent) std(msent)];
% x=yp';
% level = 5;
% [c,l] = wavedec(x,level,'db4');
% 
% a5 = appcoef(c,l,'db4');
% det5 = detcoef(c,l,level);
% det4 = detcoef(c,l,level-1);
% det3 = detcoef(c,l,level-2);
% %--------------------------------------------------------------------------
% f7 = log2(var(det3));                                       %7
% %--------------------------------------------------------------------------
% xx = downsample(x, 16);
% [f14, ~, f15] = Spectral(xx);                               %13 14
% %--------------------------------------------------------------------------
% [lp] = lpc(double(x),10);
% f161821232425 = lp([2 4 7 9 10 11]);                        %16 18 21 23 24 25
% %--------------------------------------------------------------------------
% N = length(x);
% xdft = fft(x);
% xdft = xdft(1:round(N/2)+1);
% psdx = (1/(2*pi*N)) * abs(xdft).^2;
% psdx(2:end-1) = 2*psdx(2:end-1);
% Fs=300;
% f = Fs*(0:(N/2))/N;
% % save('local1','f','psdx')
% frequency_centroid = (sum(f(:).*(psdx(1:length(f)).^2)))/(sum(psdx(1:length(f)).^2));   %26
% %-------------------------------------------------------------
% nbins = 50;
% [counts,centers] = hist(x,nbins);
% thr = mean((diff(centers))/2);
% centers(2,:) = centers + thr;
% for i=1:length(x)
%     p1 = find(x(i)<=centers(2,:));
%     if ~isempty(p1)
%         p(i) = counts(p1(1));
%     else
%         p(i) = counts(end);
%     end
% end
% p = p / length(x);
% q = 2;
% Shannon = -1*sum(p.*log(p));                                %29
% Tsallis = (1/(q-1))*sum(1 - p.^q);                          %31
% %-------------------------------------------------------------
% clear p counts centers thr
% [counts,centers] = hist(a5,nbins);
% thr = mean((diff(centers))/2);
% centers(2,:) = centers + thr;
% for i=1:length(x)
%     p1 = find(x(i)<=centers(2,:));
%     if ~isempty(p1)
%         p(i) = counts(p1(1));
%     else
%         p(i) = counts(end);
%     end
% end
% p = p / length(x);
% Shannon_a5 = -1*sum(p.*log(p));                             %32
% %-------------------------------------------------------------
% clear p counts centers thr
% [counts,centers] = hist(det5,nbins);
% thr = mean((diff(centers))/2);
% centers(2,:) = centers + thr;
% for i=1:length(x)
%     p1 = find(x(i)<=centers(2,:));
%     if ~isempty(p1)
%         p(i) = counts(p1(1));
%     else
%         p(i) = counts(end);
%     end
% end
% p = p / length(x);
% q = 2;
% Renyi_d5 = (1/(q-1))*log(sum(p.^q));                        %36
% %-------------------------------------------------------------´
% clear p counts centers thr
% [counts,centers] = hist(det4,nbins);
% thr = mean((diff(centers))/2);
% centers(2,:) = centers + thr;
% for i=1:length(x)
%     p1 = find(x(i)<=centers(2,:));
%     if ~isempty(p1)
%         p(i) = counts(p1(1));
%     else
%         p(i) = counts(end);
%     end
% end
% p = p / length(x);
% Shannon_d4 = -1*sum(p.*log(p));                             %38
%% ------------------------------------------------------------------------
% featuresMorteza = [f7 f14 f15 f161821232425 frequency_centroid,...
%     Shannon Tsallis Shannon_a5 Renyi_d5 Shannon_d4];
% size(featuresMorteza)

featuresMorteza=zeros(1,15);
features=[features1,features2,features3,features4,features5,features6,features7,features8,features9,...
    features10,features11,features12,features13,features14,features15,features16,features17,features18,...
    features19,features20,features21,features22,features23,features24,features25,features26,...
    features1a,features2a,features3a,features4a,features5a,features6a,features7a,featuresMed,featuresKurt,featuresSkew];
% size(features)
features=[features,featuresMorteza,msent];
% features=[features1,features2,features3,features4,features5,features6,features7,features8,features9,features1a,features2a,features3a,features4a,features5a,features6a,features7a,features8,features9];
% features=[features1,features2,features3,features4,features5,features6,features7,features9,features1a,features2a,features3a,features4a,features5a,features6a,features7a];
