Commit 30b72820 authored by Adam Mills's avatar Adam Mills
Browse files

Updating the HRTF loader so that it can be used by older version of matlab

parent bbda05b2
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@
%    the United Nations Convention on Contracts on the International Sales of Goods.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function IR_data = generate_HOA_HRIRs_MOD_lens(order, python_path, sofa_path, sofa_file_name, ir_len)
function IR_data = generate_HOA_HRIRs_MOD_lens(order, python_path, sofa_path, sofa_file_name, ir_len, write_out_sofa)
    % HRIR convertor - Takes sphere sampled HRIRs and converts them to
    % HOA HRIRs.
    %
@@ -37,13 +37,14 @@ function IR_data = generate_HOA_HRIRs_MOD_lens(order, python_path, sofa_path, so
    % converted.
    % sofa_file - file name of the HRTFs to be converted
    % ir_len - length of the IRs to be used.
    % write_out_sofa - boolean to enable/disable writing out of sofa
    %
    % Typical usage:
    %   generate_HOA_HRIRs_MOD_lens(1, "~/.pyenv/versions/3.8.16/bin/python", '~/git/ivas-pc-testfiles/sofa-files/', 'HRIR_128_48000.sofa', 128)
    %

% Pointing to the correct python binary
pyenv(Version=python_path);
pyenv('Version', python_path);

% Load in the support coefs
load('hrtf_support_coefs.mat', 'hrtf_support_coefs');
@@ -120,8 +121,10 @@ IR = permute(IR_HOA, [2, 1, 3]);
HOAformat_str = ['HOA',num2str(order),'S'];
save(fullfile(erase(sofa_file_name, ".sofa") + "_converted_" +  HOAformat_str + ".mat"), "IR")

if (write_out_sofa == 1)
    H.updateSOFA(IR);
    H.writeSOFA(fullfile(erase(sofa_file_name, ".sofa") + "_converted_" +  HOAformat_str + ".sofa"));
end

IR_data = IR;

+42 −3
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ classdef hrtf_library_loader < handle
    end
  
    methods

        function obj = hrtf_library_loader()
        end

@@ -91,7 +92,7 @@ classdef hrtf_library_loader < handle
            % This looks like a library built from a bunch of discrete
            % locations in free field
            % Let's figure out the source locations used in the HRTF library:
            Pos = double(sofa_file.SourcePosition)';
            Pos = hrtf_library_loader.convert_numpy_2darray(sofa_file.SourcePosition)';
            Units = strtrim(strsplit(string(sofa_file.SourcePosition_Units), ','));
            
            assert( any(strcmpi(Units{1}, {'degree','radian'})), 'Unknown units');
@@ -109,10 +110,10 @@ classdef hrtf_library_loader < handle
            
            % Now, get the impulse repsonses:
            assert( isprop(sofa_file, 'GLOBAL_DataType'), 'Expected field: GLOBAL_DataType');
            Data.IR = double(sofa_file.Data_IR);
            Data.IR = hrtf_library_loader.convert_numpy_3darray(sofa_file.Data_IR);
            Data.SamplingRate = double(sofa_file.Data_SamplingRate);
            Data.SamplingRate_Units = string(sofa_file.Data_SamplingRate_Units);
            Data.Delay = double(sofa_file.Data_Delay);
            Data.Delay = hrtf_library_loader.convert_numpy_2darray(sofa_file.Data_Delay);
            switch lower(string(sofa_file.GLOBAL_DataType))
                case 'fir'
                    assert( size(Data.IR,2)>=2, 'Expecting 2 receivers (ears)');
@@ -394,6 +395,44 @@ classdef hrtf_library_loader < handle
    
    methods(Static=true)

        function mat = convert_numpy_2darray(np_array)
            py_list = np_array.tolist();
            % List to cell
            matCell = cell(py_list)';
            shape = cell(np_array.shape);
            shape_mat = zeros(1, length(shape));
            for i=1:length(shape)
                shape_mat(i) = double(shape{i});
            end
            mat = zeros(shape_mat);
            for i = 1:length(matCell)
                if isa(matCell{i}, 'py.list')
                    mat(i,:) = cell2mat(cell(matCell{i}));
                end
            end
        end

        function mat = convert_numpy_3darray(np_array)
            py_list = np_array.tolist();
            % List to cell
            matCell = cell(py_list)';
            shape = cell(np_array.shape);
            shape_mat = zeros(1, length(shape));
            for i=1:length(shape)
                shape_mat(i) = double(shape{i});
            end
            mat = zeros(shape_mat);
            for i = 1:length(matCell)
                if isa(matCell{i}, 'py.list')
                    for j = 1:length(matCell{i})
                        if isa(matCell{i}, 'py.list')
                            mat(i,j,:) = cell2mat(cell(matCell{i}{j}));
                        end
                    end
                end
            end
        end
    
        function [Band2FR,FCs]=MakeBand2FR(FFTLen,FSample)
            % Compute the standard band filters for freq domain representations
            %