-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfdcsTableToDetection.m
More file actions
152 lines (133 loc) · 4.93 KB
/
Copy pathlfdcsTableToDetection.m
File metadata and controls
152 lines (133 loc) · 4.93 KB
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
function t = lfdcsTableToDetection(ravenFile,soundFolder,siteCode,classification)
% Convert LFDCS detections into a detection table compatible with the
% callDensity workflow.
%
% Optional inputs:
% soundFolder
% siteCode
% classification
%% Optional inputs
if nargin < 4
classification = '';
end
if nargin < 3
siteCode = '';
end
if nargin < 2
soundFolder = '';
end
%% Read table
warning('off','MATLAB:table:ModifiedVarnames');
if istable(ravenFile)
t = ravenFile;
else
t = readtable(ravenFile,'delimiter','\t');
end
nDetect = height(t);
if nDetect == 0
return;
end
%% Channel
if any(strcmpi(t.Properties.VariableNames,'channel'))
t.channel = t.channel;
else
t.channel = ones(nDetect,1);
end
%% Convert datetime columns
% Two supported source formats:
% 1) start_datetime already present as 'yyyy-MM-dd HH:mm:ss.SSS'
% 2) Detection_DateTime_Raw ('MM/dd/yy HH:mm:ss') + Fractional_Second,
% as produced by the raw LFDCS export (after header-stripping via
% standardizeLFDCSOutput.m). Reassembled here into start_datetime.
if any(strcmpi(t.Properties.VariableNames,'start_datetime'))
t.start_datetime = datetime(t.start_datetime, ...
'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSS');
elseif any(strcmpi(t.Properties.VariableNames,'Detection_DateTime_Raw'))
dt = datetime(t.Detection_DateTime_Raw, ...
'InputFormat', 'MM/dd/yy HH:mm:ss');
if any(strcmpi(t.Properties.VariableNames,'Fractional_Second'))
dt = dt + seconds(t.Fractional_Second);
end
dt.Format = 'yyyy-MM-dd HH:mm:ss.SSS';
t.start_datetime = dt;
else
error(['Could not find a start datetime column (expected ', ...
'start_datetime or Detection_DateTime_Raw + Fractional_Second).']);
end
%% Resolve Dynamic Column Names (Flexible Mapping)
% Check for duration column variations
if any(strcmpi(t.Properties.VariableNames, 'detection_duration_s'))
t.detection_duration_s = t.detection_duration_s;
elseif any(strcmpi(t.Properties.VariableNames, 'Duration_s'))
t.detection_duration_s = t.Duration_s;
else
error('Could not find a duration column (expected detection_duration_s or Duration_s).');
end
% Check for minimum frequency variations
if any(strcmpi(t.Properties.VariableNames, 'minimum_frequency_hz'))
t.minimum_frequency_hz = t.minimum_frequency_hz;
elseif any(strcmpi(t.Properties.VariableNames, 'Min_Frequency_Hz'))
t.minimum_frequency_hz = t.Min_Frequency_Hz;
else
error('Could not find a minimum frequency column.');
end
% Check for maximum frequency variations
if any(strcmpi(t.Properties.VariableNames, 'maximum_frequency_hz'))
t.maximum_frequency_hz = t.maximum_frequency_hz;
elseif any(strcmpi(t.Properties.VariableNames, 'Max_Frequency_Hz'))
t.maximum_frequency_hz = t.Max_Frequency_Hz;
else
error('Could not find a maximum frequency column.');
end
%% Calculate end_datetime using the dynamically mapped duration
t.end_datetime = t.start_datetime + seconds(t.detection_duration_s);
%% Handle missing file_start_time to avoid relative calculation crashes
if ~any(strcmpi(t.Properties.VariableNames,'file_start_time'))
t.file_start_time = t.start_datetime;
else
t.file_start_time = datetime(t.file_start_time, ...
'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSS');
end
%% Relative times within wav file
t.BeginTime_s_ = seconds(t.start_datetime - t.file_start_time);
t.EndTime_s_ = t.BeginTime_s_ + t.detection_duration_s;
t.DeltaTime_s_ = t.detection_duration_s;
%% Matlab datenums
t.t0 = datenum(t.start_datetime);
t.tEnd = datenum(t.end_datetime);
t.duration = t.detection_duration_s;
%% Frequency fields
t.fLow = t.minimum_frequency_hz;
t.fHigh = t.maximum_frequency_hz;
% LFDCS's min/max_frequency_hz columns may actually encode sweep
% start/end frequency rather than true numeric min/max -- a downsweep
% D-call would then have "minimum" (the start, higher) > "maximum" (the
% end, lower). Swap so fLow is always numerically <= fHigh, which is
% what downstream QC/SNR code assumes regardless of call direction.
swapIx = t.fLow > t.fHigh;
if any(swapIx)
tmp = t.fLow(swapIx);
t.fLow(swapIx) = t.fHigh(swapIx);
t.fHigh(swapIx) = tmp;
end
t.freq = [t.fLow t.fHigh];
%% File information
if any(strcmpi(t.Properties.VariableNames,'filename'))
t.BeginFile = string(t.filename);
else
t.BeginFile = strings(nDetect,1);
end
%% Metadata
% NOTE: wrap in cell *before* repmat (not repmat-then-cellstr) -- repmat
% of an empty string stays empty regardless of nDetect, which breaks the
% table-row-count assignment whenever soundFolder/siteCode/classification
% is '' (the default when not supplied by the caller). Wrapping in {...}
% first replicates a 1x1 cell correctly regardless of the contained
% value's emptiness. Matches the safe pattern already used in
% cnnTableToDetection.m.
t.soundFolder = repmat({soundFolder},nDetect,1);
t.siteCode = repmat({siteCode},nDetect,1);
t.classification = repmat({classification},nDetect,1);
%% Sort detections chronologically
t = sortrows(t,'t0');
end