function y = sourcesep(x, fs, K)
% SOURCESEP Separate sources using NMF.
%
% Y = SOURCESEP(X, FS, K) separates a single-channel audio signal X at
% sampling rate FS into K signals. Y is a matrix with K columns, each
% column corresponding to one separated signal. This function performs
% nonnegative matrix factorization on the magnitude spectrogram of X using
% the Euclidean distance measure with K components. Each separated
% component preserves the phase of the original spectrogram.
%
% Author: Steve Tjoa
% Institution: University of Maryland (Signals and Information Group)
% Created: July 1, 2009
% Last modified: July 9, 2009
%
% This code was written during the workshop on Music Information Retrieval
% at the Center for Computer Research in Music and Acoustics (CCRMA) at
% Stanford University.
% Initialize parameters.
frame_sz = 2^floor(log2(0.1*fs));
frame_incr = frame_sz/16;
N = length(x);
y = zeros(N, K);
% Compute STFT.
X = parsesig(x, frame_sz, frame_incr);
% Perform NMF.
[A, S] = nmf(abs(X(1:frame_sz/2,:)), K, 'euc');
A(frame_sz:-1:frame_sz/2+2,:) = A(2:end,:);
% Synthesize each separated signal.
for k=1:K
Y = (A(:,k)*S(k,:)).*exp(j.*angle(X));
y(:,k) = synsig(Y, N, frame_incr);
end