%  Demonstration of Field II, October 3, 2024, JAJ
%  Start the system and initialize the path

%path(path,'/home/jaj/programs/field_II/M_files')
%path(path,'/users/jaje_dtu/local_data/undervisning/k_22485_31545_billeder/lecturs/for_3_array_sim/demo/field/Field_II_ver_3_30_mac/')
addpath('/Users/jaje/Documents/field_II/Field_II_ver_3_30_mac')

%  Initialize the field system

%field_init
field_init(0)

%  Set parameters for transducer aperture

f0=5e6;                  %  Transducer center frequency [Hz]
fs=100e6;                %  Sampling frequency [Hz]
c=1540;                  %  Speed of sound [m/s]
lambda=c/f0;             %  Wavelength
width=lambda*0.9;        %  Width of element
element_height=10/1000;  %  Height of element [m]
kerf=lambda*0.1;         %  Kerf [m]
focus=[0 0 60]/1000;     %  Fixed focal point [m]
N_elements=64;           %  Number of physical elements

%  Set the sampling frequency

set_sampling(fs);

%  Make the aperture for the  response

aperture = xdc_linear_array (N_elements, width, element_height, kerf, 1, 1, focus);

%  Set the excitation of the aperture

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

%  Calculate for the points

x=(-10:0.25:10);         %  Lateral positions for field calculation
points=[ x; x*0; 20 + x*0]'/1000;     %  Point for calculation

disp('Finding response for array')
[p, t_start] = calc_hhp(aperture,aperture,points);

%  Find envelope and display

figure(1)
env=20*log10(abs(hilbert(p)));
env=env-max(max(env));
depth=((1:size(p,1))/fs + t_start)*c/2;
contour (x, depth*1000, env, -0.1:-6:-60)
colormap(jet)
xlabel('Lateral distance [mm]')
ylabel('Axial distance [mm]')
colorbar
title('Without apodization')
grid

%  Make an other example with apodization

xdc_apodization (aperture, 0, hanning(N_elements)');
[p, t_start] = calc_hhp(aperture,aperture,points);

%  Find envelope and display

figure(2)
env=20*log10(abs(hilbert(p)));
env=env-max(max(env));
depth=((1:size(p,1))/fs + t_start)*c/2;
contour (x, depth*1000, env, -0.1:-6:-60)
xlabel('Lateral distance [mm]')
ylabel('Axial distance [mm]')
colorbar
title('With apodization')
grid

%  Move the focus point

focus=[0 0 20]/1000;     %  Fixed focal point [m]
xdc_focus (aperture, 0, focus);
[p, t_start] = calc_hhp(aperture,aperture,points);

%  Find envelope and display

figure(3)
env=20*log10(abs(hilbert(p)));
env=env-max(max(env));
depth=((1:size(p,1))/fs + t_start)*c/2;
contour (x, depth*1000, env, -0.1:-6:-60)
xlabel('Lateral distance [mm]')
ylabel('Axial distance [mm]')
colorbar
title('At the focus points')
grid

%  More elements

N_elements=128;           %  Number of physical elements

%  Make the aperture for the  response

aperture2 = xdc_linear_array (N_elements, width, element_height, kerf, 1, 1, focus);
xdc_excitation (aperture2, excitation);
xdc_impulse (aperture2, excitation);
xdc_apodization (aperture2, 0, hanning(N_elements)');
[p, t_start] = calc_hhp(aperture2,aperture2,points);

%  Find envelope and display

figure(4)
env=20*log10(abs(hilbert(p)));
env=env-max(max(env));
depth=((1:size(p,1))/fs + t_start)*c/2;
contour (x, depth*1000, env, -0.1:-6:-60)
xlabel('Lateral distance [mm]')
ylabel('Axial distance [mm]')
title('128 elements')
hold off
colorbar
grid

