-
Notifications
You must be signed in to change notification settings - Fork 3
/
unisensCrop.m
215 lines (185 loc) · 7.86 KB
/
unisensCrop.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
function unisensCrop(path, new_path, crop_samplerate, start_samplestamp, end_samplestamp)
%UNISENSCROP crop a unisens dataset to a specified region
% Copyright 2017 movisens GmbH, Germany
crop_start_time = start_samplestamp / crop_samplerate;
crop_end_time = end_samplestamp / crop_samplerate;
%check if crop_end_time is smaller than crop_start_time
if start_samplestamp > end_samplestamp
error('crop end is smaller than crop start');
end
%open unisens dataset
j_unisensFactory = org.unisens.UnisensFactoryBuilder.createFactory();
j_unisens = j_unisensFactory.createUnisens(path);
%check if unisens dataset is long enough
if ~durationOk(j_unisens, end_samplestamp / crop_samplerate)
warning(['Unisens Dataset ' path ' is not long enough.']);
end
%create new unisens dataset
j_unisens_cropped = j_unisensFactory.createUnisens(new_path);
%set comment
j_unisens_cropped.setComment([char(j_unisens.getComment()) ' Cropped by unisensCrop(). Small jitter possible']);
%copy custom attibutes
j_custom_attributes = j_unisens.getCustomAttributes();
j_key_iterator = j_custom_attributes.keySet().iterator();
while( j_key_iterator. hasNext() )
j_key = j_key_iterator.next();
j_unisens_cropped.addCustomAttribute(j_key,j_custom_attributes.get(j_key));
end
%copy context information
j_context = j_unisens.getContext();
if ~isempty(j_context)
j_unisens_cropped.createContext(j_context.getSchemaUrl());
copyfile([path filesep 'context.xml'],new_path);
end
%set measurement id
measurement_id = j_unisens.getMeasurementId();
if ~isempty(measurement_id)
j_unisens_cropped.setMeasurementId(j_unisens.getMeasurementId());
end
%loop over all timed entries (signal, values and event entries)
j_entries = j_unisens.getEntries();
nEntries = j_entries.size();
for i = 0:nEntries-1
j_entry = j_entries.get(i);
entry_class_name= j_entry.getClass.toString;
if (strcmp(entry_class_name, 'class org.unisens.ri.SignalEntryImpl')) || ...
(strcmp(entry_class_name, 'class org.unisens.ri.ValuesEntryImpl')) || ...
(strcmp(entry_class_name, 'class org.unisens.ri.EventEntryImpl'))
%here we go
entry_samplerate= j_entries.get(i).getSampleRate();
entry_samplestamp_start = crop_start_time * entry_samplerate;
entry_samplestamp_end = crop_end_time * entry_samplerate;
if (strcmp(entry_class_name, 'class org.unisens.ri.SignalEntryImpl'))
%signalEntry
signal_entry_crop(j_entry, j_unisens_cropped, entry_samplestamp_start, entry_samplestamp_end);
elseif (strcmp(entry_class_name, 'class org.unisens.ri.ValuesEntryImpl'))
%valuesEntry
%values_entry_crop(j_entry, j_unisens_cropped, entry_samplestamp_start, entry_samplestamp_end);
elseif (strcmp(entry_class_name, 'class org.unisens.ri.EventEntryImpl'))
%eventEntry
event_entry_crop(j_entry, j_unisens_cropped, entry_samplestamp_start, entry_samplestamp_end);
end
elseif (strcmp(entry_class_name, 'class org.unisens.ri.CustomEntryImpl'))
%customEntry
disp('Crop not possible for custom Entries');
end
end
%set new duration in [s]
%TODO duration will be changed to double?
j_unisens_cropped.setDuration(crop_end_time-crop_start_time)
%set new timesamtp start if available
j_timestamp_start = j_unisens.getTimestampStart();
if ~isempty(j_timestamp_start)
j_unisens_cropped.setTimestampStart(java.util.Date(j_timestamp_start.getTime() + (crop_start_time*1000)));
end
%copy groups
j_groups = j_unisens.getGroups();
nGroups = j_groups.size();
for i = 0:nGroups-1
j_group = j_groups.get(i);
j_group_cropped = j_unisens_cropped.createGroup(j_group.getId());
j_group_entries = j_group.getEntries();
nEntries = j_group.size();
for j = 0:nEntries
j_group_cropped.addEntry(j_unisens_cropped.getEntry(j_group_entries.get(j).getId()));
end
end
%unisens speichern
j_unisens_cropped.save();
j_unisens_cropped.closeAll();
j_unisens.closeAll();
end
function signal_entry_crop(j_entry, j_unisens_cropped, samplestamp_start, samplestamp_end)
%copy entry information
j_entry_cropped=j_unisens_cropped.addEntry(j_entry.clone(),false);
%copy data piecewise
position = samplestamp_start;
while (position < samplestamp_end)
if (samplestamp_end - position > 1000000)
count = 1000000;
else
count = samplestamp_end - position;
end
data = j_entry.read(position, count);
if ~isempty(data)
j_entry_cropped.append(data);
end
position = position + count;
end
end
function values_entry_crop(j_entry, j_unisens_cropped, samplestamp_start, samplestamp_end)
j_entry_cropped=j_unisens_cropped.addEntry(j_entry.clone(),false);
%copy values with timeshifted samplestamp
while (true)
j_values = j_entry.read(100000);
nValues = j_values.size();
if nValues==0
break;
end
%TODO use arrayList of values for speed, add funtion to unisens
%library
for i=1:nValues
j_value=j_values(i);
if ~isempty(j_value)
samplestamp = j_value.getSampleStamp();
if (samplestamp > samplestamp_start)
if (samplestamp <=samplestamp_end)
%TODO deep copy needed?
j_value.setSamplestamp(j_value.getSamplestamp()-samplestamp_start);
%j_value_cropped = Value(j_value.getSamplestamp()-samplestamp_start, j_value.getData());
j_entry_cropped.append(j_value);
else
%break if first value is outside crop region
break;
end
end
else
break;
end
end
end
end
function event_entry_crop(j_entry, j_unisens_cropped, samplestamp_start, samplestamp_end)
j_entry_cropped=j_unisens_cropped.addEntry(j_entry.clone(),false);
%copy eventy with timeshifted samplestamp
while (true)
j_events = j_entry.read(100000);
nEvents = j_events.size();
if nEvents==0
break;
end
j_event_iterator = j_events.iterator();
croppedEvents = java.util.ArrayList();
while (j_event_iterator.hasNext())
j_event=j_event_iterator.next();
samplestamp = j_event.getSamplestamp();
if (samplestamp > samplestamp_start)
if (samplestamp <=samplestamp_end)
%TODO deep copy needed?
%j_event_cropped = org.unisens.Event(j_event.getSamplestamp()-samplestamp_start, j_event.getType(),j_event.getComment());
j_event.setSamplestamp(j_event.getSamplestamp()-samplestamp_start);
croppedEvents.add(j_event);
else
%break if first event is outside crop region
break;
end
end
end
j_entry_cropped.append(croppedEvents);
end
end
function result = durationOk(j_unisens, minDuration)
j_entries = j_unisens.getEntries();
nEntries = j_entries.size();
allDurations = [];
for i = 0:nEntries-1
j_entry = j_entries.get(i);
entry_class_name= j_entry.getClass.toString;
if strcmp(entry_class_name, 'class org.unisens.ri.SignalEntryImpl')
nSamples=j_entry.getCount();
sampleRate = j_entry.getSampleRate();
allDurations=[allDurations nSamples/sampleRate];
end
end
result = min(allDurations) > minDuration;
end