%  Example of using Field II by Joergen Arendt Jensen, July 2, 1999

%  Start the system and initialize the path

path(path,'c:/users/jaj/programs/field_II/m_files')

%  Initialize the field system

field_init

%  Set basic parameters

f0=1e6;                  %  Transducer center frequency [Hz]
fs=100e6;                %  Sampling frequency [Hz]
c=1540;                  %  Speed of sound [m/s]
density=1e6;             %  Density [g/m^3]
lambda=c/f0;             %  Wavelength [m]
radius=10/1000;          %  Radius of piston transducer [m]

%  Generate an aperture

dist_field=1/1000;
ele_size=sqrt(dist_field*4*lambda);
aperture = xdc_piston (radius, ele_size);
xdc_show(aperture)
show_xdc(aperture)
ele_size=0.1/1000;
aperture = xdc_piston (radius, ele_size); 

%  Set the impulse response and excitation of the aperture

impulse_response=sin(2*pi*f0*(0:1/fs:2/f0));
impulse_response=impulse_response.*hanning(max(size(impulse_response)))';
xdc_impulse (aperture, impulse_response);

excitation=sin(2*pi*f0*(0:1/fs:2/f0));
excitation=excitation.*hanning(max(size(excitation)))';
xdc_excitation (aperture, excitation);

%  Make a calculation of the spatial impulse response

[h,t] = calc_h (aperture, [0 0 10]/1000);
plot((0:length(h)-1)/fs+t, h); xlabel('Time [s]'); ylabel('h [m/s]')
[h,t] = calc_h (aperture, [2 0 10]/1000);
plot((0:length(h)-1)/fs+t, h); xlabel('Time [s]'); ylabel('h [m/s]')
[h,t] = calc_h (aperture, [5 0 10]/1000);
plot((0:length(h)-1)/fs+t, h); xlabel('Time [s]'); ylabel('h [m/s]')
[h,t] = calc_h (aperture, [8 0 10]/1000);
plot((0:length(h)-1)/fs+t, h); xlabel('Time [s]'); ylabel('h [m/s]')
[h,t] = calc_h (aperture, [20 0 10]/1000);
plot((0:length(h)-1)/fs+t, h); xlabel('Time [s]'); ylabel('h [m/s]')

%  Make calculations for a number of points

points=[0:10; zeros(1,11); 10*ones(1,11)]'/1000
[h,t] = calc_h (aperture, points);
plot((0:length(h)-1)/fs+t, h); xlabel('Time [s]'); ylabel('h [m/s]')

%  Make a calculation of the emitted pressure

[p,t] = calc_hp (aperture, [0 0 50]/1000);
plot((0:length(p)-1)/fs+t, p*density); xlabel('Time [s]'); ylabel('p [Pa]')
points=[0:10; zeros(1,11); 50*ones(1,11)]'/1000
[p,t] = calc_hp (aperture, points);
plot((0:length(p)-1)/fs+t, p*density); xlabel('Time [s]'); ylabel('p [Pa]')

%  Make a calculation of the pulse-echo voltage

[v,t] = calc_hhp (aperture, aperture, [0 0 50]/1000);
plot((0:length(v)-1)/fs+t, v)

%  Setting parameters

fs=1000e6;                %  Sampling frequency [Hz]
set_field ('fs',fs);

%  Set the impulse response and excitation of the aperture again

impulse_response=sin(2*pi*f0*(0:1/fs:2/f0));
impulse_response=impulse_response.*hanning(max(size(impulse_response)))';
xdc_impulse (aperture, impulse_response);

excitation=sin(2*pi*f0*(0:1/fs:2/f0));
excitation=excitation.*hanning(max(size(excitation)))';
xdc_excitation (aperture, excitation);

[h,t] = calc_h (aperture, [8 0 10]/1000);
plot((0:length(h)-1)/fs+t, h)

%  Release the apertures

xdc_free (aperture);

%  Shut down field

field_end


