itaHpTF_Audio.m 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classdef itaHpTF_Audio < itaHpTF
    %ITAHPTF_AUDIO - Calculation of the HpTF from measured TF and Mic.
    %Reference Measurements
    %
    %
    % Derived from itaHpTF. See itaHpTF_Meas for measurement
    %
    %
    % itaHpTF_Audio Properties:
    %         TF                (measured transfer function)
    %         fLower            (lower frequency for extrapolation)
    %         fUpper            (upper frequency for regularization)
    %         method            (mSTD, max, mean: see Masiero, Bruno; Fels, Janina: "Perceptually Robust Headphone Equalization for Binaural Reproduction"
    %                                             Audio Engineering Society Convention 130, May 2011)
    %         normalized        (Normalization of HpTF)
    %         smoothing         (Smoothing bandwidth has to be defined in fractions of octaves, see also ita_smooth)
17
    %         mic               (measured microphone transfer funcions - not inverted: length does not matter; will be adapted)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    %
    % itaHpTF_Audio Methods (private):
    %         HP_equalization   If this.mic is not set, it will be unused 
    
    % <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>

    
    properties(Access = private)
        mTF          = itaAudio;
        m_fLower     = 100;
        m_fUpper     = 18000;
        mMethod      = 'mSTD';
        mNormalized  = true;
34
        mGainComp    = true;
35
        mSmoothing   = 1/6;
36
        mSelectMeas    = 1:8; % select the channels which should be used for the HpTF
37 38 39 40 41 42 43
    end
    
    properties(Dependent = true, Hidden = false)
        TF          = itaAudio;
        fLower      = 100;
        fUpper      = 18000;
        method      = 'mSTD';
44 45
        normalized  = true; % normalization of the signal
        gainComp    = true; % microphone compensation
46
        smoothing   = 1/6;
47
        selectMeas   = 1:8; % select specific measurements
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    end
    
    properties (Dependent = true, SetAccess = private)
       init 
    end
    
    properties (Dependent = true, SetAccess = private)
    end
    
    methods
        function this = itaHpTF_Audio(varargin)
            
            this = this@itaHpTF();
            
            if nargin == 1
                % init
                if isa(varargin{1},'itaHpTF_Audio')
                    this = varargin;
                elseif isa(varargin{1},'itaHpTF_MS')
                    this.init = varargin{1};
68 69 70 71 72 73 74 75 76 77 78 79 80 81
                    
                 elseif nargin ==1 && isstruct(varargin{1}) % only for loading
                    obj = varargin{1};
                    this.data = obj.data;
                    
                    this.signalType = 'energy';
                    % additional itaHRTF data
                    objFNsaved = this.propertiesSaved;
                    objFNload = this.propertiesLoad;
                             
                    for i1 = 1:numel(objFNload)
                        this.(objFNload{i1}) = obj.(objFNsaved{i1});
                    end

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
                end
            end
        end
        
        function display(this)
            this.displayLineStart
            this.disp
        end
        
        function disp(this)
            stringM = this.method;
            classnamestring = ['^--|' mfilename('class') '|'];
            
            disp@itaHpTF(this)
            
            fullline = repmat(' ',1,this.LINE_LENGTH);
            stringS = ['      Method       ' stringM ];
            fullline(1:numel(stringS)) = stringS; 
            startvalue = length(classnamestring);
            fullline(length(fullline)-startvalue+1:end) = classnamestring;
            disp(fullline);
            
            dir = num2str(this.repeat,5);
            stringD = [dir ' repetition(s) '];
            
            middleLine = this.LINE_MIDDLE;
            middleLine(3:(2+length(stringD))) = stringD;
            fprintf([middleLine '\n']);
        end
        
        %% ................................................................
        % GET
        %..................................................................       
        function fLower= get.fLower(this)
            fLower = this.m_fLower; end
        
        function fUpper = get.fUpper(this)
            fUpper = this.m_fUpper; end
        
        function method = get.method(this)
            method = this.mMethod; end
        
        function TF = get.TF(this)
            TF = this.mTF; end
        
        function normalized = get.normalized(this)
            normalized = this.mNormalized; end
        
130 131 132 133 134 135 136
        function comp = get.gainComp(this)
            comp = this.mGainComp; end
        
        function sel = get.selectMeas(this)
            sel = this.mSelectMeas; end
        
        
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
        function smoothing = get.smoothing(this)
            smoothing = this.mSmoothing; end
        %% ................................................................
        % SET
        %.................................................................. 
        function this = set.fLower(this,fLower)
            this.m_fLower = fLower;
            this = HP_equalization(this);
        end
        
        function this = set.fUpper(this,fUpper)
            this.m_fUpper = fUpper;
            this = HP_equalization(this);
        end
        
        function this = set.method(this,method)      
                if sum(strcmpi(method,this.propertiesMethod))==1
                    this.mMethod = method;
                    this = HP_equalization(this);
                else
                    error('itaHpTF:Def', ' Valid methods: mSTD, max, mean.')
                end
        end
        
        function this = set.normalized(this,normalized)
            this.mNormalized = normalized;
            this = HP_equalization(this);
        end
        
166 167 168 169 170 171 172 173 174 175
        function this = set.selectMeas(this,ch)
            this.mSelectMeas = ch;
            this = HP_equalization(this);
        end
        
        function this = set.gainComp(this,comp)
            this.mGainComp = comp;
            this = HP_equalization(this);
        end
        
176 177 178 179 180 181 182 183 184 185 186
        function this = set.smoothing(this,smoothing)
            this.mSmoothing = smoothing;
            this = HP_equalization(this);
        end
        
        function this = set.TF(this,TF)
            this.mTF = TF; end
        %% ................................................................
        % SET PRIVATE
        %..................................................................
        function this = set.init(this,var)
