Aufgrund eines Versionsupdates wird GitLab am 01.04. zwischen 9:00 und 9:30 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to a version upgrade, GitLab won't be accessible at 01.04. between 9:00 and 9:30 a.m.

ita_roomacoustics_reverberation_time_lundeby.m 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 148 149 150 151 152 153 154 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 190 191 192 193 194 195 196 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
function varargout = ita_roomacoustics_reverberation_time_lundeby(varargin)
%ITA_ROOMACOUSTICS_LUNDEBY - reverberation time with lundeby algorithm
%  This function uses the algorithm after Lundeby et al. [1] to calculate 
%  the (late) reverberation time, intersection time and noise level estimation.
%  This function should not be called directly. Use ita_roomacoustics() to
%  get Lundeby parameters:
%
% 
%  Use ita_roomacoustics() to get parameter:
%  
%   raFilter    = ita_roomacoustics(RIR, 'T_Lundeby', 'PSNR_Lundeby', 'Intersection_Time_Lundeby' )
%   raBroadBand = ita_roomacoustics(RIR, 'T_Lundeby', 'PSNR_Lundeby', 'Intersection_Time_Lundeby' , 'broadbandAnalysis')
% 
%  Example:
%    [RT_lundeby PSNR Intersection_Time_Lundeby NoiseLundeby PNR] = ita_roomacoustics_reverberation_time_lundeby(RIR)
%
%   'shortRevTimeMode' (false): shorter time average intervals to detect very short reverberation times (or intersections)
%  See also:
%   ita_roomacoustics
%
% References
%  [1] Lundeby, Virgran, Bietz and Vorlaender - Uncertainties of Measurements in Room Acoustics - ACUSTICA Vol. 81 (1995)
% 
%   Reference page in Help browser
%        <a href="matlab:doc ita_roomacoustics_lundeby">doc ita_roomacoustics_lundeby</a>

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

% Author: Martin Guski -- Email: mgu@akustik.rwth-aachen.de
% Created:  06-Jul-2011


%% Initialization and Input Parsing
sArgs         = struct('pos1_data','itaAudio',  'bandsPerOctave', ita_preferences('bandsperoctave'), 'freqRange', ita_preferences('freqRange'), 'plot', false, 'shortRevTimeMode',false, 'broadbandAnalysis', false, 'inputIsSquared', false);
[input,sArgs] = ita_parse_arguments(sArgs,varargin);

%%

[input, timeShifted] = ita_time_shift(input, '20dB');

% cut samples from cyclic shift to avoid error in noise estimation
nSamples2cut   = -timeShifted*input.samplingRate + rem(-timeShifted*input.samplingRate,2); % always cut even number

% input        = ita_roomacoustics_shiftIR(input, 'threshold', 20);
% nSamples2cut = zeros(input.nChannels,1);

%%
if sArgs.broadbandAnalysis
    freqVec         = ones(input.nChannels,1);
    freqDepWinTime = ones(input.nChannels,1) * 0.03; % broadband: use 30 ms windows sizes

else
    freqVec         = ita_ANSI_center_frequencies(sArgs.freqRange , sArgs.bandsPerOctave, input.samplingRate);

    if numel(freqVec) ~= input.nChannels
        ita_verbose_info('Frequency vector does not fit to input channles. Frequency dependet smoothing will be wrong! ',0)
    end
    
    % % freqDepWinTime = min(max((20e3 - freqVec*6) /500+10, 10), 50) / 1000;
    % % freqDepWinTime = min(max( 1./freqVec*800+20, 10), 50) / 1000 * 1.5; % old: works well
    % freqDepWinTime = min( 5./freqVec, 50); % 2 periods per wavelength, maximum 50 ms
    freqDepWinTime =  (800 ./freqVec+10 ) / 1000;
end

if sArgs.shortRevTimeMode
    freqDepWinTime = freqDepWinTime / 5;
end

% semilogx(freqVec, freqDepWinTime*1000, 'o-'); grid on
%%
if sArgs.inputIsSquared
        rawTimeData         = max(input.timeData,0);
