1 | %======================================================================= |
---|
2 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France |
---|
3 | % http://www.legi.grenoble-inp.fr |
---|
4 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
5 | % |
---|
6 | % This file is part of the toolbox UVMAT. |
---|
7 | % |
---|
8 | % UVMAT is free software; you can redistribute it and/or modify |
---|
9 | % it under the terms of the GNU General Public License as published |
---|
10 | % by the Free Software Foundation; either version 2 of the license, |
---|
11 | % or (at your option) any later version. |
---|
12 | % |
---|
13 | % UVMAT is distributed in the hope that it will be useful, |
---|
14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
17 | %======================================================================= |
---|
18 | |
---|
19 | function ParamOut=beam_forming(Param) |
---|
20 | |
---|
21 | %% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed |
---|
22 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
23 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
24 | ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
25 | ParamOut.NbSlice='off'; %nbre of slices ('off' by default) |
---|
26 | ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
27 | ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
28 | ParamOut.FieldTransform = 'off';%can use a transform function |
---|
29 | %ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only) |
---|
30 | ParamOut.ProjObject='off';%can use projection object(option 'off'/'on', |
---|
31 | ParamOut.Mask='off';%can use mask option (option 'off'/'on', 'off' by default) |
---|
32 | index=msgbox_uvmat('INPUT_TXT','index of the series to process (1 to 5)');%choose the i index of the dat files |
---|
33 | ParamOut.OutputDirExt=['.p_formed_' index];%set the output dir extension |
---|
34 | hseries=findobj(allchild(0),'Tag','series'); |
---|
35 | hhseries=guidata(hseries); |
---|
36 | set(hhseries.num_last_i,'String',index) |
---|
37 | set(hhseries.num_first_i,'String',index) |
---|
38 | ParamOut.OutputFileMode='NbInput';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice |
---|
39 | %check the input files |
---|
40 | first_j=[]; |
---|
41 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end |
---|
42 | PairString=''; |
---|
43 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
44 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
45 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
46 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
47 | if ~exist(FirstFileName,'file') |
---|
48 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
---|
49 | end |
---|
50 | return |
---|
51 | end |
---|
52 | |
---|
53 | ParamOut=[]; %default output |
---|
54 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
55 | checkrun=1; |
---|
56 | if ischar(Param) |
---|
57 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
58 | checkrun=0; |
---|
59 | end |
---|
60 | hseries=findobj(allchild(0),'Tag','series'); |
---|
61 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
62 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
63 | |
---|
64 | %% define the directory for result file (with path=RootPath{1}) |
---|
65 | OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files |
---|
66 | if ~isfield(Param,'InputFields') |
---|
67 | Param.InputFields.FieldName=''; |
---|
68 | end |
---|
69 | |
---|
70 | %% root input file type |
---|
71 | RootPath=Param.InputTable{1,1}; |
---|
72 | RootFile=Param.InputTable{1,3}; |
---|
73 | SubDir=Param.InputTable{1,2}; |
---|
74 | NomType=Param.InputTable{1,4}; |
---|
75 | FileExt=Param.InputTable{1,5}; |
---|
76 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
77 | %%%%%%%%%%%% |
---|
78 | % The cell array filecell is the list of input file names, while |
---|
79 | % filecell{iview,fileindex}: |
---|
80 | % iview: line in the table corresponding to a given file series |
---|
81 | % fileindex: file index within the file series, |
---|
82 | % i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j |
---|
83 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
84 | %%%%%%%%%%%% |
---|
85 | % NbSlice=1;%default |
---|
86 | % if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
87 | % NbSlice=Param.IndexRange.NbSlice; |
---|
88 | % end |
---|
89 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
90 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
91 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
92 | NbField=NbField_j*NbField_i; %total number of fields |
---|
93 | |
---|
94 | %% determine the file type on each line from the first input file |
---|
95 | ImageTypeOptions={'image','multimage','mmreader','video'}; |
---|
96 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
97 | for iview=1:NbView |
---|
98 | if ~exist(filecell{iview,1}','file') |
---|
99 | disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun) |
---|
100 | return |
---|
101 | end |
---|
102 | [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1}); |
---|
103 | FileType{iview}=FileInfo{iview}.FileType; |
---|
104 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
105 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
106 | if ~isempty(j1_series{iview}) |
---|
107 | frame_index{iview}=j1_series{iview}; |
---|
108 | else |
---|
109 | frame_index{iview}=i1_series{iview}; |
---|
110 | end |
---|
111 | end |
---|
112 | |
---|
113 | % clear all |
---|
114 | % close all |
---|
115 | % read_data=1; |
---|
116 | affichage=0; |
---|
117 | % soustraction=0; |
---|
118 | |
---|
119 | %%%%%% Prepare output |
---|
120 | load (fullfile(RootPath,SubDir,[RootFile '.mat'])) |
---|
121 | Data.ListGlobalAttribute={'CoordUnit'}; %%TODO: add also time, how to get it ????? |
---|
122 | Data.CoordUnit='pixel'; |
---|
123 | Data.ListVarName={'Coord_x','Coord_y','A'}; |
---|
124 | Data.VarDimName={'Coord_x','Coord_y',{'Coord_y','Coord_x'}}; |
---|
125 | %Data.Coord_x=5*(nbvoie_reception-0.5)/numel(nbvoie_reception); % totql length of e |
---|
126 | Data.Coord_x=1:65; |
---|
127 | %Data.Coord_z=(1:A)/133 ;% to check from input parameter .... |
---|
128 | Data.Coord_y=1:332; |
---|
129 | %%%%%% |
---|
130 | % |
---|
131 | % while test_fin_fichier>0 |
---|
132 | % if read_data==1 |
---|
133 | %directory='manip_lgit';%%%%%%%%%%%%%%%%% |
---|
134 | %name='test';%%%%%%%%%%%%%%%%% |
---|
135 | % number=2; |
---|
136 | number=str2num(Param.OutputDirExt(11:end));%extract the subsequence index (from 1 to 5) |
---|
137 | numero_tir_fin_old=1%%%%%%% =0 ????? |
---|
138 | pas_fichier=20;% %20;% nbre of successive shots to read (to account for computer memory limit) |
---|
139 | Nmoy=800; %%%%% value 20 FOR TEST : to shift to VALUE 8000 set by the .mat file |
---|
140 | |
---|
141 | test_fin_fichier=1;% test to stop input file reading |
---|
142 | while test_fin_fichier>0 |
---|
143 | numero_tir_debut=1; |
---|
144 | numero_tir_fin=numero_tir_fin_old+pas_fichier-1; |
---|
145 | |
---|
146 | % eval(['load ' directory '\' name '.mat']) |
---|
147 | matrice_finale=zeros(A,length(nbvoie_reception),numero_tir_fin);%A=nbre of times (coord z)=2650, numero_tir_fin=time index |
---|
148 | time=(b/rsf+[0:A-1]/rsf); %b=250, rsf=10, |
---|
149 | freq1=0.5;freq2=1.5; |
---|
150 | [BB AA]=butter(4,[freq1 freq2]/rsf*2); |
---|
151 | |
---|
152 | for ii=1:length(nbvoie_reception)%=64 |
---|
153 | %eval(['fid=fopen(''E:\ManipLGITLecoeur\' directory '\' name '_' num2str(number) '_' num2str(nbvoie_reception(ii)) '.dat'',''r'');']); |
---|
154 | filename=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,number,[],ii); % input file name |
---|
155 | fid=fopen(filename); |
---|
156 | toto=zeros(Nsequence*A*numero_tir_fin+31,1);% Nsequence=1 |
---|
157 | toto=fread(fid,numero_tir_fin*A*Nsequence+31,'int16','ieee-le') ;% why shift by 31 ????? |
---|
158 | toto=double(bitxor(uint16(toto),uint16(2048))); |
---|
159 | toto(1:31)=[];toto(numero_tir_fin*A*Nsequence)=mean(toto); |
---|
160 | fclose(fid); |
---|
161 | |
---|
162 | tata=reshape(toto-2048,A,numero_tir_fin); |
---|
163 | matrice_finale(:,ii,:)=reshape(tata,[A,1,numero_tir_fin]); |
---|
164 | clear toto tata |
---|
165 | end |
---|
166 | |
---|
167 | % matrice_finale(:,:,numero_tir_debut:numero_tir_fin_old)=[];%%%%%%% first field removed (when numero_tir_fin_old=1) ????? |
---|
168 | matrice_finale(:,:,numero_tir_debut:numero_tir_fin_old-1)=[];%%%%%%% |
---|
169 | % numero_tir_fin=numero_tir_fin-1; ????? |
---|
170 | matrice_finale=reshape(filtfilt(BB,AA,matrice_finale(:,:)),size(matrice_finale));% low pass filtered input signal,along first (time) index? |
---|
171 | |
---|
172 | % if soustraction==1 |
---|
173 | % eval(['load moyenne_' name '_' num2str(number) '.mat matrice_finale_moy']) |
---|
174 | % for kk=1:size(matrice_finale,3) |
---|
175 | % matrice_finale(:,:,kk)=matrice_finale(:,:,kk)-matrice_finale_moy; |
---|
176 | % end |
---|
177 | % end |
---|
178 | %eval(['save matrice_finale_' num2str(numero_tir_fin_old) '_' num2str(numero_tir_fin) '.mat']) |
---|
179 | |
---|
180 | %%%%%%%%%%%%%%Imagerie |
---|
181 | fe=rsf*1e6;% sampling frequency for receptor (in Hz) |
---|
182 | cc=1475;%speed of sound |
---|
183 | hanning_window=25; |
---|
184 | hanning_vect=hanning(2*hanning_window+1); |
---|
185 | interval=[1:size(matrice_finale,1)]; |
---|
186 | freq=0:fe/length(interval):fe*(1-1/length(interval)); |
---|
187 | |
---|
188 | pas_reseau_z=0.75e-3;%0.75e-3 |
---|
189 | pas_reseau_r=0; |
---|
190 | voie_mean=length(nbvoie_reception)/2;%32; |
---|
191 | reseau_z=[0:length(nbvoie_reception)-1]*pas_reseau_z; |
---|
192 | reseau_z=reseau_z-reseau_z(voie_mean); |
---|
193 | reseau_r=[0:length(nbvoie_reception)-1]*pas_reseau_r; |
---|
194 | reseau_r=reseau_r-reseau_r(voie_mean); |
---|
195 | |
---|
196 | debut_r=(time(1)+20)*1e-6*cc/2; |
---|
197 | fin_r=(time(end)-20)*1e-6*cc/2; |
---|
198 | |
---|
199 | image_r=debut_r:.5e-3:fin_r; |
---|
200 | image_z=-24e-3:.75e-3:24e-3; |
---|
201 | |
---|
202 | image_fin=zeros(length(image_r),length(image_z),size(matrice_finale,3));%size=(332,65,pas_fichier) |
---|
203 | %image_fin_bis=zeros(length(image_r),length(image_z),size(matrice_finale,3)); |
---|
204 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
205 | |
---|
206 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
207 | for kk=1:size(matrice_finale,3) |
---|
208 | disp(kk) |
---|
209 | signal=squeeze(matrice_finale(interval,:,kk)); |
---|
210 | tata_fft=fft(signal,[],1);%FFT of the time signal size=(2650,64) |
---|
211 | if kk==1 |
---|
212 | matrice_freq_mean=mean(abs(fft(signal,[],1)),2); |
---|
213 | X=[freq1*1e6 freq2*1e6]; |
---|
214 | [I J]=find(freq>=X(1) & freq<=X(2)); |
---|
215 | int_freq=find(matrice_freq_mean(round(1:length(freq)/2))>max(matrice_freq_mean(round(1:length(freq)/2)))/2); |
---|
216 | bandwidth=freq(int_freq(end)-int_freq(1)); |
---|
217 | %clear matrice_freq_mean |
---|
218 | end |
---|
219 | |
---|
220 | for ii=1:length(image_r) |
---|
221 | for jj=1:length(image_z) |
---|
222 | |
---|
223 | delay=zeros(length(nbvoie_reception),1); |
---|
224 | delay=1/cc*sqrt((reseau_z-image_z(jj)).^2+(reseau_r-image_r(ii)).^2); |
---|
225 | |
---|
226 | [ind centre_z]=min(abs((reseau_z-image_z(jj)))); |
---|
227 | interval_utile=round(((delay(centre_z)+1/cc*abs(image_r(ii)))*fe)-(b+interval(1)-1)+round(length(motifbase)/2)+[-fe/bandwidth/2:fe/bandwidth/2]); |
---|
228 | delay=delay-delay(centre_z); |
---|
229 | |
---|
230 | hanning_vecteur=zeros(1,length(nbvoie_reception)); |
---|
231 | if centre_z>hanning_window & centre_z<(length(nbvoie_reception)-hanning_window) |
---|
232 | hanning_vecteur(centre_z+[-hanning_window:hanning_window])=hanning_vect; |
---|
233 | elseif centre_z<=hanning_window |
---|
234 | test=hanning_vect((centre_z+[-hanning_window:hanning_window])>=1); |
---|
235 | hanning_vecteur(1:length(test))=test; |
---|
236 | elseif centre_z>=(length(nbvoie_reception)-hanning_window) |
---|
237 | test=hanning_vect((centre_z+[-hanning_window:hanning_window])<=length(nbvoie_reception)); |
---|
238 | hanning_vecteur(length(nbvoie_reception)+[-length(test)+1:0])=test; |
---|
239 | end |
---|
240 | hanning_vecteur=hanning_vecteur/norm(hanning_vecteur); |
---|
241 | clear test; |
---|
242 | |
---|
243 | amplitude_weight=ones(size(signal,1),1)*hanning_vecteur; |
---|
244 | signal_new_rec=zeros(size(signal,1),length(nbvoie_reception)); |
---|
245 | |
---|
246 | tata=zeros(size(signal,1),size(signal,2)); |
---|
247 | tata(J,:)=tata_fft(J,:).*exp(1i*2*pi*(freq(J)'*delay)); |
---|
248 | signal_new_rec=2*real(ifft(tata,[],1)).*amplitude_weight; |
---|
249 | index_interval_utile=find(interval_utile>0 & interval_utile<size(signal,1)); |
---|
250 | toto=zeros(length(index_interval_utile),1); |
---|
251 | toto=mean(signal_new_rec(interval_utile(index_interval_utile),:),2); |
---|
252 | image_fin(ii,jj,kk)=sqrt(mean(toto.^2)); |
---|
253 | clear signal_bis interval_utile index_interval_utile hanning_vecteur |
---|
254 | end |
---|
255 | end |
---|
256 | end |
---|
257 | |
---|
258 | clear signal_new_em signal_new_rec m delay toto toto_bis tata tata_fft |
---|
259 | |
---|
260 | if affichage==1 |
---|
261 | for kk=1:size(image_fin,3) |
---|
262 | |
---|
263 | figure(1) |
---|
264 | imagesc(image_r*1e2,image_z*1e2,image_fin(:,:,kk)'/max(max(image_fin(:,:,kk)))'); |
---|
265 | title(['avec beamforming - energie max = ' num2str(max(max(image_fin(:,:,kk))))]) |
---|
266 | colorbar; |
---|
267 | xlabel('r (cm)');ylabel('z (cm)'); |
---|
268 | drawnow |
---|
269 | pause(.2); |
---|
270 | end |
---|
271 | end |
---|
272 | |
---|
273 | clear matrice_finale |
---|
274 | |
---|
275 | %%%%%%% TO ADAPT |
---|
276 | for iii=1:size(image_fin,3) |
---|
277 | Data.A=image_fin(:,:,iii);% time lapse decreasesas z coordinate increases. |
---|
278 | FileIndex=numero_tir_fin - pas_fichier+iii;%%%%%%TO CHECK!!!!! |
---|
279 | %%%%%%%%%% |
---|
280 | %eval(['save analyse_' name '_' num2str(number) '_' num2str(numero_tir_fin_old) '_' num2str(numero_tir_fin) '.mat']) |
---|
281 | OutputFile=fullfile_uvmat(RootPath,OutputDir,'signal','.nc','_00001',FileIndex); |
---|
282 | error=struct2nc(OutputFile,Data);%save result file |
---|
283 | if isempty(error) |
---|
284 | disp(['output file ' OutputFile ' written']) |
---|
285 | else |
---|
286 | disp(error) |
---|
287 | end |
---|
288 | end |
---|
289 | numero_tir_fin_old=numero_tir_fin+1% first index for next bloc reading |
---|
290 | if (numero_tir_fin_old+pas_fichier-1)>Nmoy |
---|
291 | test_fin_fichier=-1; |
---|
292 | end |
---|
293 | end |
---|
294 | |
---|
295 | |
---|