⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdfopen.m

📁 matlab数字信号处理工具箱
💻 M
📖 第 1 页 / 共 5 页
字号:
				
				EDF.EVENT.Fs = 1;
				if nargin>1,
				        EVENT.Fs = Fs;
				end;
				
				[tmp,ix] = sort(POS);
				EDF.EVENT.POS = (POS(ix)-1)*EVENT.Fs+1;
				EDF.EVENT.TYP = TYP(ix) + hex2dec('0100');
				EDF.EVENT.CHN = CHN(ix);
				EDF.EVENT.DUR = DUR(ix)*EVENT.Fs;
				EDF.EVENT.N   = N;
				
				%EDF.EVENT.ERG = ERG;
			end;
                end
        end;
else
	% search for WSCORE scoring file in path and in file directory. 
	tmp = [upper(EDF.FILE.Name),'.006'];
        fid2 = fopen(fullfile(EDF.FILE.Path,tmp),'r');
        if fid2 > 0,
                tmp = fread(fid2,inf,'char');
                fclose(fid2);
                [x,status] = str2double(char(tmp'));
                if ~any(isnan(status(:))),
                        EDF.EVENT.N   = size(x,1);
                        EDF.EVENT.POS = x(:,1);
                        EDF.EVENT.TYP = x(:,2);
                end;
        end;
end;


EDF.Calib = [EDF.Off'; diag(EDF.Cal)];
status = fseek(EDF.FILE.FID, 0, 'eof');
EDF.FILE.size = ftell(EDF.FILE.FID);
EDF.AS.endpos = EDF.FILE.size;

%[status, EDF.AS.endpos, EDF.HeadLen, EDF.AS.bpb EDF.NRec, EDF.HeadLen+EDF.AS.bpb*EDF.NRec]
if EDF.NRec == -1   % unknown record size, determine correct NRec
        EDF.NRec = floor((EDF.AS.endpos - EDF.HeadLen) / EDF.AS.bpb);
elseif  EDF.NRec ~= ((EDF.AS.endpos - EDF.HeadLen) / EDF.AS.bpb);
        if ~strcmp(EDF.VERSION(1:3),'GDF'),
                EDF.ErrNo=[16,EDF.ErrNo];
                fprintf(2,'\nWarning SDFOPEN: size (%i) of file %s does not fit headerinformation\n',EDF.AS.endpos,EDF.FileName);
                EDF.NRec = floor((EDF.AS.endpos - EDF.HeadLen) / EDF.AS.bpb);
        else
                EDF.AS.EVENTTABLEPOS = EDF.HeadLen + EDF.AS.bpb*EDF.NRec;
        end;
end; 

if EDF.AS.EVENTTABLEPOS > 0,  
        fseek(EDF.FILE.FID, EDF.AS.EVENTTABLEPOS, 'bof');
        EDF.EVENT.Version = fread(EDF.FILE.FID,1,'char');
        tmp = fread(EDF.FILE.FID,3,'char');
        EDF.EVENT.N = fread(EDF.FILE.FID,1,'uint32');
        if EDF.EVENT.Version==1,
                [EDF.EVENT.POS,c1] = fread(EDF.FILE.FID,[EDF.EVENT.N,1],'uint32');
                [EDF.EVENT.TYP,c2] = fread(EDF.FILE.FID,[EDF.EVENT.N,1],'uint16');
                if any([c1,c2]~=EDF.EVENT.N) | (EDF.AS.endpos~=EDF.AS.EVENTTABLEPOS+8+EDF.EVENT.N*6),
                        fprintf(2,'\nERRR SDFOPEN: Eventtable corrupted in file %s\n',EDF.FileName);
                end
        elseif EDF.EVENT.Version==3,
                [EDF.EVENT.POS,c1] = fread(EDF.FILE.FID,[EDF.EVENT.N,1],'uint32');
                [EDF.EVENT.TYP,c2] = fread(EDF.FILE.FID,[EDF.EVENT.N,1],'uint16');
                [EDF.EVENT.CHN,c3] = fread(EDF.FILE.FID,[EDF.EVENT.N,1],'uint16');
                [EDF.EVENT.DUR,c4] = fread(EDF.FILE.FID,[EDF.EVENT.N,1],'uint32');
                if any([c1,c2,c3,c4]~=EDF.EVENT.N) | (EDF.AS.endpos~=EDF.AS.EVENTTABLEPOS+8+EDF.EVENT.N*12),
                        fprintf(2,'\nERRR SDFOPEN: Eventtable corrupted in file %s\n',EDF.FileName);
                end
        else
                fprintf(2,'\nWarning SDFOPEN: Eventtable version %i not supported\n',EDF.EVENT.Version);
        end;
        EDF.AS.endpos = EDF.AS.EVENTTABLEPOS;   % set end of data block, might be important for SSEEK
end;

fseek(EDF.FILE.FID, EDF.HeadLen, 'bof');
EDF.FILE.POS = 0;

% if Channelselect, ReReferenzing and Resampling
% Overflowcheck, Adaptive FIR
% Layer 4 

%if nargin<3 %%%%%          Channel Selection 
if nargin <3 %else
        arg3=0;
end;

EDF.SIE.ChanSelect = 1:EDF.NS;
EDF.InChanSelect = 1:EDF.NS;

%EDF.SIE.RR=1; %is replaced by ~EDF.FLAG.UCAL
if ~isfield(EDF,'FLAG')
        EDF.FLAG.UCAL=0;
end;
if ~isfield(EDF.FLAG,'UCAL');
        EDF.FLAG.UCAL=0;
end;
EDF.SIE.RS=0; %exist('arg5')==1; if EDF.SIE.RS, EDF.SIE.RS==(arg5>0); end;
EDF.SIE.TH=0; %(exist('arg6')==1);
EDF.SIE.RAW=0;
EDF.SIE.REGC=0;
EDF.SIE.TECG=0;
EDF.SIE.AFIR=0;
EDF.SIE.FILT=0;
EDF.SIE.TimeUnits_Seconds=1;
%EDF.SIE.ReRefMx=eye(EDF.NS);
EDF.SIE.REG=eye(EDF.NS);
%EDF.SIE.ReRefMx = EDF.SIE.ReRefMx(:,EDF.SIE.ChanSelect);
%EDF.Calib=EDF.Calib*EDF.SIE.REG*EDF.SIE.ReRefMx; 

%if nargin>2 %%%%%          Channel Selection 
        EDF.SIE.REG=eye(EDF.NS);
        if arg3==0
                EDF.SIE.ChanSelect = 1:EDF.NS;
                EDF.InChanSelect = 1:EDF.NS;
                EDF.SIE.ReRefMx = eye(EDF.NS);
        else
                [nr,nc]=size(arg3);
                if all([nr,nc]>1), % Re-referencing
                        EDF.SIE.ReRefMx = [[arg3 zeros(size(arg3,1),EDF.NS-size(arg3,2))]; zeros(EDF.NS-size(arg3,1),EDF.NS)];
                        EDF.InChanSelect = find(any(EDF.SIE.ReRefMx'));
                        EDF.SIE.ChanSelect = find(any(EDF.SIE.ReRefMx));
                        if nargin>3
                        %        fprintf(EDF.FILE.stderr,'Error SDFOPEN: Rereferenzing does not work correctly with %s (more than 3 input arguments)\n',arg4);
                        end;
                else
                        EDF.SIE.ChanSelect = arg3; %full(sparse(1,arg3(:),1,1,EDF.NS));
                        EDF.InChanSelect = arg3;
                        EDF.SIE.ReRefMx = eye(EDF.NS);
                end;
        end;
        
        if (exist('sedfchk')==2),  
                EDF=sedfchk(EDF); % corrects incorrect Header information. 
        end;

	%EDF.SIE.CS=1;
        EDF.SIE.RS=exist('arg5')==1; if EDF.SIE.RS, EDF.SIE.RS==(arg5>0); end;
        EDF.SIE.TH=0; %(exist('arg6')==1);
        EDF.SIE.RAW=0;
        EDF.SIE.REGC=0;
        EDF.SIE.TECG=0;
        EDF.SIE.AFIR=0;
        EDF.SIE.FILT=0;
        %EDF.AS.MAXSPR=max(EDF.SPR(EDF.SIE.ChanSelect)); % Layer 3 defines EDF.AS.MAXSPR in GDFREAD
        EDF.AS.MAXSPR=EDF.SPR(EDF.SIE.ChanSelect(1));
        for k=2:length(EDF.SIE.ChanSelect),
                EDF.AS.MAXSPR = lcm(EDF.AS.MAXSPR,EDF.SPR(EDF.SIE.ChanSelect(k)));
        end;
	EDF.SampleRate = EDF.AS.MAXSPR/EDF.Dur;
        
        EDF.SIE.REG=eye(EDF.NS);

        %elseif nargin>3   %%%%% RE-REFERENCING
        if nargin>3   
                if ~isempty(findstr(upper(arg4),'SIESTA'))
                        EDF.SIE.ReRefMx = eye(EDF.NS);% speye(EDF.NS);
                        EDF.SIE.ReRefMx(7,1:6)=[1 1 1 -1 -1 -1]/2;
                        EDF.SIE.TH=1;
	                % calculate (In)ChanSelect based on ReRefMatrix
    		        EDF.InChanSelect = find(any(EDF.SIE.ReRefMx'));
            		EDF.SIE.ChanSelect = find(any(EDF.SIE.ReRefMx));
                end;
                if ~isempty(findstr(upper(arg4),'EOG'))
                        tmp=findstr(upper(arg4),'EOG');
                        tmp=lower(strtok(arg4(tmp:length(arg4)),' +'));
                        EDF.SIE.ReRefMx = sparse(EDF.NS,EDF.NS);% speye(EDF.NS);
                        if any(tmp=='h'); EDF.SIE.ReRefMx(8:9,1)=[ 1 -1 ]';  end;
                        if any(tmp=='v'); EDF.SIE.ReRefMx(1:9,2)=[ 1 0 0 1 0 0 1 -1 -1]';end;
                        if any(tmp=='r'); EDF.SIE.ReRefMx(3:9,3)=[ 1 0 0 1 2 -2 -2]'/2;  end;
                        %EDF.SIE.TH = 1;
	                % calculate (In)ChanSelect based on ReRefMatrix
    		        EDF.InChanSelect = find(any(EDF.SIE.ReRefMx'));
            		EDF.SIE.ChanSelect = find(any(EDF.SIE.ReRefMx));
                end;
                
                if ~isempty(findstr(upper(arg4),'UCAL'))
                        %EDF.SIE.ReRefMx = speye(EDF.NS); % OVERRIDES 'SIESTA' and 'REGRESS_ECG'
                        %EDF.SIE.RR  = 0;
                        EDF.FLAG.UCAL = 1;
                end;
                if ~isempty(findstr(upper(arg4),'RAW'))
                        EDF.SIE.RAW = 1;
                end;
                if ~isempty(findstr(upper(arg4),'OVERFLOW'))
                        EDF.SIE.TH  = 1;
                end;
                if ~isempty(findstr(arg4,'FailingElectrodeDetector'))
                        EDF.SIE.FED = 1;
			EDF.SIE.TH  = 2;
                end;
		
                if ~isempty(findstr(upper(arg4),'ECG')), % identify ECG channel for some ECG artifact processing method   
                        if ~isempty(findstr(upper(arg4),'SIESTA'))
                                channel1=12;
                                %channel2=1:9;
                                M=zeros(EDF.NS,1);M(channel1)=1;
                        end
                        if isfield(EDF,'ChanTyp')
                                M=upper(char(EDF.ChanTyp(channel1)))=='C';
                                channel1=find(M);
                                M=(upper(char(EDF.ChanTyp))=='E' | upper(char(EDF.ChanTyp))=='O' );
                                %channel2=find(M);
                        else
                                channel1 = 12;
                                %channel2=1:9;
                                M=zeros(EDF.NS,1);M(channel1)=1;
                        end;
                        
                end;
                if ~isempty(findstr(upper(arg4),'RECG'))
                        EDF.SIE.REGC = 1;
                        if all(EDF.InChanSelect~=channel1)
                                EDF.InChanSelect=[EDF.InChanSelect channel1];        
                        end;
                end;
                
                if ~isempty(findstr(upper(arg4),'TECG'))
                        fprintf(EDF.FILE.stderr,'SDFOPEN: status of TECG Mode: alpha test passed\n');    
                        %%%% TECG - ToDo
                        % - optimize window
                        if exist([lower(EDF.FILE.Name) 'ECG.mat']);
                                if exist('OCTAVE_VERSION')==5
                                        load(file_in_loadpath([lower(EDF.FILE.Name) 'ECG.mat']));
                                else
                                        load([lower(EDF.FILE.Name) 'ECG.mat']);
                                end;
                                if isstruct(QRS)
                                        EDF.SIE.TECG = 1;
					%%%  EDF.AS.MAXSPR=size(QRS.Templates,1)/3;
                                        EDF.AS.MAXSPR=lcm(EDF.AS.MAXSPR,EDF.SPR(channel1));
				else
                                        fprintf(EDF.FILE.stderr,'WARNING SDFOPEN: %s invalid for TECG\n',[ lower(EDF.FILE.Name) 'ECG.mat']);
                                end;
                        else
                                fprintf(EDF.FILE.stderr,'WARNING SDFOPEN: %s not found\t (needed for Mode=TECG)\n',[lower(EDF.FILE.Name) 'ECG.mat']);
                        end;
                end;
                
                if ~isempty(findstr(upper(arg4),'AFIR')) 
                        % Implements Adaptive FIR filtering for ECG removal in EDF/GDF-tb.
                        % based on the Algorithm of Mikko Koivuluoma <k7320@cs.tut.fi>
                                
                        % channel2 determines which channels should be corrected with AFIR 
                        
                        %EDF = sdf_afir(EDF,12,channel2);
                        channel2=EDF.SIE.ChanSelect;	
                        fprintf(EDF.FILE.stderr,'Warning SDFOPEN: option AFIR still buggy\n');    
                        if isempty(find(channel2))
                                EDF.SIE.AFIR=0;
                        else
                                EDF.SIE.AFIR=1;
                                EDF.AFIR.channel2=channel2;
                                
                                EDF.AFIR.alfa=0.01;
                                EDF.AFIR.gamma=1e-32;
                                
                                EDF.AFIR.delay  = ceil(0.05*EDF.AS.MAXSPR/EDF.Dur); 
                                EDF.AFIR.nord = EDF.AFIR.delay+EDF.AS.MAXSPR/EDF.Dur; 
                                
                                EDF.AFIR.nC = length(EDF.AFIR.channel2);
                                EDF.AFIR.w = zeros(EDF.AFIR.nC, max(EDF.AFIR.nord));
                                EDF.AFIR.x = zeros(1, EDF.AFIR.nord);
                                EDF.AFIR.d = zeros(EDF.AFIR.delay, EDF.AFIR.nC);
                                
                                channel1=12;
                                
                                if isfield(EDF,'ChanTyp')
                                        if upper(char(EDF.ChanTyp(channel1)))=='C';
                                                EDF.AFIR.channel1 = channel1;
                                        else
                                                EDF.AFIR.channel1 = find(EDF.ChanTyp=='C');
                                                fprintf(EDF.FILE.stderr,'Warning %s: #%i is not an ECG channel, %i used instead\n' ,filename,channel1,EDF.AFIR.channel1);
                                        end;
                                else
                                        EDF.AFIR.channel1 = channel1;
                                end;
                                
                                if all(EDF.InChanSelect~=channel1)
                                        EDF.InChanSelect=[EDF.InChanSelect channel1];        
                                end;
                        end;
                end;


                if isempty(findstr(upper(arg4),'NOTCH50')) 
                        EDF.Filter.A = 1;
                        EDF.Filter.B = 1;
                else
                        EDF.SIE.FILT = 1;
                        EDF.Filter.A = 1;
                        EDF.Filter.B = 1;
                        %if all(EDF.SampleRate(EDF.SIE.ChanSelect)==100)
                        if EDF.AS.MAXSPR/EDF.Dur==100
                                EDF.Filter.B=[1 1]/2;
                                %elseif all(EDF.SampleRate(EDF.SIE.ChanSelect)==200)
                        elseif EDF.AS.MAXSPR/EDF.Dur==200
                                EDF.Filter.B=[1 1 1 1]/4;
                                %elseif all(EDF.SampleRate(EDF.SIE.ChanSelect)==256)
                        elseif EDF.AS.MAXSPR/EDF.Dur==400
                                EDF.Filter.B=ones(1,8)/8;
                                %elseif all(EDF.SampleRate(EDF.SIE.ChanSelect)==256)
                        elseif EDF.AS.MAXSPR/EDF.Dur==256
                                EDF.Filter.B=poly([exp([-1 1 -2 2]*2*pi*i*50/256)]); %max(EDF.SampleRate(EDF.SIE.ChanSelect)))]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -