forked from junejing/EEGALY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN1_process_chunk.m
163 lines (141 loc) · 5.7 KB
/
N1_process_chunk.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function N1_process_chunk = N1_process_chunk(Dmeg,Begpts,Endpts, flags)
% N2_chunk = crc_N2_chunk(Dmeg,Begpts,Endpts, flags)
%
% Chunks MEEG object Dmeg with start at Begpts and stop at Endpts, both of
% these expressed in "time points".
% 'flags' is a structure with several optional fields:
% flags
% .overwr : previous chunks are overwritten [1] or not [0, def.]
% .numchunk : fix the chunk index, useful when overwriting. Ortherwise
% the index is incremented from already existing files on disk
% The default value is 1
% .prefix : filename prefic, 'chk' by default
% .clockt : use the clocktime [1] or not [0, def.]
% All default values are defined in crc_defaults.m
%
% The chunked data file is written on disk, and its filename returned.
flags_def = crc_get_defaults('chk');
flags = crc_check_flag(flags_def,flags);
% Identifying file chunk number, overwriting or not
filename='N1_chunk';
mkdir(path(Dmeg),'N1_chunk');
mkdir(path(Dmeg),'N1_chunk_mat');
filepath1=path(Dmeg);
filepath = strcat(filepath1,'\N1_chunk');
filepath2=strcat(filepath1,'\N1_chunk_mat');
numchk = flags.numchunk;
if ~flags.overwr
while exist(fullfile( ...
filepath,[flags.prefix,num2str(numchk),'_n1_' fname(Dmeg)]),'file')
numchk=numchk+1;
end
end
prefix = [flags.prefix,num2str(numchk),'_n1_'];
N1_process_chunk = fullfile(Dmeg.path,[prefix Dmeg.fname]);
if numchk<10
prefix2=['0',num2str(numchk)];
else
prefix2=num2str(numchk);
end
prefix2=fullfile(Dmeg.path,[prefix2 '.mat']);
% if Begpts==1
% winsize = median([Dmeg.CRC.score{3,:}]);
% Begpts = 1;
% lastwin = ceil(Endpts/(fsample(Dmeg)*winsize));
% Endpts = lastwin*winsize*fsample(Dmeg);
% else
% try
% winsize = median([Dmeg.CRC.score{3,:}]);
%
% firstwin =floor(Begpts/(fsample(Dmeg)*winsize))+1;
% Begpts = (firstwin )*winsize*fsample(Dmeg)+1; %%%%%%将firstwin-1改为firstwin时,窗口没有延时,
%
% lastwin = ceil(Endpts/(fsample(Dmeg)*winsize));
% Endpts = lastwin*winsize*fsample(Dmeg);
% end
% end
Dcopy = clone(Dmeg,N1_process_chunk,[Dmeg.nchannels Endpts-Begpts+1 1]);%%%经过这一步产生一个DAT文件
%==========================================================================
% DIRTY CODE :
% getting rid of offset in orginal data file.
% This should actually be part of the clone function or at least handled
% somewhere in the object definition and methods...
% => have to use the structure form
% DO NOT DO THIS AT HOME. :-)
Dcs = struct(Dcopy);
Dcs.data.y.offset = 0;
Dcopy = meeg(Dcs);
%==========================================================================
try
for nsc=1:size(Dmeg.CRC.score,2)
Dcopy.CRC.score{1,nsc} = Dmeg.CRC.score{1,nsc}(firstwin+1:lastwin); %%%%%%%firstwin+1
Dcopy.CRC.score{4,nsc} = [1/fsample(Dmeg) (Endpts-Begpts-1)/fsample(Dmeg)];
tmp = Dmeg.CRC.score{5,nsc}-Begpts/fsample(Dmeg);
idxkept = find(~all((tmp<0) + (tmp>(Endpts-Begpts)/fsample(Dmeg)),2));
tmp = tmp(idxkept,:);
tmp(tmp<0) = 1/fsample(Dmeg);
tmp(tmp>(Endpts-Begpts)/fsample(Dmeg)) = (Endpts-Begpts-1)/fsample(Dmeg);
Dcopy.CRC.score{5,nsc} = tmp;
if size(Dmeg.CRC.score,1)==8 ...
&& ~isempty(Dmeg.CRC.score{8,nsc})
if ~isempty(idxkept)
Dcopy.CRC.score{8,nsc} = {Dmeg.CRC.score{8,nsc}{idxkept}};
else
Dcopy.CRC.score{8,nsc} = {};
end
end
tmp = Dmeg.CRC.score{6,nsc}-Begpts/fsample(Dmeg);
idxkept = find(~all((tmp<0) + (tmp>(Endpts-Begpts)/fsample(Dmeg)),2));
tmp = tmp(idxkept,:);
tmp(tmp<0) = 1/fsample(Dmeg);
tmp(tmp>(Endpts-Begpts)/fsample(Dmeg)) = (Endpts-Begpts-1)/fsample(Dmeg);
Dcopy.CRC.score{6,nsc}=tmp;
tmp = Dmeg.CRC.score{7,nsc}-Begpts/fsample(Dmeg);
idxkept = find(~all((tmp<0) + (tmp>(Endpts-Begpts)/fsample(Dmeg)),2));
tmp = tmp(idxkept,:);
tmp(tmp<0) = 1/fsample(Dmeg);
tmp(tmp>(Endpts-Begpts)/fsample(Dmeg)) = (Endpts-Begpts-1)/fsample(Dmeg);
Dcopy.CRC.score{7,nsc}=tmp;
end
end
% Limit size of data chunk loaded in memory in one step.
Maxmem = crc_get_defaults('mem.maxmemload'); % Maxmem in byte
% Assuming data are in 32 bits/ 4 bytes
Maxelts = floor(Maxmem/(nchannels(Dmeg)*4));
Nmbchk = ceil((Endpts-Begpts)/Maxelts);
% Update structure and save file
% handling the starting time information if available
if flags.clockt
hour = Dcopy.info.hour;
date = Dcopy.info.date;
timeshift = ...
datenum(double([0 0 0 crc_time_converts(Begpts/fsample(Dmeg))]));
newbeg = datevec(datenum(double([date hour])) + timeshift);
Dcopy.info.hour = newbeg(4:6);
Dcopy.info.date = newbeg(1:3);
end
% Not too sure about his about handling the events/trials !!!!
% Should check with a file with loads of events...
Ev = Dcopy.events;
l_keep = []; % Look for events within the new file
for mm=1:numel(Ev)
Ev(mm).time = Ev(mm).time - Begpts/fsample(Dmeg);
if Ev(mm).time>=0 && Ev(mm).time<(Endpts-Begpts)/fsample(Dmeg)
l_keep = [l_keep mm];
end
end
Dcopy = events(Dcopy,1,Ev(l_keep));
for ii=1:Nmbchk
ch_data = Dmeg(:,(Begpts+Maxelts*(ii-1)): ...
min([Begpts+Maxelts*ii-1 nsamples(Dmeg) Endpts]));
Dcopy(:,Maxelts*(ii-1)+(1:size(ch_data,2))) = ch_data;
end
filepath=path(Dmeg);
filepath = strcat(filepath,'\N1_chunk');
save(Dcopy); %%%%%%%%%%%%%%%在后面加上分号,就不会显示ans=1;
b=Dcopy(:,:,1);
save(prefix2,'b');
movefile(prefix2,filepath2);
movefile(N1_process_chunk,filepath);
datFile=strcat(N1_process_chunk(1:end-4),'.dat');
movefile(datFile,filepath);