I used the following code"
clear all;
%% CREATE COMPUTATIONAL GRID
Nx= 128; % number of grid points in the x direction
Ny= 256; % number of grid points in the y direction
dx = 20.6e-3/Nx; % grid point spacing in the y direction [m]
dy = 40e-3/Ny; % grid point spacing in the x direction [m]
kgrid = makeGrid(Nx, dx, Ny, dy);
%% SET MEDIUM
medium.sound_speed = 1500; % [m/s]
%% DEFINE THE TRANSDUCER
dia=2*floor(inch2m(0.5)/(2*dx)); % diameter of the transducer in grid pts
source.p_mask = makeLine(Nx, Ny, [Nx/2-dia/2 1], [Nx/2+dia/2 1]);
%% DEFINE INPUT SIGNAL
excitation='CW';
switch excitation
case 'toneBurst'
source_mag = 1e5; % [Pa]
f = 1e6; % [Hz]
tone_burst_cycles = 5;
[kgrid.t_array, dt] = makeTime(kgrid, medium.sound_speed);
source.p = source_mag.*toneBurst(1/kgrid.dt, f, tone_burst_cycles);
case 'CW'
f = 1e6; % excitation frequency [Hz]
source_mag = 0.5e6; % excitation amplitude [Pa]
nTimeSteps=2500;
dt=2e-8;
kgrid.t_array=(0:1:nTimeSteps)*dt;
source.p = source_mag*sin(2*pi*f*kgrid.t_array); % generate cw sine
ramp_length = round((2*pi/f)/dt);
ramp = (-cos( (0:(ramp_length-1))*pi/ramp_length ) + 1)/2; % prepare ramp wfm for moothing
source.p(1:ramp_length) = ramp .* source.p(1:ramp_length); % "windowed" cw sine
end
%% SENSOR
sensor.mask = [1, 1, Nx, Ny].';
sensor.record = {'p'};
sensor.record_start_index = kgrid.Nt - 10*1/(f*dt) + 1;
%% ASSIGN INPUT PARAMETERS AND RUN SIMULATION
display_mask = source.p_mask; % create a display mask to display the transducer
input_args = {'DisplayMask', display_mask, 'PMLInside', false,...
'DataCast', 'gpuArray-single', 'DataRecast', true,...
'PlotScale', [-1, 1]*source_mag, 'DisplayMask', source.p_mask,};
sensor_data = kspaceFirstOrder2D(kgrid, medium, source, sensor, input_args{:});
%% POST-PROCESSING
NormPressField=rms(sensor_data.p,3);
NormPressField=NormPressField./maxND(NormPressField);
%% VISUALISATION
figure % beam pattern
pcolor((0:1:Ny-1)*dy*1e3, kgrid.x_vec*1e3,NormPressField);
shading flat
caxis([0 1])
colormap(jet);
xlabel('x [mm]','FontSize',14);
ylabel('y [mm]','FontSize',14);
title('Beam Pattern','FontSize',16)
axis image;
colorbar
figure % on axis pressure
Pressure=NormPressField(end/2,2:end);
plot((2:1:Ny)*dy*1e3,Pressure,'b*')
xlabel('x-position [mm]');
ylabel('RMS pressure');
axis tight;
title('On axis RMS pressure');