Commit 0869b784 authored by Archit Tamarapu's avatar Archit Tamarapu
Browse files

add flt2hex matlab function and switch to writing hexadecimal float...

add flt2hex matlab function and switch to writing hexadecimal float representation for FastConv and ParamBin
parent 47516234
Loading
Loading
Loading
Loading
+45999 −45999

File changed.

Preview size limit exceeded, changes collapsed.

+29 −29
Original line number Diff line number Diff line
@@ -43,54 +43,54 @@ function write_fastconv_rom_table_flt(output_file, FastConv_SHD_IR_FOA, FastConv
    
    %% HRIRs (SHD)
    % HOA3
    fprintf(fid, ['const float FASTCONV_' FastConv_SHD_IR_HOA3.order '_latency_s = %10.9ff;\n'], FastConv_SHD_IR_HOA3.latency_s);
    writeData3L(fid, ['const float leftHRIRReal_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', real(squeeze(FastConv_SHD_IR_HOA3.IR(1,:,:,:))));
    writeData3L(fid, ['const float leftHRIRImag_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'], '%+ff',imag(squeeze(FastConv_SHD_IR_HOA3.IR(1,:,:,:))));
    writeData3L(fid, ['const float rightHRIRReal_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', real(squeeze(FastConv_SHD_IR_HOA3.IR(2,:,:,:))));
    writeData3L(fid, ['const float rightHRIRImag_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', imag(squeeze(FastConv_SHD_IR_HOA3.IR(2,:,:,:))));
    fprintf(fid, ['const float FASTCONV_' FastConv_SHD_IR_HOA3.order '_latency_s = %s;\n'], flt2hex(FastConv_SHD_IR_HOA3.latency_s));
    writeData3L(fid, ['const float leftHRIRReal_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', real(squeeze(FastConv_SHD_IR_HOA3.IR(1,:,:,:))));
    writeData3L(fid, ['const float leftHRIRImag_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'], '%a',imag(squeeze(FastConv_SHD_IR_HOA3.IR(1,:,:,:))));
    writeData3L(fid, ['const float rightHRIRReal_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', real(squeeze(FastConv_SHD_IR_HOA3.IR(2,:,:,:))));
    writeData3L(fid, ['const float rightHRIRImag_' FastConv_SHD_IR_HOA3.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA3.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', imag(squeeze(FastConv_SHD_IR_HOA3.IR(2,:,:,:))));
    % HOA2
    fprintf(fid, ['const float FASTCONV_' FastConv_SHD_IR_HOA2.order '_latency_s = %10.9ff;\n'], FastConv_SHD_IR_HOA2.latency_s);
    writeData3L(fid, ['const float leftHRIRReal_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', real(squeeze(FastConv_SHD_IR_HOA2.IR(1,:,:,:))));
    writeData3L(fid, ['const float leftHRIRImag_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', imag(squeeze(FastConv_SHD_IR_HOA2.IR(1,:,:,:))));
    writeData3L(fid, ['const float rightHRIRReal_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', real(squeeze(FastConv_SHD_IR_HOA2.IR(2,:,:,:))));
    writeData3L(fid, ['const float rightHRIRImag_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', imag(squeeze(FastConv_SHD_IR_HOA2.IR(2,:,:,:))));
    fprintf(fid, ['const float FASTCONV_' FastConv_SHD_IR_HOA2.order '_latency_s = %s;\n'], flt2hex(FastConv_SHD_IR_HOA2.latency_s));
    writeData3L(fid, ['const float leftHRIRReal_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', real(squeeze(FastConv_SHD_IR_HOA2.IR(1,:,:,:))));
    writeData3L(fid, ['const float leftHRIRImag_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', imag(squeeze(FastConv_SHD_IR_HOA2.IR(1,:,:,:))));
    writeData3L(fid, ['const float rightHRIRReal_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', real(squeeze(FastConv_SHD_IR_HOA2.IR(2,:,:,:))));
    writeData3L(fid, ['const float rightHRIRImag_' FastConv_SHD_IR_HOA2.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_HOA2.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', imag(squeeze(FastConv_SHD_IR_HOA2.IR(2,:,:,:))));
    % FOA
    fprintf(fid, ['const float FASTCONV_' FastConv_SHD_IR_FOA.order '_latency_s = %10.9ff;\n'], FastConv_SHD_IR_FOA.latency_s);
    writeData3L(fid, ['const float leftHRIRReal_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', real(squeeze(FastConv_SHD_IR_FOA.IR(1,:,:,:))));
    writeData3L(fid, ['const float leftHRIRImag_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%+ff', imag(squeeze(FastConv_SHD_IR_FOA.IR(1,:,:,:))));
    writeData3L(fid, ['const float rightHRIRReal_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'], '%+ff',real(squeeze(FastConv_SHD_IR_FOA.IR(2,:,:,:))));
    writeData3L(fid, ['const float rightHRIRImag_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'], '%+ff',imag(squeeze(FastConv_SHD_IR_FOA.IR(2,:,:,:))));
    fprintf(fid, ['const float FASTCONV_' FastConv_SHD_IR_FOA.order '_latency_s = %s;\n'], flt2hex(FastConv_SHD_IR_FOA.latency_s));
    writeData3L(fid, ['const float leftHRIRReal_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', real(squeeze(FastConv_SHD_IR_FOA.IR(1,:,:,:))));
    writeData3L(fid, ['const float leftHRIRImag_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'],'%a', imag(squeeze(FastConv_SHD_IR_FOA.IR(1,:,:,:))));
    writeData3L(fid, ['const float rightHRIRReal_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'], '%a',real(squeeze(FastConv_SHD_IR_FOA.IR(2,:,:,:))));
    writeData3L(fid, ['const float rightHRIRImag_' FastConv_SHD_IR_FOA.order '[BINAURAL_CONVBANDS][' FastConv_SHD_IR_FOA.order '_CHANNELS][BINAURAL_NTAPS_SBA]'], '%a',imag(squeeze(FastConv_SHD_IR_FOA.IR(2,:,:,:))));

    %% HRIRs (SD)
    fprintf(fid, 'const float FASTCONV_HRIR_latency_s = %10.9ff;\n', FastConv_SD_IR.latency_s);
    writeData3L(fid, 'const float leftHRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]','%+ff', real(squeeze(FastConv_SD_IR.IR(1,:,:,:))));
    writeData3L(fid, 'const float leftHRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]','%+ff', imag(squeeze(FastConv_SD_IR.IR(1,:,:,:))));
    writeData3L(fid, 'const float rightHRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]', '%+ff',real(squeeze(FastConv_SD_IR.IR(2,:,:,:))));
    writeData3L(fid, 'const float rightHRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]','%+ff', imag(squeeze(FastConv_SD_IR.IR(2,:,:,:))));
    fprintf(fid, 'const float FASTCONV_HRIR_latency_s = %s;\n', flt2hex(FastConv_SD_IR.latency_s));
    writeData3L(fid, 'const float leftHRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]','%a', real(squeeze(FastConv_SD_IR.IR(1,:,:,:))));
    writeData3L(fid, 'const float leftHRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]','%a', imag(squeeze(FastConv_SD_IR.IR(1,:,:,:))));
    writeData3L(fid, 'const float rightHRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]', '%a',real(squeeze(FastConv_SD_IR.IR(2,:,:,:))));
    writeData3L(fid, 'const float rightHRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS]','%a', imag(squeeze(FastConv_SD_IR.IR(2,:,:,:))));
    
    %% BRIRs (SD)
    fprintf(fid, '/* Binaural rendering data set based on BRIRs \n');
    fprintf(fid, ' * Tables derived from Mozart IIS BRIRs.*/\n');
    fprintf(fid, 'const float FASTCONV_BRIR_latency_s = %10.9ff;\n', FastConv_SD_BRIR.rev_param.latency_s);
    writeData3L(fid, 'const float leftBRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]','%+ff', real(squeeze(FastConv_SD_BRIR.IR(1,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));
    writeData3L(fid, 'const float leftBRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]', '%+ff',imag(squeeze(FastConv_SD_BRIR.IR(1,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));
    writeData3L(fid, 'const float rightBRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]', '%+ff',real(squeeze(FastConv_SD_BRIR.IR(2,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));
    writeData3L(fid, 'const float rightBRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]','%+ff', imag(squeeze(FastConv_SD_BRIR.IR(2,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));
    fprintf(fid, 'const float FASTCONV_BRIR_latency_s = %s;\n', flt2hex(FastConv_SD_BRIR.rev_param.latency_s));
    writeData3L(fid, 'const float leftBRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]','%a', real(squeeze(FastConv_SD_BRIR.IR(1,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));
    writeData3L(fid, 'const float leftBRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]', '%a',imag(squeeze(FastConv_SD_BRIR.IR(1,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));
    writeData3L(fid, 'const float rightBRIRReal[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]', '%a',real(squeeze(FastConv_SD_BRIR.IR(2,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));
    writeData3L(fid, 'const float rightBRIRImag[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][BINAURAL_NTAPS_MAX]','%a', imag(squeeze(FastConv_SD_BRIR.IR(2,:,:,1:FastConv_SD_BRIR.rev_param.NFilter))));

    % RT60
    fprintf(fid,'const float fastconvReverberationTimes[CLDFB_NO_CHANNELS_MAX] = \n{');
    fprintf(fid,'\n\t');
    fprintf(fid,'\n    ');
    for bandIdx = 1:FastConv_SD_BRIR.rev_param.kAna
        fprintf(fid,'%f%s,    ', FastConv_SD_BRIR.rev_param.rt60(bandIdx),'f');
        fprintf(fid,'%s,    ', flt2hex(FastConv_SD_BRIR.rev_param.rt60(bandIdx)));
    end
    fprintf(fid,'\n};\n');
    fprintf(fid,'\n\n');

    % energyReverb
    fprintf(fid,'const float fastconvReverberationEneCorrections[CLDFB_NO_CHANNELS_MAX] = \n{');
    fprintf(fid,'\n\t');
    fprintf(fid,'\n    ');
    for bandIdx = 1:FastConv_SD_BRIR.rev_param.kAna
        fprintf(fid,'%f%s,    ', FastConv_SD_BRIR.rev_param.nrgLr(bandIdx),'f');
        fprintf(fid,'%s,    ', flt2hex(FastConv_SD_BRIR.rev_param.nrgLr(bandIdx)));
    end
    fprintf(fid,'\n};\n');
    fprintf(fid,'\n\n');
+2 −2
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ function write_fastconv_rom_table_fx(output_file, FastConv_SHD_IR_FOA, FastConv_
    % RT60
    fprintf(fid, 'const int16_t fastconvReverberationTimes_factorQ = %d;\n', FastConv_SD_BRIR.rev_param.factorQ_rt60);
    fprintf(fid,'const int16_t fastconvReverberationTimes[CLDFB_NO_CHANNELS_MAX] = \n{');
    fprintf(fid,'\n\t');
    fprintf(fid,'\n    ');
    FastConv_SD_BRIR.rev_param.rt60 = FastConv_SD_BRIR.rev_param.rt60 .* (2.^double(FastConv_SD_BRIR.rev_param.factorQ_rt60));
    for bandIdx = 1:FastConv_SD_BRIR.rev_param.kAna
        fprintf(fid,'%d,    ', FastConv_SD_BRIR.rev_param.rt60(bandIdx));
@@ -106,7 +106,7 @@ function write_fastconv_rom_table_fx(output_file, FastConv_SHD_IR_FOA, FastConv_
    % energyReverb
    fprintf(fid, 'const int16_t fastconvReverberationEneCorrections_factorQ = %d;\n', FastConv_SD_BRIR.rev_param.factorQ_nrgLr);
    fprintf(fid,'const int16_t fastconvReverberationEneCorrections[CLDFB_NO_CHANNELS_MAX] = \n{');
    fprintf(fid,'\n\t');
    fprintf(fid,'\n    ');
    FastConv_SD_BRIR.rev_param.nrgLr = FastConv_SD_BRIR.rev_param.nrgLr .* (2.^double(FastConv_SD_BRIR.rev_param.factorQ_nrgLr));
    for bandIdx = 1:FastConv_SD_BRIR.rev_param.kAna
        fprintf(fid,'%d,    ', FastConv_SD_BRIR.rev_param.nrgLr(bandIdx));
+78 −0
Original line number Diff line number Diff line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   (C) 2022-2024 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
%   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
%   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
%   contributors to this repository. All Rights Reserved.
%
%   This software is protected by copyright law and by international treaties.
%   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
%   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
%   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
%   contributors to this repository retain full ownership rights in their respective contributions in
%   the software. This notice grants no license of any kind, including but not limited to patent
%   license, nor is any license granted by implication, estoppel or otherwise.
%
%   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
%   contributions.
%
%   This software is provided "AS IS", without any express or implied warranties. The software is in the
%   development stage. It is intended exclusively for experts who have experience with such software and
%   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
%   and fitness for a particular purpose are hereby disclaimed and excluded.
%
%   Any dispute, controversy or claim arising under or in relation to providing this software shall be
%   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
%   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
%   the United Nations Convention on Contracts on the International Sales of Goods.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function str = flt2hex(f)
%   flt2hex
%   C99 %a style printout of single precision float values
%   Input value will be forced to single precision float!
%   No special handling is done for inf, NaN and denormal values

% force input to single precision
f = single(f);

% special case for zero
if f == 0
    str = '0x0p+0';
    return;
end

% get the bits of the float value
x = typecast(f, 'uint32');
bits = dec2bin(x, 32);

%% parse bits
% extract the sign, exponent and mantissa
sign = bits(1);
exponent = bits(2:9);
mantissa = bits(10:end);

%% construct output string
% sign
s = '';
if sign == '1'
    s = '-';
end

% remove exponent bias
e = bin2dec(exponent) - 127;

% 23 bit mantissa grouped 4 bits at a time
% add trailing 0 to allow reshape
mantissa = [mantissa '0'];
m = dec2hex(bin2dec(reshape(mantissa, 4, [])'));

% final C99 %a style printout
str = sprintf('%s0x1.%sp%+d', s, lower(m), e);

% debugging
%     disp(f)
%     disp(hex2dec(['1' m']) * 16^-6 * 2^e);
end
+5 −1
Original line number Diff line number Diff line
@@ -50,7 +50,11 @@ for A = 1:indices(1)
        end
        counter=1;
        for C = 1:indices(3)
            if strcmp(floatformat, "%a")
                fprintf(fid_source, '%s', flt2hex(real(data(A,B,C))));
            else
                fprintf(fid_source,floatformat,real(data(A,B,C)));
            end
            if C < indices(3)
                if mod(counter,10) == 0
                    fprintf(fid_source,',');
Loading