clear
load P4.mat
load('SampelrateP4.mat')
counter=0;
for ii=40:10:200
    counter=counter+1;
    %hold on
    %plot(P4.(['t',num2str(ii)]),P4.(['d',num2str(ii)]))
    P4.(['d',num2str(ii)])=bandpass(P4.(['d',num2str(ii)]),[0.6*1e3*ii 1.4*1e3*ii],1/SampelrateP4.(['Samplerate', num2str(ii)]).VarName2);
    zloc(counter,1)=find(P4.(['t',num2str(ii)])==0);
    if max(P4.(['t',num2str(ii)]))==7.9980e-4
    deltime(counter,1)=P4.(['t',num2str(ii)])(end,1)-P4.(['t',num2str(ii)])(end-1,1);    
    data1.(['sig',num2str(ii)])(:,1)=P4.(['t',num2str(ii)])(zloc(counter,1):end,1);
    data1.(['sig',num2str(ii)])(:,2)=P4.(['d',num2str(ii)])(zloc(counter,1):end,1);
    elseif max(P4.(['t',num2str(ii)]))>=8e-4
     mloc(counter,1)=find(P4.(['t',num2str(ii)])==7.9980e-4);
    deltime(counter,1)=P4.(['t',num2str(ii)])(mloc(counter,1),1)-P4.(['t',num2str(ii)])(mloc(counter,1)-1,1);
     data1.(['sig',num2str(ii)])(:,1)=P4.(['t',num2str(ii)])(zloc(counter,1):mloc(counter,1),1);
    data1.(['sig',num2str(ii)])(:,2)=P4.(['d',num2str(ii)])(zloc(counter,1):mloc(counter,1),1);
    elseif max(P4.(['t',num2str(ii)]))<7.9990e-4
        ii
        [tlimitval(counter,1),tlimitind(counter,1)]=max(P4.(['t',num2str(ii)]));
        deltime(counter,1)=(P4.(['t',num2str(ii)])(tlimitind(counter,1),1)-P4.(['t',num2str(ii)])(tlimitind(counter,1)-1,1));
        datat1=P4.(['t',num2str(ii)])(zloc(counter,1):end,1);%0:deltime(counter,1):7.9990e-4;
        datat=0:deltime(counter,1):7.9990e-4;
        data1.(['sig',num2str(ii)])=zeros(length(datat),2);
        data1.(['sig',num2str(ii)])(1:size(P4.(['d',num2str(ii)])(zloc(counter,1):end,1)),2)=P4.(['d',num2str(ii)])(zloc(counter,1):end,1);
        data1.(['sig',num2str(ii)])(:,1)=datat;
     end
    [loc(counter,1),val(counter,1)]=max(P4.(['t',num2str(ii)]));
num2(counter,1)=size(data1.(['sig',num2str(ii)]),1);
if deltime(counter,1)>2.01e-7
   deltime(counter,1)
    data.(['sig',num2str(ii)])(:,1) = (0:2e-7:7.9980e-4)';
    data.(['sig',num2str(ii)])(:,2) = resample(data1.(['sig',num2str(ii)])(:,2),2,1);
else
    data.(['sig',num2str(ii)])=data1.(['sig',num2str(ii)]);
end
datmat(:,counter)=data.(['sig',num2str(ii)])(:,2);
freqmat=(40:10:200)';
timemat=(0:2e-7:7.9980e-4)';
end
[X,Y]=meshgrid(timemat,freqmat);
Ndatmat=datmat./max(abs(datmat));
figure;
surf(X,Y,abs(hilbert(Ndatmat)')); shading interp; view(2)
figure;
surf(X(:,1:951),Y(:,1:951),abs(hilbert(Ndatmat(1:951,:))')); shading interp; view(2)
save('P4_3Dplot','X','Y','datmat','Ndatmat');
% if num2<=8000
%     ups = 4;
% dns = 1;
% 
% fu = fs*ups;
% tu = 0:1/fu:1-1/fu;
% 
% y = resample(x,ups,dns);
% 
% 
% 
% 
% for ii= 1:17
%     freq= 30+(ii*10);
%     figure(ii+100)
%     plot(timemat,Ndatmat(:,ii))
%     title(['Frequency', num2str(freq) 'kHz'])
% end

