ita_sph_mimo_error_simulation.m 22.7 KB
Newer Older
1 2 3
function varargout = ita_sph_mimo_error_simulation(varargin)
%ITA_SPH_MIMO_ERROR_SIMULATION - Simulate aliasing and noise errors in
%  spherical MIMO systems.
4 5 6
%  This function simulates the mismatch errors from noise, sampling dispacement and aliasing 
%  for a MIMO system comprised of a spherical loudspeaker array and a spherical microphone
%  array.
7 8 9 10 11
%
%  Syntax:
%   itaResult = ita_sph_mimo_error_simulation(options)
%
%   Options (default):
12 13 14 15 16 17
%           'SNR'				(60)	: SNR at each transducer
%           'nRuns'				(5)		: calculate average of nRuns number of source-receiver orientations
%           'dirMeasurementFile' ( [] ) : Filename for a directivity file that is to be included
%
%  Directivity files need to be in hdf5 format. The name of data fields need to be 'fullDirRe' and 
%  'fullDirIm' for the real and imaginary part respectively.
18 19
%
%  Example:
20
%   [individualTerms, allTerms, totalError] = ita_sph_mimo_error_simulation(sourceParams, receiverParams, opts)
21 22 23 24 25 26 27 28 29 30 31 32 33
%
%  See also:
%   ita_toolbox_gui, ita_read, ita_write, ita_generate
%
%   Reference page in Help browser 
%        <a href="matlab:doc ita_sph_mimo_error_simulation">doc ita_sph_mimo_error_simulation</a>

% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved. 
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder. 
% </ITA-Toolbox>


34
% Author: Marco Berzborn -- Email: marco.berzborn@akustik.rwth-aachen.de
35 36 37 38 39 40 41 42 43
% Created:  29-Mar-2016 


sArgs = struct('pos1_source','struct',...
               'pos2_receiver','struct',...
               'fftDegree',ita_preferences('fftDegree'),...
               'samplingRate',ita_preferences('samplingRate'),...
               'freqRange',[20 ita_preferences('samplingRate')/2],...
               'samplingDisplacement',[],...
44
			   'samplingDisplacementAbsolute',false,...
45 46 47 48 49 50 51 52 53 54 55
               'nRuns',1,...
               'SNR',60,...
               'sma',true,...
               'sla',true,...
               'regulParam',[],...
               'regulPower',1,...
               'simWNG',false,...
               'simDI',false,...
               'dirMeasurementFile',[]);
[source,receiver,sArgs] = ita_parse_arguments(sArgs,varargin);

56
% save all struct fields as variables for speed improvements in the parfor loop
57 58 59 60 61 62 63 64 65 66 67 68 69
receiverNmax = receiver.Nmax;
receiverSampling = receiver.sampling;

sourceNmax = source.Nmax;
sourcerMem = source.rMem;
sourceSampling = source.sampling;
sourceSamplingR = sourceSampling.r;

simSMA = sArgs.sma;
simSLA = sArgs.sla;

SNR = sArgs.SNR;
samplingDisplacement = sArgs.samplingDisplacement;
70
displacementType = sArgs.samplingDisplacementAbsolute;
71 72 73 74 75 76 77 78 79 80 81 82 83 84

ao = ita_generate_impulse('fftDegree',sArgs.fftDegree,'samplingRate',sArgs.samplingRate);
freqVec = ao.freqVector(ao.freq2index(sArgs.freqRange(1)):ao.freq2index(sArgs.freqRange(2)));
kVec = freqVec*2*pi/double(ita_constants('c'));


sourceNmaxAlias = floor(2*pi/double(ita_constants('c'))*sArgs.freqRange(2)*max(sourceSampling.r));
receiverNmaxAlias = floor(2*pi/double(ita_constants('c'))*sArgs.freqRange(2)*max(receiverSampling.r));

simDI = sArgs.simDI;
simWNG = sArgs.simWNG;

yMIMOgroundTruth = zeros(sArgs.nRuns,1,numel(kVec));