else
    rawTimeData         = input.timeData.^2;
end

[nSamples, nChannels]= size(rawTimeData);
nSamples            = nSamples - nSamples2cut;
[ revT, noiseLevel, intersectionTime, noisePeakLevel]   = deal(nan(nChannels,1));

%%

% semilogx(freqVec, 1000*[ (800 ./freqVec+10 ) / 1000;  min( 5./freqVec, 50);  min(max( 1./freqVec*800+20, 10), 50)/1000], 'o-'); 
% grid on
% legend({'sota' 'old' 'new'})

%%

nPartsPer10dB            =  5;   % time intervals per 10 dB decay. lundeby: 3 ... 10
dbAboveNoise             = 10;   % end of regression 5 ... 10 dB
useDynRangeForRegression = 20 ;  % 10 ... 20 dB

% % % dbAboveNoise             = 0;   % end of regression 5 ... 10 dB
% % % useDynRangeForRegression = 10 ;  % 10 ... 20 dB


% plot parameters
if sArgs.plot
    figure;
    subplotSize = [ceil(sqrt(nChannels)) round(sqrt(nChannels)) ];
end

for iChannel = 1:nChannels
    
    % 1) smooth
    nSamplesPerBlock = round(freqDepWinTime(iChannel)* input.samplingRate);
    timeWinData      = squeeze(sum(reshape(rawTimeData(1:floor(nSamples(iChannel)/nSamplesPerBlock)*nSamplesPerBlock,iChannel), nSamplesPerBlock,floor(nSamples(iChannel)/nSamplesPerBlock) ,1),1)).'/nSamplesPerBlock;
    timeVecWin       = (0:size(timeWinData,1)-1).'*nSamplesPerBlock/input.samplingRate;
    
    % 2) estimate noise
    noiseEst = mean(timeWinData(end-round(size(timeWinData,1)/10):end,:))+realmin;
    
    % 3) regression

    [del, startIdx] = max(timeWinData);
    stopIdx = find(10*log10(timeWinData(startIdx+1:end)) > 10*log10(noiseEst)+ dbAboveNoise, 1, 'last') + startIdx;
%     stopIdx = find(10*log10(timeWinData(startIdx+1:end)) < 10*log10(noiseEst)+ dbAboveNoise, 1, 'first') + startIdx;


    dynRange = diff(10*log10(timeWinData([startIdx stopIdx])));
    
    if isempty(stopIdx) || (stopIdx == startIdx) || dynRange > -5 
        ita_verbose_info('Regression did not work due to low SNR, continuing with next channel/band',1);
        continue;
    end
    
