function onsets = findOnsetsKde(y, fs, model)

% find onsets in a piece of sound by modelling the background noise
% as a gmm and finding things that don't fit this model.

frame = 32;
hop = frame/2;
thresh = exp(-40);

[S, freqs, times] = specgram(y, frame, fs);
S = log(abs(S));
good = find(isfinite(sum(S)));
Sg = S(:,good);

p = evaluate(model, Sg);

subplot 311, imagesc(Sg), axis xy, colormap gray(256);
subplot 312, plot(p), xlim([1 length(p)]);
subplot 313, plot(log(p)), xlim([1 length(p)]);
% $$$ subplot 313, plot(p < thresh), xlim([1 length(p)]);
subplot 111

onsets = find(p < thresh);
onsets = (onsets-1)*hop + 1;