function f = fold(x)
% Fold the spectrum given in x.
%
% Inputs:
% x : site frequency spectrum.
%
% Outputs:
% f : folded site frequency spectrum.
% fold.m
% Written by Andreas Wollstein, Ludwig Maximilians University of Munich.
% Copyright (c) 2015
%
% This file is part of a set of Matlab scripts published with:
% Bozicevic et al. 2015 Molecular Ecology
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see .
if mod(length(x),2)==1
n = floor(length(x)/2);
i1 = 1:n;
i2 = length(x):-1:n+2;
f = x(i1)+x(i2);
f(n+1) = x(n+1);
else
n = floor(length(x)/2);
i1 = 1:n;
i2 = length(x):-1:n+1;
f = x(i1)+x(i2);
end
end