annotate simulateBinauralSignals.m @ 8:aeb703465220

added simulateBinauralSignals.m
author Timos Papadopoulos <tp@isvr.soton.ac.uk>
date Mon, 25 Nov 2013 17:29:43 +0000
parents
children d9262cdbfb38 dbefe500e5f4
rev   line source
tp@8 1 function simulateBinauralSignals(inputstruct)
tp@8 2 %Creates .wav file of 2sec long binaural signals for specific board dimensions and gaussian white noise stimulus
tp@8 3 %
tp@8 4 % The .wav is created in the current directory with filename is 'binsimecho.wav'
tp@8 5 %
tp@8 6 % INPUTS:
tp@8 7 % The input must be a structure with fields:
tp@8 8 % .dist (1st input argument) is the distance in meters
tp@8 9 % .azim (2nd input argument) is the azimuth in degrees (0 means straight ahead, positive angles are left and negative
tp@8 10 % are right)
tp@8 11 % .orient (3rd input argument) must be either 'horz' or 'angled' corresponding to flat and angled descriptions in
tp@8 12 % Papadopoulos et al. BSPC 2011.
tp@8 13 % .dirweight (4tht input argument) must be a nonnegative real scalar determining what is the relative weight of the
tp@8 14 % emission path to the echo path (i.e. due to directivity focus in the frontal direction of the source, the emission
tp@8 15 % which is directed upwards and backwards in our specific geometry is significantly attenuated, typically by factor in
tp@8 16 % the vicinity of 0.2)
tp@8 17 %
tp@8 18
tp@8 19 %% Internal workings description
tp@8 20 %
tp@8 21 % Center of spherical coordinates system is taken to be the center of head. The azimuth, elevation and distance
tp@8 22 % coordinates are as in matlabs sph2cart function (azimuth and elevation are angular displacements from the positive
tp@8 23 % x-axis and from the x-y plane, respectively) with positive x axis taken to be extending to the right of the head from
tp@8 24 % top view, positive y axis to be extending forward of the head in top view and positive z axis to be extending upwards
tp@8 25 % in top view.
tp@8 26 %
tp@8 27 % Board dimensions are defined as
tp@8 28 % params.board_size_x (width in meters for the 'center' orientation of Papadopoulos et al. BSPC 2011)
tp@8 29 % params.board_size_y (depth in meters for the 'center' orientation of Papadopoulos et al. BSPC 2011)
tp@8 30 % params.board_size_z (height in meters for the 'center' orientation of Papadopoulos et al. BSPC 2011)
tp@8 31 %
tp@8 32 % Board center position is defined as
tp@8 33 % params.board_distance (distance in meters from coords origin to center of board following coordinate system described
tp@8 34 % above), can be row vector.
tp@8 35 % params.board_azim (azimuth in radians of the center of the board following coordinate system described above),
tp@8 36 % can be row vector.
tp@8 37 % params.board_elev (elevation in radians of the center of the board following coordinate system described above),
tp@8 38 % must be scalar
tp@8 39 %
tp@8 40 % params.board_orientation scalar or 1x2 cell with elements 'horz' or/and 'angled'
tp@8 41 % Board is taken to always be vertically positioned (i.e. with its width-height plane vertical to the y coordinate) and
tp@8 42 % two cases of orientation are considered: horizontal and angled corresponding to flat and angled descriptions in
tp@8 43 % Papadopoulos et al. BSPC 2011.
tp@8 44 %
tp@8 45 % params.source_down non-negative scalar in meters
tp@8 46 % params.source_front non-negative scalar in meters
tp@8 47 % The source is assumed to always be in front of the chest and below the head. (params.source_down and
tp@8 48 % params.source_front cannot both be 0)
tp@8 49 %
tp@8 50 %
tp@8 51 % params.horz_disamb string which can be either 'BSPC2011' or 'collocated'. If equal to 'BSPC2011' then board distance
tp@8 52 % for the horizontal (flat) case is as described in Papadopoulos et al. BSPC 2011, i.e. the board
tp@8 53 % distance is the distance between the centre of the head and the PLANE OF THE BOARD.
tp@8 54 % Alternatively, in the 'collocated' case, the board distance in the horizontal (flat) case is taken
tp@8 55 % as the distance between the centre of the head and the centre of the board
tp@8 56 %
tp@8 57 % NOTE: in the 'BSPC2011' case of params.horz_disamb, it is easy to see that the geometry is ill-specified for board
tp@8 58 % positions far away from the median plane.
tp@8 59 %
tp@8 60 % OUTPUTS:
tp@8 61 %
tp@8 62 % --- simulation_data is a structure with the following fields:
tp@8 63 % - simulation_data.echo is a cell of dimensions
tp@8 64 % length(params.board_dist) x length(params.board_azim) x length(params.board_orientation)
tp@8 65 % each element of which is a 2-row matrix with the left (top row) and right (bottom row) ear responses corresponding the
tp@8 66 % echo only.
tp@8 67 %
tp@8 68 % - simulation_data.emission is a cell of dimensions
tp@8 69 % length(params.board_dist) x length(params.board_azim) x length(params.board_orientation)
tp@8 70 % each element of which is a 2-row matrix with the left (top row) and right (bottom row) ear responses corresponding the
tp@8 71 % direct source-to-receiver path only. (NOTE: source term (binaural_irs_emission) does not need to be computed for each
tp@8 72 % different board distance/azimuth/orientation as it is the same irs but containing a different number of trailing zero
tp@8 73 % samples. I include this redundancy because it simplified the computation a bit and also because this way the emission
tp@8 74 % part is always equal in length with the corresponding echo part and can be added easier)
tp@8 75 %
tp@8 76 % Adding any given
tp@8 77 % directivity_weighting * simulation_data.emission{ii,jj,kk}(1,:) + simulation_data.echo{ii,jj,kk}(1,:)
tp@8 78 % for any given ii, jj, kk will give the total left ear IR and the same for right ear by
tp@8 79 % directivity_weighting * simulation_data.emission{ii,jj,kk}(2,:) + simulation_data.echo{ii,jj,kk}(2,:)
tp@8 80 %
tp@8 81 % - simulation_data.params is the stucture params described above
tp@8 82 % - simulation_data.board_coords is an array of dimensions
tp@8 83 % 8 x 3 x length(params.board_dist) x length(params.board_azim) x length(params.board_orientation)
tp@8 84 % containing the x y z coordinates (2nd dimension) of the 8 edges (1st dimension) of the board geometries for different
tp@8 85 % board distances, azimuths and orientations.
tp@8 86 % - simulation_data.azerr and simulation_data.elerr are arrays of dimensions
tp@8 87 % 2 x 1+length(params.board_azim))
tp@8 88 % which contain the error (in degrees) between the azimuth and elevation respectively of the HRTFs that are used (as
tp@8 89 % existing in the CIPIC spherical grid) and the actual board center azimuth and elevation.
tp@8 90 %
tp@8 91 %
tp@8 92 %
tp@8 93 % DEVELOPMENT NOTES
tp@8 94 %
tp@8 95 % TODO: include case of multiple boards and of non-vertically oriented boards (i.e. all cases of pitch, roll, yaw for
tp@8 96 % board orientation)
tp@8 97 %
tp@8 98 % TODO: include geometrical description for all possible positions of the source
tp@8 99 %
tp@8 100 % TODO: include validateattributes for all parameters
tp@8 101 %
tp@8 102 % TODO: include parameter controls for specorder (now set to 1), difforder (now set to 1), elemsize (now set to [1]) and
tp@8 103 % nedgesubs (now set to 2)
tp@8 104 %
tp@8 105 %
tp@8 106
tp@8 107 %% Take inputs args from input structure
tp@8 108 dist = inputstruct.dist;
tp@8 109 azim = inputstruct.azim;
tp@8 110 orient = inputstruct.orient;
tp@8 111 dirweight = inputstruct.dirweight;
tp@8 112
tp@8 113 %% Validate attributes
tp@8 114 validateattributes(dist,{'double'},{'scalar','>',0})
tp@8 115 validateattributes(azim,{'double'},{'scalar','<=',90,'>=',-90})
tp@8 116 validateattributes(orient,{'char'},{'nonempty'})
tp@8 117 validateattributes(dirweight,{'double'},{'scalar','>',0})
tp@8 118
tp@8 119 %% Computation parameters (Some fixed, some taken from input arguments)
tp@8 120 params.board_size_x = .55;
tp@8 121 params.board_size_y = .02;
tp@8 122 params.board_size_z = .55;
tp@8 123
tp@8 124 params.board_azim = mod(90+azim,360)*pi/180;
tp@8 125 params.board_elev = 0*pi/180;
tp@8 126 params.board_dist = dist;
tp@8 127 %
tp@8 128 % params.board_azim = mod(90+[-17 17],360)*pi/180; % corresponds to a board 17degress to the right and a board 17
tp@8 129 % degrees to the left. The elements of the vector ([-17 17] in this example) must be in the range (-180,180] and are
tp@8 130 % converted by the line in this example to the coordinate system described in the help preample.
tp@8 131 %
tp@8 132 % params.board_elev = 10*pi/180; % corresponds to a board 10 degrees above the azimuthal plane. The value (0 in this
tp@8 133 % example) must be in the range [-90,90] and is converted by the line in this example to the coordinate system described
tp@8 134 % in the help preample.
tp@8 135 %
tp@8 136 % params.board_dist is in meters and it can be a vector of (strictly positive) distances as needed
tp@8 137
tp@8 138 params.board_orientation = {validatestring(orient,{'horz','angled'})};
tp@8 139
tp@8 140 params.source_down = 0.25; % Take the source to always be directly below the chin.
tp@8 141 params.source_front = 0.05; % Take the source to always be directly in front of chest.
tp@8 142
tp@8 143 params.Fs = 44100;
tp@8 144 params.Cair = 344;
tp@8 145 params.Rhoair = 1.21;
tp@8 146 params.WavDurationSec = 2;
tp@8 147 params.WavScaling = 0.1;
tp@8 148 params.dirweight = dirweight;
tp@8 149
tp@8 150 params.horz_disamb = 'BSPC2011';
tp@8 151 params.wavfilename = 'binsimecho.wav';
tp@8 152
tp@8 153 %% Compute free field (no head) IRs
tp@8 154
tp@8 155 temp_edges = [
tp@8 156 +params.board_size_x/2 +params.board_size_y/2 +params.board_size_z/2
tp@8 157 -params.board_size_x/2 +params.board_size_y/2 +params.board_size_z/2
tp@8 158 -params.board_size_x/2 -params.board_size_y/2 +params.board_size_z/2
tp@8 159 +params.board_size_x/2 -params.board_size_y/2 +params.board_size_z/2
tp@8 160 +params.board_size_x/2 +params.board_size_y/2 -params.board_size_z/2
tp@8 161 -params.board_size_x/2 +params.board_size_y/2 -params.board_size_z/2
tp@8 162 -params.board_size_x/2 -params.board_size_y/2 -params.board_size_z/2
tp@8 163 +params.board_size_x/2 -params.board_size_y/2 -params.board_size_z/2
tp@8 164 ];
tp@8 165
tp@8 166 FFresp{length(params.board_dist),length(params.board_azim),length(params.board_orientation)} = [];
tp@8 167 binaural_irs_echo{length(params.board_dist),length(params.board_azim),length(params.board_orientation)} = [];
tp@8 168 binaural_irs_emission{length(params.board_dist),length(params.board_azim),length(params.board_orientation)} = [];
tp@8 169 % NOTE: in the above initialisation the source term (binaural_irs_emission) does not need to be computed for each
tp@8 170 % different board distance/azimuth/orientation as it is the same. I include this redundancy so that the emission
tp@8 171 % binaural irs that I compute each time in in a few lines are correct, i.e. I can check that all the binaural responses
tp@8 172 % in the binaural_irs_emission cell are equal.
tp@8 173 board_coords(8,3,length(params.board_dist),length(params.board_azim),length(params.board_orientation)) = 0;
tp@8 174
tp@8 175
tp@8 176 geom.Fs = params.Fs;
tp@8 177 geom.Cair = params.Cair;
tp@8 178 geom.Rhoair = params.Rhoair;
tp@8 179
tp@8 180 geom.source_position = [0 params.source_front -params.source_down];
tp@8 181
tp@8 182 for ii = 1:length(params.board_dist)
tp@8 183 for jj = 1:length(params.board_azim)
tp@8 184 for kk = 1:length(params.board_orientation)
tp@8 185
tp@8 186 if strcmp(params.board_orientation{kk},'angled')
tp@8 187 theta_angled = params.board_azim(jj) - pi/2;
tp@8 188 Rot_mat = [
tp@8 189 cos(theta_angled) -sin(theta_angled) 0
tp@8 190 sin(theta_angled) cos(theta_angled) 0
tp@8 191 0 0 1
tp@8 192 ];
tp@8 193 temp2_edges = ( Rot_mat * temp_edges.' ) .' ;
tp@8 194 % for the rotation matrix see http://en.wikipedia.org/wiki/Rotation_matrix
tp@8 195 [trans_x, trans_y, trans_z] = sph2cart(params.board_azim(jj),params.board_elev,params.board_dist(ii));
tp@8 196 geom.scatterer_edges = ...
tp@8 197 temp2_edges + repmat([trans_x, trans_y, trans_z],8,1);
tp@8 198 board_coords(:,:,ii,jj,kk) = geom.scatterer_edges;
tp@8 199 elseif strcmp(params.board_orientation{kk},'horz')
tp@8 200 temp2_edges = temp_edges;
tp@8 201 if strcmp(params.horz_disamb,'collocated')
tp@8 202 [trans_x, trans_y, trans_z] = sph2cart(params.board_azim(jj),params.board_elev,params.board_dist(ii));
tp@8 203 elseif strcmp(params.horz_disamb,'BSPC2011')
tp@8 204 [trans_x, trans_y, trans_z] = sph2cart(...
tp@8 205 params.board_azim(jj),...
tp@8 206 params.board_elev,...
tp@8 207 params.board_dist(ii)/abs(cos(params.board_azim(jj)-pi/2)));
tp@8 208 else
tp@8 209 error('params.horz_disamb must be set to either ''BSPC2011'' or ''collocated''')
tp@8 210 end
tp@8 211 geom.scatterer_edges = ...
tp@8 212 temp2_edges + repmat([trans_x, trans_y, trans_z],8,1);
tp@8 213 board_coords(:,:,ii,jj,kk) = geom.scatterer_edges;
tp@8 214 end
tp@8 215
tp@8 216 FFresp(ii,jj,kk) = edhrir_single_cuboid(geom);
tp@8 217
tp@8 218 end
tp@8 219 end
tp@8 220 end
tp@8 221
tp@8 222 %% Compute binaural IRs
tp@8 223
tp@8 224 temp = load('hrir_final.mat'); %This is subject_165 CIPIC data
tp@8 225 h3D_lear = temp.hrir_l;
tp@8 226 h3D_rear = temp.hrir_r;
tp@8 227 clear temp
tp@8 228
tp@8 229 cipic_source_az = 0*180/pi;
tp@8 230 cipic_source_el = atan(-params.source_down/params.source_front)*180/pi;
tp@8 231 [source_lear_ir, azerr(1,1+length(params.board_azim)), elerr(1,1+length(params.board_azim))] = ...
tp@8 232 getNearestUCDpulse(cipic_source_az,cipic_source_el,h3D_lear);
tp@8 233 [source_rear_ir, azerr(2,1+length(params.board_azim)), elerr(2,1+length(params.board_azim))] = ...
tp@8 234 getNearestUCDpulse(cipic_source_az,cipic_source_el,h3D_rear);
tp@8 235
tp@8 236 % NOTE: in the following computation the source term (binaural_irs_emission) does not need to be computed for each
tp@8 237 % different board distance/azimuth/orientation as it is the same. I include the redundancy as a check that the geometry
tp@8 238 % modelling and computations are correct, i.e. I can check that all the binaural responses in the binaural_irs_emission
tp@8 239 % cell are equal.
tp@8 240
tp@8 241 for ii = 1:length(params.board_azim)
tp@8 242
tp@8 243 cipic_board_az = 90-params.board_azim(ii)*180/pi;
tp@8 244 cipic_board_el = params.board_elev*180/pi;
tp@8 245 [lear_ir, azerr(1,ii), elerr(1,ii)] = getNearestUCDpulse(cipic_board_az,cipic_board_el,h3D_lear);
tp@8 246 [rear_ir, azerr(2,ii), elerr(2,ii)] = getNearestUCDpulse(cipic_board_az,cipic_board_el,h3D_rear);
tp@8 247
tp@8 248 for jj = 1:length(params.board_dist)
tp@8 249 for kk = 1:length(params.board_orientation)
tp@8 250
tp@8 251 binaural_irs_emission{jj,ii,kk}(1,:) = fftconv(source_lear_ir,FFresp{jj,ii,kk}(2,:)); %source term
tp@8 252 binaural_irs_emission{jj,ii,kk}(2,:) = fftconv(source_rear_ir,FFresp{jj,ii,kk}(2,:)); %source term
tp@8 253
tp@8 254 binaural_irs_echo{jj,ii,kk}(1,:) = fftconv(lear_ir,FFresp{jj,ii,kk}(1,:)+FFresp{jj,ii,kk}(3,:));
tp@8 255 %board echo term
tp@8 256 binaural_irs_echo{jj,ii,kk}(2,:) = fftconv(rear_ir,FFresp{jj,ii,kk}(1,:)+FFresp{jj,ii,kk}(3,:));
tp@8 257 %board echo term
tp@8 258
tp@8 259 end
tp@8 260 end
tp@8 261 end
tp@8 262
tp@8 263 simulation_data.echo = binaural_irs_echo;
tp@8 264 simulation_data.emission =binaural_irs_emission;
tp@8 265 simulation_data.params = params;
tp@8 266 simulation_data.board_coords = board_coords;
tp@8 267 simulation_data.azerr = azerr;
tp@8 268 simulation_data.elerr = elerr;
tp@8 269
tp@8 270 %% Compute binaural signals and save binaural wav file
tp@8 271
tp@8 272 binaural_ir = params.dirweight*simulation_data.emission{1,1,1} + simulation_data.echo{1,1,1};
tp@8 273 stim = params.WavScaling*randn(1,params.WavDurationSec*params.Fs);
tp@8 274 temp = [fftfilt(binaural_ir(1,:),stim);fftfilt(binaural_ir(2,:),stim)].';
tp@8 275 if max(max(abs(temp))) > 1
tp@8 276 warning('wavfile clipped')
tp@8 277 end
tp@8 278 audiowrite(params.wavfilename,temp,params.Fs);
tp@8 279
tp@8 280 %% Subfunctions
tp@8 281
tp@8 282 function [pulse, azerr, elerr] = getNearestUCDpulse(azimuth,elevation,h3D)
tp@8 283 azimuth = pvaldeg(azimuth);
tp@8 284 elevation = pvaldeg(elevation);
tp@8 285
tp@8 286 elmax = 50;
tp@8 287 elindices = 1:elmax;
tp@8 288 elevations = -45 + 5.625*(elindices - 1);
tp@8 289 el = round((elevation + 45)/5.625 + 1);
tp@8 290 el = max(el,1);
tp@8 291 el = min(el,elmax);
tp@8 292 elerr = pvaldeg(elevation - elevations(el));
tp@8 293
tp@8 294 azimuths = [-80 -65 -55 -45:5:45 55 65 80];
tp@8 295 [azerr, az] = min(abs(pvaldeg(abs(azimuths - azimuth))));
tp@8 296
tp@8 297 pulse = squeeze(h3D(az,el,:));
tp@8 298
tp@8 299 function angle = pvaldeg(angle)
tp@8 300 dtr = pi/180;
tp@8 301 angle = atan2(sin(angle*dtr),cos(angle*dtr))/dtr;
tp@8 302 if angle < - 90
tp@8 303 angle = angle + 360;
tp@8 304 end
tp@8 305
tp@8 306 function out=fftconv(in1,in2)
tp@8 307 %
tp@8 308 % Computes the convolution output out of the input vectors in1 and in2
tp@8 309 % either with time-domain convolution (as implemented by conv) or by
tp@8 310 % frequency-domain filtering (as implemented by zero-padded fftfilt).
tp@8 311 %
tp@8 312 % out=fftconv(in1,in2)
tp@8 313 %
tp@8 314 in1=in1(:).';in2=in2(:).';
tp@8 315
tp@8 316 if (length(in1)>100 && length(in2)>100)
tp@8 317 if length(in1)>=length(in2)
tp@8 318 out=fftfilt(in2,[in1 zeros(1,length(in2)-1)]);
tp@8 319 else
tp@8 320 out=fftfilt(in1,[in2 zeros(1,length(in1)-1)]);
tp@8 321 end
tp@8 322 else
tp@8 323 out=conv(in1,in2);
tp@8 324 end
tp@8 325
tp@8 326 function ir_matrix=edhrir_single_cuboid(geom)
tp@8 327 %
tp@8 328 % Matrix of irs for a single cuboid scatterer using EDTB
tp@8 329 %
tp@8 330 % Works for external geometry. Definition of corners and planes in the created CAD file should be pointing outwards (in
tp@8 331 % cartesian coordinates as defined below).
tp@8 332 %
tp@8 333 % x coordinate (increasing to right in top view, right hand thumb)
tp@8 334 % y coordinate (increasing in front in top view, right hand index)
tp@8 335 % z coordinate (increasing upwards in top view, right hand middle)
tp@8 336 %
tp@8 337 % For more details about geometry description see the main help preample.
tp@8 338 %
tp@8 339 % geom input parameter should be a structure with the following fields:
tp@8 340 %
tp@8 341 % geom.scatterer_edges is a 8x3 matrix with elements:
tp@8 342 % x y z coordinates of back right top edge (point 1)
tp@8 343 % x y z coordinates of back left top edge (point 2)
tp@8 344 % x y z coordinates of front left top edge (point 3)
tp@8 345 % x y z coordinates of front right top edge (point 4)
tp@8 346 % x y z coordinates of back right bottom edge (point 1)
tp@8 347 % x y z coordinates of back left bottom edge (point 2)
tp@8 348 % x y z coordinates of front left bottom edge (point 3)
tp@8 349 % x y z coordinates of front right bottom edge (point 4)
tp@8 350 %
tp@8 351 % (or any sequence of rotations of a cuboid with edges as above)
tp@8 352 %
tp@8 353 % geom.source_position (Single) source coordinates in cartesian system defined 1x3 positive real vector. (Single)
tp@8 354 % receiver always taken to be at coordinates origin (0,0,0)
tp@8 355 %
tp@8 356 % geom.Fs
tp@8 357 % geom.Cair
tp@8 358 % geom.Rhoair
tp@8 359 %
tp@8 360
tp@8 361 %% 1
tp@8 362 temp_filename=['a' num2str(now*1e12,'%-24.0f')];
tp@8 363
tp@8 364 fid=fopen([temp_filename '.cad'],'w');
tp@8 365 % TODO add onCleanup to fclose this file
tp@8 366 fprintf(fid,'%%CORNERS\n\n');
tp@8 367 fprintf(fid,'%.0f %9.6f %9.6f %9.6f\n',...
tp@8 368 1, geom.scatterer_edges(1,1), geom.scatterer_edges(1,2), geom.scatterer_edges(1,3),...
tp@8 369 2, geom.scatterer_edges(2,1), geom.scatterer_edges(2,2), geom.scatterer_edges(2,3),...
tp@8 370 3, geom.scatterer_edges(3,1), geom.scatterer_edges(3,2), geom.scatterer_edges(3,3),...
tp@8 371 4, geom.scatterer_edges(4,1), geom.scatterer_edges(4,2), geom.scatterer_edges(4,3),...
tp@8 372 5, geom.scatterer_edges(5,1), geom.scatterer_edges(5,2), geom.scatterer_edges(5,3),...
tp@8 373 6, geom.scatterer_edges(6,1), geom.scatterer_edges(6,2), geom.scatterer_edges(6,3),...
tp@8 374 7, geom.scatterer_edges(7,1), geom.scatterer_edges(7,2), geom.scatterer_edges(7,3),...
tp@8 375 8, geom.scatterer_edges(8,1), geom.scatterer_edges(8,2), geom.scatterer_edges(8,3));
tp@8 376 fprintf(fid,'\n%%PLANES\n');
tp@8 377 fprintf(fid,'\n1 / /RIGID\n1 2 3 4\n');
tp@8 378 fprintf(fid,'\n2 / /RIGID\n5 8 7 6\n');
tp@8 379 fprintf(fid,'\n3 / /RIGID\n1 4 8 5\n');
tp@8 380 fprintf(fid,'\n4 / /RIGID\n1 5 6 2\n');
tp@8 381 fprintf(fid,'\n5 / /RIGID\n2 6 7 3\n');
tp@8 382 fprintf(fid,'\n6 / /RIGID\n7 8 4 3\n');
tp@8 383 fprintf(fid,'\n%%EOF');
tp@8 384 fclose(fid);
tp@8 385
tp@8 386 fid=fopen([temp_filename '_setup.m'],'wt');
tp@8 387 % TODO add onCleanup to fclose this file
tp@8 388 fprintf(fid,'\n global FSAMP CAIR RHOAIR SHOWTEXT');
tp@8 389 fprintf(fid,['\n FSAMP = ' num2str(geom.Fs) ';']);
tp@8 390 fprintf(fid,['\n CAIR = ' num2str(geom.Cair) ';']);
tp@8 391 fprintf(fid,['\n RHOAIR = ' num2str(geom.Rhoair) ';']);
tp@8 392 fprintf(fid,'\n SHOWTEXT = 0;');
tp@8 393 fprintf(fid,'\n SUPPRESSFILES = 0;');
tp@8 394 temp = strrep([cd filesep],'\','\\');
tp@8 395 fprintf(fid,['\n Filepath=''''''' temp ''''''';']);
tp@8 396 fprintf(fid,['\n Filestem=''' temp_filename ''';']);
tp@8 397 fprintf(fid,['\n CADfile=''' temp temp_filename '.cad'';']);
tp@8 398 fprintf(fid,'\n open_or_closed_model = ''open'';');
tp@8 399 fprintf(fid,'\n int_or_ext_model = ''ext'';');
tp@8 400 fprintf(fid,'\n EDcalcmethod = ''n'';');
tp@8 401 fprintf(fid,'\n directsound = 1;');
tp@8 402 fprintf(fid,'\n specorder = 1;');
tp@8 403 fprintf(fid,'\n difforder = 1;');
tp@8 404 fprintf(fid,'\n elemsize = [1];');
tp@8 405 fprintf(fid,'\n nedgesubs = 2;');
tp@8 406 fprintf(fid,'\n calcpaths = 1;');
tp@8 407 fprintf(fid,'\n calcirs = 1;');
tp@8 408 fprintf(fid,'\n sources=[');
tp@8 409 for n=1:1
tp@8 410 fprintf(fid,['\n' num2str(geom.source_position(1)) ' ' ...
tp@8 411 num2str(geom.source_position(2)) ' ' ...
tp@8 412 num2str(geom.source_position(3))]);
tp@8 413 end
tp@8 414 fprintf(fid,'\n ];');
tp@8 415 fprintf(fid,'\n receivers=[');
tp@8 416 for n=1:1
tp@8 417 fprintf(fid,'\n 0 0 0');
tp@8 418 end
tp@8 419 fprintf(fid,'\n ];');
tp@8 420 fprintf(fid,'\n skipcorners = 1000000;');
tp@8 421 fprintf(fid,'\n Rstart = 0;');
tp@8 422
tp@8 423 fclose(fid);
tp@8 424
tp@8 425 pause(1);
tp@8 426
tp@8 427 [~, ir_matrix]=myver_edtb([cd filesep temp_filename '_setup.m']);
tp@8 428
tp@8 429 delete([temp_filename '.cad']);
tp@8 430 delete([temp_filename '_setup.m']);
tp@8 431
tp@8 432 function [ir,varargout]=myver_edtb(EDsetupfile)
tp@8 433 % implements the computation of EDToolbox (by Peter Svensson) with a front-end
tp@8 434 % designed for my needs.
tp@8 435 %
tp@8 436 % Takes one input which a setup file as specified by Svensson and gives
tp@8 437 % either one vector output (which is the irtot output of Svensson's implementation for
tp@8 438 % the first source and first receiver in the input "EDsetupfile" setup file) or
tp@8 439 % two outputs, the first as above and the second being a {NxM} cell (N the number
tp@8 440 % of sources and M the number of receivers) each element of which is a 4xL matrix
tp@8 441 % with its 4 rows being the irdiff, irdirect, irgeom and irtot outputs of Svensson's
tp@8 442 % computation for the corresponding source and receiver.
tp@8 443 %
tp@8 444 % The code deletes all other .mat files created by Svensson's
tp@8 445 % implementation.
tp@8 446 %
tp@8 447 % [ir ir_all_data]=myver_edtb(EDsetupfile);
tp@8 448
tp@8 449 %keep record of files in the current directory before computation
tp@8 450 dir_bef=dir;
tp@8 451 ir=1;
tp@8 452
tp@8 453 %run computation
tp@8 454 EDB1main(EDsetupfile);
tp@8 455
tp@8 456 %get files list in the current directory after computation
tp@8 457 dir_aft=dir;
tp@8 458
tp@8 459 %get all genuine files (excluding other directories) in current directory
tp@8 460 %after computation
tp@8 461 k=1;
tp@8 462 for n=1:size(dir_aft,1)
tp@8 463 if(~dir_aft(n).isdir)
tp@8 464 names_aft{k}=dir_aft(n).name;k=k+1; %#ok<AGROW>
tp@8 465 end
tp@8 466 end
tp@8 467
tp@8 468 %get all genuine files (excluding other directories) in current directory
tp@8 469 %before computation
tp@8 470 k=1;
tp@8 471 for n=1:size(dir_bef,1)
tp@8 472 if(~dir_bef(n).isdir)
tp@8 473 names_bef{k}=dir_bef(n).name;k=k+1; %#ok<AGROW>
tp@8 474 end
tp@8 475 end
tp@8 476
tp@8 477 %get irtot for 1st source 1st receiver and delete files created by
tp@8 478 %computation.
tp@8 479 % for n=1:length(names_aft)
tp@8 480 % if ~strcmp(names_aft(n),names_bef)
tp@8 481 % if ~isempty(strfind(names_aft{n},'_1_1_ir.mat'))
tp@8 482 % temp=load(names_aft{n});
tp@8 483 % ir=full(temp.irtot);
tp@8 484 % clear temp
tp@8 485 % end
tp@8 486 % delete(names_aft{n})
tp@8 487 % end
tp@8 488 % end
tp@8 489 for n=1:length(names_aft)
tp@8 490 if ~strcmp(names_aft(n),names_bef)
tp@8 491 if ~isempty(strfind(names_aft{n},'_1_1_ir.mat'))
tp@8 492 temp=load(names_aft{n});
tp@8 493 ir=full(temp.irtot);
tp@8 494 clear temp
tp@8 495 end
tp@8 496 if nargout>1
tp@8 497 if ~isempty(strfind(names_aft{n},'_ir.mat'))
tp@8 498 temp=load(names_aft{n});
tp@8 499 temp2=regexp(regexp(names_aft{n},'_\d*_\d*_','match'),'\d*','match');
tp@8 500 temp3{str2double(temp2{1}(1)),str2double(temp2{1}(2))}(4,:)=full(temp.irtot); %#ok<AGROW>
tp@8 501 temp3{str2double(temp2{1}(1)),str2double(temp2{1}(2))}(1,:)=full(temp.irdiff); %#ok<AGROW>
tp@8 502 temp3{str2double(temp2{1}(1)),str2double(temp2{1}(2))}(2,:)=full(temp.irdirect); %#ok<AGROW>
tp@8 503 temp3{str2double(temp2{1}(1)),str2double(temp2{1}(2))}(3,:)=full(temp.irgeom); %#ok<AGROW>
tp@8 504 clear temp temp2
tp@8 505 end
tp@8 506 end
tp@8 507 delete(names_aft{n})
tp@8 508 end
tp@8 509 end
tp@8 510 if nargout>1
tp@8 511 varargout(1)={temp3};
tp@8 512 end
tp@8 513