function [ts_clone] = clonets(ts_orig);

%  [ts_clone] = clonets(ts_orig);
%  
%  Clone Time Series (clonets) will take an input time series (ts_orig) and
%  create a clone time series (ts_clone). This clone has the same spectral
%  density and variance as the original however the phases of each of the
%  Fourier components have been randomized. Unfortunately this method will
%  not preserve higher order properties of an original time series (such as
%  skewness or kurtosis).  

i = sqrt(-1);
ts_orig = ts_orig(:); % ensure it is a column vector
N = length(ts_orig);

if mod(N,2) == 0 % even sample length

    F = fft(ts_orig); % take FFT to get periodogram
    phase = 2*pi*(rand(N/2-1,1)-0.5); % generate random phases
    phase = [ pi ; flipud(phase) ; 0 ; -phase]; % construct phase portion of periodogram
    F = abs(F).*exp(i*phase); % generate new periodogram with randomized phase and same amplitude
    ts_clone = real(ifft(F)); % inverse the periodogram to generate clone time series

else % odd sample length

    F = fft(ts_orig); % take FFT to get periodogram
    phase = 2*pi*(rand((N-1)/2,1)-0.5); % generate random phases
    phase=[ pi ; flipud(phase) ; -phase]; % construct phase portion of periodogram
    F = abs(F).*exp(i*phase); % generate new periodogram with randomized phase and same amplitude
    ts_clone = real(ifft(F)); % inverse the periodogram to generate clone time series

end