85
% check if source and receiver sampling have a unique radius within a certain tolerance
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
if numel(unique(sourceSampling.r)) > 1
    uniRSort = sort(unique(sourceSampling.r));
    if uniRSort(1) > uniRSort(end)*(1-2*eps) && uniRSort(end) < uniRSort(1)*(1+2*eps)
        sourceSampling.r = repmat(mean(uniRSort),size(sourceSampling.r));
    end
end
receiverSamplingUniqueRad = ~(numel(unique(receiverSampling.r)) > 1);

if numel(unique(receiverSampling.r)) > 1
    uniRSort = sort(unique(receiverSampling.r));
    if uniRSort(1) > uniRSort(end)*(1-2*eps) && uniRSort(end) < uniRSort(1)*(1+2*eps)
        receiverSampling.r = repmat(mean(uniRSort),size(receiverSampling.r));
    end
end

101
% include measured source directivity
102
dirMeasurementFile = sArgs.dirMeasurementFile;
103 104

% initialize constant variables
105 106 107 108 109 110 111 112
receiverYalias = ita_sph_base(receiverSampling,receiverNmaxAlias);
sourceYalias = ita_sph_base(sourceSampling,sourceNmaxAlias);
sourceGalias = ita_sph_aperture_function_sla(sourceSampling,sourceNmaxAlias,sourcerMem);

sourceG = sourceGalias(:,1:(sourceNmax+1)^2);
sourceY = sourceYalias(:,1:(sourceNmax+1)^2);
receiverY = receiverYalias(:,1:(receiverNmax+1)^2);

113
% declare non constant variables
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
eMismatchReceiver = zeros(sArgs.nRuns,1,numel(freqVec));
eAliasReceiver = zeros(sArgs.nRuns,1,numel(freqVec));
eMismatchSource = zeros(sArgs.nRuns,1,numel(freqVec));
eAliasSource = zeros(sArgs.nRuns,1,numel(freqVec));
elapsedTime = zeros(sArgs.nRuns,1);
receiverWNG = zeros(sArgs.nRuns,1,numel(freqVec));
receiverDI = zeros(sArgs.nRuns,1,numel(freqVec));
sourceWNG = zeros(sArgs.nRuns,1,numel(freqVec));
sourceDI = zeros(sArgs.nRuns,1,numel(freqVec));

% order dependent tikhonov regularization parameter
% for regulVec = zeros(...) this simplifies to the standard tikhonov
% regularization
regulPower = sArgs.regulPower;
orderVec = ita_sph_eye(receiverNmax,'n-nm').' * (0:receiverNmax).';
regulMatReceiver = diag(1 + orderVec.*(orderVec + 1).^regulPower);
orderVec = ita_sph_eye(sourceNmax,'n-nm').' * (0:sourceNmax).';
regulMatSource = diag(1 + orderVec.*(orderVec + 1).^regulPower);
regulParam = sArgs.regulParam;

%%
for idxRun = 1:sArgs.nRuns
    timerRuns = tic;
    % positionig parameters
    distSourceReceiver = 4;
    posSource = itaCoordinates([1 0 0]);
    posReceiver = ceil(rand(1,3)*10);
    posReceiver = posSource.cart+(posSource.cart-posReceiver)/norm(posSource.cart-posReceiver)*distSourceReceiver;
    posReceiver = itaCoordinates(posReceiver);
    [receiverLookDir,sourceLookDir,distSourceReceiver] = array_orientation(posReceiver,posSource);
    
    equalizationRadSource = 2;
    
    % room transfer function (free field assumption at this point)
Marco Berzborn's avatar
Marco Berzborn committed
148
    Psi = sph_transfer_path(posReceiver,receiverNmaxAlias,posSource,sourceNmaxAlias,kVec,...
149
        'r',distSourceReceiver,'r_eq',equalizationRadSource,'norm',false);
