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

% len = length(ts);
% N = 2^(nextpow2(len)-1)%N points for fft
% ts = ts(1:N);
 
dt = t(2)- t(1);
 
% sf = 1/dt;
% nf = sf/2;
% t = dt:dt:dt*len;

%this is the conventional method of calculating spectrum
%instead we use matlab signal processing toolbox option
%which makes use of the windowing technique

% p = fft(ts,N);
% 
% df = nf/(N/2);
% f=nf*(1:N/2 +1)/(N/2);
% 
% sp = 2 * p(1:N/2+1) .* conj(p(1:N/2+1))/(N*N);%Periodgram with total power equal to "mean squared amplitude" of signal
%                                               % i.e. Units of power per sample.
%sp3 = sp(1:N/2+1)'.*(2*pi*(0:N/2))/(2*pi*dt);


[Pxx3,w] = pwelch(ts); %requires Signal Processing Toolbox
                                              

%%converts spectrum to units, Power/Hz

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

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