%     if (stopIdx-startIdx+1) <= 3 % less than 3 samples for regression (maybe problem of too large time intervals for short reverberation)
%         % if dynamic range is big engough => use shorter averiging intervals
%         if dynRange < -30   
%         end    
%     end
    
    
    X = [ones(stopIdx-startIdx+1,1) timeVecWin(startIdx:stopIdx)]; % X*c = edc
    c = X\(10*log10(timeWinData(startIdx:stopIdx)));
    %     T = -60/c(2);
    
    if c(2) == 0 || any(isnan(c))
        ita_verbose_info('Regression did not work due, T would be Inf, setting to 0, continuing with next channel/band',1);
        continue;
    end
    
    % 4) preliminary crossing point
    crossingPoint = (10*log10(noiseEst) - c(1)) / c(2);
    if crossingPoint > (input.trackLength + timeShifted(iChannel))  * 2 
        continue
    end
    
    % 5) new local time interval length
    nBlocksInDecay   = diff(10*log10(timeWinData([startIdx stopIdx]))) / -10 * nPartsPer10dB;
    nSamplesPerBlock = round(diff(timeVecWin([startIdx stopIdx])) / nBlocksInDecay * input.samplingRate);
    
    % 6) average
    timeWinData = squeeze(sum(reshape(rawTimeData(1:floor(nSamples(iChannel)/nSamplesPerBlock)*nSamplesPerBlock,iChannel), nSamplesPerBlock,floor(nSamples(iChannel)/nSamplesPerBlock) ,1),1)).'/nSamplesPerBlock;
    timeVecWin = (0:size(timeWinData,1)-1).'*nSamplesPerBlock/input.samplingRate;
    [del, idxMax] = max(timeWinData);
    
    
    oldCrossingPoint = 11+crossingPoint; % high start value to enter while-loop
    loopCounter = 0;
    
    while(abs(oldCrossingPoint-crossingPoint) > 0.01)
        % 7) estimate backgroud level
        correspondingDecay = 10;  % 5...10 dB
        idxLast10percent        = round(size(timeWinData,1)*0.9);
        idx10dBBelowCrosspoint  = max(1,round( (crossingPoint - correspondingDecay ./ c(2)) * input.samplingRate / nSamplesPerBlock));
        noiseEst                = mean(timeWinData(min(idxLast10percent,idx10dBBelowCrosspoint ):end,:)) +realmin;
        
        % 8) estimate late decay slope
        startIdx = find(10*log10(timeWinData(idxMax:end)) < 10*log10(noiseEst)+ dbAboveNoise + useDynRangeForRegression, 1, 'first') + idxMax - 1;
        if isempty(startIdx)
            startIdx = 1;
        end
        stopIdx  = find(10*log10(timeWinData(startIdx+1:end)) < 10*log10(noiseEst)+ dbAboveNoise, 1, 'first')           + startIdx;
        if isempty(stopIdx)
            ita_verbose_info('Regression did not work due to low SNR, continuing with next channel/band',1);
            break;
        end
        X = [ones(stopIdx-startIdx+1,1) timeVecWin(startIdx:stopIdx)]; % X*c = edc
        c = X\(10*log10(timeWinData(startIdx:stopIdx)));
        
        if c(2) >= 0
            ita_verbose_info('Regression did not work due, T would be Inf, setting to 0, continuing with next channel/band',1);
            c(2) = Inf;
            break;
        end
        
        % 9) find croosspoint
        oldCrossingPoint = crossingPoint;
        crossingPoint = (10*log10(noiseEst) - c(1)) / c(2);
        
        
        % iteration faild
        % in the case of no noise tail his might be possible, or not?
% % %         if crossingPoint > input.trackLength + timeShifted(iChannel)
% % %             [c(2), crossingPoint, noiseEst] = deal(nan);
% % %             break
% % %         end
        

        %
        loopCounter = loopCounter +1;
        if loopCounter > 30
            ita_verbose_info('30 iterations => cancel',1);
            break;
        end
        
    end
    