Marco Berzborn's avatar
Marco Berzborn committed
150 151 152 153 154

    PsiAliasReceiver = Psi(1:(receiverNmaxAlias+1)^2,1:(sourceNmax+1)^2,:);
    PsiAliasSource = Psi(1:(receiverNmax+1)^2,1:(sourceNmaxAlias+1)^2,:);
    Psi = Psi(1:(receiverNmax+1)^2,1:(sourceNmax+1)^2,:);
    
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    
    if ~isempty(samplingDisplacement) && isempty(SNR)
        % rest happens inside loop, only here in case of an error
        % inititialization needed here since parfor otherwise throws a
        % runtime error
        irReceiver = zeros(1,numel(freqVec));
        irSource = zeros(1,numel(freqVec));
    elseif ~isempty(SNR) && isempty(samplingDisplacement)
        noiseReceiver = ones(receiverSampling.nPoints,numel(freqVec));
        irReceiver = add_awgn(noiseReceiver,SNR,'fftDegree',sArgs.fftDegree,...
            'samplingRate',sArgs.samplingRate,'freqRange',sArgs.freqRange);
        
        noiseSource = ones(sourceSampling.nPoints,numel(freqVec));
        irSource = add_awgn(noiseSource,SNR,'fftDegree',sArgs.fftDegree,...
            'samplingRate',sArgs.samplingRate,'freqRange',sArgs.freqRange);
    else
        disp('MIMO_ERRORS:: I cannot simulate both sampling displacement and transducer noise!')
        varargout = cell(1,nargout);
        return;
    end

    % random beam pattern weigths
    receiverRandPattern = rand((receiverNmax+1)^2,1) + 1i*rand((receiverNmax+1)^2,1);
    sourceRandPattern = rand((sourceNmax+1)^2,1) + 1i*rand((sourceNmax+1)^2,1);
    
    parfor idxFreq=1:numel(freqVec)
        sourceBalias = [];
        dirMat = [];
        EreceiverMismatch = [];
        EsourceMismatch = [];
        
        if simSLA
            if isempty(dirMeasurementFile)
                sourceBalias = ita_sph_modal_strength(sourceSampling,sourceNmaxAlias,kVec(idxFreq),'rigid','transducer','ls');
                sourceB = sourceBalias(1:(sourceNmax+1)^2,1:(sourceNmax+1)^2);
Marco Berzborn's avatar
Marco Berzborn committed
190
                if numel(unique(sourceSampling.r)) == 1
191 192 193 194 195
                    Msource = sourceB * (sourceG.'.*sourceY');
                else
                    Msource = sourceB .* (sourceG.'.*sourceY');
                end
            else
196
				% include measured source directivity
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
                dirMat = h5read(dirMeasurementFile,'/dir/fullRe',[1,1,idxFreq],[sourceSampling.nPoints,(sourceNmaxAlias+1)^2,1]) +...
                    1i* h5read(dirMeasurementFile,'/dir/fullIm',[1,1,idxFreq],[sourceSampling.nPoints,(sourceNmaxAlias+1)^2,1]);
                Msource = dirMat(:,1:(sourceNmax+1)^2).';
            end
        else
            Msource = [];
            sourceBalias = [];
        end
        if simSMA
            receiverBalias = ita_sph_modal_strength(receiverSampling,receiverNmaxAlias,kVec(idxFreq),'rigid');
            if receiverSamplingUniqueRad
                receiverB = receiverBalias(1:(receiverNmax+1)^2,1:(receiverNmax+1)^2);
                Mreceiver = receiverY*receiverB;
            else
                receiverB = receiverBalias(:,1:(receiverNmax+1)^2);
                Mreceiver = receiverB.*receiverY;
            end
        else
            Mreceiver = [];
            receiverBalias = [];
        end
        
              
        % apply regularization to the equalization problem
        if ~isempty(regulParam)
            if simSMA
                MreceiverInv = pinv(Mreceiver'*Mreceiver+regulParam^2*regulMatReceiver)*Mreceiver';
            else
                MreceiverInv = [];
            end
            if simSLA
                MsourceInv = Msource'*pinv(Msource*Msource'+regulParam^2*regulMatSource);
            else
                MsourceInv = [];
            end
        % if no regulization parameter is given use the Moore Penrose
        % inverse
        else
            if simSMA
                MreceiverInv = pinv(Mreceiver);
            else
                MreceiverInv = [];
            end
            if simSLA
                MsourceInv = pinv(Msource);
            else
                MsourceInv = [];
            end
        end
        
        if simSMA
