% a high-level function for computing a DTFT based DDM 
% (constructs the linear system of equations using distribution derivative rule and solves it via 
% pseudo-inverse (pinv). Use this function if non-Fourier kernels are needed or
% DTFT at specific frequency rather than FFT is required for whatever reason...
% no zero-padding is used internally
function [gdi,r, A,b, gdi_lns, r_lns] = ddm_gen(krnls, krlns_ders, mf_ders, sig, N, tol)
%generic multi-frequency distribution derivative based estimator for
% non-stationary sinusoidal analysis
%
%
% [1] Michael Betser: Sinusoidal Polynomial Estimation Using The Distribution
% Derivative, in IEEE Transactions on Signal Processing, Vol.57, Nr. 12,
% December 2009
%
% krnls: matrix of all the kernels... N x R , where R is the number of 
%      non-static parameters to estimate and at the same time, the number 
%      of kernels
%
% krlns_ders: matrix of all the kernel time derivatives... N x R , where R 
%     is the number of non-static parameters to estimate and at the same 
%     time, the number of kernels
%
% mf_ders: matrix of all the model function time derivatives... N x Q , where Q 
%     is the number of model functions
%
%
% sig: vector - signal, N x 1 (CAUTION: MUST be column vector!!!)
%
% N: odd integer - signal buffer length, ...
%
% For any reasonable use, Q equals R, otherwise it makes little sense.
% Kernels must include the window function...

[A,b] = ddm_lin_sys(krnls, krlns_ders, mf_ders, sig, N);

% solving via pinv with provided tolerance
gdi = pinv(A,tol) * b;
% r = rcond(A);
% [gdi_lns r_lns]  = linsolve(A,b);
end