mathieu@14: function scale = adjustScaleForEBR (fg, bg, offset, targetEBR, snrType, eps) mathieu@14: % Computes the value of 'scale' such that fg*scale has a given EBR mathieu@14: % relatively to bg at position 'offset'. mathieu@14: % ebrType is used to select what kind of EBR should be considered: mathieu@14: % 1 - standard temporal ebr over whole length of sample mathieu@14: % 2 - max of standard temporal ebr over 1/4s windows mathieu@14: % 3 - more to come ? mathieu@14: % eps is the epsilon used to check for convergence; optional, defaults to mathieu@14: % 0.01 mathieu@14: mathieu@14: % This program was written by Mathias Rossignol & Grégoire Lafay mathieu@14: % is Copyright (C) 2015 IRCAM mathieu@14: % mathieu@14: % This program is free software: you can redistribute it and/or modify it mathieu@14: % under the terms of the GNU General Public License as published by the Free mathieu@14: % Software Foundation, either version 3 of the License, or (at your option) mathieu@14: % any later version. mathieu@14: % mathieu@14: % This program is distributed in the hope that it will be useful, but mathieu@14: % WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY mathieu@14: % or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mathieu@14: % for more details. mathieu@14: % mathieu@14: % You should have received a copy of the GNU General Public License along mathieu@14: % with this program. If not, see . mathieu@14: mathieu@14: if (nargin == 5) eps = 0.01; end mathieu@14: %if (targetSNR <= 0) targetSNR = exp(targetSNR); end mathieu@14: prevScale = 1; mathieu@14: scale = 1; mathieu@14: ebrFunc=@ebr; mathieu@14: if (snrType==2) ebrFunc=@maxWindowEBR; end mathieu@14: count = 0; mathieu@14: while (ebrFunc(fg, bg, offset, scale) > targetEBR && count < 100) mathieu@14: prevScale = scale; mathieu@14: scale = scale/2; mathieu@14: count = count+1; mathieu@14: end mathieu@14: if (count == 100) mathieu@14: msg = ['Max count, stage 1, scale = ', num2str(scale), ' - ', num2str(ebrFunc(fg, bg, offset, scale)), ' - ', num2str(ebrFunc(fg, bg, offset, prevScale)), ' - ', num2str(ebrFunc(fg, bg, offset, 1))]; mathieu@14: error(msg) mathieu@14: end mathieu@14: count=0; mathieu@14: while (ebrFunc(fg, bg, offset, scale) < targetEBR && count < 100) mathieu@14: prevScale = scale; mathieu@14: scale = scale*2; mathieu@14: count = count+1; mathieu@14: end mathieu@14: if (count == 100) mathieu@14: msg = ['Max count, stage 2, scale = ', num2str(scale)]; mathieu@14: error(msg) mathieu@14: end mathieu@14: count=0; mathieu@14: if (scale eps && count < 100) mathieu@14: if (delta>0) mathieu@14: b = (a+b)/2; mathieu@14: else mathieu@14: a = (a+b)/2; mathieu@14: end mathieu@14: delta = ebrFunc(fg, bg, offset, (a+b)/2) - targetEBR; mathieu@14: count = count+1; mathieu@14: end mathieu@14: if (count == 100) mathieu@14: msg = ['Max count, stage 3, scale = ', num2str(a)]; mathieu@14: error(msg) mathieu@14: end mathieu@14: scale = (a+b)/2; mathieu@14: end