function model = trainBackgroundKde(y, fs);

% Train a kernel density estimate on a sound so that very unlikely
% frames of another sound based on the same background model can be
% assumed to be interesting.

frame = 32;
maxFrames = 1000;

S = log(abs(specgram(y, frame, fs)));
good = find(isfinite(sum(S)));
Sg = S(:,good);
N = size(Sg, 2)

model = kde(Sg, 'rot')

% If we have too many frames, resample the kde
if(N > maxFrames)
  model = resample(model, maxFrames)
% $$$   model = reduce(model, 'rsde')      % >710 seconds
% $$$   model = reduce(model, 'grad', maxFrames) % >1100 seconds
% $$$   model = reduce(model, 'kdtree')   % >678 seconds
end