Marco Berzborn's avatar
Marco Berzborn committed
248
            if numel(unique(receiverSampling.r)) == 1
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
                EreceiverAlias = receiverYalias*receiverBalias;
            else
                EreceiverAlias = receiverYalias.*receiverBalias;
            end
        else
            EreceiverAlias = [];
        end
        if simSLA
            if isempty(dirMeasurementFile)
                EsourceAlias = sourceBalias*(sourceGalias.'.*sourceYalias');
            else
                EsourceAlias = dirMat.';
            end
        else
            EsourceAlias = [];
        end
        
266
        % use random weighting coefficients for simulation
267 268 269 270 271 272 273
        sourceLambda = sourceRandPattern / norm(sourceRandPattern) .* ita_sph_base(sourceLookDir,sourceNmax)' * 4*pi/(sourceNmax+1)^2;
        receiverLambda = receiverRandPattern / norm(receiverRandPattern) .* ita_sph_base(receiverLookDir,receiverNmax)' * 4*pi/(receiverNmax+1)^2;
        
        
        if ~isempty(samplingDisplacement) && isempty(SNR)
            
            if simSMA
274
                receiverSamplingErroneous = ita_sph_sampling_displacement(receiverSampling,samplingDisplacement,'absolute',displacementType);
275 276
                receiverYmismatch = ita_sph_base(receiverSamplingErroneous,receiverNmax);
                EreceiverMismatch = ita_sph_modal_strength(receiverSamplingErroneous,receiverNmax,kVec(idxFreq),'rigid');
277 278 279 280 281
                if receiverSamplingUniqueRad
                    EreceiverMismatch = receiverYmismatch*EreceiverMismatch - Mreceiver;
                else
                    EreceiverMismatch = receiverYmismatch.*EreceiverMismatch - Mreceiver;
                end
282 283 284 285
            else
                EreceiverMismatch = [];
            end
            if simSLA
286
                sourceSamplingErroneous = ita_sph_sampling_displacement(sourceSampling,samplingDisplacement,'absolute',displacementType);
287 288 289
                sourceYmismatch = ita_sph_base(sourceSamplingErroneous,sourceNmax);
                sourceGmismatch = ita_sph_aperture_function_sla(sourceSamplingErroneous,sourceNmax,sourcerMem,'r',unique(sourceSamplingR));
                EsourceMismatch = ita_sph_modal_strength(sourceSamplingErroneous,sourceNmax,kVec(idxFreq),'rigid','transducer','ls');
