function [sig,S,a,D] = findOnsetsGaussian(y, fs, sig)

% find onsets in a piece of sound by looking for derivatives in the
% rows of a spectrogram that many rows share.

frame = 32;
thresh = 1.5;

[S, freqs, times] = specgram(y, frame, fs);
S = log(abs(S));

% Under the assumption that the freqs are distributed according to a
% multivariate gaussian, how likely is it to get these values?
good = find(isfinite(sum(S)));
Sg = S(:,good);
mu = repmat(mean(Sg')', 1, size(S,2));
if(nargin < 3)
% $$$   sig = diag(1 ./ std(Sg'));  % Independent
  sig = inv(corrcoef(Sg'));   % Dependent
end
a = sig * (S - mu);   % whiten
a = sqrt(mean(a.^2)); % add independent gaussians together?
D = 1/2 * erf(a / sqrt(2));   % find likelihood

subplot 311, imagesc(S), axis xy, colormap gray(256);
subplot 312, plot(a), xlim([1 size(D,2)]);
subplot 313, plot(D), xlim([1 size(D,2)]);
subplot 111
