%   ReadVPM.m

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.


NPoints=165000;
Mat=zeros(NPoints,3);
FilePath=['Paul:256Data:VPM:01:S1001002.00A'];
[fid, message]=fopen(FilePath,'r','l');
fseek(fid,26,-1);
                                                                      
Mat(:,1)=fread(fid,NPoints,'int16');
fclose(fid);
FilePath=['Paul:256Data:VPM:01:S1001002.01A'];
[fid, message]=fopen(FilePath,'r','l');
fseek(fid,26,-1);
Mat(:,2)=fread(fid,NPoints,'int16');
fclose(fid);
FilePath=['Paul:256Data:VPM:01:S1001002.02A'];
[fid, message]=fopen(FilePath,'r','l');
fseek(fid,26,-1);
Mat(:,3)=fread(fid,NPoints,'int16');
fclose(fid);
Mat(:,1)=Mat(:,1)-mean(Mat(1:1000,1));
Mat(:,2)=Mat(:,2)-mean(Mat(1:1000,2));
Mat(:,3)=Mat(:,3)-mean(Mat(1:1000,3));


plot(Mat(10000:10400,1));
EcgVec=zeros(2000,1);
NEcg=0;
for i=2:NPoints
	if Mat(i,1)>400 & Mat(i-1,1)<400
		NEcg=NEcg+1;
		EcgVec(NEcg)=i;
	end
end
DiffECGVec=EcgVec(2:length(EcgVec))-EcgVec(1:length(EcgVec)-1);
plot(DiffECGVec)
axis([100 200 225 325])

TrigVec=zeros(2000,1);
NTrig=0;
for i=2:NPoints
	if Mat(i,3)<-500 & Mat(i-1,3)>-500
		NTrig=NTrig+1;
		TrigVec(NTrig)=i;
	end
end
for i=2:37
	DiffVec(i-1)=TrigVec(i)-TrigVec(i-1);
end
if DiffVec(35)<82 | DiffVec(36)>85 | DiffVec(36)<164 | DiffVec(36)>170
	error('trigger problems')
end
Start=TrigVec(36);
for i=1:
	
plot(Mat(10000:11000,:))
figure
plot(DiffVec)
