ita_roomacoustics_parameters.m 8.62 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
function varargout = ita_roomacoustics_parameters(varargin)
%ITA_ROOMACOUSTICS_PARAMETERS - Manages the default roomacoustic parameters
%  This function ++++ FILL IN INFO HERE +++
%
%  Syntax:
%               ita_roomacoustics_parameters()                           % calls GUI (automatically generated from the struct in ita_preferences('roomacousticParameters')
%   s       =   ita_roomacoustics_parameters()                           % calls GUI and returns struct
%
%   val     =   ita_roomacoustics_parameters('T20');                     % val = true if 'T20' has to be calculated; else val = false
%   val     =   ita_roomacoustics_parameters('EDT', Center_Time', 'SNR') % multiple input parameters possible (val is cell)
%
%               ita_roomacoustics_parameters('reset')                    % resets all parameters
%
%   strCell =   ita_roomacoustics_parameters('getAvailableParameters')  % returns a cell with strings of all implemented  parameters
%   strCell =   ita_roomacoustics_parameters('getAvailableParameters', 'Reverberation_Times') % only parameters of one category
%
%    Currently available parameters: EDT, T10, T15, T20, T30, T40, T50, T60, C50, C80, D50, D80, Center_Time, PSNR_Lundeby, PSPNR_Lundeby, Intersection_Time_Lundeby
%    Currently available categories: Reverberation_Times,  Clarity_and_Definition, Others
%
%
%  See also:
%   ita_roomacoustics, ita_roomacoustics_reverberation_time(), ita_roomacoustics_energy_parameters()
%
%   Reference page in Help browser
%        <a href="matlab:doc mgu_ra_results">doc mgu_ra_results</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:  04-May-2011

%% TODO:
%   - set auch sinnvoll?

%%

40
defaultPar = struct( 'Reverberation_Times',      struct('EDT', true, 'T10', false, 'T15', false, 'T20', true, 'T25', false, 'T30', true, 'T40', false, 'T50', false, 'T60', false, 'T_Huszty', false, 'T_Lundeby', false),...
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    'Clarity_and_Definition',   struct('C50', false, 'C80',false,  'D50', false ,'D80', false, 'Center_Time', false ), ...
    'Others',                   struct( 'PSNR_Lundeby', false, 'PSPNR_Lundeby', false, 'Intersection_Time_Lundeby', false, 'EDC', false ));

if nargin == 1 && strcmpi('getDefaultStruct',varargin{1} )                 % return default struct
    varargout = {defaultPar};
elseif nargin == 1 && strcmpi('reset',varargin{1} )                        % reset to default
    ita_preferences('roomacousticParameters', defaultPar);
    ita_verbose_info('Resetting roomacoustic parameter...', 1)
    if nargout
        varargout = {defaultPar};
    end
elseif nargin >= 1 && strcmpi('getAvailableParameters',varargin{1} )
    
    categoryNames = fieldnames(defaultPar);
    
    if nargin == 2   % if just one catergory
        idxOfCat = strcmpi(varargin{2},categoryNames );
        if any(idxOfCat)
            categoryNames = categoryNames(idxOfCat);
        else
61
            error('second parameter must specify category name. (possible: %sor %s)', sprintf('%s, ', categoryNames{1:end-1}), categoryNames{end}) 
62 63 64
        end
    end
    
65
    varargout = cell(1,1);
66
    for iCat = 1:numel(categoryNames)
67
        varargout(1) = {[varargout{1}; fieldnames(defaultPar.(categoryNames{iCat}))]};  
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
    end
    return
else
    
    % CHECK IF STRUCT IN PREFERENCES IS UP TO DATE
    raPar           = ita_preferences('roomacousticParameters');
    if ~isstruct(raPar)
        raPar = defaultPar;
        ita_verbose_info('Set roomacoustic parameter to default values! ' ,1)
    else % check if all fields exist
        defaultCat = fieldnames(defaultPar);
        
        for iCat = 1:numel(defaultCat)
            defaultParNames = fieldnames(defaultPar.(defaultCat{iCat}));
            currentParNames = fieldnames(raPar.(defaultCat{iCat}));
            
            crateNewFields = setdiff(defaultParNames, currentParNames);
            for iPar = 1:numel(crateNewFields)
                    raPar.(defaultCat{iCat}).(crateNewFields{iPar}) = defaultPar.(defaultCat{iCat}).(crateNewFields{iPar});
                    ita_verbose_info(['Added new roomacoustic parameter: ''' crateNewFields{iPar} '''' ],1)
            end                
            
            deleteFields = setdiff(currentParNames, defaultParNames);
            for iPar = 1:numel(deleteFields)
                  raPar.(defaultCat{iCat}) =  rmfield(raPar.(defaultCat{iCat}), deleteFields{iPar});
                    ita_verbose_info(['Deleted  roomacoustic parameter: ''' deleteFields{iPar} '''' ],1)
            end  
            

%             parNames = fieldnames(defaultPar.(defaultCat{iCat}));
%             
%             for iPar = 1: numel(parNames)
%                 if ~isfield(raPar.(defaultCat{iCat}), parNames{iPar}) % field don't exist => create it
%                     raPar.(defaultCat{iCat}).(parNames{iPar}) = defaultPar.(defaultCat{iCat}).(parNames{iPar});
%                     ita_verbose_info(['Added new roomacoustic parameter: ''' parNames{iPar} '''' ],1)
%                 end
%             end
        end
        ita_preferences('roomacousticParameters', raPar);
    end
    
    
    if nargin                              % input parameters => search corresponding values
        outputVec = cell(nargin,1); % or better vector?
        for iInput = 1:nargin
            if ~ischar(varargin{iInput})
                error('input must be char')
            else
                value = getFieldValue(raPar, varargin{iInput});
                if isempty(value)
                    error('There is no roomacoustic parameter with name %s', varargin{iInput})
                end
                outputVec{iInput} = getFieldValue(raPar, varargin{iInput});
            end
        end
        
        varargout = {outputVec};
        
    else                                            % no input parameters => Call GUI
        guiOutputMapping = [];
        pList{1}.description = 'Parameters';
        pList{1}.helptext    = '';
        pList{1}.text        = 'Default parameter to calculate (used for console not GUI)';
        pList{1}.datatype    = 'simple_text';
        pList{1}.color       = [0.2 0.5 0.2];
        
        
        categoryNameCell = fieldnames(raPar);
        for iCat = 1:numel(categoryNameCell)
            ele = length(pList) + 1;
            pList{ele}.datatype    = 'line';
            
            ele = length(pList) + 1;
            pList{ele}.datatype    = 'text';
            pList{ele}.description    = strrep(categoryNameCell{iCat}, '_', ' ');
            
            fieldNameCell = fieldnames(raPar.(categoryNameCell{iCat}));
            for iField = 1:numel(fieldNameCell)
                ele = length(pList) + 1;
                fieldStr = strrep(fieldNameCell{iField}, '_', ' ');
                pList{ele}.description = fieldStr;
                pList{ele}.helptext    = fieldStr;
                if isa(raPar.(categoryNameCell{iCat}).(fieldNameCell{iField}), 'logical')
                    pList{ele}.datatype    = 'bool';
                elseif isa(raPar.(categoryNameCell{iCat}).(fieldNameCell{iField}), 'numeric')
                    pList{ele}.datatype    = 'int';
                else
                    error(sprintf('unknown type in struct (field: %s). i don''t know how to generate gui', fieldNameCell{iField}))
                end
                pList{ele}.default     = raPar.(categoryNameCell{iCat}).(fieldNameCell{iField});
                guiOutputMapping = [guiOutputMapping; categoryNameCell(iCat), fieldNameCell(iField) ];
            end
            
        end
        guiOutput = ita_parametric_GUI(pList,'Roomacoustic Parameters');
        
        if isempty(guiOutput)
            ita_verbose_info(' Canceled by user',1)
            return
        end
        
        % write output in struct
        raParOut = raPar;
        for iOutput = 1:numel(guiOutputMapping)/2
            raParOut.(guiOutputMapping{iOutput,1}).(guiOutputMapping{iOutput,2}) = guiOutput{iOutput};
        end
        
        ita_preferences('roomacousticParameters', raParOut );
        if nargout
            varargout = {raParOut};
        end
    end
end



%end function
end


function value = getFieldValue(struct, fieldName)
fNameCell = fieldnames(struct);

if any(strcmpi(fieldName, fNameCell))
    value = struct.(fieldName);
    return
else
    value = [];
    for iField = 1: numel(fNameCell)
        if isstruct(struct.(fNameCell{iField}))
            value = getFieldValue(struct.(fNameCell{iField}), fieldName);
            if ~isempty(value)     % value found => return
                return
            end
        end
    end
    
end

end