diff --git a/Sound Fields/MATLAB/Functions/ptSrcFieldTD.m b/Sound Fields/MATLAB/Functions/ptSrcFieldTD.m index 3a1ccfce887ad7e4ea8d74193649143ec26ef2b9..01fa2c62872193b9362ebd5ccb9fd37ef4134279 100644 --- a/Sound Fields/MATLAB/Functions/ptSrcFieldTD.m +++ b/Sound Fields/MATLAB/Functions/ptSrcFieldTD.m @@ -3,7 +3,7 @@ % Author: Achilles Kappis % e-mail: axilleaz@protonmail.com % -% Date: 13/09/2024 (DD/MM/YYYY) +% Date: 14/09/2024 (DD/MM/YYYY) % % Copyright: MIT % -------------------------------------------------- @@ -25,12 +25,23 @@ % Q [numeric] (Optional): The source signals. This can be either an IxNxJ % array, where I is the length of the source % signals in samples and J is the number of -% trials/sound field realisations, or a real -% positive integer denoting the length of the -% source signals. In case the length of the source -% signals is given, the generated signals are -% zero-mean, (approximately) uniformly distributed -% and normalised to unity. [Default: 128]. +% trials/sound field realisations, a vector of +% positive real values holding the source strengths +% (this is just an amplitude scaling factor), or a +% real positive integer denoting the common +% strength of all the source signals. [Default: 1]. +% +% sigLen [numeric] (Optional): The length of the source signals. This must +% be real positive integer. The generated +% signals are zero-mean, (approximately) +% uniformly distributed and normalised to +% unity. If "Q" is an array, "sigLen" is used +% to set the length of the signals. If +% "sigLen" is smaller than I, the signals are +% truncated and if it is larger the signals +% are padded with zeros. Leave empty to have +% the signals unchanged. +% [Default: 128 or if Q is matrix size(Q, 1)]. % % nTrials [numeric] (Optional): This is the number of trials/sound field % realisations that will be generated. If "Q" @@ -72,11 +83,11 @@ % long source signals % % -------------------------------------------------- -function [rSig, rSigMean, rSigMtx, rSigMtxMean, Q] = ptSrcFieldTD(sPos, rPos, fs, Q, nTrials, c) +function [rSig, rSigMean, rSigMtx, rSigMtxMean, Q] = ptSrcFieldTD(sPos, rPos, fs, Q, sigLen, nTrials, c) % ==================================================== % Check for number of arguments % ==================================================== - narginchk(3, 6); + narginchk(3, 7); nargoutchk(0, 5); % ==================================================== @@ -91,23 +102,36 @@ function [rSig, rSigMean, rSigMtx, rSigMtxMean, Q] = ptSrcFieldTD(sPos, rPos, fs if nargin > 3 && ~isempty(Q) if isscalar(Q) validateattributes(Q, "numeric", {'scalar', 'real', 'nonnan', 'nonempty', 'finite', 'positive', 'integer'}, mfilename, "Length of source signals in samples", 4); + elseif isvector(Q) + validateattributes(Q, "numeric", {'vector', 'real', 'nonnan', 'nonempty', 'finite', 'numel', size(sPos, 1)}, mfilename, "Source signals", 4); else validateattributes(Q, "numeric", {'3d', 'real', 'nonnan', 'nonempty', 'finite', 'ncols', size(sPos, 1)}, mfilename, "Source signals", 4); end else - Q = 128; + Q = 1; end - if ~isscalar(Q) + if nargin > 4 && ~isempty(sigLen) + validateattributes(sigLen, "numeric", {'scalar', 'nonempty', 'nonnan', 'finite', 'real', 'positive', 'integer'}, mfilename, "The length of the source signals", 5) + else + if ~isvector(Q) + sigLen = size(Q, 1); + else + sigLen = 128; + end + end + + + if ~ismatrix(Q) nTrials = size(Q, 3); - elseif nargin > 4 && ~isempty(nTrials) && isscalar(Q) - validateattributes(nTrials, "numeric", {'scalar', 'positive', 'nonnan', 'nonempty', 'finite'}, mfilename, "Number of trials/sound field realisations", 5); + elseif nargin > 5 && ~isempty(nTrials) + validateattributes(nTrials, "numeric", {'scalar', 'positive', 'nonnan', 'nonempty', 'finite', 'integer'}, mfilename, "Number of trials/sound field realisations", 6); else nTrials = 1; end - if nargin > 5 && ~isempty(c) - validateattributes(c, "numeric", {'scalar', 'nonempty', 'nonnan', 'finite', 'real', 'positive'}, mfilename, "The speed of sound", 6); + if nargin > 6 && ~isempty(c) + validateattributes(c, "numeric", {'scalar', 'nonempty', 'nonnan', 'finite', 'real', 'positive'}, mfilename, "The speed of sound", 7); else c = 343; end @@ -117,10 +141,17 @@ function [rSig, rSigMean, rSigMtx, rSigMtxMean, Q] = ptSrcFieldTD(sPos, rPos, fs % Pre-process data % ==================================================== % Generate source signals - if isscalar(Q) - Q = rand(Q, size(sPos, 1), nTrials); - Q = Q - mean(Q); - Q = Q./max(abs(Q)); + if isvector(Q) + tmp = rand(sigLen, size(sPos, 1), nTrials); + tmp = tmp - mean(tmp); + tmp = tmp./max(abs(tmp)); + Q = Q(:).' .* tmp; + elseif sigLen ~= size(Q, 1) + if sigLen > size(Q, 1) + Q = cat(1, Q, zeros(sigLen - size(Q, 1), size(Q, 2), size(Q, 3))); + else + Q = Q(1:sigLen, :, :); + end end % Calculate source-receiver distances