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

📄 sload.m

📁 matlab数字信号处理工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
function [signal,H] = sload(FILENAME,CHAN,Fs)
% SLOAD loads signal data of various data formats
% 
% Currently are the following data formats supported: 
%    EDF, CNT, EEG, BDF, GDF, BKR, MAT(*), 
%    PhysioNet (MIT-ECG), Poly5/TMS32, SMA, RDF, CFWB,
%    Alpha-Trace, DEMG, SCP-ECG.
%
% [signal,header] = sload(FILENAME [,CHANNEL [,Fs]])
% FILENAME      name of file, or list of filenames
% channel       list of selected channels
%               default=0: loads all channels
% Fs            force target samplerate Fs (only 
%               integer and 256->100 conversion is supported) 
%
% [signal,header] = sload(dir('f*.emg'), CHAN)
% [signal,header] = sload('f*.emg', CHAN)
%  	loads channels CHAN from all files 'f*.emg'
%
% see also: SOPEN, SREAD, SCLOSE, MAT2SEL, SAVE2TXT, SAVE2BKR
%
% Reference(s):


%	$Revision: 1.32 $
%	$Id: sload.m,v 1.32 2004/09/13 17:28:26 schloegl Exp $
%	Copyright (C) 1997-2004 by Alois Schloegl 
%	a.schloegl@ieee.org	
%    	This is part of the BIOSIG-toolbox http://biosig.sf.net/

% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Library General Public
% License as published by the Free Software Foundation; either
% Version 2 of the License, or (at your option) any later version.
%
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% Library General Public License for more details.
%
% You should have received a copy of the GNU Library General Public
% License along with this library; if not, write to the
% Free Software Foundation, Inc., 59 Temple Place - Suite 330,
% Boston, MA  02111-1307, USA.


if nargin<2; CHAN=0; end;
if nargin<3; Fs=NaN; end;

if CHAN<1 | ~isfinite(CHAN),
        CHAN=0;
end;

if (ischar(FILENAME) & any(FILENAME=='*'))
        p = fileparts(FILENAME);
        f = dir(FILENAME);
        for k = 1:length(f);
                f(k).name = fullfile(p,f(k).name);
        end;
        FILENAME=f;
end;        

if ((iscell(FILENAME) | isstruct(FILENAME)) & (length(FILENAME)>1)),
	signal = [];
	for k = 1:length(FILENAME),
		if iscell(FILENAME(k))
			f = FILENAME{k};
		else 
			f = FILENAME(k);
		end	

		[s,h] = sload(f,CHAN,Fs);
		if k==1,
			H = h;
			signal = s;  
			LEN = size(s,1);
		else
			H.FILE(k) = h.FILE;
			if (H.SampleRate ~= h.SampleRate),
				fprintf(2,'Warning SLOAD: sampling rates of multiple files differ %i!=%i.\n',H.SampleRate, h.SampleRate);
			end;
			if size(s,2)==size(signal,2), %(H.NS == h.NS) 
				signal = [signal; repmat(NaN,100,size(s,2)); s];
			else
				fprintf(2,'ERROR SLOAD: incompatible channel numbers %i!=%i of multiple files\n',H.NS,h.NS);
				return;
			end;

                        if isfield(H,'TriggerOffset'),
                                if H.TriggerOffset ~= h.TriggerOffset,
                                        fprintf(2,'Warning SLOAD: Triggeroffset does not fit.\n',H.TriggerOffset,h.TriggerOffset);
                                        return;
                                end;
                        end;
                        if isfield(H,'Classlabel'),
                                if isfield(H,'ArtifactSelection'),
                                        H.ArtifactSelection = [H.ArtifactSelection(:);h.ArtifactSelection(:)+length(H.Classlabel)];
                                end;
                                H.Classlabel = [H.Classlabel(:);h.Classlabel(:)];
                        end;
                        if h.EVENT.N > 0,
                                H.EVENT.POS = [H.EVENT.POS; h.EVENT.POS+size(signal,1)-size(s,1)];
                                H.EVENT.TYP = [H.EVENT.TYP; h.EVENT.TYP];
                                if isfield(H.EVENT,'CHN');
                                        H.EVENT.CHN = [H.EVENT.CHN; h.EVENT.CHN];
                                end;
                                if isfield(H.EVENT,'DUR');
                                        H.EVENT.DUR = [H.EVENT.DUR; h.EVENT.DUR];
                                end;
                                H.EVENT.N   =  H.EVENT.N  + h.EVENT.N;
                        end;			
                        clear s
                end;
	end;
        ix = find(H.EVENT.TYP==hex2dec('0300')); 
        H.TRIG = mod(H.EVENT.POS(ix),256);

        
	fprintf(1,'  SLOAD: data segments are concanated with NaNs in between.\n');
	return;	