290 291 292 293 294
                if numel(unique(sourceSampling.r)) == 1
                    EsourceMismatch = (EsourceMismatch * (sourceGmismatch.'.*sourceYmismatch')) - Msource;
                else
                    EsourceMismatch = (EsourceMismatch.' .* (sourceGmismatch.'.*sourceYmismatch')) - Msource;
                end
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
            else
                EsourceMismatch = [];
            end
            
        elseif ~isempty(SNR) && isempty(samplingDisplacement)
            if simSMA
                EreceiverMismatch = (diag(irReceiver(:,idxFreq))-diag(ones(receiverSampling.nPoints,1)))*Mreceiver;
            else
                EreceiverMismatch = [];
            end
            if simSLA
                EsourceMismatch = Msource*(diag(irSource(:,idxFreq))-diag(ones(sourceSampling.nPoints,1)));
            else
                EsourceMismatch = [];
            end
        end

        if simSMA
            eMismatchReceiver(idxRun,:,idxFreq) = receiverLambda' * MreceiverInv * EreceiverMismatch * Psi(:,:,idxFreq) * sourceLambda;
            % aliasing only by setting n<N_sampling = 0
            eAliasReceiver(idxRun,:,idxFreq) = receiverLambda' * MreceiverInv * [zeros(receiverSampling.nPoints,(receiverNmax+1)^2),EreceiverAlias(:,(receiverNmax+1)^2+1:end)] ...
                * PsiAliasReceiver(:,:,idxFreq) * sourceLambda;
            if simDI
                receiverDI(idxRun,:,idxFreq) = 4*pi* sum(abs(receiverLambda'*MreceiverInv*Mreceiver*ita_sph_base(receiverLookDir,receiverNmax)').^2)/sum(abs(receiverLambda'*MreceiverInv*Mreceiver).^2);
            end
            if simWNG
                receiverWNG(idxRun,:,idxFreq) = 1 / norm(receiverLambda' * (MreceiverInv' * MreceiverInv) * receiverLambda);
            end
        end
        if simSLA
            eMismatchSource(idxRun,:,idxFreq) = receiverLambda' * Psi(:,:,idxFreq) * EsourceMismatch * MsourceInv * sourceLambda;
            % aliasing only by setting n<N_sampling = 0
            eAliasSource(idxRun,:,idxFreq) = receiverLambda' * PsiAliasSource(:,:,idxFreq) ...
                * [zeros((sourceNmax+1)^2,sourceSampling.nPoints);EsourceAlias((sourceNmax+1)^2+1:end,:)] * MsourceInv * sourceLambda;
            if simDI
                sourceDI(idxRun,:,idxFreq) = 4*pi* sum(abs(ita_sph_base(sourceLookDir,sourceNmax)*Msource*MsourceInv*sourceLambda).^2)/sum(abs(Msource*MsourceInv*sourceLambda).^2);
            end
            if simWNG
                sourceWNG(idxRun,:,idxFreq) = 1 / norm(sourceLambda' * (MsourceInv' * MsourceInv) * sourceLambda);
            end
        end
        
        %% groundthruth and errors
        yMIMOgroundTruth(idxRun,:,idxFreq) = receiverLambda'* Psi(:,:,idxFreq) * sourceLambda;

    end
    elapsedTime(idxRun) = toc(timerRuns);
    approxRemain = (sArgs.nRuns-idxRun)*sum(elapsedTime)/idxRun;
    disp(['MIMO_ERRORS:: finished run ', num2str(idxRun), ' of ', num2str(sArgs.nRuns),' after ',num2str(sum(elapsedTime)/60),' min - approx. remaining: ',num2str(approxRemain/60),' min']);
end

if simWNG
    receiverWNG = itaResult(permute(mean(receiverWNG(1,1,:),1),[3,1,2]),freqVec,'freq');
    sourceWNG = itaResult(permute(mean(sourceWNG(1,1,:),1),[3,1,2]),freqVec,'freq');
    wng = merge(receiverWNG,sourceWNG);
    wng.channelNames = {'Receiver','Source'};
else
    wng = [];
end

if simDI
    receiverDI = itaResult(permute(mean(receiverDI(1,1,:),1),[3,1,2]),freqVec,'freq');
    sourceDI = itaResult(permute(mean(sourceDI(1,1,:),1),[3,1,2]),freqVec,'freq');
    di = merge(receiverDI,sourceDI);
    di.channelNames = {'Receiver','Source'};
else
    di = [];
end

% init errorverctors
errorMismatchReceiver = zeros(sArgs.nRuns,numel(freqVec));
errorAliasReceiver = zeros(sArgs.nRuns,numel(freqVec));
errorMismatchSource = zeros(sArgs.nRuns,numel(freqVec));
errorAliasSource = zeros(sArgs.nRuns,numel(freqVec));


for idxRun = 1:sArgs.nRuns
    for idxFreq = 1:numel(freqVec)
        if simSMA
            errorMismatchReceiver(idxRun,idxFreq) = (norm(eMismatchReceiver(idxRun,:,idxFreq) ./ yMIMOgroundTruth(idxRun,:,idxFreq)));
            errorAliasReceiver(idxRun,idxFreq) = (norm(eAliasReceiver(idxRun,:,idxFreq) ./ yMIMOgroundTruth(idxRun,:,idxFreq)));
        end
        if simSLA
            errorMismatchSource(idxRun,idxFreq) = (norm(eMismatchSource(idxRun,:,idxFreq) ./ yMIMOgroundTruth(idxRun,:,idxFreq)));
            errorAliasSource(idxRun,idxFreq) = (norm(eAliasSource(idxRun,:,idxFreq) ./ yMIMOgroundTruth(idxRun,:,idxFreq)));
        end
    end
end

errorMismatchReceiver = itaResult(mean(errorMismatchReceiver).',freqVec,'freq');
errorMismatchReceiver.channelNames = {'mismatch receiver'};
errorAliasReceiver = itaResult(mean(errorAliasReceiver).',freqVec,'freq');
errorAliasReceiver.channelNames = {'alias receiver'};

errorMismatchSource = itaResult(mean(errorMismatchSource).',freqVec,'freq');
errorMismatchSource.channelNames = {'mismatch source'};
errorAliasSource = itaResult(mean(errorAliasSource).',freqVec,'freq');
errorAliasSource.channelNames = {'alias source'};

errorAliasSourceAliasReceiver = errorAliasSource * errorAliasReceiver;
errorAliasSourceAliasReceiver.channelNames = {'alias receiver, alias source'};
errorAliasSourceMismatchReceiver = errorAliasSource * errorMismatchReceiver;
errorAliasSourceMismatchReceiver.channelNames = {'mismatch receiver, alias source'};
errorMismatchSourceAliasReceiver = errorMismatchSource * errorAliasReceiver;
errorMismatchSourceAliasReceiver.channelNames = {'alias receiver, mismatch source'};
errorMismatchSourceMismatchReceiver = errorMismatchSource * errorMismatchReceiver;
errorMismatchSourceMismatchReceiver.channelNames = {'mismatch receiver, mismatch source'};

cmp = ita_merge(errorMismatchReceiver,errorMismatchSource,errorAliasReceiver,errorAliasSource);
cmpAll = ita_merge(errorMismatchReceiver,errorMismatchSource,errorAliasReceiver,errorAliasSource,errorAliasSourceAliasReceiver,errorAliasSourceMismatchReceiver,errorMismatchSourceAliasReceiver,errorMismatchSourceMismatchReceiver);

errorSum = sum(cmpAll);

varargout{1} = cmp;
varargout{2} = cmpAll;
varargout{3} = errorSum;
varargout{4} = wng;
varargout{5} = di;
end


function Psi = sph_transfer_path(varargin)
% calculates the transfer path from a source to a receiver in the spherical
% harmonic domain
%
% [Psi] = nSH_R x nSH_S x nBins

sArgs = struct('pos1_receiverCoords','itaCoordinates',...
               'pos2_receiverNmax','integer',...
               'pos3_sourceCoords','itaCoordinates',...
               'pos4_sourceNmax','integer',...
               'pos5_k','double',...
               'r',[],...
               'r_eq',1,...
               'norm',false,...
               'shType','complex');
[receiverCoords,receiverNmax,sourceCoords,sourceNmax,k,sArgs] = ita_parse_arguments(sArgs,varargin);

[receiverLookDirection, sourceLookDirection,r] = array_orientation(receiverCoords,sourceCoords);
yReceiver = ita_sph_base(receiverLookDirection,receiverNmax,'Williams',strcmp(sArgs.shType,'complex'))';
ySource = ita_sph_base(sourceLookDirection,sourceNmax,'Williams',strcmp(sArgs.shType,'complex'))';

% avoid sArgs in parfor since it is a broadcast variable
if ~isempty(sArgs.r)
    dist = double(sArgs.r);
else
    dist = r;
end

r_eq = sArgs.r_eq;

Psi = zeros((receiverNmax+1)^2,(sourceNmax+1)^2,numel(k));

if sArgs.norm
    % normalization according to the spherical harmonic addition theorem
    % normFactor = 1/norm(yReceiver*ySource');
    normFactor = 4*pi/((receiverNmax+1)*(sourceNmax+1));
else
    normFactor = 1;
end

parfor idxFreq = 1:numel(k)
    Psi(:,:,idxFreq) = yReceiver  * ySource' .* normFactor .* (exp(-1i*(k(idxFreq)*(dist-r_eq)))/dist*r_eq);
end

end


function varargout = array_orientation(receiverPos,sourcePos)
% calculate the orientation vectors for two arrays in a 3 dimensional
% domain

dist = norm(receiverPos.cart-sourcePos.cart);
orientationSource = (receiverPos.cart-sourcePos.cart) / dist;
orientationReceiver = (sourcePos.cart-receiverPos.cart) / dist;

varargout{1} = itaCoordinates(orientationReceiver,'cart');
varargout{2} = itaCoordinates(orientationSource,'cart');
varargout{3} = dist;


end


function data = add_awgn(varargin)
% add awgn to a frequency domain signal
% data may be 2 dimensional or 3 dimensional. 
% frequency axis along the last dimension

sArgs = struct('pos1_data','double',...
               'pos2_snr','double',...
               'samplingRate',ita_preferences('samplingRate'),...
               'fftDegree',ita_preferences('fftDegree'),...
               'freqRange',[20 ita_preferences('samplingRate')/2],...
               'ref',20);
[data,snr,sArgs] = ita_parse_arguments(sArgs,varargin);

switch numel(size(data))
    case 2
        % insert singleton dimension
        data = permute(data,[3,1,2]);
        dim = 2;
    case 3
        % everything already correct, nothing to be done here
        % data = permute(data,[2,1,3]);
        dim = 3;
    otherwise
        ita_verbose_info('Invalid dimension of data.',0);
        data = [];
        return;
end

% take the upper nyquist for the sweep generation because of the high pass
% shelving filter in the sweep function
sweep = ita_generate_sweep('stopMargin',0,'samplingRate',sArgs.samplingRate,'fftDegree',...
        sArgs.fftDegree,'freqRange',[sArgs.freqRange(1), sArgs.samplingRate/2],'bandwidth',0);
upperIdx = sweep.freq2index(sArgs.freqRange(2));
lowerIdx = sweep.freq2index(sArgs.freqRange(1));


% extract all needed data from the audio object and delete afterwards
sweepTime = permute(repmat(sweep.timeData,1,size(data,2)),[2,1]);
freqData = permute(repmat(sweep.freqData(lowerIdx:upperIdx),1,size(data,2)),[2,1]);
nSamples = sweep.nSamples;

% generate noisy ir and convolve with the input data
for idxDimOne=1:size(data,1)
    noise = randn(size(data,2),nSamples);
    noise = bsxfun(@rdivide,noise,rms(noise,2)) * 10^(-snr/sArgs.ref);
    noise = mbe_fft(permute(sweepTime + noise,[2,1]));
    irMicrophone = noise(lowerIdx:upperIdx,:).' ./ freqData;
    data(idxDimOne,:,:) = data(idxDimOne,:,:) .* permute(irMicrophone,[3,1,2]);
end

% if data was 2 dimensional restore the matrix format by removing the
% singleton dimension
if dim == 2
    data = permute(data,[2,3,1]);
end

end


function data =  mbe_fft(data,varargin)
% now multidimensional, fft will be calculated along the 1st dimension

% dimension one are the samples
nSamples = size(data,1);

if mod(nSamples,2) == 0
    isEven = true;
else
    isEven = false;
end

if nargin == 2
    signalType = varargin{1};
else
    signalType = 'power';
end

data = fft(data);

if isEven
    data = data(1:(nSamples+2)/2,:,:);
else
Marco Berzborn's avatar
Marco Berzborn committed
561
    disp('MBE_FFT:: Be careful with odd numbers of time samples!');
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    data = data(1:(nSamples+1)/2,:,:);
end

% is power signal divide by number of samples
if strcmp(signalType,'power')
    data = data/nSamples;
    % divide by sqrt(2) to get the effective values
    if isEven
        data(end,:,:) = data(end,:,:)./sqrt(2);
        data(2:end-1,:,:) = data(2:end-1,:,:).*sqrt(2);
    else
        data(2:end,:,:) = data(2:end,:,:).*sqrt(2);
    end
end

Marco Berzborn's avatar
Marco Berzborn committed
577
end