-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSiemensRead_RE.m
executable file
·360 lines (289 loc) · 14.6 KB
/
SiemensRead_RE.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
%
% Read spectroscopy data from Siemens machine
%
% Read a .rda file
%
%Version 111212
function [MRS_struct] = SiemensRead_RE(MRS_struct, off_filename, on_filename, water_filename)
% [filename , pathname ] = uigetfile('*.rda', 'Select an RDA file')
% rda_filename = [pathname , filename]; %'c:/data/spectroscopy/spec raw data/MrSpec.20020531.160701.rda'
%%%First load in the OFF data
rda_filename=off_filename; %This is generally file2
fid = fopen(rda_filename);
head_start_text = '>>> Begin of header <<<';
head_end_text = '>>> End of header <<<';
tline = fgets(fid);
while (isempty(strfind(tline , head_end_text)))
tline = fgets(fid);
if ( isempty(strfind (tline , head_start_text)) + isempty(strfind (tline , head_end_text )) == 2)
% Store this data in the appropriate format
occurence_of_colon = findstr(':',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' }
eval(['rda.' , variable , ' = value; ']);
case { 'PatientSex' }
% Sex converter! (int to M,F,U)
switch value
case 0
rda.sex = 'Unknown';
case 1
rda.sex = 'Male';
case 2
rda.sex = 'Female';
end
case { 'SeriesNumber' , 'InstanceNumber' , 'AcquisitionNumber' , 'NumOfPhaseEncodingSteps' , 'NumberOfRows' , 'NumberOfColumns' , 'VectorSize' }
%Integers
eval(['rda.' , variable , ' = str2num(value); ']);
case { 'PatientWeight' , 'TR' , 'TE' , 'TM' , 'DwellTime' , 'NumberOfAverages' , 'MRFrequency' , 'MagneticFieldStrength' , 'FlipAngle' , ...
'SliceThickness' , 'FoVHeight' , 'FoVWidth' , 'PercentOfRectFoV' , 'PixelSpacingRow' , 'PixelSpacingCol'}
%Floats
eval(['rda.' , variable , ' = str2num(value); ']);
case {'SoftwareVersion[0]' }
rda.software_version = value;
case {'CSIMatrixSize[0]' }
rda.CSIMatrix_Size(1) = str2num(value);
case {'CSIMatrixSize[1]' }
rda.CSIMatrix_Size(2) = str2num(value);
case {'CSIMatrixSize[2]' }
rda.CSIMatrix_Size(3) = str2num(value);
case {'PositionVector[0]' }
rda.PositionVector(1) = str2num(value);
case {'PositionVector[1]' }
rda.PositionVector(2) = str2num(value);
case {'PositionVector[2]' }
rda.PositionVector(3) = str2num(value);
case {'RowVector[0]' }
rda.RowVector(1) = str2num(value);
case {'RowVector[1]' }
rda.RowVector(2) = str2num(value);
case {'RowVector[2]' }
rda.RowVector(3) = str2num(value);
case {'ColumnVector[0]' }
rda.ColumnVector(1) = str2num(value);
case {'ColumnVector[1]' }
rda.ColumnVector(2) = str2num(value);
case {'ColumnVector[2]' }
rda.ColumnVector(3) = str2num(value);
otherwise
% We don't know what this variable is. Report this just to keep things clear
%disp(['Unrecognised variable ' , variable ]);
end
else
% Don't bother storing this bit of the output
end
end
%%RE 110726 Take the used bits of the header info
MRS_struct.LarmorFreq = rda.MRFrequency;
MRS_struct.npoints = rda.VectorSize;
MRS_struct.sw = 1/rda.DwellTime*1E6;
MRS_struct.TR=rda.TR;
%
% So now we should have got to the point after the header text
%
% Siemens documentation suggests that the data should be in a double complex format (8bytes for real, and 8 for imaginary?)
%
bytes_per_point = 16;
complex_data = fread(fid , rda.CSIMatrix_Size(1) * rda.CSIMatrix_Size(1) *rda.CSIMatrix_Size(1) *rda.VectorSize * 2 , 'double');
%fread(fid , 1, 'double'); %This was a check to confirm that we had read all the data (it passed!)
fclose(fid);
% Now convert this data into something meaningful
%Reshape so that we can get the real and imaginary separated
hmm = reshape(complex_data, 2 , rda.VectorSize , rda.CSIMatrix_Size(1) , rda.CSIMatrix_Size(2) , rda.CSIMatrix_Size(3) );
%Combine the real and imaginary into the complex matrix
hmm_complex = complex(hmm(1,:,:,:,:),hmm(2,:,:,:,:));
%RE 110726 This is the complex time domain data
MRS_struct.offdata = hmm_complex;
%%%Now load in the ON data
rda_filename=on_filename; %This is generally file3
fid = fopen(rda_filename);
head_start_text = '>>> Begin of header <<<';
head_end_text = '>>> End of header <<<';
tline = fgets(fid);
while (isempty(strfind(tline , head_end_text)))
tline = fgets(fid);
if ( isempty(strfind (tline , head_start_text)) + isempty(strfind (tline , head_end_text )) == 2)
% Store this data in the appropriate format
occurence_of_colon = findstr(':',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' }
eval(['rda.' , variable , ' = value; ']);
case { 'PatientSex' }
% Sex converter! (int to M,F,U)
switch value
case 0
rda.sex = 'Unknown';
case 1
rda.sex = 'Male';
case 2
rda.sex = 'Female';
end
case { 'SeriesNumber' , 'InstanceNumber' , 'AcquisitionNumber' , 'NumOfPhaseEncodingSteps' , 'NumberOfRows' , 'NumberOfColumns' , 'VectorSize' }
%Integers
eval(['rda.' , variable , ' = str2num(value); ']);
case { 'PatientWeight' , 'TR' , 'TE' , 'TM' , 'DwellTime' , 'NumberOfAverages' , 'MRFrequency' , 'MagneticFieldStrength' , 'FlipAngle' , ...
'SliceThickness' , 'FoVHeight' , 'FoVWidth' , 'PercentOfRectFoV' , 'PixelSpacingRow' , 'PixelSpacingCol'}
%Floats
eval(['rda.' , variable , ' = str2num(value); ']);
case {'SoftwareVersion[0]' }
rda.software_version = value;
case {'CSIMatrixSize[0]' }
rda.CSIMatrix_Size(1) = str2num(value);
case {'CSIMatrixSize[1]' }
rda.CSIMatrix_Size(2) = str2num(value);
case {'CSIMatrixSize[2]' }
rda.CSIMatrix_Size(3) = str2num(value);
case {'PositionVector[0]' }
rda.PositionVector(1) = str2num(value);
case {'PositionVector[1]' }
rda.PositionVector(2) = str2num(value);
case {'PositionVector[2]' }
rda.PositionVector(3) = str2num(value);
case {'RowVector[0]' }
rda.RowVector(1) = str2num(value);
case {'RowVector[1]' }
rda.RowVector(2) = str2num(value);
case {'RowVector[2]' }
rda.RowVector(3) = str2num(value);
case {'ColumnVector[0]' }
rda.ColumnVector(1) = str2num(value);
case {'ColumnVector[1]' }
rda.ColumnVector(2) = str2num(value);
case {'ColumnVector[2]' }
rda.ColumnVector(3) = str2num(value);
otherwise
% We don't know what this variable is. Report this just to keep things clear
%disp(['Unrecognised variable ' , variable ]);
end
else
% Don't bother storing this bit of the output
end
end
%%RE 110726 Take the used bits of the header info
MRS_struct.LarmorFreq = rda.MRFrequency;
MRS_struct.npoints = rda.VectorSize;
MRS_struct.Navg(MRS_struct.ii) = rda.NumberOfAverages;
% So now we should have got to the point after the header text
%
% Siemens documentation suggests that the data should be in a double complex format (8bytes for real, and 8 for imaginary?)
%
bytes_per_point = 16;
complex_data = fread(fid , rda.CSIMatrix_Size(1) * rda.CSIMatrix_Size(1) *rda.CSIMatrix_Size(1) *rda.VectorSize * 2 , 'double');
%fread(fid , 1, 'double'); %This was a check to confirm that we had read all the data (it passed!)
fclose(fid);
% Now convert this data into something meaningful
%Reshape so that we can get the real and imaginary separated
hmm = reshape(complex_data, 2 , rda.VectorSize , rda.CSIMatrix_Size(1) , rda.CSIMatrix_Size(2) , rda.CSIMatrix_Size(3) );
%Combine the real and imaginary into the complex matrix
hmm_complex = complex(hmm(1,:,:,:,:),hmm(2,:,:,:,:));
%RE 110726 This is the complex time domain data
MRS_struct.ondata = hmm_complex;
switch MRS_struct.ONOFForder
case 'offfirst'
MRS_struct.data =[MRS_struct.offdata;MRS_struct.ondata].';
case 'onfirst'
MRS_struct.data =[MRS_struct.ondata;MRS_struct.offdata].';
end
%MRS_struct.data =[MRS_struct.ondata;MRS_struct.offdata].';
if(nargin==4)
%%%Now load in the Water data
rda_filename=water_filename; %This is generally file3
fid = fopen(rda_filename);
head_start_text = '>>> Begin of header <<<';
head_end_text = '>>> End of header <<<';
tline = fgets(fid);
while (isempty(strfind(tline , head_end_text)))
tline = fgets(fid);
if ( isempty(strfind (tline , head_start_text)) + isempty(strfind (tline , head_end_text )) == 2)
% Store this data in the appropriate format
occurence_of_colon = findstr(':',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' }
eval(['rda.' , variable , ' = value; ']);
case { 'PatientSex' }
% Sex converter! (int to M,F,U)
switch value
case 0
rda.sex = 'Unknown';
case 1
rda.sex = 'Male';
case 2
rda.sex = 'Female';
end
case { 'SeriesNumber' , 'InstanceNumber' , 'AcquisitionNumber' , 'NumOfPhaseEncodingSteps' , 'NumberOfRows' , 'NumberOfColumns' , 'VectorSize' }
%Integers
eval(['rda.' , variable , ' = str2num(value); ']);
case { 'PatientWeight' , 'TR' , 'TE' , 'TM' , 'DwellTime' , 'NumberOfAverages' , 'MRFrequency' , 'MagneticFieldStrength' , 'FlipAngle' , ...
'SliceThickness' , 'FoVHeight' , 'FoVWidth' , 'PercentOfRectFoV' , 'PixelSpacingRow' , 'PixelSpacingCol'}
%Floats
eval(['rda.' , variable , ' = str2num(value); ']);
case {'SoftwareVersion[0]' }
rda.software_version = value;
case {'CSIMatrixSize[0]' }
rda.CSIMatrix_Size(1) = str2num(value);
case {'CSIMatrixSize[1]' }
rda.CSIMatrix_Size(2) = str2num(value);
case {'CSIMatrixSize[2]' }
rda.CSIMatrix_Size(3) = str2num(value);
case {'PositionVector[0]' }
rda.PositionVector(1) = str2num(value);
case {'PositionVector[1]' }
rda.PositionVector(2) = str2num(value);
case {'PositionVector[2]' }
rda.PositionVector(3) = str2num(value);
case {'RowVector[0]' }
rda.RowVector(1) = str2num(value);
case {'RowVector[1]' }
rda.RowVector(2) = str2num(value);
case {'RowVector[2]' }
rda.RowVector(3) = str2num(value);
case {'ColumnVector[0]' }
rda.ColumnVector(1) = str2num(value);
case {'ColumnVector[1]' }
rda.ColumnVector(2) = str2num(value);
case {'ColumnVector[2]' }
rda.ColumnVector(3) = str2num(value);
otherwise
% We don't know what this variable is. Report this just to keep things clear
%disp(['Unrecognised variable ' , variable ]);
end
else
% Don't bother storing this bit of the output
end
end
%%RE 110726 Take the used bits of the header info
MRS_struct.LarmorFreq = rda.MRFrequency;
MRS_struct.npoints = rda.VectorSize;
%
% So now we should have got to the point after the header text
%
% Siemens documentation suggests that the data should be in a double complex format (8bytes for real, and 8 for imaginary?)
%
bytes_per_point = 16;
complex_data = fread(fid , rda.CSIMatrix_Size(1) * rda.CSIMatrix_Size(1) *rda.CSIMatrix_Size(1) *rda.VectorSize * 2 , 'double');
%fread(fid , 1, 'double'); %This was a check to confirm that we had read all the data (it passed!)
fclose(fid);
% Now convert this data into something meaningful
%Reshape so that we can get the real and imaginary separated
hmm = reshape(complex_data, 2 , rda.VectorSize , rda.CSIMatrix_Size(1) , rda.CSIMatrix_Size(2) , rda.CSIMatrix_Size(3) );
%Combine the real and imaginary into the complex matrix
hmm_complex = complex(hmm(1,:,:,:,:),hmm(2,:,:,:,:));
%RE 110726 This is the complex time domain data
MRS_struct.data_water = hmm_complex.';
%MRS_struct.data_water(1)
end
end