function yV = gTSspectralexponent(n,eta,offset)
% yV = gTSspectralexponent(n,eta,offset)
% GTSSPECTRALEXPONENT generates time series with a power spectrum of
% 1/f^eta where f is the frequency and 'eta' is the spectral exponent. 
% An offset from the origin of frequencies can be given, so that the slope
% starts beyond the given 'offset'. 
% INPUT
% - n       : the length of the time series to be generated.
% - eta     : the spectral exponent.
% - offset  : the offset for the start of the spectral slope.
% OUTPUT
% - yV      : the generated time series.

if nargin==2
    offset = 0;
end
n2 = floor(n/2);
fV = linspace(0,0.5,n2)'; % the frequencies.
fV = fV(2:n2);
ampV = fV.^(-eta/2); % the spectral exponent is halved because it regards the
                     % amplitudes of FT, i.e. square root of power spectrum.
ifV = find(fV<offset); % In case an offset is given
if ~isempty(ifV)
     ampV(ifV)=ampV(ifV(end)); % For frequencies up to the given offset set
                               % the spectrum flat 
end
angV = [rand(n2-1,1)-0.5]*2*pi;
yft1V = ampV.*exp(i*angV);
yftV = [ampV(1); yft1V; (n2+1)^(-eta/2); flipud(conj(yft1V))];
yV = ifft(yftV);
