-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_process_128_poly5.m
163 lines (156 loc) · 6.38 KB
/
pre_process_128_poly5.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
160
161
162
163
function pre_process_128_poly5(output_filename, poly5_file_extensor, poly5_file_flexor, options)
%PRE_PROCESS_128_POLY5 For pre-processing sets of 4x poly5 recordings, using 8x8 standard PCB grids and acquired using polybench, so that they are ready for the CKC reader.
%
% Syntax:
% ckc.pre_process_128_poly5(output_filename, poly5_file_extensor, poly5_file_flexor, 'Name', value, ...);
%
% Inputs:
% output_filename (1,1) string
% poly5_file_extensor (1,1) string
% poly5_file_flexor (1,1) string
%
% Options:
% DataRoot {mustBeTextScalar} = "";
% ApplyFilter (1,1) logical = true;
% HighpassFilterCutoff (1,1) double = 100;
% ApplyRMSCutoff (1,1) logical = false;
% RMSCutoff (1,2) double = [1, 100];
% ApplyGridInterpolation (1,1) logical = true;
% InitialPulseOffset (1,1) {mustBeInteger} = 0; % Samples prior to first rising pulse, to include.
% SampleRate (1,1) double {mustBeMember(options.SampleRate, [2000, 4000])} = 2000;
% TriggerChannelIndicator {mustBeTextScalar} = 'TRIG';
% TriggerBitMask = [];
% ExcludedPulseIndices (1,:) {mustBeInteger,mustBePositive} = [];
%
%
% Output:
% Saves the concatenated and synchronized monopolar array data from all
% input files to the file indicated by `output_filename,` in an
% appropriate which should be compatible with the corresponding
% `ckc.reader_<pipeline>.m` file.
%
% See also: Contents
arguments
output_filename (1,1) string
poly5_file_extensor (1,1) string
poly5_file_flexor (1,1) string
options.DataRoot {mustBeTextScalar} = "";
options.AlignSync (1,1) logical = true;
options.ApplyFilter (1,1) logical = true;
options.ApplyCAR (1,1) logical = true;
options.ApplySpatialLaplacian (1,1) logical = false;
options.HighpassFilterCutoff (1,1) double = 100;
options.ApplyRMSCutoff (1,1) logical = true;
options.RMSCutoff (1,2) double = [1, 100];
options.ZeroMissing (1,1) logical = true; % Sets "missing" samples as zeros
options.ApplyGridInterpolation (1,1) logical = true;
options.InitialPulseOffset (1,1) {mustBeInteger} = 0; % Samples prior to first rising pulse, to include.
options.SampleRate (1,1) double {mustBeMember(options.SampleRate, [2000, 4000])} = 2000;
options.Sync = []; % If specified, should be a 2 x k array where first row is time and second is sync value
options.SyncTarget = [];
options.InvertSyncLogic = [];
options.TriggerChannelIndicator {mustBeTextScalar} = 'TRIG';
options.TriggerBitMask = 1;
options.ExcludedPulseIndices (1,:) {mustBeInteger,mustBePositive} = [];
options.IsTextile64 (1,1) logical = true;
options.Description {mustBeTextScalar} = "1-64 = PROX-EXT; 65-128 = DIST-EXT; 129-192 = PROX-FLX; 193-256 = DIST-FLX."
end
if exist(output_filename,'file')~=0
result = questdlg(sprintf('Output file (%s) already exists. Overwrite existing file?', output_filename), ...
'Overwrite Existing Pre-Processing?', ...
'Yes', 'No', 'No');
if ~strcmpi(result,'Yes')
disp("Pre-processing canceled.");
return;
end
end
if strlength(options.DataRoot) == 0
poly5_files = [poly5_file_extensor; ...
poly5_file_flexor];
else
poly5_files = [fullfile(options.DataRoot,poly5_file_extensor); ...
fullfile(options.DataRoot,poly5_file_flexor)];
end
if isscalar(options.TriggerBitMask)
triggerBitMask = ones(numel(poly5_files),1).*options.TriggerBitMask;
else
triggerBitMask = options.TriggerBitMask;
end
[data,~,ch_name] = io.load_align_saga_data_many(poly5_files, ...
'ApplyFilter', options.ApplyFilter, ...
'ApplyCAR', options.ApplyCAR, ...
'HighpassFilterCutoff', options.HighpassFilterCutoff, ...
'ApplyRMSCutoff', options.ApplyRMSCutoff, ...
'RMSCutoff', options.RMSCutoff, ...
'ZeroMissing',options.ZeroMissing,...
'ApplyGridInterpolation', options.ApplyGridInterpolation, ...
'ApplySpatialLaplacian', options.ApplySpatialLaplacian, ...
'InitialPulseOffset', options.InitialPulseOffset, ...
'InvertLogic', options.InvertSyncLogic, ...
'SampleRate', options.SampleRate, ...
'TriggerChannelIndicator', options.TriggerChannelIndicator, ...
'TriggerBitMask', triggerBitMask, ...
'IsTextile64', options.IsTextile64, ...
'ExcludedPulseIndices', options.ExcludedPulseIndices);
[iUni,~,iTrig] = ckc.get_saga_channel_masks(ch_name,...
'ReturnNumericIndices',true);
uni = data.samples(iUni,:);
sample_rate = data.sample_rate;
ii = 1;
sync = data.samples(iTrig(1),:);
if isempty(options.InvertSyncLogic)
sync = double(bitand(data.samples,triggerBitMask(1))==triggerBitMask(1));
else
if numel(options.InvertSyncLogic) > 1
else
if options.InvertSyncLogic
sync = double(bitand(data.samples,triggerBitMask(1))~=triggerBitMask(1));
else
sync = double(bitand(data.samples,triggerBitMask(1))==triggerBitMask(1));
end
end
end
while ((ii < numel(iTrig)) && (numel(unique(sync))<2))
ii = ii + 1;
if isempty(options.InvertSyncLogic)
sync = double(bitand(data.samples,triggerBitMask(ii))==triggerBitMask(ii));
else
if numel(options.InvertSyncLogic) > 1
else
if options.InvertSyncLogic
sync = double(bitand(data.samples,triggerBitMask(ii))~=triggerBitMask(ii));
else
sync = double(bitand(data.samples,triggerBitMask(ii))==triggerBitMask(ii));
end
end
end
end
t_data = 0:(1/data.sample_rate):((numel(sync)-1)/data.sample_rate);
t_start = t_data(find(sync > 0,1,'first'));
if ~isempty(options.Sync)
if options.AlignSync
sync_in = [0, 0, options.Sync(2,:), 0, 0];
sync_time = options.Sync(1,:) + t_start;
sync_time = [0, sync_time(1)-(1/data.sample_rate), sync_time, sync_time(end)+(1/data.sample_rate), t_data(end)];
sync = interp1(sync_time, sync_in, t_data, 'linear');
else
sync = options.Sync;
end
end
if ~isempty(options.SyncTarget)
sync_in = [0, options.SyncTarget(:,2)', 0];
sync_time = options.SyncTarget(:,1)' + t_start;
[sync_time, ikeep] = unique([0, sync_time, t_data(end)]);
sync_out = interp1(sync_time, sync_in(ikeep), t_data, 'previous');
sync = [sync; sync_out];
end
aux = [];
description = options.Description;
[p,~,~] = fileparts(output_filename);
if strlength(p) > 0
if exist(p,'dir')==0
mkdir(p);
end
end
save(output_filename,'uni','aux','sync','sample_rate','description','-v7.3');
end