function fr = detectLine(F,S, band)
    % F = vector of frequencies as returned by MATLAB spectrogram function
    % S = spectrogram matrix as returned by MATLAB spectrogram function
    % band = [fmin, fmax] is the band where the line is wandering.
    
    % function to fit a gaussian + background
    g = @(p, f) p(1) + p(2)*exp(- (f - p(3)).^2 / p(4));
    
    % init frequency vector
    fr = zeros(size(S,2),1);
    % find the right indexes
    idx = find(F>band(1) & F<band(2));
    % loop over all time slices
    npt = size(S,2);
    ct = 0;
    for i=1:size(S,2)
        % just some couting to print out progress
        ct = ct + 1;
        if ct > npt/10
            fprintf(2, '%d%%\n', round(i/npt*100));
            ct = 0;
        end
        % find a first estimate of the peak position using the maximum of
        % the PSD
        [aa,ii] = max(abs(S(idx,i)));
        fr(i) = F(idx(ii));
        % estimate background
        sc = mean(abs(S(idx,i)));
        % initial guess
        p0 = [1, aa/sc, fr(i), 1];
        % fit gaussian to data
        p = lsqcurvefit(g, p0, F(idx), abs(S(idx,i))/sc, [],[], optimset('display', 'off'));
        % save center frequency
        fr(i) = p(3);
    end
end