function [f, Pxx3] = tseries2spectrum(t,ts,n_windows)

 

dt = t(2)- t(1);
 
if((exist('n_windows','var')  == 0)|| (n_windows == 0))
[Pxx3,w] = pwelch(ts); %requires Signal Processing Toolbox
else
[Pxx3,w] = pwelch(ts,floor(length(ts)/n_windows)); %requires Signal Processing Toolbox
end                                          

%%converts spectrum to units, Power/Hz

Pxx3 = Pxx3*(2*pi*dt);

f = w/(2*pi*dt);
