function MDF = medianfreq(X_psd,Fs)

w=[0:length(X_psd)-1]*Fs/length(X_psd);
w= w(1:ceil(length(X_psd)/2));

% X_psd= X.*conj(X); 
X_psd= X_psd(1:ceil(length(X_psd)/2));

P= 0;
k= 0;

Puk= sum(X_psd);
    
while P < 0.5*Puk
    
    k= k+1;
    P= P+X_psd(k);
    P1= P-X_psd(k);
    
end

% if (P-0.5*Puk) > (0.5*Puk-P1)
%     
%     k= k-1;
%     
% end   

% MDF=w(k);

if k==1 MDF=w(k); %tj. MDF= 0;
else
    % lineana interpolacija MDF izmeðu dvije frekvencije
    MDF= (w(k-1)+(0.5*Puk-P1)*1/(P-P1)*(w(k)-w(k-1)));    
end;
