| 1 |
classdef WaveformHeader < handle |
| 2 |
|
| 3 |
properties |
| 4 |
|
| 5 |
version = uint16([]); % First unsigned int indicates the version % |
| 6 |
flags = uint64([]); % bit field with flags % |
| 7 |
measurement_uid = uint32([]); % Unique ID for the measurement % |
| 8 |
scan_counter = uint32([]); % Current acquisition number in the measurement % |
| 9 |
time_stamp = uint32([]); % Physiology time stamps, e.g. ecg, breating, etc. % |
| 10 |
number_of_samples = uint16([]); % Number of samples acquired % |
| 11 |
channels = uint16([]); % Available channels % |
| 12 |
sample_time_us = single([]); % Time between samples in micro seconds, sampling BW % |
| 13 |
waveform_id = uint16([]); |
| 14 |
|
| 15 |
|
| 16 |
end |
| 17 |
|
| 18 |
methods |
| 19 |
|
| 20 |
function obj = WaveformHeader(arg) |
| 21 |
% Constructor |
| 22 |
|
| 23 |
switch nargin |
| 24 |
case 0 |
| 25 |
% No argument constructor |
| 26 |
% initialize to a single acquisition header |
| 27 |
extend(obj,1); |
| 28 |
|
| 29 |
case 1 |
| 30 |
% One argument constructor |
| 31 |
if isstruct(arg) |
| 32 |
% plain struct |
| 33 |
fromStruct(obj,arg); |
| 34 |
elseif (length(arg)==1 && ismrmrd.util.isInt(arg)) == 1 |
| 35 |
% number |
| 36 |
extend(obj,arg); |
| 37 |
elseif isa(arg,'uint8') |
| 38 |
% Byte array |
| 39 |
fromBytes(obj,arg); |
| 40 |
else |
| 41 |
% Unknown type |
| 42 |
error('Unknown argument type.') |
| 43 |
end |
| 44 |
|
| 45 |
otherwise |
| 46 |
error('Wrong number of arguments.') |
| 47 |
|
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
function nacq = getNumber(obj) |
| 52 |
% Return the number of headers |
| 53 |
|
| 54 |
nacq = length(obj.version); |
| 55 |
|
| 56 |
end |
| 57 |
|
| 58 |
function hdr = select(obj, range) |
| 59 |
% Return a copy of a range of acquisition headers |
| 60 |
|
| 61 |
% create an empty acquisition header |
| 62 |
M = length(range); |
| 63 |
hdr = ismrmrd.AcquisitionHeader(M); |
| 64 |
|
| 65 |
% Fill |
| 66 |
hdr.version = obj.version(range); |
| 67 |
hdr.flags = obj.flags(range); |
| 68 |
hdr.measurement_uid = obj.measurement_uid(range); |
| 69 |
hdr.scan_counter = obj.scan_counter(range); |
| 70 |
hdr.time_stamp= obj.time_stamp(range); |
| 71 |
hdr.number_of_samples = obj.number_of_samples(range); |
| 72 |
hdr.channels = obj.channels(range); |
| 73 |
hdr.sample_time_us = obj.sample_time_us(range); |
| 74 |
hdr.waveform_id = obj.waveform_id(range); |
| 75 |
|
| 76 |
end |
| 77 |
|
| 78 |
function extend(obj,N) |
| 79 |
% Extend with blank header |
| 80 |
|
| 81 |
range = obj.getNumber + (1:N); |
| 82 |
obj.version(1,range) = zeros(1,N,'uint16'); |
| 83 |
obj.flags(1,range) = zeros(1,N,'uint64'); |
| 84 |
obj.measurement_uid(1,range) = zeros(1,N,'uint32'); |
| 85 |
obj.scan_counter(1,range) = zeros(1,N,'uint32'); |
| 86 |
obj.time_stamp(1,range) = zeros(1,N,'uint32'); |
| 87 |
obj.number_of_samples(1,range) = zeros(1,N,'uint16'); |
| 88 |
obj.channels(1,range) = zeros(1,N,'uint16'); |
| 89 |
obj.channels(1,range) = zeros(1,N,'uint16'); |
| 90 |
obj.sample_time_us(1,range) = zeros(1,N,'single'); |
| 91 |
obj.waveform_id(1,range) = zeros(1,N,'uint16'); |
| 92 |
end |
| 93 |
|
| 94 |
function append(obj, head) |
| 95 |
% Append a header |
| 96 |
|
| 97 |
Nstart = obj.getNumber + 1; |
| 98 |
Nend = obj.getNumber + length(head.version); |
| 99 |
Nrange = Nstart:Nend; |
| 100 |
obj.version(1,Nrange) = hdr.version(:); |
| 101 |
obj.flags(1,Nrange) = hdr.flags(:); |
| 102 |
obj.measurement_uid(1,Nrange) = hdr.measurement_uid(:); |
| 103 |
obj.scan_counter(1,Nrange) = hdr.scan_counter(:); |
| 104 |
obj.time_stamp(1,Nrange) = hdr.time_stamp(:); |
| 105 |
obj.number_of_samples(1,Nrange) = hdr.number_of_samples(:); |
| 106 |
obj.channels(1,Nrange) = hdr.channels(:); |
| 107 |
obj.sample_time_us(1,Nrange) = hdr.sample_time_us(:); |
| 108 |
obj.waveform_id(1,Nrange) = hdr.waveform_id(:); |
| 109 |
|
| 110 |
end |
| 111 |
|
| 112 |
function fromStruct(obj, hdr) |
| 113 |
% Convert a struct to the object |
| 114 |
N = length(hdr.version); |
| 115 |
%warning! no error checking |
| 116 |
obj.version = reshape(hdr.version,[1,N]); |
| 117 |
obj.flags = reshape(hdr.flags,[1,N]); |
| 118 |
obj.measurement_uid = reshape(hdr.measurement_uid,[1,N]); |
| 119 |
obj.scan_counter = reshape(hdr.scan_counter,[1,N]); |
| 120 |
obj.time_stamp = reshape(hdr.time_stamp,[1,N]); |
| 121 |
obj.number_of_samples = reshape(hdr.number_of_samples,[1,N]); |
| 122 |
obj.channels = reshape(hdr.channels,[1,N]); |
| 123 |
obj.sample_time_us = reshape(hdr.sample_time_us,[1,N]); |
| 124 |
obj.waveform_id = reshape(hdr.waveform_id, [1,N]); |
| 125 |
end |
| 126 |
|
| 127 |
function hdr = toStruct(obj) |
| 128 |
% Convert the object to a plain struct |
| 129 |
|
| 130 |
%warning! no error checking |
| 131 |
hdr = struct(); |
| 132 |
hdr.version = obj.version; |
| 133 |
hdr.flags = obj.flags; |
| 134 |
hdr.measurement_uid = obj.measurement_uid; |
| 135 |
hdr.scan_counter = obj.scan_counter; |
| 136 |
hdr.time_stamp = obj.time_stamp; |
| 137 |
hdr.number_of_samples = obj.number_of_samples; |
| 138 |
hdr.channels = obj.channels; |
| 139 |
hdr.sample_time_us = obj.sample_time_us; |
| 140 |
hdr.waveform_id = obj.waveform_id; |
| 141 |
|
| 142 |
end |
| 143 |
|
| 144 |
function fromBytes(obj, bytearray) |
| 145 |
% Convert from a byte array to an ISMRMRD AcquisitionHeader |
| 146 |
% This conforms to the memory layout of the C-struct |
| 147 |
|
| 148 |
if size(bytearray,1) ~= 38 |
| 149 |
error('Wrong number of bytes for AcquisitionHeader.') |
| 150 |
end |
| 151 |
N = size(bytearray,2); |
| 152 |
|
| 153 |
obj.version = reshape(typecast(reshape(bytearray( 1: 2, :), 1, 2 * N), 'uint16'), 1, N); % First unsigned int indicates the version % |
| 154 |
obj.flags = reshape(typecast(reshape(bytearray( 9: 16, :), 1, 8 * N), 'uint64'), 1, N); % bit field with flags % |
| 155 |
obj.measurement_uid = reshape(typecast(reshape(bytearray( 17: 20, :), 1, 4 * N), 'uint32'), 1, N); % Unique ID for the measurement % |
| 156 |
obj.scan_counter = reshape(typecast(reshape(bytearray( 21: 24, :), 1, 4 * N), 'uint32'), 1, N); % Current acquisition number in the measurement % |
| 157 |
obj.acquisition_time_stamp = reshape(typecast(reshape(bytearray( 25: 28, :), 1, 4 * N), 'uint32'), 1, N); % Acquisition clock % |
| 158 |
obj.number_of_samples = reshape(typecast(reshape(bytearray( 29: 30, :), 1, 2 * N), 'uint16'), 1, N); % Number of samples acquired % |
| 159 |
obj.channels = reshape(typecast(reshape(bytearray( 31 : 32, :), 1, 2 * N), 'uint16'), 1, N); % Available channels% |
| 160 |
obj.sample_time_us = reshape(typecast(reshape(bytearray( 33: 36, :), 1, 2 * N), 'single'), 1, N); % Sample time in micro seconds % |
| 161 |
obj.waveform_id = reshape(typecast(reshape(bytearray( 37: 38, :), 1, 2 * N), 'uint16'), 1, N); % Waveform ID % |
| 162 |
|
| 163 |
|
| 164 |
end |
| 165 |
|
| 166 |
function bytes = toBytes(obj) |
| 167 |
% Convert to an ISMRMRD AcquisitionHeader to a byte array |
| 168 |
% This conforms to the memory layout of the C-struct |
| 169 |
|
| 170 |
N = obj.getNumber; |
| 171 |
bytes = zeros(38,N,'uint8'); |
| 172 |
for p = 1:N |
| 173 |
off = 1; |
| 174 |
bytes(off:off+1,p) = typecast(obj.version(p) ,'uint8'); off=off+8; |
| 175 |
bytes(off:off+7,p) = typecast(obj.flags(p) ,'uint8'); off=off+8; |
| 176 |
bytes(off:off+3,p) = typecast(obj.measurement_uid(p) ,'uint8'); off=off+4; |
| 177 |
bytes(off:off+3,p) = typecast(obj.scan_counter(p) ,'uint8'); off=off+4; |
| 178 |
bytes(off:off+3,p) = typecast(obj.time_stamp(p) ,'uint8'); off=off+4; |
| 179 |
bytes(off:off+1,p) = typecast(obj.number_of_samples(p) ,'uint8'); off=off+2; |
| 180 |
bytes(off:off+1,p) = typecast(obj.channels(p) ,'uint8'); off=off+2; |
| 181 |
bytes(off:off+3,p) = typecast(obj.sample_time_us(p) ,'uint8'); off=off+4; |
| 182 |
bytes(off:off+1,p) = typecast(obj.waveform_id(p) ,'uint8'); off=off+2; |
| 183 |
end |
| 184 |
end |
| 185 |
|
| 186 |
|
| 187 |
function ret = flagIsSet(obj, flag, range) |
| 188 |
% bool = obj.flagIsSet(flag, range) |
| 189 |
|
| 190 |
if nargin < 3 |
| 191 |
range = 1:obj.getNumber; |
| 192 |
end |
| 193 |
if isa(flag, 'char') |
| 194 |
b = obj.FLAGS.(flag); |
| 195 |
elseif (flag>0) |
| 196 |
b = uint64(flag); |
| 197 |
else |
| 198 |
error('Flag is of the wrong type.'); |
| 199 |
end |
| 200 |
bitmask = bitshift(uint64(1),(b-1)); |
| 201 |
ret = zeros(size(range)); |
| 202 |
for p = 1:length(range) |
| 203 |
ret(p) = (bitand(obj.flags(range(p)), bitmask)>0); |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
function flagSet(obj, flag, range) |
| 208 |
if nargin < 3 |
| 209 |
range = 1:obj.getNumber; |
| 210 |
end |
| 211 |
if isa(flag, 'char') |
| 212 |
b = obj.FLAGS.(flag); |
| 213 |
elseif (flag>0) |
| 214 |
b = uint64(flag); |
| 215 |
else |
| 216 |
error('Flag is of the wrong type.'); |
| 217 |
end |
| 218 |
bitmask = bitshift(uint64(1),(b-1)); |
| 219 |
|
| 220 |
alreadyset = obj.flagIsSet(flag,range); |
| 221 |
for p = 1:length(range) |
| 222 |
if ~alreadyset(p) |
| 223 |
obj.flags(range(p)) = obj.flags(range(p)) + bitmask; |
| 224 |
end |
| 225 |
end |
| 226 |
end |
| 227 |
|
| 228 |
function flagClear(obj, flag, range) |
| 229 |
if nargin < 3 |
| 230 |
range = 1:obj.getNumber; |
| 231 |
end |
| 232 |
|
| 233 |
if isa(flag, 'char') |
| 234 |
b = obj.FLAGS.(flag); |
| 235 |
elseif (flag>0) |
| 236 |
b = uint64(flag); |
| 237 |
else |
| 238 |
error('Flag is of the wrong type.'); |
| 239 |
end |
| 240 |
bitmask = bitshift(uint64(1),(b-1)); |
| 241 |
|
| 242 |
alreadyset = obj.flagIsSet(flag,range); |
| 243 |
for p = 1:length(range) |
| 244 |
if alreadyset(p) |
| 245 |
obj.flags(range(p)) = obj.flags(range(p)) - bitmask; |
| 246 |
end |
| 247 |
end |
| 248 |
end |
| 249 |
|
| 250 |
function flagClearAll(obj, range) |
| 251 |
if nargin < 2 |
| 252 |
range = 1:obj.getNumber; |
| 253 |
end |
| 254 |
obj.flags(range) = zeros(1,length(range),'uint64'); |
| 255 |
end |
| 256 |
|
| 257 |
end |
| 258 |
|
| 259 |
end |