for l=10:30 clearvars cemCount tenPercentCountVec noisyVec adjustedCountVec altCountVec length size for k=1:30 clearvars size t = linspace(-l*pi,l*pi,200); signal = sin(t); %original signal sigma = 0.2; %noise standard deviation noisy = signal + sigma*randn(size(signal)); %noisy signal noisy10=(noisy.*10)+100; noisy10=round(noisy10); a=noisy10; standardDev=std(a); clearvars count idx clearvars closestTopVec closestBottomVec [m,n]=size(noisy10); x=[1:n]; x=x'; noisyShift=noisy10'; f=fit(x,noisyShift,'smoothingspline'); y=feval(f,x); noisyGauss=y; [val,loc,width]=findpeaks(noisyGauss); [m,n]=size(val); clearvars cutOffVec size clearvars completePeakList completeTroughList idx incrementCountVec cutOffGaussVec finalIncrementCountVec a(isnan(a(:,1)),:)=[]; [m,n]=size(a); if m>n a=a'; end %%%% ESTABLISH CUTOFFS AND PEAK/TROUGH DATA%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %work out the length of cutoffs testVec=a; [m,n]=size(testVec); if m>n cutOff=round(m/7); elseif n>m cutOff=round(n/7); end %generate cutoffs cutOffVec(1,:)=noisy10(1:cutOff); for i=2:6 cutOffVec(i,:)= noisy10((cutOff*(i-1))+1:(cutOff.*i)); end if m>n cutOffVec(7,:)=noisy10((cutOff.*6)+1:(m)); elseif n>m size= size(noisy10((cutOff.*6)+1:(n))); if size(2)9 topCutOff=cutOffMeanVec(i)+round(cutOffStDevVec(i)/2); elseif standardDev topCutOff=cutOffMeanVec(i)+round(cutOffStDevVec(i)); end topCutOffVec(i)=topCutOff; end clearvars cutOffLength topCutOff %create cutoff vectors of values below the bottom standard deviation for i=1:7 cutOffLength=cutOffGaussVec(i,:); if standardDev>9 bottomCutOff=cutOffMeanVec(i)-round(cutOffStDevVec(i)/2); elseif standardDev<9 bottomCutOff=cutOffMeanVec(i)-round(cutOffStDevVec(i)/2); end bottomCutOffVec(i)=bottomCutOff; end clearvars cutOffLength bottomCutOff topCutOffVec=cutOffGaussVec; bottomCutOffVec=cutOffGaussVec; %change all of the values below the cutoff mean into zero for the top cutoff vectors for i= 1:7 topCutOff=topCutOffVec(i,:); cutOffMean=cutOffMeanVec(i); topCutOff(topCutOff0 topValVec(i,1:p)=realTopVal1; elseif p<1 topValVec(i,1)=1; end [o,p]=size(realTopLoc1); if p>0 topLocVec(i,1:p)=realTopLoc1; lastPeakVec(i)=realTopLoc1(end); elseif p<1 if cutOffGaussVec(i,1)>cutOffGaussVec(i,end) topLocVec(i,1)=2; lastPeakVec(i)=2; elseif cutOffGaussVec(i,1)0 topWidthVec(i,1:p)=realTopWidth1; elseif p<1 topWidthVec(i,1)=1; end end clearvars topValSumVec topLocSumVec topWidthSumVec %%%%%%% %generate vectors for the value, location and width of troughs %%%%%%% for i=1:7 bottomCutOff=bottomCutOffVec(i,:); [realBottomVal1,realBottomLoc1,realBottomWidth1]=findpeaks(bottomCutOff); [o,p]=size(realBottomVal1); bottomValSumVec(i)=p; [o,p]=size(realBottomLoc1); bottomLocSumVec(i)=p; [o,p]=size(realBottomWidth1); bottomWidthSumVec(i)=p; end clearvars o p bottomValVec=zeros(7,(max(bottomValSumVec))); bottomLocVec=zeros(7,(max(bottomLocSumVec))); bottomWidthVec=zeros(7,(max(bottomWidthSumVec))); for i=1:7 bottomCutOff=bottomCutOffVec(i,:); [realBottomVal1,realBottomLoc1,realBottomWidth1]=findpeaks(bottomCutOff); [o,p]=size(realBottomVal1); if p>0 bottomValVec(i,1:p)=realBottomVal1; elseif p<1 bottomValVec(i,1)=1; end [o,p]=size(realBottomLoc1); if p>0 bottomLocVec(i,1:p)=realBottomLoc1; lastTroughVec(i)=realBottomLoc1(end); elseif p<1 if cutOffGaussVec(i,1)>cutOffGaussVec(i,end) bottomLocVec(i,1)=cutOff; lastTroughVec(i)=cutOff; elseif cutOffGaussVec(i,1)0 bottomWidthVec(i,1:p)=realBottomWidth1; elseif p<1 bottomWidthVec(i,1)=1; end end clearvars o p bottomValSumVec bottomLocSumVec bottomWidthSumVec %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%Start increment counting%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% incrementCountVec=zeros(7,50); %find position of last peak/trough in first cutoff for i=1:7 peakLocations=topLocVec(i,:); peakLocations(peakLocations<1)=[]; firstPeakLocVec(i)=peakLocations(1); lastPeakLocVec(i)=peakLocations(end); %%% troughLocations=bottomLocVec(i,:); troughLocations(troughLocations<1)=[]; firstTroughLocVec(i)=troughLocations(1); lastTroughLocVec(i)=troughLocations(end); if lastTroughLocVec(i)>lastPeakLocVec(i) lastElementOfCutOff(i)=-1; elseif lastPeakLocVec(i)>lastTroughLocVec(i) lastElementOfCutOff(i)=1; end end clearvars peakLocations troughLocations completePeakList = zeros(7,100); completeTroughList = zeros(7,100); clearvars completePeakList completeTroughList %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for j=1:7 clearvars closestTopVec closestBottomVec idx lastIdx val closest topValueVec bottomValueVec target clearvars peakCountVec troughCountVec peakTestVec troughTestVec peakValVec topPeak = topLocVec(j,:); topPeak(topPeak<1)=[]; bottomTrough = bottomLocVec(j,:); bottomTrough(bottomTrough<1)=[]; if min((max(find(topPeak))),(max(find(bottomTrough))))>1 if topPeak(1)1 if val>closest idx=idx-1; val=topPeak(idx); end end b=size(topPeak); if val>topPeak(1) for i=1:b(end) if topPeak(i)0 for b=1:max(find(bottomTrough)) if bottomTrough(b)>closest if bottomTrough(b)peakTestVec(1) troughTestVec(b)=0; end elseif bottomTrough(b)0 troughTestVec=[closest troughTestVec]; troughTestVec(troughTestVec<1)=[]; if sum(troughTestVec)>0 for b=1:max(find(troughTestVec)) troughValVec(b)=cutOffGaussVec(j,(troughTestVec(b))); end end [value location]=min(troughValVec); closest=troughTestVec(location); end closestBottomVec(1)=closest; closestTopVec(1)=val; clearvars value location peakTestVec troughTestVec peakValVec troughValVec elseif bottomTrough(1)1 if closest>val idx=idx-1; closest=bottomTrough(idx); end end clearvars firstTroughVal b=size(bottomTrough); for i=1:b(end) if bottomTrough(i)0 for b=1:max(find(topPeak)) if topPeak(b)>val if topPeak(b)<(troughTestVec(1)) peakTestVec(b)=topPeak(b); elseif topPeak>(b)(troughTestVec(1)) peakTestVec(b)=0; end elseif topPeak(b)0 for b=1:max(find(peakTestVec)) peakValVec(b)=cutOffGaussVec(j,(peakTestVec(b))); end %peakValVec=[cutOffGaussVec(j,val) peakValVec]; [value location]=max(peakValVec); val=peakTestVec(location); closestTopVec(1) = val; closestBottomVec(1) = closest; clearvars value location peakTestVec troughTestVec peakValVec troughValVec elseif sum(peakTestVec)<0.5 closestTopVec(1) = val; closestBottomVec(1) = closest; clearvars value location peakTestVec troughTestVec peakValVec peakValVec end end lastIdx=idx; [n,m]=size(topPeak); [h,g]=size(bottomTrough); if min(m,g)<3 if g<3 if closestTopVec(1)1 if m<3 if (topPeak(2))>(closestBottomVec(1)) closestTopVec(2)=topPeak(2); end elseif m>2 clearvars firstPeakTestVec midPeakTestVec endPeakTestVec clearvars endPeakValVec firstPeakValVec midPeakValVec for b=1:max(find(topPeak)) if topPeak(b)bottomTrough(1) firstPeakTestVec(b)=0; if topPeak(b)bottomTrough(2) firstPeakTestVec(b)=0; midPeakTestVec(b)=0; endPeakTestVec(b)=topPeak(b); end end end firstPeakTestVec(firstPeakTestVec<0.5)=[]; midPeakTestVec(midPeakTestVec<0.5)=[]; endPeakTestVec(endPeakTestVec<0.5)=[]; firstPeakValVec = 0; midPeakValVec = 0; endPeakValVec = 0; for b=1:max(find(firstPeakTestVec)) firstPeakValVec(b)=cutOffGaussVec(j,(firstPeakTestVec(b))); end if sum(firstPeakValVec)>0 [value location] = max(firstPeakValVec); closestTopVec(1)=topPeak(location); end for b=1:max(find(midPeakTestVec)) midPeakValVec(b)=cutOffGaussVec(j,(midPeakTestVec(b))); end if sum(midPeakValVec)>0 [value location] = max(midPeakValVec); topPeakTest=topPeak; topPeakTest(topPeakTest>(midPeakTestVec(location)))=[]; closestTopVec(2)=max(topPeakTest); end for b=1:max(find(endPeakTestVec)) endPeakValVec(b)=cutOffGaussVec(j,(endPeakTestVec(b))); end if sum(endPeakValVec)>0 [value location] = max(endPeakValVec); topPeakTest=topPeak; topPeakTest(topPeakTest>(endPeakTestVec(location)))=[]; closestTopVec(3)=max(topPeakTest); end if bottomTrough(end)>closestTopVec(end) closestBottomVec(end)=bottomTrough(end); end end end if g>1 if bottomTrough(2)>topPeak(2) closestBottomVec(2)=bottomTrough(2); end end elseif closestBottomVec(1)1 if (bottomTrough(2))>(closestTopVec(1)) closestBottomVec(2)=bottomTrough(2); end end if m>1 if topPeak(2)>bottomTrough(2) closestTopVec(2)=topPeak(2); end end end end elseif min(m,g)>2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if closestBottomVec(1)>closestTopVec(1) [n,m]=size(bottomTrough); [h,g]=size(topPeak); for i=2:50 for b=1:g if topPeak(b)>closest peakCountVec(b)=topPeak(b); elseif topPeak(b)0 closestTopVec(i)=peakCountVec(min(find(peakCountVec))); val=closestTopVec(i); for b=1:m if bottomTrough(b)>val troughCountVec(b)=bottomTrough(b); elseif bottomTrough(b)0 peakTestVec=0; for b=1:g if topPeak(b)>val if topPeak(b)troughCountVec(min(find(troughCountVec))) peakTestVec(b)=0; end elseif topPeak(b)>val peakTestVec(b)=0; end end if sum(peakTestVec)>0 peakTestVec(peakTestVec<0.5)=[]; if sum(peakTestVec)>0 peakTestVec=[val peakTestVec]; for b=1:max(find(peakTestVec)) peakTestVal(b)=cutOffGaussVec(j,peakTestVec(b)); end [value location]=max(peakTestVal); val=peakTestVec(location); closestTopVec(i)=val; peakCountVec(peakCountVec0 %troughCountVec(troughCountVec<0.5)=[]; closestBottomVec(i)=troughCountVec(min(find(troughCountVec))); closest=closestBottomVec(i); if (min(find(peakCountVec)))<(max(find(peakCountVec))) target=peakCountVec((min(find(peakCountVec)))+1); if closest>target if max((find(peakCountVec)))>(min((find(peakCountVec))))+1 target=peakCountVec((min(find(peakCountVec)))+2); elseif max((find(peakCountVec)))<(min((find(peakCountVec))))+2 target=peakCountVec(end); end end elseif (min(find(peakCountVec)))>((max(find(peakCountVec)))-1) target= peakCountVec(end); end for b=1:m if bottomTrough(b)>closest if bottomTrough(b)target troughTestVec(b)=0; end elseif bottomTrough(b)0 troughTestVec=[closest troughTestVec]; for b=1:max(find(troughTestVec)) troughTestVal(b)=cutOffGaussVec(j,troughTestVec(b)); end [value location]=min(troughTestVal); closest=troughTestVec(location); closestBottomVec(i)=closest; clearvars troughTestVec troughTestVal location value clearvars troughTestVec peakTestVec elseif sum(troughTestVec)<0.5 troughCountVec(troughCountVec<0.5)=[]; closestBottomVec(i)=min(troughCountVec); closest=closestBottomVec(i); clearvars value location val troughCountVec peakCountVec peakTestVal troughTestVal clearvars troughTestVec peakTestVec end elseif (sum(troughCountVec))<1 closest=100; clearvars troughCountVec peakCountVec value location end elseif (sum(troughCountVec))<0.5 peakCountVec(peakCountVec<0.5)=[]; for b=1:max(find(peakCountVec)) peakTestVal(b)=cutOffGaussVec(j,(peakCountVec(b))); end [value location]=max(peakTestVal); closestTopVec(end)=round(peakCountVec(location)); closest=100; clearvars value location val troughCountVec peakCountVec peakTestVal troughTestVal clearvars troughTestVec peakTestVec end end end elseif closestTopVec(1)>closestBottomVec(1) [n,m]=size(bottomTrough); [h,g]=size(topPeak); for i=2:50 for b=1:m if bottomTrough(b)>val troughCountVec(b)=bottomTrough(b); elseif bottomTrough(b)0 closestBottomVec(i)=troughCountVec(min(find(troughCountVec))); closest=closestBottomVec(i); for b=1:g if topPeak(b)>closest peakCountVec(b)=topPeak(b); elseif topPeak(b)0 for b=1:m if bottomTrough(b)>closest if bottomTrough(b)peakCountVec(min(find(peakCountVec))) troughTestVec(b)=0; end elseif bottomTrough(b)0 troughTestVec(troughTestVec<0.5)=[]; if sum(troughTestVec)>0 troughTestVec=[closest troughTestVec]; for b=1:max(find(troughTestVec)) troughTestVal(b)=cutOffGaussVec(j,troughTestVec(b)); end [value location]=min(troughTestVal); closest=troughTestVec(location); closestBottomVec(i)=closest; troughCountVec(troughCountVec0 closestTopVec(i)=peakCountVec(min(find(peakCountVec))); val=closestTopVec(i); if (min(find(troughCountVec)))<(max(find(troughCountVec))) target=troughCountVec((min(find(troughCountVec)))+1); if val>target if max((find(troughCountVec)))>(min((find(troughCountVec))))+1 target=troughCountVec((min(find(troughCountVec)))+2); elseif max((find(troughCountVec)))<(min((find(troughCountVec))))+2 target=troughCountVec(end); end end elseif (min(find(troughCountVec)))>(max(find(troughCountVec)))-1 target = troughCountVec(end); end for b=1:g if topPeak(b)>val if topPeak(b)target peakTestVec(b)=0; end elseif topPeak(b)0 peakTestVec=[val peakTestVec]; for b=1:max(find(peakTestVec)) peakTestVal(b)=cutOffGaussVec(j,peakTestVec(b)); end [value location]=max(peakTestVal); val=peakTestVec(location); closestTopVec(i)=val; clearvars value location closest troughCountVec peakCountVec peakTestVal troughTestVal clearvars troughTestVec peakTestVec elseif sum(peakTestVec)<0.5 peakCountVec(peakCountVec<0.5)=[]; closestTopVec(i)=min(peakCountVec); val=closestTopVec(i); clearvars value location closest troughCountVec peakCountVec peakTestVal troughTestVal clearvars troughTestVec peakTestVec end elseif (sum(peakCountVec))<1 val=100; clearvars peakCountVec troughCountVec value location end elseif (sum(peakCountVec))<0.5 troughCountVec(troughCountVec<0.5)=[]; for b=1:max(find(troughCountVec)) troughTestVal(b)=cutOffGaussVec(j,(troughCountVec(b))); end [value location]=min(troughTestVal); closestBottomVec(i)=round(troughCountVec(location)); val=100; clearvars value location closest troughCountVec peakCountVec peakTestVal troughTestVal clearvars troughTestVec peakTestVec end end end end clearvars firstDiffVec secondDiffVec minimumDiff diffValue minimumDiff for b=1:min(max(find(closestTopVec)),max(find(closestBottomVec))) peakValue(b)=cutOffGaussVec(j,closestTopVec(b)); troughValue(b)=cutOffGaussVec(j,closestBottomVec(b)); diffValue(b)=peakValue(b)-troughValue(b); end if max(find(closestTopVec))>6 if max(find(closestTopVec))>8 minimumDiff=(mean(diffValue))-15; elseif max(find(closestTopVec))<9 if standardDev<9 minimumDiff=(mean(diffValue))-10; elseif standardDev>9 minimumDiff=(mean(diffValue))-20; end end elseif max(find(closestTopVec))<7 if standardDev<9 minimumDiff=(mean(diffValue))-5; elseif standardDev>9 minimumDiff=(mean(diffValue))-10; end end if min((max(find(closestTopVec))),(max(find(closestBottomVec))))>1 if closestTopVec(1)(max(find(closestBottomVec))) for b=1:min((max(find(closestTopVec))),(max(find(closestBottomVec)))) firstDiffVec(b)=closestBottomVec(b)-closestTopVec(b); secondDiffVec(b)=closestTopVec(b+1)-closestBottomVec(b); end for b=1:max(find(firstDiffVec)) if firstDiffVec(b)<4 if diffValue(b)< minimumDiff if closestBottomVec(b)>0 closestBottomVec(b)=0; if secondDiffVec(b)<4 if secondDiffVec(b)>0 if closestTopVec(b)>0 if diffValue(b)< minimumDiff if cutOffGaussVec(j,(closestTopVec(b)))>cutOffGaussVec(j,(closestTopVec(b+1))) closestTopVec(b+1)=0; elseif cutOffGaussVec(j,(closestTopVec(b)))0 if closestTopVec(b)>0 if diffValue(b)< minimumDiff closestTopVec(b+1)=0; closestBottomVec(b)=0; end end end end end elseif (max(find(closestTopVec)))<(max(find(closestBottomVec)))+1 if (max(find(closestTopVec)))>(max(find(closestBottomVec)))-1 for b=1:(min(max(find(closestTopVec))))-1 firstDiffVec(b)=closestBottomVec(b)-closestTopVec(b); secondDiffVec(b)=closestTopVec(b+1)-closestBottomVec(b); end for b=1:max(find(firstDiffVec)) if firstDiffVec(b)<4 if diffValue(b)< minimumDiff if closestBottomVec(b)>0 closestBottomVec(b)=0; if secondDiffVec(b)<4 if secondDiffVec(b)>0 if closestTopVec(b)>0 if diffValue(b)< minimumDiff if cutOffGaussVec(j,(closestTopVec(b)))>cutOffGaussVec(j,(closestTopVec(b+1))) closestTopVec(b+1)=0; elseif cutOffGaussVec(j,(closestTopVec(b)))0 if closestTopVec(b)>0 if diffValue(b)< minimumDiff closestTopVec(b+1)=0; closestBottomVec(b)=0; end end end end end end end elseif closestBottomVec(1)(max(find(closestTopVec))) for b=1:min((max(find(closestTopVec))),(max(find(closestBottomVec)))) firstDiffVec(b)=closestTopVec(b)-closestBottomVec(b); secondDiffVec(b)=closestBottomVec(b+1)-closestTopVec(b); end for b=1:max(find(firstDiffVec)) if firstDiffVec(b)<4 if diffValue(b)< minimumDiff if closestBottomVec(b)>0 closestBottomVec(b)=0; if secondDiffVec(b)<4 if secondDiffVec(b)>0 if closestTopVec(b)>0 if diffValue(b)< minimumDiff if max(find(closestTopVec))>b if cutOffGaussVec(j,(closestTopVec(b)))>cutOffGaussVec(j,(closestTopVec(b+1))) closestTopVec(b+1)=0; elseif cutOffGaussVec(j,(closestTopVec(b)))0 if closestBottomVec(b)>0 if diffValue(b)< minimumDiff if max(find(closestTopVec))>2 closestTopVec(b+1)=0; closestTopVec(b)=0; elseif max(find(closestTopVec))<3 closestTopVec(b)=0; end end end end end end elseif (max(find(closestBottomVec)))<(max(find(closestTopVec)))+1 if (max(find(closestBottomVec)))>(max(find(closestTopVec)))-1 for b=1:(min(max(find(closestTopVec))))-1 firstDiffVec(b)=closestTopVec(b)-closestBottomVec(b); secondDiffVec(b)=closestBottomVec(b+1)-closestTopVec(b); end for b=1:max(find(firstDiffVec)) if firstDiffVec(b)<4 if diffValue(b)< minimumDiff if closestBottomVec(b)>0 closestBottomVec(b)=0; if secondDiffVec(b)<4 if secondDiffVec(b)>0 if closestTopVec(b)>0 if diffValue(b)< minimumDiff if cutOffGaussVec(j,(closestTopVec(b)))>cutOffGaussVec(j,(closestTopVec(b+1))) closestTopVec(b+1)=0; elseif cutOffGaussVec(j,(closestTopVec(b)))0 if closestBottomVec(b)>0 if diffValue(b)< minimumDiff closestBottomVec(b+1)=0; closestTopVec(b)=0; end end end end end end end end end clearvars firstDiffVec secondDiffVec peakValue troughValue diffValue closestTopVec(closestTopVec<0.5)=[]; closestBottomVec(closestBottomVec<0.5)=[]; [m,n]=size(closestTopVec); [g,h]=size(closestBottomVec); clearvars topValueVec bottomValueVec diffVec for b=1:n topValueVec(b)=cutOffGaussVec(j,(closestTopVec(b))); end for b=1:h bottomValueVec(b)=cutOffGaussVec(j,(closestBottomVec(b))); end for b=1:(min(n,h)) diffVec(b)=topValueVec(b)-bottomValueVec(b); end %if closestTopVec(1)0 if n>h for b=1:h if (topValueVec(b+1)-bottomValueVec(b))<(mean(diffVec))/2 closestTopVec(b+1)=0; closestBottomVec(b)=0; topValueVec(b+1)=0; bottomValueVec(b)=0; end end elseif h>n for b=2:n if (topValueVec(b)-bottomValueVec(b-1))<(mean(diffVec))/2 closestTopVec(b)=0; closestBottomVec(b-1)=0; topValueVec(b)=0; bottomValueVec(b-1)=0; end end end elseif ((n-h)^2)<0.5 if closestTopVec(1)closestBottomVec(1) for b=1:n-1 if ((topValueVec(b)-bottomValueVec(b))1 for b=1:(min(n,h)) if (topValueVec(b)-bottomValueVec(b))< (mean(diffVec))/2 closestTopVec(b)=0; closestBottomVec(b)=0; end end end closestTopVec(closestTopVec<0.5)=[]; closestBottomVec(closestBottomVec<0.5)=[]; end if closestBottomVec(end)closestBottomVec(1) for b=i:min((max(find(closestTopVec))),(max(find(closestBottomVec)))) if closestBottomVec(b)>closestTopVec(b) peakToRemove(b)=0; elseif closestBottomVec(b)closestTopVec(1) for b=i:min((max(find(closestTopVec))),(max(find(closestBottomVec)))) if closestBottomVec(b)closestTopVec(b) peakToRemove(b)=closestTopVec(b); end end end if peakToRemove(end)<0.5 peakToRemove(peakToRemove<0.5)=[]; if lastPeakRemovedVec(i-1)closestTopVec(i-1) %peakToRemove(peakToRemove<0.5)=[]; if cutOffGaussVec(j,(closestTopVec((max(find(peakToRemove)))+1)))cutOffGaussVec(j,(closestTopVec(max(find(peakToRemove))))) %closestTopVec(max(find(peakToRemove)))=0; peakToRemoveVec(max(find(peakToRemove)))=1; end end end end lastPeakRemovedVec(i)=peakToRemove(end); end if peakToRemoveVec(end)<1 for i = 1:(max(find(peakToRemoveVec))-1) if peakToRemoveVec(i)>0 closestTopVec(i)=0; end end elseif peakToRemoveVec(end)>0 for i = 1:(max(find(peakToRemoveVec))) if peakToRemoveVec(i)>0 closestTopVec(i)=0; end end end closestTopVec(closestTopVec<0.5)=[]; clearvars peakToRemoveVec peakToRemoveVec=0; for i=2:min((max(find(closestTopVec))),(max(find(closestBottomVec)))) if closestTopVec(1)1 peakToRemoveVec(i)=1; elseif location<2 peakToRemoveVec(i-1)=1; end end elseif closestBottomVec(1)1 peakToRemoveVec(i)=1; elseif location<2 peakToRemoveVec(i-1)=1; end end end end if sum(peakToRemoveVec)>0 for i=max(find(peakToRemoveVec)) if peakToRemoveVec(i)>0 closestTopVec(i)=0; end end end closestTopVec(closestTopVec<0.5)=[]; clearvars peakToRemoveVec [q,r] = size(closestTopVec); completePeakList(j,1:r)=closestTopVec; peakListSize(j)=r; [q,r] = size(closestBottomVec); completeTroughList(j,1:r)=closestBottomVec; troughListSize(j)=r; elseif min((max(find(topPeak))),(max(find(bottomTrough))))<2 closestTopVec=topPeak; closestBottomVec=bottomTrough; if min((max(find(closestTopVec))),(max(find(closestBottomVec))))>1 if closestTopVec(1)1 if (topPeak(2))>(closestBottomVec(1)) closestTopVec(2)=topPeak(2); end end if g>1 if bottomTrough(2)>topPeak(2) closestBottomVec(2)=bottomTrough(2); end end elseif closestBottomVec(1)1 if (bottomTrough(2))>(closestTopVec(1)) closestBottomVec(2)=bottomTrough(2); end end if m>1 if topPeak(2)>bottomTrough(2) closestTopVec(2)=topPeak(2); end end end end if closestBottomVec(1)>closestTopVec(end) if max(find(closestTopVec))>1 for b=1:max(find(closestTopVec)) peakValVec(b)=cutOffGaussVec(j,(closestTopVec(b))); end [value location]=max(peakValVec); closestTopVec(1:(location-1))=0; closestTopVec((location+1):end)=0; closestTopVec(closestTopVec<0.5)=[]; end if max(find(closestBottomVec))>1 for b=1:max(find(closestBottomVec)) peakValVec(b)=cutOffGaussVec(j,(closestBottomVec(b))); end [value location]=max(peakValVec); closestBottomVec(1:(location-1))=0; closestBottomVec((location+1):end)=0; closestBottomVec(closestBottomVec<0.5)=[]; end elseif closestTopVec(1)>closestBottomVec(end) if max(find(closestTopVec))>1 for b=1:max(find(closestTopVec)) peakValVec(b)=cutOffGaussVec(j,(closestTopVec(b))); end [value location]=max(peakValVec); closestTopVec(1:(location-1))=0; closestTopVec((location+1):end)=0; closestTopVec(closestTopVec<0.5)=[]; end if max(find(closestBottomVec))>1 for b=1:max(find(closestBottomVec)) peakValVec(b)=cutOffGaussVec(j,(closestBottomVec(b))); end [value location]=max(peakValVec); closestBottomVec(1:(location-1))=0; closestBottomVec((location+1):end)=0; closestBottomVec(closestBottomVec<0.5)=[]; end end [q,r] = size(closestTopVec); completePeakList(j,1:r)=closestTopVec; peakListSize(j)=r; [q,r] = size(closestBottomVec); completeTroughList(j,1:r)=closestBottomVec; troughListSize(j)=r; end end completePeakList(:,((max(peakListSize))+1):end)=[]; completeTroughList(:,((max(troughListSize))+1):end)=[]; clearvars q r closestBottomVec closestTopVec val closest idx m g h n lastVal ans c VERYFirstPixel=cutOffGaussVec(1,1); for j=1:7 clearvars troughCountFinal peakCountFinal incrementCount peakKnockOutVec troughKnockOutVec peakList=completePeakList(j,:); peakList(peakList<1)=[]; troughList=completeTroughList(j,:); troughList(troughList<1)=[]; [m,n]=size(peakList); [g,h]=size(troughList); for i=1:(n-1) thisPeakVal=peakList(i); nextPeakVal=peakList(i+1); if thisPeakVal>nextPeakVal-1; peakKnockOutVec(i)=i; elseif thisPeakValnextTroughVal-1; troughKnockOutVec(i)=i; elseif thisTroughVal0 peakList(i)=0; end end for i=1:(h-1) if troughKnockOutVec(i)>0 troughList(i)=0; end end peakList(peakList<1)=[]; troughList(troughList<1)=[]; [m,n]=size(peakList); [g,h]=size(troughList); clearvars peakValList for i=1:n peakValList(i)=cutOffGaussVec(j,(peakList(i))); end clearvars troughValList for i=1:h troughValList(i)=cutOffGaussVec(j,(troughList(i))); end clearvars peakKnockOutVec troughKnockOutVec lastValToCheck=min(n,h); for i=1:lastValToCheck-1 if troughList(1)2 peakCountFinal=1; troughCountFinal=1; incrementCount=1; elseif difference<3 incrementCount=0.5; end elseif h>1 if peakList(1)2 peakCountFinal=1; incrementCount=0.5; end difference=troughList(2)-peakList(1); if difference>2 troughCountFinal=1; incrementCount=0.5; end lastPixel=cutOffGaussVec(j,end); if lastPixel>(cutOffMeanVec(1)+cutOffStDevVec(1)) incrementCount=[incrementCount 0.5]; elseif lastPixel<((cutOffMeanVec(1)+cutOffStDevVec(1))+1) if lastPixel>(cutOffMeanVec(1)-cutOffStDevVec(1)) incrementCount=[incrementCount 0.5]; end end elseif troughList(1)2 peakCountFinal=1; incrementCount=0.5; end difference=troughList(2)-peakList(1); if difference>0 if difference>2 peakCountFinal=1; incrementCount=0.5; end lastPixel=cutOffGaussVec(j,end); if lastPixel<(cutOffMeanVec(1)-cutOffStDevVec(1)) incrementCount=[incrementCount 0.5]; elseif lastPixel<((cutOffMeanVec(1)+cutOffStDevVec(1))+1) if lastPixel>(cutOffMeanVec(1)-cutOffStDevVec(1)) incrementCount=[incrementCount 0.5]; end end elseif difference<0 for b=1:max(find(troughList)) if troughList(b)peakList(end) firstTroughVec(b)=0; end end firstTroughVec(firstTroughVec<0.5)=[]; for b=1:max(find(firstTroughVec)) firstTroughVal(b)=cutOffGaussVec(j,(firstTroughVec(b))); end for b=1:max(find(peakList)) firstPeakVal(b)=cutOffGaussVec(j,(peakList(b))); end if ((max(firstPeakVal))-(min(firstTroughVal)))>2 incrementCount=1; end end end end elseif min(n,h)>1 if troughList(1)2 troughCountFinal(1)=1; elseif troughSide<3 troughCountFinal(1)=0.5; end if n>h-2 for i=2:h trough=troughList(i); lastTrough=troughList(i-1); if peakList(i-1)2 peakCountFinal(i-1)=1; end end end elseif n2 peakCountFinal(i-1)=1; end end end end for i=2:n peak=peakList(i); lastPeak=peakList(i-1); if (peak-lastPeak)>2 troughCountFinal(i)=1; if troughValList(i)<(mean(troughValList)) if standardDev>9 if (peak-lastPeak)<(cutOff/h)/3 troughCountFinal(i)=0; peakCountFinal(i)=0; end elseif standardDev<9 peakCountFinal(i)=1; end end end end if peakList(end)>troughList(end) if troughCountFinal(1)<1 peakCountFinal(end)=0.5; end end if n>h if n>sum(peakCountFinal) if (peakList(end)-troughList(end))>2 peakCountFinal=[peakCountFinal 1]; end end elseif hsum(troughCountFinal) if (troughList(end)-peakList(end))>2 troughCountFinal=[troughCountFinal 1]; end end elseif nh-1 if sum(troughCountFinal)>sum(peakCountFinal) if peakList(end)>troughList(end) peakCountFinal=[peakCountFinal 1]; end end end end elseif peakList(1)2 peakCountFinal(1)=1; elseif peakSide<3 peakCountFinal(1)=0.5; end for i=2:n peak=peakList(i); lastPeak=peakList(i-1); if (peak-lastPeak)>2 troughCountFinal(i-1)=1; end end for i=2:h trough=troughList(i); lastTrough=troughList(i-1); if (trough-lastTrough)>2 peakCountFinal(i)=1; if peakValList(i)9 if (trough-lastTrough)<(cutOff/n)/3 peakCountFinal(i)=0; troughCountFinal(i)=0; end end end end end if sum(troughCountFinal)peakList(end) if (cutOffGaussVec(end)-peakList(end))>2 troughCountFinal=[troughCountFinal 1]; end end end if n>h if n>sum(peakCountFinal) if (peakList(end)-troughList(end))>2 peakCountFinal=[peakCountFinal 1]; end end elseif hsum(troughCountFinal) if (troughList(end)-peakList(end))>2 troughCountFinal=[troughCountFinal 1]; end end end if troughList(end)>peakList(end) if peakCountFinal(1)<1 troughCountFinal(end)=0.5; end end end clearvars firstTroughSide troughSide nextTroughSide peakWidth peakSide clearvars nextPeakSide troughWidth lastTopVal penultimateTopVal difference clearvars firstPeakSide %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %peakCountFinal(peakCountFinal<0.5)=[]; %troughCountFinal(troughCountFinal<0.5)=[]; [m,n]=size(peakCountFinal); [g,h]=size(troughCountFinal); if n>1 if n>h if peakList(1)>troughList(1) if n1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5; elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0; end end incrementCount=[incrementCount 0.5]; end elseif n>h+1 for i=1:h if troughCountFinal(i)+peakCountFinal(i)>1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5; elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0; end end incrementCount=[incrementCount 0.5]; end end elseif peakList(1)1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5; elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0.5; end end end incrementCount=[incrementCount 0.5]; end elseif h>n if troughList(1)1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5 elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0.5; end end end elseif troughList(1)>peakList(1) incrementCount(1)=1; if n>h-2 for i=2:(h-1) if troughCountFinal(i)+peakCountFinal(i)>1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5; elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0.5; end end end elseif n1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5; elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0.5; end end end if (troughCountFinal(h-1)+troughCountFinal(h))>1 incrementCount(end)=1; incrementCount=[incrementCount 1]; end end end elseif h>n-1 if h1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5; elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0.5; end end end elseif troughList(1)>peakList(1) for i=1:h if troughCountFinal(i)+peakCountFinal(i)>1 incrementCount(i)=1; elseif troughCountFinal(i)+peakCountFinal(i)<2 if troughCountFinal(i)+peakCountFinal(i)>0 incrementCount(i)=0.5; elseif troughCountFinal(i)+peakCountFinal(i)<1 incrementCount(i)=0.5; end end end end end end elseif n<2 incrementCount=1; end %%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%% veryFirstPixel=cutOffGaussVec(j,1);%find out if the cutoff starts with a trough or peak if j>1 if veryFirstPixel>(cutOffMeanVec(j)+(cutOffStDevVec(j)/2))%if the first pixel is above the top cutoff if troughList(1)2%if the trough is significant veryFirstBottomPixel=1; veryFirstTopPixel=0; % SO STARTS WITH TOP PIXEL elseif troughList(1)<3 veryFirstBottomPixel=0; % SO STARTS WITH BOTTOM PIXEL veryFirstTopPixel=1; end %if VERYFirstPixel>(cutOffMeanVec(1)+(cutOffStDevVec(1)/2))%if the VERY first pixel is above its top cutoff if max(completePeakList(j-1,:))>max(completeTroughList(j-1,:))%if the last cutoff ends in a recorded peak %and this peak is NOT split half way %through if cutOffGaussVec(j-1,end)<(cutOffMeanVec(1)+(cutOffStDevVec(1)/2))%and this peak is NOT split half way %through if troughList(1)>2 incrementCount(1)=1; end elseif cutOffGaussVec(j-1,end)>(cutOffMeanVec(1)+(cutOffStDevVec(1)/2))%elseif the cutoff IS split halfway through incrementCount=[0.5 incrementCount]; end elseif max(completePeakList(j-1,:))2 incrementCount=[0.5 incrementCount]; end end elseif peakList(1)max(completeTroughList(j-1,:))%if the last cutoff ends in a recorded peak incrementCount(1)=0.5; end end elseif veryFirstPixel<(cutOffMeanVec(j)+(cutOffStDevVec(j)/2))%elseif the cutoff starts below the top cutoff if veryFirstPixel>(cutOffMeanVec(j)-(cutOffStDevVec(j)/2))%but ABOVE the bottom cutoff %then we need to check if the first element is %complete of half if peakList(1)(cutOffMeanVec(j)+(cutOffStDevVec(j)/2))% if the ENTIRE pattern starts with a peak %then we need to make peaks the start of every %increment if peakList(1)>2 if incrementCount(1)<1 incrementCount(1)=1; end end elseif VERYFirstPixel<(cutOffMeanVec(j)-(cutOffStDevVec(j)/2))%elseif the ENTIRE pattern starts with a trough if max(completePeakList(j-1,:))0.5 incrementCount(1)=0.5; end end end elseif troughList(1)(cutOffMeanVec(j)+(cutOffStDevVec(j)/2))% if the ENTIRE pattern starts with a peak if max(completePeakList(j-1,:))>max(completeTroughList(j-1,:))%if the last cutoff ends in a recorded PEAK if incrementCount(1)>0.5 incrementCount(1)=0.5; end end elseif VERYFirstPixel<(cutOffMeanVec(j)-(cutOffStDevVec(j)/2))% elseif the ENTIRE pattern starts with a trough if troughList(1)>2 if incrementCount(1)<1 incrementCount(1)=1; end end end end elseif veryFirstPixel<(cutOffMeanVec(j)-(cutOffStDevVec(j)/2))%elseif the cutoff starts below the top cutoff AND BELOW THE BOTTOM CUTOFF if peakList(1)(cutOffMeanVec(j)+(cutOffStDevVec(j)/2))%elseif the very first recorded element is a peak if lastIncrementCount(end)>0.5 incrementCount=[0.5 incrementCount]; elseif lastIncrementCount(end)<1 if incrementCount(1)>0.5 incrementCount(1)=0.5; end end end elseif troughList(1)0.5 if incrementCount(1)<1 incrementCount(1)=1; end end elseif VERYFirstPixel>(cutOffMeanVec(j)+(cutOffStDevVec(j)/2))%elseif the very first recorded element is a peak if incrementCount(1)<1 incrementCount(1)=1; end end end end end elseif j<2 if VERYFirstPixel>(cutOffMeanVec(j)+(cutOffStDevVec(j)/2)) if troughList(1)sum(troughCountFinal) incrementCount(1)=0.5; end end elseif VERYFirstPixel<(cutOffMeanVec(j)-(cutOffStDevVec(j)/2)) if peakList(1)sum(peakCountFinal) incrementCount(1)=0.5; end end elseif VERYFirstPixel<(cutOffMeanVec(j)+(cutOffStDevVec(j)/2)) if VERYFirstPixel>(cutOffMeanVec(j)-(cutOffStDevVec(j)/2)) if troughList(1)peakList(1) veryFirstTopPixel=0; veryFirstBottomPixel=1; if sum(peakCountFinal)<(sum(troughCountFinal)+1) relativeFirstPeakHeight=(cutOffGaussVec(1,(completePeakList(1))))-cutOffMeanVec(1); if relativeFirstPeakHeight0.5 incrementCount(1)=0.5; end end end end end end end %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% veryLastPixel=cutOffGaussVec(j,end); if veryFirstBottomPixel(cutOffMeanVec(j)+(cutOffStDevVec(j)/2))% if the last pixel of the cutoff is above the top cutOff if peakList(end)(max(find(peakList)))-1 incrementCount(end)=0.5; elseif sum(incrementCount)(cutOffMeanVec(j)-(cutOffStDevVec(j)/2))%but above the bottom cutoff if peakList(end)2 incrementCount(end)=1; end end elseif veryLastPixel<(cutOffMeanVec(1)-(cutOffStDevVec(1)/2)) if incrementCount(end)<1 incrementCount(end)=1; end end end elseif veryFirstTopPixel(cutOffMeanVec(1)-(cutOffStDevVec(1)/2))%if the cutoff doesnt start half way through an increments if cutOffGaussVec(j,1)<(cutOffMeanVec(1)+(cutOffStDevVec(1)/2)) if incrementCount(1)<1%then you need to make sure the last increment is counted as complete incrementCount(end)=1; end if incrementCount(end)<1 incrementCount(end)=1; end end end elseif veryLastPixel>(cutOffMeanVec(1)-(cutOffStDevVec(1)/2))% and ends in a peak if veryLastPixel<(cutOffMeanVec(1)+(cutOffStDevVec(1)/2))%but the peak is below the st dev if troughList(end)0.5 incrementCount = [incrementCount 0.5]; elseif incrementCount(end)<1 incrementCount(end)=0.5; end elseif peakList(end)(cutOffMeanVec(1)-(cutOffStDevVec(1)/2))%and is above the bottom cutoff if veryLastPixel>(cutOffMeanVec(1)+(cutOffStDevVec(1)/2))%if the last value is above the top cutOff if sum(peakCountFinal)<(sum(troughCountFinal))+1 if sum(peakCountFinal)>(sum(troughCountFinal))-1 incrementCount=[incrementCount 0.5]; end end elseif veryLastPixel<(cutOffMeanVec(1)+(cutOffStDevVec(1)/2)) if incrementCount(end)>0.5 incrementCount=[incrementCount 0.5]; elseif incrementCount(end)<1 incrementCount(end)=0.5; end end end end end end lastIncrementCount=incrementCount; [dummy,length]=size(incrementCount); lengthVec(j)=length; incrementCountVec(j,1:length)=incrementCount; clearvars veryLastPixel veryFirstBottomPixel veryFirstTopPixel troughCountFinal peakCountFinal end clearvars dummy length for i=1:7 finalIncrementCount=incrementCountVec(i,:); finalIncrementCount(finalIncrementCount<0.5)=[]; finalIncrementCountVec(i)=sum(finalIncrementCount); end finalIncrementCount=sum(finalIncrementCountVec); if finalIncrementCount>l finalIncrementCount=floor(finalIncrementCount); elseif finalIncrementCount