% %    % estimate late rev time according ISO 3382 (without 10 db offset above noise)
% %    % 8) estimate late decay slope
% %         startIdx = find(10*log10(timeWinData(idxMax:end)) < 10*log10(noiseEst)+ 0 + 10, 1, 'first') + idxMax - 1;
% %         if isempty(startIdx)
% %             startIdx = 1;
% %         end
% %         stopIdx  = find(10*log10(timeWinData(startIdx+1:end)) < 10*log10(noiseEst)+ 0, 1, 'first')           + startIdx;
% %         if isempty(stopIdx)
% %             ita_verbose_info('Regression did not work due to low SNR, continuing with next channel/band',1);
% %             break;
% %         end
% %         X = [ones(stopIdx-startIdx+1,1) timeVecWin(startIdx:stopIdx)]; % X*c = edc
% %         linRegRes = X\(10*log10(timeWinData(startIdx:stopIdx)));
% %         revT(iChannel)             = -60/linRegRes(2);

    revT(iChannel)             = -60/c(2);
    noiseLevel(iChannel)       = 10*log10(noiseEst);
    intersectionTime(iChannel) = crossingPoint;
    
    noisePeakLevel(iChannel)   =  10*log10(max(timeWinData(min(idxLast10percent,idx10dBBelowCrosspoint ):end,:)));
    
    
     %     ita_verbose_info(['end after ' num2str(loopCounter) ' iterations'],1)
    % plot
    if sArgs.plot
        subplot(subplotSize(1), subplotSize(2), iChannel)
        plot(input.timeVector(1:nSamples(iChannel)), 10*log10(abs(rawTimeData(1:nSamples(iChannel),iChannel))), 'color', [.7 .7 1])                          % rawdata
        title(sprintf(' %2.0fHz (SNR %2.1fdB, T: %1.2f s)', freqVec(iChannel), 10*log10(max(abs(rawTimeData(:,iChannel)))/noiseEst), revT(iChannel) ))
        hold all
        plot([0 (10*log10(noiseEst)-40-c(1)) / c(2) ], [c(1) 10*log10(noiseEst)-40],'linewidth', 3 );               % EDC line
        plot(timeVecWin, 10*log10(timeWinData ),'o-', 'linewidth', 2)                                                    % smoothed data
        scatter(timeVecWin([startIdx stopIdx]), 10*log10(timeWinData([startIdx stopIdx])) , 'filled')               % limits for regression
        %         axLimit = [0 input.trackLength 10*log10(noiseEst)-30 max(10*log10(abs(rawTimeData(:,iChannel))))+20 ];%
%         axLimit = [0 crossingPoint *1.2 10*log10(noiseEst)-30 max(10*log10(abs(rawTimeData(:,iChannel))))+20 ];%
        axLimit = [0 max(crossingPoint *1.2,input.timeVector(nSamples(iChannel))) 10*log10(noiseEst)-30 max(10*log10(abs(rawTimeData(:,iChannel))))+20 ];%
        plot(axLimit(1:2), [1 1]*10*log10(noiseEst), 'linewidth', 2)                                                % noise line
        plot([1 1]*crossingPoint, axLimit(3:4), 'linewidth', 2)                                                     % intersection time
        
        if all(isnan(axLimit))
            axis(axLimit)
        end
        hold off
%         ita_legend({'IR' 'reverberation slope' 'envelope','late rev eval interval', 'noise level estimation', 'intersection time'})
    end
end


resultDummy             = itaResult(freqVec(:), freqVec, 'freq');
resultDummy.comment     = input.comment;
resultDummy.channelNames = input.channelNames;
resultDummy.channelCoordinates = input.channelCoordinates;

result                  = resultDummy;
result.allowDBPlot      = false;
result.freqData         = revT ;
result.channelUnits(:)  = {'s'};
result.comment          = [input.comment ' -> T (Lundeby)'];


%% Set Output
varargout(1) = {result};

if nargout >= 2
    psnr = resultDummy;
    psnr.freqData  = sqrt(max(rawTimeData).') ./ 10.^(noiseLevel/20) ;
    psnr.comment          = [input.comment ' -> Peak SNR (Lundeby)'];
    psnr.channelUnits(:) = {''};
    varargout(2) = {psnr};
end
if nargout >= 3
    
    iTime = resultDummy;
    iTime.freqData  = intersectionTime -  timeShifted(:);
    iTime.channelUnits(:)  = {'s'};
    iTime.comment          = [input.comment ' -> Intersection Time (Lundeby)'];
    
    varargout(3) = {iTime};
end

if nargout >= 4
    noise = resultDummy;
    noise.freqData   = 10.^(noiseLevel/20) ;
    noise.comment          = [input.comment ' -> NoiseEstimation (Lundeby)'];
    noise.channelUnits(:)  = {'Pa'};
    varargout(4) = {noise};
end



if nargout >= 5
    pspnr = resultDummy;
    pspnr.freqData   = sqrt(max(rawTimeData).') ./ 10.^(noisePeakLevel /20) ;
    pspnr.comment          = [input.comment ' -> Peak Signal to Peak Noise Ratio(Lundeby)'];
    pspnr.channelUnits(:)  = {''};
    varargout(5) = {pspnr};
end


%end function
end