source: trunk/src/series/beam_forming.m @ 782

Last change on this file since 782 was 782, checked in by sommeria, 10 years ago

AX and AY changed to Coord_x and Coord_y

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