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

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

beam_forming added (for acoustic images)

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