This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwritemicroforspykingcircus_concatinated_trials.m
65 lines (55 loc) · 2.91 KB
/
writemicroforspykingcircus_concatinated_trials.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
function [trialinfo,circuspath] = writemicroforspykingcircus(cfg, MuseStruct_micro)
fname_output = fullfile(cfg.datasavedir,[cfg.prefix,'single_pattern_',cfg.label,'_SC_trialinfo.mat']);
if exist(fname_output,'file') && cfg.force == false
fprintf('Loading trialinfo: %s \n',fname_output);
load(fname_output,'trialinfo','circuspath');
else
% read aligned micro data at full samplerate
% Note: no artefacts based on correlations are removed
cfgtemp = cfg;
cfgtemp.force = cfg.forcereload;
dat_microFs = readMicroFs(cfgtemp,MuseStruct_micro);
% workaround, readMicroFs will now add fsample
fnametemp = fullfile(MuseStruct_micro{1}.directory, MuseStruct_micro{1}.filenames(1).name);
hdrtemp = ft_read_header(fnametemp);
dat_microFs.fsample = hdrtemp.Fs;
% concatinate over repetitions, on preallocated memory
l = 0;
for itrial = 1 : size(dat_microFs.trial,2)
l = l + size(dat_microFs.trial{itrial},2);
end
dat = rmfield(dat_microFs,{'trial','time','cfg'});
dat.trial{1} = zeros(size(dat_microFs.trial{1},1),l);
isample = 1;
trialinfo = zeros(size(dat_microFs.trial,2),2);
for itrial = 1 : size(dat_microFs.trial,2)
fprintf('Concatinating trial %d of %d\n',itrial,size(dat_microFs.trial,2))
dat.trial{1}(:,isample:isample+size(dat_microFs.trial{itrial},2)-1) = dat_microFs.trial{itrial};
trialinfo(itrial,1) = isample;
trialinfo(itrial,2) = isample+size(dat_microFs.trial{itrial},2)-1;
isample = isample + size(dat_microFs.trial{itrial},2);
end
% select data
dat.trialdimord = '{rpt}_chan_time';
dat.time{1} = (0:size(dat.trial{1},2)-1); % *hdr_temp.Fs;
cfgtemp = [];
cfgtemp.channel = cellstr(cfg.channel);
dat = ft_selectdata(cfgtemp,dat);
% use headerinfo of first data file
hdr_temp = ft_read_header(fullfile(MuseStruct_micro{1}.directory,MuseStruct_micro{1}.filenames(1).name));
% write data in .ncs format
hdr = [];
hdr.Fs = hdr_temp.Fs;
hdr.nSamples = size(dat.trial{1},2);
hdr.nSamplePre = 0;
hdr.nChans = 1;
hdr.FirstTimeStamp = 0;
hdr.TimeStampPerSample = hdr_temp.TimeStampPerSample;
for ichan = 1 : size(dat.trial{1},1)
hdr.label{1} = dat_microFs.label{ichan};
fname = fullfile(cfg.datasavedir,[cfg.prefix,'single_pattern_',cfg.label,'_',dat.label{ichan},'.ncs']);
ft_write_data(fname,dat.trial{1}(ichan,:),'chanindx',1,'dataformat','neuralynx_ncs','header',hdr);
circuspath{ichan} = fname;
end
save(fname_output,'trialinfo','circuspath');
end