function [Tfit, Q2, Fs] = ajusta_MHD( t, Yfit )
% [Tfit, Q2, Fs] = ajusta_MHD( t, Yfit );
t = t-mean(t);

ind0up = find( Yfit(1:end-1)<0 & Yfit(2:end)>=0 );
T = median( diff( t(ind0up) ) );

Tfit = fminsearch( @(vT)FUN( vT, t, Yfit ), T );
[Q2,Fs] = FUN( Tfit, t, Yfit );

% Ts = T+.1*T*(-1:.1:1);
% for qTs = 1:length(Ts)
%     Xfit = [ cos( 2*pi*t/Ts(qTs) )  sin( 2*pi*t/Ts(qTs) ) ];
%     [ A, sA, Q2(qTs), NGL, VA, Fs(:,qTs) ] = mmqLINCOV( Xfit, Yfit, ones(size(Yfit)) );
% end
% Tfit = Ts( Q2==min(Q2) );

function [Q2,Fs] = FUN( T, t, Yfit )
    Xfit = [ cos( 2*pi*t/T )  sin( 2*pi*t/T ) ];
    [ A, sA, Q2, NGL, VA, Fs ] = ...
        mmqLINCOV( Xfit, Yfit, ones(size(Yfit)) );