187
             this.nameHP      = var.nameHP;
188 189 190 191
            this.nameMic     = var.nameMic;
            this.nameSubj    = var.nameSubj;
            this.repeat      = var.repeat;
            this.mTF.time    = var.time;
192
            this.selectMeas  = 1:var.repeat;
193 194 195 196 197 198 199 200
            this = HP_equalization(this);
        end
        
        function thisEQ = HP_equalization(this) 
            % Masiero, Bruno; Fels, Janina: "Perceptually Robust Headphone Equalization for Binaural Reproduction"
            %     Audio Engineering Society Convention 130, May 2011
            
            tWin    = this.TF.trackLength; % crop HPTF
201 202
            measSel = sort([2*this.selectMeas-1 2*this.selectMeas]);
            TF = this.TF.ch(measSel);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            
            %init
            thisEQ  = this;
            Rec     = itaAudio(zeros(TF.nSamples,1), TF.samplingRate, 'time');
            Rec.signalType = 'energy';
            mRec    = Rec;
            HpTF    = itaAudio(zeros(this.TF.nSamples,2), this.TF.samplingRate, 'time');
            HpTF.signalType = 'energy';
            
            for cdx = 1:2 % L/R ...
                switch this.method
                    case 'mean'
                        Rec.freqData = mean(abs(TF.freqData(:,cdx:2:end)),2);
                    case 'max'
                        Rec.freqData = max(abs(TF.freqData(:,cdx:2:end)),[],2);
                    case 'mSTD'
                        Rec.freqData = mean(abs(TF.freqData(:,cdx:2:end)),2) + 2*std(abs(TF.freqData(:,cdx:2:end)),0,2);
                    otherwise
                        error('Unknown type');
                end
223 224 225 226 227 228 229
                
                if this.mic.dimensions == 2 % Compensation of mic
                    minMic  = ita_minimumphase(this.mic);
                    RecM    = ita_convolve(Rec,minMic.ch(cdx));
                    RecM.fftDegree = TF.fftDegree;
                else
                    RecM = Rec;
230
                end
231 232
                %% Short Filter with no correction for low freqs and minimum phase
                aux = max(abs(RecM.freqData),[],2);
233 234
                
                % find first maximum and truncate low freq correction at this point
235
                idxDF   = RecM.freq2index([this.fLower  this.fLower*1.5 ]);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                d_aux   = diff(aux(idxDF(1):idxDF(2)));
                idx     = find(diff(sign(d_aux)) ~= 0,1,'first'); % Bruno style...
                aux(1:idxDF(1)+idx+1) = aux(idxDF(1)+idx+2);
                aux(aux==0) = rand(1)*eps; % für die Regularisierung ~=0
                mRec.freqData = aux;
                
                % smoothing
                mRec_s = ita_smooth(mRec,'LogFreqOctave1',this.smoothing,'Abs');
                HpTF_tmp  = ita_invert_spk_regularization(mRec_s ,[0 this.fUpper]);
                
                HpTF.freqData(:,cdx) = HpTF_tmp.freqData;
            end
            
            % or guaranty that the average level of the signal is not altered
            % this also means that the overall loudness of the signal will not be
            % considerably altered.
            this_min   = ita_minimumphase(ita_time_shift(HpTF,tWin/2));
            this_win   = ita_time_window(this_min,[tWin*0.99,tWin],'time','crop');
           
255 256 257 258 259 260 261 262 263 264
            if this.gainComp % compensate gains from mics (equalize)
                idxDF   = this_win.freq2index(this.fLower);
                chGain  = 20*log10(abs(this_win.freqData(idxDF,:)));
                this_comp = this_win;
                this_comp.freqData = bsxfun(@times,this_win.freqData,10.^(-chGain/20));
            else, this_comp = this_win;
            end
            
            if this.normalized
                this_norm = ita_normalize_dat(this_comp);
265
                thisEQ.timeData = this_norm.timeData;
266
            else, thisEQ.timeData = this_comp.timeData;
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
            end
            
            if ~isempty(this.savePath)
                HpTFaudio = itaAudio(thisEQ.timeData, TF.samplingRate, 'time');
                ita_write(HpTFaudio,[this.savePath,'\HpTF_HP_' this.nameHP '_Subj_' ...
                    this.nameSubj '.ita']);
                ita_write(thisEQ,[this.savePath,'\HP_' this.nameHP '_Subj_' ...
                    this.nameSubj '.ita']);
            end
        end
        
    end
    
    methods(Hidden = true)
    end
    
    methods(Hidden = true)
        function sObj = saveobj(this)
            %             % Called whenever an object is saved
            %             % have to get save objects for both base classes
            
            sObj = saveobj@itaHpTF(this);
            
            % Copy all properties that were defined to be saved
            propertylist = itaHpTF_Audio.propertiesSaved;
            for idx = 1:numel(propertylist)
                sObj.(propertylist{idx}) = this.(propertylist{idx});
            end
        end
    end
    
    methods(Static)
        function this = loadobj(sObj)
            this = itaHpTF_Audio(sObj);
        end
        
        function result = propertiesSaved
304 305
            result = {'nameHP','nameMic','nameSubj','repeat','mic','savePath',...
                'TF','fLower','fUpper','method','normalized','smoothing'};
306 307 308
        end
        
        function result = propertiesLoad
309 310
            result = {'nameHP','nameMic','nameSubj','repeat','mic','savePath',...
                'mTF','m_fUpper','m_fLower','mMethod','mNormalized','mSmoothing'};
311 312 313 314 315 316 317 318 319
        end
        
        function result = propertiesMethod
            result = {'mSTD', 'max','mean'};
        end
    end
end