function D = ndiff(X, n)

% Take the difference of a signal with itself delayed by N.
% Operates on rows of a matrix, i.e. along the time dimension of a
% spectrogram.  The returned matrix D is the same size as the
% original matrix X, with the first N columns of D being the same
% as X (i.e. X is assumed to be zero before t=0).

if(nargin < 2) n = 1; end

z = zeros(size(X,1), n);
x1 = [X z];
x2 = [z X];
D = x1 - x2;
D = D(:, 1:size(X,2));
