function fps = find_peaks(x,th2,fpmin,fpmax,fsmp)
  % Given a sinal {x}, returns a table {fps} of frequencies and presumed
  % widths of peaks in its power spectrum.
  %
  % A peak is a set of consecutive or 
  % almost-consecutive elements
  % in the power spectrum with value greater than {th2}
  % and whose central frequency is {fpmin} or more.
  % Each row of {fps} has three elements {f,fsigma,pwr}
  % where {f} is the estimated central frequency
  % of the peak, {fsigma} is its estimated standard
  % deviation, and {pwr} is the estimated total power.  
  % 
  % Assumes that the sampling frequency is {fsmp}.
  % All frequencies are (in Hz)
  assert(size(x,2) == 1)
  N = size(x,1);
  assert(mod(N,2) == 0)
  NP = floor(N/2) + 1;  % Number of entries in power spectrum.
  
  u = cosine_window(N,fsmp); % Use a cosine window to get cleaner peaks.
  xu = x .* u;
  XU = fft(xu);
  XUP = power_spectrum_from_fft(XU);
  assert(size(XUP,1) == NP)
  assert(size(XUP,2)==1)
  fps = zeros(NP,3);
  nf = 0; % Number of peaks already found.
  df_ini = -1; % First recent digital freq above threshold.
  v2max = NaN;
  ct = NaN;  % Num of small elements after last big one.
  imin = 1;
  imax = NP;
  for ii = imin:imax+1
    if (ii <= imax) && (XUP(ii,1) > th2)
      % Big element:
      v2 = XUP(ii,1);
      if df_ini < 0
        % Save first digital freq above threshold.
        df_ini = ii - 1; % Digital freq of peak start.
        v2max = v2;
      elseif v2 > v2max
        v2max = v2;
      end
      ct = 0; % Counts small elems after last big one.
    else 
      % Not a big element:
      if df_ini >= 0
        ct = ct + 1;
        if (ii > imax) || (ct >= 25)
          % Found tentative end of peak
          df_fin = ii - 1 - ct;
          fm = fsmp*((df_ini + df_fin)/2)/N; % Est mean value.
          fs = 4*fsmp*((df_fin - df_ini+1)/2)/N; % Est std dev (hack).
          if (fm >= fpmin) && (fm <= fpmax)
            nf = nf + 1;
            fps(nf,1) = fm;
            fps(nf,2) = fs; 
            fps(nf,3) = v2max; 
          end
          df_ini = -1;
          v2max = NaN;
          ct = NaN;
        end
      end
    end
  end
  fps = fps(1:nf,:);
  