% Compute coefficients to apply to 2 sounds (vectors of samples) in order to merge them
%
% desiredSNRdB and currentSNRdB are in dB
% max is the maximum absolute value a sample can take
% max1 is the actual maximum absolute value of samples of sound vector 1
% max2 is the actual maximum absolute value of samples of sound vector 2
%
% The merging has to be done this way: alpha1 * soundvector1 + alpha2 * soundvector2
%
% by Guillaume LATHOUD at IDIAP (lathoud@idiap.ch)
function [alpha1, alpha2] = changeSNR(desiredSNRdB, currentSNRdB, max, max1, max2)
currentSNR = 10 ^ (currentSNRdB / 10);
desiredSNR = 10 ^ (desiredSNRdB / 10);
ratio = sqrt(desiredSNR/currentSNR);
% Below .5 makes sure when merging we won't exceed max
alpha2 = .5 * min(max / max1 / ratio, max / max2);
alpha1 = ratio * alpha2;