end;
%%% end of multi-file section 

%%%% start of single file section

if isstruct(FILENAME),
	HDR = FILENAME;
	if isfield(HDR,'FileName'),
    		FILENAME = HDR.FileName;
	elseif isfield(HDR,'name'),
    		FILENAME = HDR.name;
	else
    		fprintf(2,'Error SLOAD: missing FileName.\n');	
    		return; 
	end;
end;

if ~isnumeric(CHAN),
        MODE = CHAN;
        CHAN = 0; 
else
        MODE = '';
end;

signal = [];
[p,f,FileExt] = fileparts(FILENAME);
FileExt = FileExt(2:length(FileExt));
H.FileName = FILENAME;
H = sopen(H,'rb',CHAN);

if H.FILE.OPEN > 0,
        [signal,H] = sread(H);
        H = sclose(H);


elseif strcmp(H.TYPE,'EVENTCODES')
        signal = H.EVENT;


elseif strcmp(H.TYPE,'AKO')
        signal = fread(H.FILE.FID,inf,'uint8')*H.Calib(2,1)+H.Calib(1,1);
        
        fclose(H.FILE.FID);
        

elseif strcmp(H.TYPE,'DAQ')
	fprintf(1,'Loading a matlab DAQ data file - this can take a while.\n');
	tic;
 	[signal, tmp, H.DAQ.T0, H.DAQ.events, DAQ.info] = daqread(H.FileName);
	fprintf(1,'Loading DAQ file finished after %.0f s.\n',toc);
	H.NS   = size(signal,2);
        
	H.SampleRate = DAQ.info.ObjInfo.SampleRate;
        sz     = size(signal);
        if length(sz)==2, sz=[1,sz]; end;
        H.NRec = sz(1);
        H.Dur  = sz(2)/H.SampleRate;
        H.NS   = sz(3);
        H.FLAG.TRIGGERED = H.NRec>1;
        H.FLAG.UCAL = 1;
	
        H.PhysDim = {DAQ.info.ObjInfo.Channel.Units};
        H.DAQ   = DAQ.info.ObjInfo.Channel;
        
        H.Cal   = diff(cat(1,DAQ.info.ObjInfo.Channel.InputRange),[],2).*(2.^(-DAQ.info.HwInfo.Bits));
        H.Off   = cat(1,DAQ.info.ObjInfo.Channel.NativeOffset); 
        H.Calib = sparse([H.Off';eye(H.NS)]*diag(H.Cal));
        
        if CHAN<1,
                CHAN = 1:H.NS; 
        end;
        if ~H.FLAG.UCAL,
	        Calib = H.Calib;	% Octave can not index sparse matrices within a struct
        	signal = [ones(size(signal,1),1),signal]*Calib(:,CHAN);
        end;
     
        
elseif strncmp(H.TYPE,'MAT',3),
        tmp = load('-MAT',FILENAME);
        if isfield(tmp,'y'),		% Guger, Mueller, Scherer
                H.NS = size(tmp.y,2);
                if ~isfield(tmp,'SampleRate')
                        %fprintf(H.FILE.stderr,['Samplerate not known in ',FILENAME,'. 125Hz is chosen']);
                        H.SampleRate=125;
                else
                        H.SampleRate=tmp.SampleRate;
                end;
                fprintf(H.FILE.stderr,'Sensitivity not known in %s.\n',FILENAME);
                if any(CHAN),
                        signal = tmp.y(:,CHAN);
                else
        	        signal = tmp.y;
                end;
                
        elseif isfield(tmp,'clab') & isfield(tmp,'x_train') & isfield(tmp,'y_train') & isfield(tmp,'x_test');	
                % BCI competition 2003, dataset 4 (Berlin) 
                %load('berlin/sp1s_aa_1000Hz.mat');
                HDR.Classlabel=[repmat(nan,size(tmp.x_test,3),1);tmp.y_train';repmat(nan,size(tmp.x_test,3),1)];
                HDR.NRec = length(HDR.Classlabel);
                
                HDR.SampleRate=1000;
                HDR.Dur = .5; 
                HDR.NS = size(tmp.x_test,2);
                HDR.SPR = HDR.SampleRate*HDR.Dur;
                HDR.FLAG.TRIGGERED = 1; 
                sz = [HDR.NS,HDR.SPR,HDR.NRec];
                
                signal=reshape(permute(cat(3,tmp.x_test,tmp.x_train,tmp.x_test),[2,1,3]),sz(1),sz(2)*sz(3))';
                
                
        elseif isfield(tmp,'RAW_SIGNALS')    % TFM Matlab export 
                H.Label = fieldnames(tmp.RAW_SIGNALS);
                H.SampleRate = 1000; 
                H.TFM.SampleRate = 1000./[10,20,5,1,2];
                signal = [];
                for k1 = 4;1:length(H.Label);
                        s = getfield(tmp.RAW_SIGNALS,H.Label{k1});
                        ix = [];
                        for k2 = 1:length(s);
                                ix = [ix;length(s{k2})];   
                        end;
                        H.EVENT.POS(:,k1) = cumsum(ix);
                        signal = cat(1,s{k1})';
                end;

                
        elseif isfield(tmp,'daten');	% Woertz, GLBMT-Uebungen 2003
                H = tmp.daten;
                signal = H.raw*H.Cal;
                
                
        elseif isfield(tmp,'eeg');	% Scherer
                fprintf(H.FILE.stderr,'Warning SLOAD: Sensitivity not known in %s,\n',FILENAME);
                H.NS=size(tmp.eeg,2);
                if ~isfield(tmp,'SampleRate')
                        %fprintf(H.FILE.stderr,['Samplerate not known in ',FILENAME,'. 125Hz is chosen']);
                        H.SampleRate=125;
                else
                        H.SampleRate=tmp.SampleRate;
                end;
                if any(CHAN),
                        signal = tmp.eeg(:,CHAN);
                else
        	        signal = tmp.eeg;
                end;
                if isfield(tmp,'classlabel'),
                	H.Classlabel = tmp.classlabel;
                end;        

        elseif isfield(tmp,'P_C_S');	% G.Tec Ver 1.02, 1.5x data format
                if isa(tmp.P_C_S,'data'), %isfield(tmp.P_C_S,'version'); % without BS.analyze	
                        if any(tmp.P_C_S.Version==[1.02, 1.5, 1.52]),
                        else
                                fprintf(H.FILE.stderr,'Warning: PCS-Version is %4.2f.\n',tmp.P_C_S.Version);
                        end;
                        H.Filter.LowPass  = tmp.P_C_S.LowPass;
                        H.Filter.HighPass = tmp.P_C_S.HighPass;
                        H.Filter.Notch    = tmp.P_C_S.Notch;
                        H.SampleRate      = tmp.P_C_S.SamplingFrequency;
                        H.gBS.Attribute   = tmp.P_C_S.Attribute;
                        H.gBS.AttributeName = tmp.P_C_S.AttributeName;
                        H.Label = tmp.P_C_S.ChannelName;
                        H.gBS.EpochingSelect = tmp.P_C_S.EpochingSelect;
                        H.gBS.EpochingName = tmp.P_C_S.EpochingName;

                        signal = double(tmp.P_C_S.Data);
                        
                else %if isfield(tmp.P_C_S,'Version'),	% with BS.analyze software, ML6.5
                        if any(tmp.P_C_S.version==[1.02, 1.5, 1.52]),
                        else
                                fprintf(H.FILE.stderr,'Warning: PCS-Version is %4.2f.\n',tmp.P_C_S.version);
                        end;        
                        H.Filter.LowPass  = tmp.P_C_S.lowpass;
                        H.Filter.HighPass = tmp.P_C_S.highpass;
                        H.Filter.Notch    = tmp.P_C_S.notch;
                        H.SampleRate      = tmp.P_C_S.samplingfrequency;
                        H.gBS.Attribute   = tmp.P_C_S.attribute;
                        H.gBS.AttributeName = tmp.P_C_S.attributename;
                        H.Label = tmp.P_C_S.channelname;
                        H.gBS.EpochingSelect = tmp.P_C_S.epochingselect;
                        H.gBS.EpochingName = tmp.P_C_S.epochingname;
                        
                        signal = double(tmp.P_C_S.data);
                end;
                tmp = []; % clear memory

                sz     = size(signal);
                H.NRec = sz(1);
                H.Dur  = sz(2)/H.SampleRate;
                H.NS   = sz(3);
                H.FLAG.TRIGGERED = H.NRec>1;
                
                if any(CHAN),
                        %signal = signal(:,CHAN);
                        sz(3)= length(CHAN);
                else
                        CHAN = 1:H.NS;
                end;
                signal = reshape(permute(signal(:,:,CHAN),[2,1,3]),[sz(1)*sz(2),sz(3)]);

                % Selection of trials with artifacts
                ch = strmatch('ARTIFACT',H.gBS.AttributeName);
                if ~isempty(ch)
                        H.ArtifactSelection = H.gBS.Attribute(ch,:);
                end;
                
                % Convert gBS-epochings into BIOSIG - Events
                map = zeros(size(H.gBS.EpochingName,1),1);
                map(strmatch('AUGE',H.gBS.EpochingName))=hex2dec('0101');
                map(strmatch('EOG',H.gBS.EpochingName))=hex2dec('0101');
                map(strmatch('MUSKEL',H.gBS.EpochingName))=hex2dec('0103');
                map(strmatch('MUSCLE',H.gBS.EpochingName))=hex2dec('0103');
                map(strmatch('ELECTRODE',H.gBS.EpochingName))=hex2dec('0105');

                if ~isempty(H.gBS.EpochingSelect),
                        H.EVENT.TYP = map([H.gBS.EpochingSelect{:,9}]');
                        H.EVENT.POS = [H.gBS.EpochingSelect{:,1}]';
                        H.EVENT.CHN = [H.gBS.EpochingSelect{:,3}]';
                        H.EVENT.DUR = [H.gBS.EpochingSelect{:,4}]';
                end;
                
	elseif isfield(tmp,'P_C_DAQ_S');
                if ~isempty(tmp.P_C_DAQ_S.data),
                        signal = double(tmp.P_C_DAQ_S.data{1});
                        
                elseif ~isempty(tmp.P_C_DAQ_S.daqboard),
                        [tmppfad,file,ext] = fileparts(tmp.P_C_DAQ_S.daqboard{1}.ObjInfo.LogFileName),
                        file = [file,ext];
                        if exist(file,'file')
                                signal=daqread(file);        
                                H.info=daqread(file,'info');        
                        else
                                fprintf(H.FILE.stderr,'Error SLOAD: no data file found\n');
                                return;
                        end;
                        

⌨️ 快捷键说明

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