na = 8;
noverlap = 100;
x=0.05;

signal = vinj_unlock;
signal = signal(round(x*length(signal)):length(signal));
nx = max(size(signal));
w = hanning(floor(nx/na));
[Pxx,f]=pwelch(signal,w,noverlap,[],1/ts);
Pxx_unlock = Pxx;

signal = vinj_lock;
signal = signal(round(x*length(signal)):length(signal));
nx = max(size(signal));
w = hanning(floor(nx/na));
[Pxx,f]=pwelch(signal,w,noverlap,[],1/ts);
Pxx_lock = Pxx;

psd_db_unlocked = pow2db(Pxx_unlock);
psd_db_locked = pow2db(Pxx_lock);

PSD_unlocked = [1e-6.*f(100:1700), psd_db_unlocked(100:1700)];
PSD_locked = [1e-6.*f(100:1700), psd_db_locked(100:1700)];

figure
loglog(f,Pxx_unlock, 'DisplayName','not locked')
hold on
grid on
loglog(f,Pxx_lock, 'DisplayName','locked')

writematrix(PSD_unlocked,'PSD_unlocked.csv');
writematrix(PSD_locked,'PSD_locked.csv');