itaHpTF_Audio.m 9.92 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
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)
    %
    % 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;
        mSmoothing   = 1/6;
    end
    
    properties(Dependent = true, Hidden = false)
        TF          = itaAudio;
        fLower      = 100;
        fUpper      = 18000;
        method      = 'mSTD';
        normalized  = true;
        smoothing   = 1/6;
    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};
                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
        
        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
        
        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)
            this.nameHP      = var.nameHP;
            this.nameMic     = var.nameMic;
            this.nameSubj    = var.nameSubj;
            this.repeat      = var.repeat;
            this.mTF.time    = var.time;
            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
            if this.mic.dimensions == 2 % mic min phase + extension
                minMic = ita_minimumphase(this.mic);
                [mic, TF] = ita_extend_dat(minMic,this.TF);
            else TF = this.TF;
            end
            
            %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
                if this.mic.dimensions == 2
                    Rec = ita_multiply_spk(Rec,mic);
                end
                %% Short Filter with no correction for low freqs and minimun phase
                aux = max(abs(Rec.freqData),[],2);
                
                % find first maximum and truncate low freq correction at this point
                idxDF   = Rec.freq2index([this.fLower  this.fLower*1.5 ]);
                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');
           
            if this.normalized,                    
                this_norm = ita_normalize_dat(this_win);
                thisEQ.timeData = this_norm.timeData;
            else thisEQ.timeData = this_win.timeData;
            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
            result = {'TF','fLower','fUpper','method','normalized','smoothing'};
        end
        
        function result = propertiesLoad
            result = {'mTF','m_fUpper','m_fLower','mMethod','mNormalized','mSmoothing'};
        end
        
        function result = propertiesMethod
            result = {'mSTD', 'max','mean'};
        end
    end
end