-
Notifications
You must be signed in to change notification settings - Fork 13
/
SiemensRead.m
164 lines (111 loc) · 5.21 KB
/
SiemensRead.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
function MRS_struct = SiemensRead(MRS_struct, off_filename, on_filename, water_filename)
ii = MRS_struct.ii;
% Load water-suppressed data
[off_data, hdr] = read_rda_data(off_filename);
on_data = read_rda_data(on_filename);
MRS_struct.fids.data = [on_data; off_data].';
% Header info
if isfield(hdr, 'VOIRotationInPlane')
MRS_struct.p.VoI_InPlaneRot(ii) = hdr.VOIRotationInPlane;
else
MRS_struct.p.VoI_InPlaneRot(ii) = 0;
end
MRS_struct.p.LarmorFreq(ii) = hdr.MRFrequency;
MRS_struct.p.npoints(ii) = hdr.VectorSize;
MRS_struct.p.sw(ii) = 1/hdr.DwellTime * 1e6;
MRS_struct.p.TR(ii) = hdr.TR;
MRS_struct.p.TE(ii) = hdr.TE;
MRS_struct.p.Navg(ii) = hdr.NumberOfAverages;
MRS_struct.p.voxdim(ii,1) = hdr.FoVHeight;
MRS_struct.p.voxdim(ii,2) = hdr.FoVWidth;
MRS_struct.p.voxdim(ii,3) = hdr.SliceThickness;
MRS_struct.p.voxoff(ii,:) = hdr.PositionVector;
% Load water data
if nargin == 4
[water_data, hdr] = read_rda_data(water_filename);
MRS_struct.fids.data_water = water_data.';
if isfield(hdr, 'TR')
MRS_struct.p.TR_water(ii) = hdr.TR;
end
if isfield(hdr, 'TE')
MRS_struct.p.TE_water(ii) = hdr.TE;
end
end
end
function [data, hdr] = read_rda_data(fname)
fid = fopen(fname);
head_start_text = '>>> Begin of header <<<';
head_end_text = '>>> End of header <<<';
tline = fgets(fid);
while isempty(strfind(tline, head_end_text)) %#ok<*STREMP>
tline = fgets(fid);
if isempty(strfind(tline, head_start_text)) && isempty(strfind(tline, head_end_text))
% Store this data in the appropriate format
occurence_of_colon = strfind(tline,':');
variable = tline(1:occurence_of_colon-1);
value = tline(occurence_of_colon+1:length(tline));
switch variable
case {'PatientID', 'PatientName', 'StudyDescription', 'PatientBirthDate', 'StudyDate', 'StudyTime', 'PatientAge', 'SeriesDate', ...
'SeriesTime', 'SeriesDescription', 'ProtocolName', 'PatientPosition', 'ModelName', 'StationName', 'InstitutionName', ...
'DeviceSerialNumber', 'InstanceDate', 'InstanceTime', 'InstanceComments', 'SequenceName', 'SequenceDescription', 'Nucleus', ...
'TransmitCoil'}
hdr.(variable) = value;
case 'PatientSex'
% Sex converter (int to M,F,U)
switch value
case 0
hdr.sex = 'Unknown';
case 1
hdr.sex = 'Male';
case 2
hdr.sex = 'Female';
end
case {'SeriesNumber', 'InstanceNumber', 'AcquisitionNumber', 'NumOfPhaseEncodingSteps', 'NumberOfRows', 'NumberOfColumns', 'VectorSize'}
%Integers
hdr.(variable) = str2double(value);
case {'PatientWeight', 'TR', 'TE', 'TM', 'DwellTime', 'NumberOfAverages', 'MRFrequency', 'MagneticFieldStrength', 'FlipAngle', ...
'SliceThickness', 'FoVHeight', 'FoVWidth', 'PercentOfRectFoV', 'PixelSpacingRow', 'PixelSpacingCol', 'VOIRotationInPlane'}
%Floats
hdr.(variable) = str2double(value);
case 'SoftwareVersion[0]'
hdr.software_version = value;
case 'CSIMatrixSize[0]'
hdr.CSIMatrix_Size(1) = str2double(value);
case 'CSIMatrixSize[1]'
hdr.CSIMatrix_Size(2) = str2double(value);
case 'CSIMatrixSize[2]'
hdr.CSIMatrix_Size(3) = str2double(value);
case 'PositionVector[0]'
hdr.PositionVector(1) = str2double(value);
case 'PositionVector[1]'
hdr.PositionVector(2) = str2double(value);
case 'PositionVector[2]'
hdr.PositionVector(3) = str2double(value);
case 'RowVector[0]'
hdr.RowVector(1) = str2double(value);
case 'RowVector[1]'
hdr.RowVector(2) = str2double(value);
case 'RowVector[2]'
hdr.RowVector(3) = str2double(value);
case 'ColumnVector[0]'
hdr.ColumnVector(1) = str2double(value);
case 'ColumnVector[1]'
hdr.ColumnVector(2) = str2double(value);
case 'ColumnVector[2]'
hdr.ColumnVector(3) = str2double(value);
case 'TablePosSag'
hdr.TablePosition(1) = str2double(value);
case 'TablePosCor'
hdr.TablePosition(2) = str2double(value);
case 'TablePosTra'
hdr.TablePosition(3) = str2double(value);
end
end
end
data = fread(fid, hdr.CSIMatrix_Size(1) * hdr.CSIMatrix_Size(1) * hdr.CSIMatrix_Size(1) * hdr.VectorSize * 2, 'double');
% Reshape so that we can get the real and imaginary separated
data = reshape(data, 2, hdr.VectorSize, hdr.CSIMatrix_Size(1), hdr.CSIMatrix_Size(2), hdr.CSIMatrix_Size(3));
% Combine the real and imaginary into a complex matrix
data = complex(data(1,:,:,:,:), data(2,:,:,:,:));
fclose(fid);
end