function f_mean = mean_psd_freq(y, fs, wind, winover, padd)

% f_mean = mean_med_psd_freq(y, fs, wind, winover, padd)
% 
% This function calculates the mean frequency of power spectrum.
% 
% y         - input column vector
% fs        - sampling frequency
% wind      - length of window in samples or window samples
% winover   - window overlapp in samples
% padd      - if set to 1, the last segment will be padded by zeros 
%             if necessary (default: 0)
% f_mean    - output mean frequency of power spectrum

%% Paths and variables
if ((nargin < 3) || (isempty(wind)))
    wind = hamming(0.02*fs); 
end
if ((nargin < 4) || (isempty(winover)))
    winover =  length(wind)/2;
end
if ((nargin < 5) || (isempty(padd)))
    padd = 0;
end

%% Segment the signal
[m, n] = size(y);
if ((m == 1) || (n == 1))
    if (m < n)
        y = y.';
    end
    Y = segmentation(y, wind, winover, padd);
else
    Y = y;
end

%% Calculate the power spectrum and integral
psd = abs(fft(Y)).^2;
psd = psd(1:fix(end/2), :);

int = sum(psd);
tmp = cumsum(psd)./int(ones(size(psd, 1), 1), :);

%% Calculate the mean frequency of power spectrum
f_mean = sum(tmp <= 0.5);
f_mean = (f_mean.*fs)./(size(psd, 1)*2);