]> Creatis software - CreaPhase.git/blob - SimuPBI_3WiresPhant_func.m
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/CreaPhase
[CreaPhase.git] / SimuPBI_3WiresPhant_func.m
1 ## Copyright (C) 2016 Loriane Weber
2 ## 
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ## 
13 ## You should have received a copy of the GNU General Public License
14 ## along with Octave; see the file COPYING.  If not, see
15 ## <http://www.gnu.org/licenses/>.
16
17 ## SimuPBI_3WiresPhant_func
18
19 ## Author: Loriane Weber <lweber@gpid16a-1802>
20 ## Created: 2016-03-30
21
22 ## Example of use :
23 ## SimuPBI_3WiresPhant_func('Analytical', 2, 'test', [0 0.1 0.5], 19, 2.7, 300, 180, 1, 0, '', 'gaussian', 35)
24
25 ## Please note that dir_out, noise_type, noise_amount are optional arguments.
26
27 function [] = SimuPBI_3WiresPhant_func (vers, oversamp, basename_output, dist, energy, ps, nbProj, range_angle, model_ctf, model_Fresnel, height, dir_out, noise_type, noise_amount)
28
29 addpath('utilities_ESRF')
30 addpath('utilities_LW')
31 addpath(genpath('octave_packages'))
32
33 ################################################
34 ############### INPUT parameters ############### 
35 ################################################
36
37 ################################
38 ######### Parameter related to the computation - and names of the result
39 ################################
40
41 ## vers: are the projections calculated analytically or using the Radon transform? use 'Radon' OR 'Analytical'
42 %vers='Radon' ; 
43
44 ## Oversampling of the projections : use 2 or 4
45 %oversamp=2
46
47 ## Basename of the result file
48 %basename_output='phantom_al_mg_PET_thibaut_3dist';
49
50 ################################
51 ######### Parameter related to the physics
52 ################################
53
54 ## Distances of propagation (in m)
55 %dist=[0 0.28 0.560]; 
56
57 ## Energy of the incoming X-ray beam (in keV)
58 %energy=19 
59
60 ## Pixel size of the detector (in um)
61 %ps=3.5; 
62
63 ## Number of projections
64 %nbProj=100;
65
66 ## Range of the tomography : 180 or 360 degrees
67 %range_angle=180; 
68
69 ## Which model do you want to use for the propagation? use 1 or 0
70 %model_ctf=0;
71 %model_Fresnel=1;
72
73 ################################
74 ############# Parameters related to the object
75 ################################
76
77 ## height of the object. If height == 1, the simulation is computed in 1D. If height >=2 , the simulation turns into 2D.
78 %height=10;
79
80
81 ################################
82 ############# OPTIONAL Parameters
83 ################################
84 ## Output directory : to be removed
85 %dir_out='/mntdirect/_users/lweber/Matlab/SimulationsPBI/Results_PhantTibo'; 
86 %dir_out='';
87
88 ## Noise-related part
89 ## Noise addition to the simulated data? 
90 ## Use noise='gaussian' (addition of gaussian noise) or noise='poisson' ( generation of Poisson noise) or '' (no noise is added).
91
92 %noise_type='gaussian' 
93 % default value is '' (no noise)
94
95 ## Amount of noise
96 ## If 'gaussian' (additive noise), please specify the Peak-to-peak Signe-to-noise ratio (PPSNR, in dB)
97 % noise_amount=40;
98 %default value is 35 dB
99 ## if 'poisson', please specify the amount of noise 
100 %noise_amount=0.05; 
101 % default value is 5%. 
102 ## !! note that poisson noise is here not implemented yet
103
104 #######################################################
105 ############### End of INPUT parameters ############### 
106 #######################################################
107
108 #################################################
109 ############### OUTPUT parameters ############### 
110 #################################################
111
112 ## None, file directly save 
113
114 ########################################################
115 ############### End of OUTPUT parameters ############### 
116 ########################################################
117
118 ############################# check varargin - optional arguments ###########################
119  if nargin<11
120        error("Some arguments are missing.\n");
121  elseif nargin==11
122     dir_out='';
123     noise_type='';
124  elseif nargin==12
125         noise_type='';
126  elseif nargin==13
127         if strcmp(noise_type, 'gaussian')
128                 noise_amount=35;
129         elseif strcmp(noise_type, 'poisson')
130         error("poisson noise is not implemented yet")
131         return; %noise_amount=0.05;
132         else 
133                 error("Unknown type of noise.\n")
134     endif
135 endif
136
137
138 ############# Unchanged parameters ##############
139 if !strcmp(dir_out, '')
140         dir_out=strcat(dir_out, '/');
141 end
142
143 z1=145; % source to sample distance (in m)
144
145 % calcul des angles
146 angles=[0:1:nbProj-1];
147 delta_angle=range_angle/nbProj;
148 angles=angles*delta_angle;
149
150 % Wavelength, in meter
151 lambda=12.4*10^-10/energy;
152 ps=ps*10^-6; % put the pixel size in meter
153
154 % Effective prop distance
155 D = (z1*dist)./(z1+dist);
156 betash = lambda*D; % in meter^2
157
158 % pading method
159 pad_method='extend';
160
161 % Absorption image ('mu', in cm-1)
162 mu=edfread('attenuation.edf'); % map of mu in cm-1
163 [m n]=size(mu)
164
165 absorption=mu*100; % in m-1 , to be consistant with lambda!! /10  to have less attenuation
166 beta=(lambda/(4*pi))*absorption; % one slice of beta /!\ units
167
168 delta_beta=edfread('delta_beta_map_1200.edf');
169 delta=delta_beta.*beta;
170
171 ## Noise addition
172 if(strcmp(noise_type, 'gaussian'))
173         PPSNR=noise_amount;
174         noise_str=strcat(noise_type, num2str(PPSNR), 'dB_');
175 %elseif(strcmp(noise_type, 'poisson'))
176         %scale_fact=noise_amount;
177         %noise_str=strcat(noise_type, num2str(scale_fact), '_');
178 elseif(strcmp(noise_type, ''))
179         noise_str=''; 
180 end
181
182
183 if height ==1
184         basename_output=strcat(basename_output, '-1D_');
185 else
186         basename_output=strcat(basename_output, '-2D_');
187 end
188
189 %############# END OF Unchanged parameters ##############
190
191 %############# Simulations des donnees ##############
192 ps=ps/oversamp;
193
194 ## Creation of the sinogram
195 if(strcmp(vers, 'Radon'))
196         sino_abs=radon(absorption, angles)*ps*oversamp; % to get rif of oversamp in the pixelsize
197         sino_delta=radon(delta, angles)*ps*oversamp;
198 elseif(strcmp(vers, 'Analytical'))
199         [sino_abs, sino_delta, sino_beta] = PhantTibo_Analytical (angles, ps*oversamp, 1);
200 else
201         disp('error in the variable vers')
202         return;
203 endif
204
205 ## Creation of the projections
206 abs_proj=cell(1, nbProj);
207 B_proj=cell(1, nbProj);
208 Phi_proj=cell(1, nbProj);
209
210
211 if height ==1
212
213         for j=1:nbProj
214                 abs_proj{j}=interp(sino_abs(:,j), oversamp);
215                 B_proj{j}=(1/2)*abs_proj{j}; 
216                 Phi_proj{j}=(-2*pi/lambda)*interp(sino_delta(:,j), oversamp); 
217         end
218         
219         ## Fourier domain
220         [mp np]=size(Phi_proj{j});
221         [ftot]=FrequencySpace1(2*mp, ps);
222
223         ### CTF model ###
224         if(model_ctf==1)
225                 IdCTF=cell(length(D), nbProj);
226                 sino_IdCTF=cell(length(D),1);
227                 
228                 for k=1:length(D)
229
230                         for j=1:nbProj
231                                 sino_IdCTF{k}(:,j)=CTFPropagation_1D(Phi_proj{j}, B_proj{j}, D(k), lambda, oversamp, ftot);
232                         end
233                 
234                         if strcmp(noise_type, 'gaussian')
235                                 ampsignal=max(max(sino_IdCTF{k}))-min(min(sino_IdCTF{k})); %add the amount of noise wrt the amplitude at the first dist
236                                 bruit=randn(size(sino_IdCTF{k})); % gaussian white noise 
237                                 bruit=bruit/max(max(bruit)); 
238                                 bruit=bruit*ampsignal*10^(-PPSNR/20); %/2;
239                                 sino_IdCTF{k}=sino_IdCTF{k}+bruit;
240                         end
241
242                         name_ctf=strcat(dir_out, basename_output, 'CTF_', vers, '_oversamp', num2str(oversamp), '_', noise_str, num2str(nbProj), 'proj_', num2str(k),  '_.edf')
243                         edfwrite(name_ctf, sino_IdCTF{k}, 'float32');
244                 end
245         end % end of model CTF
246
247         ### Fresnel model ###
248         if(model_Fresnel==1)
249                 IdFresnel=cell(length(D), nbProj);
250                 sino_IdFresnel=cell(length(D),1);
251
252                 for k=1:length(D)
253                         for j=1:nbProj
254                                 tmp=FresnelTransform_1D(Phi_proj{j}, B_proj{j}, D(k), lambda, oversamp, ftot);
255                                 IdFresnel{k, j}=tmp;
256                                 sino_IdFresnel{k}(:,j)=tmp;
257                         end
258
259                         if strcmp(noise_type, 'gaussian') 
260                                 ampsignal=max(max(sino_IdFresnel{k}))-min(min(sino_IdFresnel{k})); %add the amount of noise wrt the amplitude at the first dist
261                                 bruit=randn(size(sino_IdFresnel{k})); % gaussian white noise 
262                                 bruit=bruit/max(max(bruit)); 
263                                 bruit=bruit*ampsignal*10^(-PPSNR/20); %/2;
264                                 sino_IdFresnel{k}=sino_IdFresnel{k}+bruit;
265                         end
266                         
267                         name_fre=strcat(dir_out, basename_output, 'Fresnel_', vers, '_oversamp', num2str(oversamp), '_', noise_str, num2str(nbProj), 'proj_', num2str(k), '_.edf')
268                         edfwrite(name_fre, sino_IdFresnel{k}, 'float32');
269                 end
270         end % end of Fresnel model
271
272 ##### case height >=2 (2D) #####
273 else
274
275         for j=1:nbProj
276                 abs_proj{j}=repmat(interp(sino_abs(:,j), oversamp), [1 oversamp*height]);
277                 B_proj{j}=(1/2)*abs_proj{j}; 
278                 Phi_proj{j}=repmat( interp(sino_delta(:,j), oversamp), [1 oversamp*height]);
279                 Phi_proj{j}=(-2*pi/lambda)*Phi_proj{j};
280         end
281
282         ## Fourier domain
283         [mp np]=size(Phi_proj{j}); % take into account the oversampling
284         [ftot, gtot]=FrequencySpace(2*mp, 2*np, ps);
285
286         ### CTF model ###
287         if(model_ctf==1)
288                 IdCTF=cell(length(D), nbProj);
289
290                 for k=1:length(D)
291                         nom=strcat(basename_output, 'CTF_', vers, '_oversamp', num2str(oversamp), '_', noise_str, num2str(nbProj), 'proj_', num2str(k), '_' );
292                 
293                                 if(exist(strcat(dir_out, nom))!=7)
294                                         unix(['mkdir ' dir_out nom]); % create directory
295                                 else 
296                                         unix(['rm ' dir_out nom '/' basename_output 'CTF*' vers '*edf']); % erase existing proj
297                                 end % end create directory
298                 
299                         for j=1:nbProj
300                                 IdCTF{k, j}=CTFPropagation_2D(Phi_proj{j}, B_proj{j}, D(k), lambda, oversamp, ftot, gtot);
301
302                                 if strcmp(noise_type, 'gaussian') 
303                                         ampsignal=max(max(IdCTF{k, j}))-min(min(IdCTF{k, j})); %add the amount of noise wrt the amplitude at the first dist
304                                         bruit=randn(size(IdCTF{k, j})); % gaussian white noise 
305                                         bruit=bruit/max(max(bruit)); 
306                                         bruit=bruit*ampsignal*10^(-PPSNR/20); %/2;
307                                         IdCTF{k, j}=IdCTF{k, j}+bruit;
308                                 end
309                 
310                                 name_ctf=strcat(dir_out, nom, '/', nom, num2str(j-1, '%0.4d'), '.edf');
311                                 edfwrite(name_ctf, IdCTF{k,j}, 'float32');
312                         end % end proj
313                 end % end length
314         end % end ctf
315
316         #### Fresnel model ###
317         if(model_Fresnel==1)
318                 IdFresnel=cell(length(D), nbProj);
319                 
320                 for k=1:length(D)       
321                         nom=strcat(basename_output, 'Fresnel_', vers, '_oversamp', num2str(oversamp), '_', noise_str, num2str(nbProj), 'proj_', num2str(k), '_' );
322                         
323                         if(exist(strcat(dir_out, nom))!=7)
324                                 unix(['mkdir ' dir_out nom]); % create directory
325                         else 
326                                 unix(['rm ' dir_out nom '/' basename_output 'Fresnel*' vers '*edf']); % erase existing proj
327                         end % end create directory
328                         
329                         for j=1:nbProj
330                                 IdFresnel{k, j}=FresnelTransform_2D(Phi_proj{j}, B_proj{j}, D(k), lambda, oversamp, ftot, gtot);
331                                 if strcmp(noise_type, 'gaussian')
332                                         ampsignal=max(max(IdFresnel{k, j}))-min(min(IdFresnel{k, j})); %add the amount of noise wrt the amplitude at the first dist
333                                         bruit=randn(size(IdFresnel{k, j})); % gaussian white noise 
334                                         bruit=bruit/max(max(bruit)); 
335                                         bruit=bruit*ampsignal*10^(-PPSNR/20); %/2;
336                                         IdFresnel{k, j}=IdFresnel{k, j}+bruit;
337                                 end
338                                 name_fre=strcat(dir_out, nom, '/', nom, num2str(j-1, '%0.4d'), '.edf');
339                                 edfwrite(name_fre, IdFresnel{k,j}, 'float32');
340                         end     % end proj
341                 end % end distances
342
343         end %end Fresnel
344
345 end
346         
347
348
349 endfunction