%==================================================================
%
%	ReadTAWChan.m
%
%	Markus Junghoefer	[1997]
%   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.                                                                      
%
%
%	Function definition
%
%
	function[ChanVec,FilePath,Header,LHeader,Version, ...
	Year, Month, Day, Hour, Minute, Sec, MSec, ...
	SampRate, NChan, Gain, Bits, Range, NPoints, NEvents, EventCodes]= ...
	ReadTAWChan(FilePath,SpecChan);
%

%==================================================================
if nargin<2; SpecChan=1; end
if nargin<1; FilePath=[]; end
if isempty(FilePath)
	[File,Path]=uigetfile(['*.TAW'],'Choose TAW file:');
	FilePath=[Path,File];
end
fprintf('\n')
clc;
fprintf('Read header of file...\n\n');
fprintf(FilePath);
fprintf('\n\n');

fid=fopen(FilePath,'r');
Version=fread(fid,1,'long');
Year=fread(fid,1,'short');
Month=fread(fid,1,'short');
Day=fread(fid,1,'short');
Hour=fread(fid,1,'short');
Minute=fread(fid,1,'short');
Sec=fread(fid,1,'short');
MSec=fread(fid,1,'long');
SampRate=fread(fid,1,'short');
NChan=fread(fid,1,'short');
Gain=fread(fid,1,'short');
Bits=fread(fid,1,'short');
Range=fread(fid,1,'short');
NPoints=fread(fid,1,'long')
NEvents=fread(fid,1,'short');
EventCodes=fread(fid,[NEvents,4],'char');
Tell1=ftell(fid);
LHeader=Tell1;

NPoints=NPoints;
NFFT=2^17;
FrequVec=linspace(0,250,NFFT./2+1);
global Test
Test=zeros(1,128);

for SpecChan=1:128

fseek(fid,0,-1);
Header=fread(fid,LHeader,'int8');
fprintf(['Read channel #' int2str(SpecChan) '\n\n']);

SeekStatus=fseek(fid,(SpecChan-1).*NPoints.*2,0);
if SeekStatus==-1;
	fprintf('Sorry, fseek error in ReadTAWChan !')
	ChanVec=[];
end

[ChanVec,Count]=fread(fid,[1,NPoints],'int16');	
if Count~=NPoints;
	fprintf('Sorry, fread error in ReadTAWChan !')
	ChanVec=[];
	return;
end

% 
% [a,b]=min(abs(FrequVec-2.865))
% [c,d]=min(abs(FrequVec-2.873))

FFTDataVec=fft(ChanVec(1:NFFT));
PFFTDataVec=abs(FFTDataVec.*conj(FFTDataVec))./NFFT;
PFFTDataVec=PFFTDataVec./mean(PFFTDataVec);
Test(SpecChan)=sum(PFFTDataVec(752:754));
plot(FrequVec,PFFTDataVec(1:NFFT./2+1))
hold on
plot([2.865 2.865],[0 10000000],'--r')
plot([2.873 2.873],[0 10000000],'--r')
axis([0 5 0 200])
hold off
pause(.1)
end
% 
% plot(FrequVec,Test(1:NFFT./2+1))
% hold on
% plot([1.3021 1.3021],[0 10000000],'-r')
% axis([2.5 3 0 100000])
% hold off
fclose(fid);

return;
