% u_disperse1.m homework problem on dispersion of ultrasonic pulses
%? skeleton

f0 = 1.5;	% MHz
beta = 0.1;	% /MHz /cm	approximate 1dB/cm/MHz
rect = @(t) abs(t) < 0.5; % a handy 'rect' function

% sample points for envelope in time domain
n = 256;
ii = [0:(n-1)]'-n/2;
tt = ii/n / f0 * 30;			% time in microsec
dt = tt(2) - tt(1);
disp(sprintf('sampling rate %g MHz', 1/dt))

for kk=2:4
	% assign a(t), the envelope
	if kk == 1
		at = ones(size(tt));	% no modulation 
		atype = 'None';
	elseif kk == 2
		at = rect(tt*f0/3);	% rect modulation
		atype = 'Rect';
	elseif kk == 3
%?		at = ?			% you make the quadratic pulse
		atype = 'Quadratic';
	elseif kk == 4
%?		at = ?			% you make the gaussian pulse
		atype = 'Gaussian';
	end

	if 0 % plot pulse with modulation
		pt = at .* exp(2i*pi*f0*tt);
		plot(tt, real(pt), '-.')
	end

	% compute spectrum of envelope
	Af = fftshift(fft(at));

	% determine frequency samples
%?	fs = ?

	if 0 % plot spectrum
		plot(fs, abs(Af))
		xlabel 'f [MHz]', ylabel '|A(f)|'
		title 'Envelope spectrum'
		disp 'hit key'; pause, clf
	end

	% loop over different depths [cm]
	zlist = [0 4 8 12];
	for iz = 1:length(zlist)
		subplot(4, 3, (iz-1)*3+kk-1)
		z = zlist(iz);		% which depth

		% attenuation effects
%?		atten = ?
		% plot(fs, [atten abs(Af) / max(abs(Af))])

		% spectrum of dispersed pulse
%?		Adf = ?

		% plot(fs, abs(Adf)), xlabel 'f [MHz]', ylabel 'Attenuated spectrum'

		% return to time domain
%?		adt = ?

		if 1 % gain for attenuation correction (at center frequency)
%?			gain = ?
			adt = gain * adt;
		end

		plot(tt, abs(adt), '-', tt, at, ':')	% plot envelope
		axis([-8 8 0 1.4])
		text(0, 1.2, sprintf('z = %g cm', z))
		if iz == 4
			xlabel 't [{\mu}sec]', ylabel '|v(t)|'
		end
		if iz == 1, title(atype), end
	end
end
% print -deps output
%savefig tall u_disperse1
