piezo1 = load('mat/cedrat_la75b_med_1_stack.mat', 't', 'V_in', 'V_out');
piezo2 = load('mat/cedrat_la75b_med_2_stack.mat', 't', 'V_in', 'V_out');
piezo3 = load('mat/cedrat_la75b_med_3_stack.mat', 't', 'V_in', 'V_out');

%%
run setup;

win = hann(ceil(0.1/Ts));

[tf_1, f_1] = tfestimate(piezo1.V_in, piezo1.V_out, win, [], [], 1/Ts);
[co_1, ~] = mscohere(piezo1.V_in, piezo1.V_out, win, [], [], 1/Ts);


[tf_2, f_2] = tfestimate(piezo2.V_in, piezo2.V_out, win, [], [], 1/Ts);
[co_2, ~] = mscohere(piezo2.V_in, piezo2.V_out, win, [], [], 1/Ts);


[tf_3, f_3] = tfestimate(piezo3.V_in, piezo3.V_out, win, [], [], 1/Ts);
[co_3, ~] = mscohere(piezo3.V_in, piezo3.V_out, win, [], [], 1/Ts);

%%
figure;
ax1 = subplot(2, 1, 1);
hold on;
plot(f_1, abs(tf_1), 'DisplayName', '1 stack')
plot(f_2, abs(tf_2), 'DisplayName', '2 stacks')
plot(f_3, abs(tf_3), 'DisplayName', '3 stacks')
set(gca, 'Xscale', 'log'); set(gca, 'Yscale', 'log');
ylabel('Amplitude'); xlabel('Frequency [Hz]');
hold off;
legend('location', 'southwest');
ylim([0.1, 20]);

ax2 = subplot(2, 1, 2);
hold on;
plot(f_1, 180/pi*unwrap(angle(tf_1)))
plot(f_2, 180/pi*unwrap(angle(tf_2)))
plot(f_3, 180/pi*unwrap(angle(tf_3)))
set(gca, 'Xscale', 'log'); set(gca, 'Yscale', 'lin');
ylabel('Phase'); xlabel('Frequency [Hz]');
hold off;
ylim([-360, 0]);

linkaxes([ax1,ax2], 'x');
xlim([10, 5000]);