function [faults, out, f0, f1, f2, f3] = analyze(where, trans, what)
% WHERE is the folder containing the experiment.
% TRANS is false if the experiment was run using branch galene and true if
% the experiment was run using galene-whiplash-detection.
% WHAT is 'all', or 'rX' for which residual to plot. Anything else will
% result in no plots.
  
  cd(where);
  out = process(trans);

  thresholds;
  model_config;

  f0 = abs(out.r0)>threshold0;
  f1 = abs(out.r1)>threshold1;
  f2 = abs(out.r2)>threshold2;
  f3 = abs(out.r3)>threshold3;
  if trans % for experiments run with branch galene-whiplash-detection
	   % we need to filter out the transients in one of two ways:
    % way #1, not used in paper
%    out.trans = out.detect_trans; % filter out according to detection algo
    % way #2, used in paper
    out.trans = out.time_trans; % filter out as if experiment had been run
				% with branch galene
    out.t = out.t(~out.trans);
    out.p = out.p(~out.trans);
    out.r0 = out.r0(~out.trans);
    out.r1 = out.r1(~out.trans);
    out.r2 = out.r2(~out.trans);
    out.r3 = out.r3(~out.trans);
    f0 = abs(out.r0)>threshold0;
    f1 = abs(out.r1)>threshold1;
    f2 = abs(out.r2)>threshold2;
    f3 = abs(out.r3)>threshold3;
  end
  % The isolation strategy used for the paper:
  % find runs of 5 consecutive faults for each residual
  runs0 = zeros(size(f0));
  runs1 = zeros(size(f1));
  runs2 = zeros(size(f2));
  runs3 = zeros(size(f3));
  for j = 5:size(f0,2)
    runs0(j) = sum(f0(j-4:j));
    runs1(j) = sum(f1(j-4:j));
    runs2(j) = sum(f2(j-4:j));
    runs3(j) = sum(f3(j-4:j));
  end

  out.runs0 = runs0==5;
  out.runs1 = runs1==5;
  out.runs2 = runs2==5;
  out.runs3 = runs3==5;
  
  dT = out.t(2:end)-out.t(1:end-1);
  idx = find(dT < 1);
  avgdT = sum(dT(idx))/size(dT(idx),2);
  
  files = dir('fdi*.hdf5');
  experiments = size(files, 1)-1; % last file was thrown out from processing
  faults = [sum(f0), sum(f1), sum(f2), sum(f3), experiments, avgdT];
  
  cd ../
  
  if strcmp(what, 'r0') | strcmp(what, 'all')
    %% plot r0
    figure;
    ax0 = plot(out.T, alpha_param*out.E, '.');
    hold on
    plot(out.T, out.S, '.');
    plot(out.t, out.r0, '.');
    idx = find(out.runs0);
    plot(out.t(idx), out.r0(idx), '*');
    title('r0')
  end

  if strcmp(what, 'r1') | strcmp(what, 'all')
    %% plot r1
    figure;
    ax1 = plot(out.T, out.C, '.');
    hold on
    plot(out.t, out.r1, '.');
    idx = find(out.runs1)
    plot(out.t(idx), out.r1(idx), '*');
    title('r1')
  end

  if strcmp(what, 'r2') | strcmp(what, 'all')
    %% plot r2
    figure;
    ax2 = plot(out.T, out.C, '.');
    hold on
    plot(out.t, out.r2, '.');
    idx = find(out.runs2);
    plot(out.t(idx), out.r2(idx), '*');
    title('r2')
  end
  
  if strcmp(what, 'r3') | strcmp(what, 'all')
    %% plot r3
    figure;
    ax3 = plot(out.T, out.C, '.');
    hold on
    plot(out.t, out.r3, '.');
    idx = find(out.runs3);
    plot(out.t(idx), out.r3(idx), '*');
    idx = find(f3);
    plot(out.t(idx), out.r3(idx), 'o');
  
    title('r3')
  end
end
