%% Triangular Targets

%% Triangular Wave Functions
%
% Max dims of the parts is 406 mmW x 355mm L x 406mm H

close all; clear

x = 0:.1:310;

% Triangular wave

tri = @(x,lambda,A) A*sawtooth(2*pi./lambda * x, 0.5);

% Chirp
k = @(lam0,lam1,x) (1/lam1-1/lam0)/(x(end)-x(1)); %freq rate change
f = @(lam0,lam1,x) (1/lam0) + k(lam0,lam1,x)*x./2;
fmchirp = @(lam0,lam1,x,A) A * cos(2*pi*f(lam0,lam1,x).*x);

%% Row 1: Period of 150-mm (amplitude 30-mm)
%
figure(1)
lambda = 150;
A = 30; 
plot(x,tri(x,lambda,A),'LineWidth',1);
title(['Row 1: Triangular \lambda = ' num2str(lambda) '-mm | Amp = ' num2str(A) '-mm']); 
axis square equal;
%%
axis off;
title '';
saveas(gcf,'triangRow1.svg');

%% Row 2: Period of 75-mm (amplitude 15-mm)
%
figure(2)
lambda = 75;
A = 15; 
plot(x,tri(x,lambda,A),'LineWidth',1);
title(['Row 2: Triangular \lambda = ' num2str(lambda) '-mm | Amp = ' num2str(A) '-mm']); 
axis square equal;
%%
axis off;
title '';
saveas(gcf,'triangRow2.svg');

%% Row 3: Period of 60-mm to 30-mm (amplitude 10-mm)
%
figure(3)
lambda0 = 60;
lambda1 = 30;
A = 10;
y = fmchirp(lambda0,lambda1,x,A);
tol = 0.01;
idx = (abs(diff(y)) <= tol);
plot(x(idx), -y(idx),'LineWidth',1);
title(['Row 3: Triangular \lambda = ' num2str(lambda0) '-mm to ' num2str(lambda1) '-mm | Amp = ' num2str(A) '-mm']);
axis square equal;

%%
axis off;
title '';
saveas(gcf,'triangRow3.svg');

%% Row 4: Period of 30-mm to 5-mm (amplitude 5-mm)
% You'll see here that the tolerance for detecting peaks doesn't have
% enough range for a perfect transform, but its good enough for the
% printer tolerances.
figure(4)
lambda0 = 30;
lambda1 = 5;
A = 5;

y = fmchirp(lambda0,lambda1,x,A);
tol = 0.05;
idx = (abs(diff(y)) <= tol);
plot(x(idx), -y(idx),'LineWidth',1);
title(['Row 4: Triangular \lambda = ' num2str(lambda0) '-mm to ' num2str(lambda1) '-mm | Amp = ' num2str(A) '-mm']);
axis square equal;
%%
axis off;
title '';
saveas(gcf,'triangRow4.svg');

