% Emacs mode header: -*-Octave-*- source. %

clear all
cd ../tw-ode

xhp=+3.5; %positive x-hat-coeff
xhn=-3.5; %negative x-hat-coeff

loff=+15; xoff=+35; %latency offset/ x offset
omega=23.51; %11.1, 10, 20, %23.51, 82, 286,
[x,zres,ih,rh,ah] = tw_lin_M(omega); %tw_nonlin_4
yy=abs(rh);
F_int_convex_M
F_lambda_M
recc=1000./(3.5*omega.*lambda(:));  %(1:8) -> (:)
latcum=cumtrapz(-x(ilist(:)),recc)+loff; NL_1 = length(ilist)-1;

subplot(2,1,1) % velo
hold on, grid on
plot(xoff-3.5*x(ilist(1:NL_1)),3.5*omega.*lambda(1:NL_1)/50,'r')%(1:8)->(:)
plot(xoff-3.5*x(ilist(1:NL_1)),3.5*omega.*lambda(1:NL_1)/50,'r+')

subplot(2,1,2) % laten
hold on, grid on
plot(xoff-3.5*x(ilist),latcum,'kx')
plot(xoff-3.5*x(ilist(:)),latcum,'k')
clear all

loff=+15; xoff=+35;
omega=82; %11.1, 10, 20, %23.51, 82, 286,
[x,zres,ih,rh,ah] = tw_lin_M(omega); %tw_nonlin_4
yy=abs(rh);
F_int_convex_M
F_lambda_M
recc=1000./(3.5*omega.*lambda(:));  %(1:8) -> (:)
latcum=cumtrapz(-x(ilist(:)),recc)+loff; NL_1 = length(ilist)-1;

subplot(2,1,1) % velo
%hold on, grid on
plot(xoff-3.5*x(ilist(1:NL_1)),3.5*omega.*lambda(1:NL_1)/50,'r')%(1:8)->(:)
plot(xoff-3.5*x(ilist(1:NL_1)),3.5*omega.*lambda(1:NL_1)/50,'r+')

subplot(2,1,2) % laten
%hold on, grid on
plot(xoff-3.5*x(ilist),latcum,'kx')
plot(xoff-3.5*x(ilist(:)),latcum,'k')
clear all

loff=+15; xoff=+35;
omega=286; %11.1, 10, 20, %23.51, 82, 286,
[x,zres,ih,rh,ah] = tw_lin_M(omega); %tw_nonlin_4
yy=abs(rh);
F_int_convex_M
F_lambda_M
recc=1000./(3.5*omega.*lambda(:));  %(1:8) -> (:)
latcum=cumtrapz(-x(ilist(:)),recc)+loff; NL_1 = length(ilist)-1;

%title('Velocity and latency in non-dimensional units')
subplot(2,1,1) % velo
%hold on, grid on
plot(xoff-3.5*x(ilist(1:NL_1)),3.5*omega.*lambda(1:NL_1)/50,'r')%(1:8)->(:)
plot(xoff-3.5*x(ilist(1:NL_1)),3.5*omega.*lambda(1:NL_1)/50,'r+')
axis([xoff-3.5*10 xoff 0 30])
legend('red: velocity','+ sign: zero crossing',...
 'Location','NorthEast')
title('Velocity in m/s and latency in ms')
xlabel('Distance from stapes, mm')
ylabel('Velocities, m/s')

subplot(2,1,2) % laten
%hold on, grid on
plot(xoff-3.5*x(ilist),latcum,'kx')
plot(xoff-3.5*x(ilist(:)),latcum,'k')
%title(' Numerical calculation of latency and velocity')
axis([xoff-3.5*10 xoff -15+loff 0+loff])
legend('black: latency','x sign: zero crossing',...
 'Location','SouthEast')
xlabel('Distance from stapes, mm')
ylabel('Latencies, ms')
%set(gca,'XDir','reverse')
cd ../mkfig
