Loading scripts/binauralRenderer_interface/matlab_hrir_generation_scripts/SHD_2_ROM.m 0 → 100644 +368 −0 Original line number Diff line number Diff line %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % (C) 2022-2023 Baseline Development Group 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 Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation. All Rights Reserved. % % This software is protected by copyright law and by international treaties. % The Baseline Development Group 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 Corporation, Qualcomm Technologies, Inc., and VoiceAge Corporation retain full ownership % rights in their respective contributions in the software. No license of any kind, including but not % limited to patent license, of any foregoing parties is hereby granted by implication, estoppel or % otherwise. % % 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/or 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 IR_cldfb = SHD_2_ROM( py_path, rom_c_file, sofa_file ) % SHD_2_ROM( python_path, rom_c_file, sofa_file ) % % - converts sphere-sampled Head Related Impulse Responses (HRIRs) given in sofa_file to the Spherical Harmonics domain (SHD) % - converts SHD HRIRs to Complex Low Delay Filter Bank (CLDFB) domain % - writes CLDFB HRIRs to c-code ROM tables. % % Processing chain: % % HRIRs --> SHD --> CLDFB --> ROM % HRIRs --> SHD: Using ./generate_HOA_HRIRs_MOD_lens.m % SHD --> CLDFB: Least squares optimization % CLDFB --> ROM: Using ../param_bin/writeData3L.m [thispath,~,~] = fileparts(mfilename('fullpath')); thispath = [thispath,filesep]; %py_path = 'C:\Users\xxxx\AppData\Local\Programs\Python\Python39\python.exe'; % may look like this if ~exist('sofa_file','var') || isempty(sofa_file) sofa_file = fullfile(thispath,'..','HRIRs_sofa','HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000.sofa'); end %% convert sphere-sampled HRIRs to SHD HRIRs % requires: % python -m pip install sofar % python -m pip install numpy % convert sphere-sampled HRIRs to SHD HRIRs hrir_len = 128; ambi_order = 3; [sofa_path,sofa_name, sofa_ext] = fileparts(sofa_file); IR = generate_HOA_HRIRs_MOD_lens(ambi_order, py_path, sofa_path, [sofa_name,sofa_ext], hrir_len); % load([thispath,'HRIR_128_48000_dolby_SBA3.mat'],'IR'); gives the same data as the above with default sofa file %% SHD -> CLDFB via least squares error optimization [~,num_ears,num_ch] = size(IR); num_cldfb_taps = 3; IR_cldfb = zeros(60,num_cldfb_taps,num_ears,num_ch); % 60 frequency bands eval_flag = 0; % optional, = 1 requires signal processing toolbox (fftfilt) for pos = 1:num_ch disp(['Channel ',num2str(pos),'/',num2str(num_ch)]) for ear = 1:num_ears IR_cldfb(:,:,ear,pos) = fir_to_cldfb_fir( IR(:,ear,pos), num_cldfb_taps, eval_flag ); end end %% CLDFB -> ROM % Note: The following section is copied from generate_HRIR_in_SHD_CLDFB_FASTCONV.m provided by @emerit latency_s = 2.083333333333333e-05; % No added latency from conversion method max_band = 50; % Compute 60 bands, but only use 50 in ROM table IR_cldfb_rom = permute(IR_cldfb, [3 1 4 2]); % after permute: [ears(2), bands(60), chans(16), taps(3)] order_int = (round(sqrt(size(IR_cldfb_rom,3))-1)); if order_int == 1 order = 'FOA'; else order = ['HOA' int2str(order_int)]; end if ~exist('rom_c_file','var') || isempty(rom_c_file) rom_c_file = [thispath,'ivas_rom_binauralRenderer_',order,'.c']; % fullfile(thispath,'..','..','..','lib_rend',['ivas_rom_binauralRenderer_',order,'.c']); end fid = fopen(rom_c_file,'wt'); fprintf(fid, ['const float FASTCONV_' order '_latency_s = %10.9ff;\n'], latency_s); addpath(fullfile(thispath,'..','param_bin')); % for writeData3L() writeData3L(fid, ['const float leftHRIRReal_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], real( squeeze(IR_cldfb_rom(1,1:max_band,:,:)))); writeData3L(fid, ['const float leftHRIRImag_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], imag( squeeze(IR_cldfb_rom(1,1:max_band,:,:)))); writeData3L(fid, ['const float rightHRIRReal_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], real( squeeze(IR_cldfb_rom(2,1:max_band,:,:)))); writeData3L(fid, ['const float rightHRIRImag_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], imag( squeeze(IR_cldfb_rom(2,1:max_band,:,:)))); fclose(fid); rmpath(fullfile(thispath,'..','param_bin')); function F = fir_to_cldfb_fir( target_fir, num_cldfb_taps, eval_flag ) [pFilter, D, S, L] = get_cldfb_filter; cldfb_delay = D - S + 1; % processing delay given stride pFilt = sqrt(S)/L * pFilter(:); N = length(pFilt); legacy_flag = 1; % = 1 used to indicate slightly too short buffers as used to generate tested coefficients % Filter Bank analysis / synthesis filters cldfb_mod_mat = exp(1i*((0:N-1)'-D/2) * ((0:L-1)+0.5) * pi/L ); % N x L modulation matrix with alternating sign periodicity of 2*L in time (row) direction cldfb_ansyn_filters = pFilt .* cldfb_mod_mat; % all (L) analysis/synthesis filters target_fir = target_fir(:); fir_length = length(target_fir); %% brute force least squares if legacy_flag num_slots = ceil(fir_length/S) + 10; % used to generate tested IVAS coefficients in CLDFB HRIR ROM with slightly too short buffers inp_len = S * num_slots; len = inp_len - cldfb_delay; idx_opt = cldfb_delay + (1:len); pos_offset = 200; else num_slots = ceil((fir_length + 2 * N - 1) / S); % convolution with target fir, analysis + synthesis filters (of length N each) len = S * num_slots; inp_len = len; idx_opt = (1:len); % use all output samples in the optimization, including the filter bank delay pos_offset = 0; end % Big trial and error Nbins = num_cldfb_taps * L; Vreal = zeros(S*len, Nbins); % all real for all shifts concatenated for each mask bin, Vimag = zeros(S*len, Nbins); % then all imag r = zeros(S*len,1); % right hand side all shifts concatenated x = zeros(inp_len,1); for pos = 1:S % input dirac pulse x(:) = 0; % target impulse response y0 for input pulse at pos out = x; out(pos+pos_offset+(0:fir_length-1)) = target_fir; y0 = [zeros(cldfb_delay,1);out(1:end-cldfb_delay)]; r(len*(pos-1)+(1:len)) = y0(idx_opt); fprintf('.'); % CLDFB analysis for impulse input x(pos+pos_offset) = 1; X = zeros(L,num_slots); frm_idx = (1:N)'; x_tmp = [zeros(N-S,1);x]; for slt = 1:num_slots X(:,slt) = sum(cldfb_ansyn_filters.*flipud(x_tmp(frm_idx))).'; frm_idx = frm_idx + S; end for cldfb_bin = 1:Nbins cldfb_band = rem(cldfb_bin-1,L)+1; cldfb_lag = fix((cldfb_bin-1)/L); % real-valued filter contribution of a single tap frm_idx = cldfb_lag * S + (1:N)'; y_tmp = zeros( num_slots * S + N - S,1); for slt = cldfb_lag+1:num_slots y_tmp(frm_idx) = y_tmp(frm_idx) + real(X(cldfb_band,slt-cldfb_lag) * cldfb_ansyn_filters(:,cldfb_band)); frm_idx = frm_idx + S; end Vreal(len*(pos-1)+1:len*pos,cldfb_bin) = y_tmp(idx_opt); % imaginary-valued filter contribution of a single tap frm_idx = cldfb_lag * S + (1:N)'; y_tmp(:) = 0; for slt = cldfb_lag+1:num_slots y_tmp(frm_idx) = y_tmp(frm_idx) + real(1i*X(cldfb_band,slt-cldfb_lag) * cldfb_ansyn_filters(:,cldfb_band)); frm_idx = frm_idx + S; end Vimag(len*(pos-1)+1:len*pos,cldfb_bin) = y_tmp(idx_opt); end % bin end % pos fprintf('\n'); % solve lsq V = [Vreal,Vimag]; M = V'*V; Mrel = M + 1e-8*norm(M)*eye(size(M)); c = Mrel\(V'*r); % map back to mask locations F = zeros(L,num_cldfb_taps); for cldfb_bin = 1:Nbins F(cldfb_bin) = c(cldfb_bin)+1i*c(Nbins+cldfb_bin); end if eval_flag % evaluation (not needed for table generation, needs signal processing toolbox) get_snr( target_fir, F, cldfb_ansyn_filters, cldfb_delay, S ); end function get_snr( target_fir, cldfb_firs, cldfb_ansyn_filters, cldfb_delay, S ) rng(0); num_samples = 5*48000; x = randn(num_samples,1); % Filter noise in time domain y1 = fftfilt(target_fir,x); % Filter noise in CLDFB domain [N,L] = size(cldfb_ansyn_filters); num_slots = ceil((num_samples + cldfb_delay) / S); % analysis X = zeros(L,num_slots); frm_idx = (1:N)'; x_tmp = [zeros(N-S,1);x;zeros(cldfb_delay,1)]; for slt = 1:num_slots X(:,slt) = sum(cldfb_ansyn_filters.*flipud(x_tmp(frm_idx))).'; frm_idx = frm_idx + S; end % filter X = fftfilt(cldfb_firs.',X.').'; % synthesis frm_idx = (1:N)'; y_tmp = zeros( num_slots * S + N - S,1); for slt = 1:num_slots y_tmp(frm_idx) = y_tmp(frm_idx) + real(sum(X(:,slt).' .* cldfb_ansyn_filters,2)); frm_idx = frm_idx + S; end y2 = y_tmp(cldfb_delay+(1:num_samples)); plot(y1), hold on, plot(y2,'r--'), plot(y1-y2,'k'), hold off set(gca,'xlim',[1,num_samples]) fprintf('SNR: %.1f dB \n', 10*log10(sum(y1.^2)/sum((y1-y2).^2))); legend('time domain','CLDFB domain','difference') title('Filtered noise') function [h, D, S, L] = get_cldfb_filter() % const float LDQMF_60[] in \lib_com\rom_com.c, line 5219 S = 60; % stride L = 60; % frequency bands D = 240 + S - 1; % system delay h = [ 0.0000953076, 0.0001230230, 0.0001279964, 0.0001260533, 0.0001211219 0.0001122123, 0.0001010860, 0.0000876540, 0.0000719883, 0.0000545472 0.0000352143, 0.0000145686, -0.0000074264, -0.0000303788, -0.0000539205 -0.0000782743, -0.0001028838, -0.0001275374, -0.0001520015, -0.0001760167 -0.0001997108, -0.0002226708, -0.0002446725, -0.0002655797, -0.0002852145 -0.0003034996, -0.0003203036, -0.0003356283, -0.0003493345, -0.0003614030 -0.0003719004, -0.0003807641, -0.0003881051, -0.0003939842, -0.0003985357 -0.0004019095, -0.0004041938, -0.0004056677, -0.0004065430, -0.0004069925 -0.0004072535, -0.0004075877, -0.0004083676, -0.0004098394, -0.0004122990 -0.0004160839, -0.0004214063, -0.0004285777, -0.0004378651, -0.0004495422 -0.0004637682, -0.0004806494, -0.0005003878, -0.0005231378, -0.0005489803 -0.0005777747, -0.0006095612, -0.0006443121, -0.0006813223, -0.0007226231 -0.0007722576, -0.0008268412, -0.0008839625, -0.0009417336, -0.0010004630 -0.0010601623, -0.0011206097, -0.0011817788, -0.0012432419, -0.0013045983 -0.0013656860, -0.0014260965, -0.0014855355, -0.0015435946, -0.0015999591 -0.0016543545, -0.0017062968, -0.0017554691, -0.0018015467, -0.0018441341 -0.0018829798, -0.0019177221, -0.0019480695, -0.0019736972, -0.0019943134 -0.0020097434, -0.0020197174, -0.0020240925, -0.0020226294, -0.0020152442 -0.0020017736, -0.0019820682, -0.0019561697, -0.0019240153, -0.0018855907 -0.0018409232, -0.0017900462, -0.0017330211, -0.0016699535, -0.0016009507 -0.0015261442, -0.0014456788, -0.0013597424, -0.0012685407, -0.0011722331 -0.0010710671, -0.0009652392, -0.0008549765, -0.0007405236, -0.0006221444 -0.0005001140, -0.0003745670, -0.0002458634, -0.0001142541, 0.0000199491 0.0001564174, 0.0002949402, 0.0004350246, 0.0005769439, 0.0007203126 -0.0008803223, -0.0010328424, -0.0011841310, -0.0013346316, -0.0014848098 -0.0016343417, -0.0017832819, -0.0019316213, -0.0020790498, -0.0022252349 -0.0023701149, -0.0025136294, -0.0026556554, -0.0027960713, -0.0029348312 -0.0030717771, -0.0032068293, -0.0033399195, -0.0034709862, -0.0035999804 -0.0037267797, -0.0038513308, -0.0039736414, -0.0040935921, -0.0042111278 -0.0043262239, -0.0044388464, -0.0045489701, -0.0046565188, -0.0047614835 -0.0048637423, -0.0049632201, -0.0050599808, -0.0051539382, -0.0052450863 -0.0053333500, -0.0054187514, -0.0055012843, -0.0055808770, -0.0056575472 -0.0057313135, -0.0058021732, -0.0058701355, -0.0059352517, -0.0059975707 -0.0060571772, -0.0061141332, -0.0061685541, -0.0062205540, -0.0062703062 -0.0063179093, -0.0063635921, -0.0064075105, -0.0064498796, -0.0064908965 -0.0065308069, -0.0065698619, -0.0066083665, -0.0066466411, -0.0066849431 -0.0067233290, -0.0067621553, -0.0068021296, -0.0068436749, -0.0068870094 -0.0069324085, -0.0069801519, -0.0070305937, -0.0070840055, -0.0071406048 -0.0072006541, -0.0072644479, -0.0073321410, -0.0074039386, -0.0074799177 -0.0075602704, -0.0076450342, -0.0077342330, -0.0078278277, -0.0079257628 -0.0080279401, -0.0081341872, -0.0082442267, -0.0083577875, -0.0084744738 -0.0085938899, -0.0087156557, -0.0088391500, -0.0089637861, -0.0090888245 -0.0092134504, -0.0093367994, -0.0094579896, -0.0095760096, -0.0096898535 -0.0097982995, -0.0099003557, -0.0099947909, -0.0100801717, -0.0101551116 -0.0102182031, -0.0102678994, -0.0103026126, -0.0103207529, -0.0103206923 -0.0103006857, -0.0102590285, -0.0101939747, -0.0101036867, -0.0099863587 -0.0098401112, -0.0096632261, -0.0094537362, -0.0092098210, -0.0089295702 -0.0086111929, -0.0082527259, -0.0078523541, -0.0074084769, -0.0069190590 0.0063841688, 0.0057985946, 0.0051621343, 0.0044734711, 0.0037309236 0.0029329660, 0.0020781513, 0.0011651339, 0.0001925042, -0.0008409545 -0.0019364181, -0.0030950012, -0.0043176264, -0.0056051607, -0.0069584334 -0.0083780792, -0.0098646237, -0.0114185056, -0.0130400723, -0.0147295250 -0.0164868534, -0.0183120724, -0.0202049762, -0.0221651513, -0.0241921283 -0.0262852497, -0.0284437388, -0.0306666382, -0.0329528190, -0.0353010744 -0.0377098918, -0.0401776619, -0.0427025780, -0.0452826768, -0.0479161367 -0.0506004691, -0.0533332452, -0.0561118126, -0.0589331910, -0.0617944039 -0.0646922663, -0.0676232576, -0.0705836788, -0.0735698044, -0.0765774846 -0.0796026587, -0.0826408863, -0.0856874809, -0.0887378305, -0.0917868018 -0.0948293805, -0.0978601947, -0.1008738130, -0.1038645208, -0.1068264544 -0.1097536832, -0.1126400903, -0.1154794544, -0.1182654947, -0.1209914312 -0.1236500666, -0.1262341589, -0.1287376434, -0.1311538219, -0.1334753036 -0.1356947273, -0.1378047168, -0.1397978216, -0.1416664869, -0.1434033662 -0.1450008005, -0.1464512348, -0.1477471888, -0.1488809884, -0.1498452872 -0.1506324410, -0.1512351334, -0.1516460329, -0.1518578976, -0.1518635303 -0.1516559124, -0.1512281001, -0.1505732536, -0.1496847868, -0.1485562176 -0.1471813470, -0.1455538720, -0.1436681300, -0.1415183097, -0.1390990764 -0.1364052594, -0.1334318966, -0.1301742792, -0.1266280264, -0.1227891371 -0.1186537445, -0.1142183766, -0.1094799563, -0.1044355705, -0.0990828425 -0.0934195668, -0.0874440819, -0.0811550021, -0.0745511875, -0.0676321834 -0.0603975877, -0.0528475679, -0.0449828543, -0.0368040986, -0.0283128861 -0.0195108838, -0.0104003223, -0.0009837818, 0.0087356847, 0.0187546927 0.0290693250, 0.0396753438, 0.0505682528, 0.0617432520, 0.0731955394 -0.0849232078, -0.0969054326, -0.1091460735, -0.1216373071, -0.1343720406 -0.1473424733, -0.1605402082, -0.1739567965, -0.1875831038, -0.2014097124 -0.2154271752, -0.2296251506, -0.2439934313, -0.2585212290, -0.2731975317 -0.2880111337, -0.3029502928, -0.3180032372, -0.3331578076, -0.3484017253 -0.3637222052, -0.3791064322, -0.3945416212, -0.4100143015, -0.4255111217 -0.4410185516, -0.4565227628, -0.4720100164, -0.4874662757, -0.5028775334 -0.5182296634, -0.5335084200, -0.5486994982, -0.5637886524, -0.5787616372 -0.5936041474, -0.6083019376, -0.6228409410, -0.6372069120, -0.6513859630 -0.6653640866, -0.6791275144, -0.6926627755, -0.7059561610, -0.7189947963 -0.7317654490, -0.7442554235, -0.7564523220, -0.7683438063, -0.7799182534 -0.7911639810, -0.8020697832, -0.8126249313, -0.8228194118, -0.8326428533 -0.8420860767, -0.8511404991, -0.8597975969, -0.8680517077, -0.8758881092 -0.8832823634, -0.8902196884, -0.8967157602, -0.9027729034, -0.9083824754 -0.9135394692, -0.9182395935, -0.9224776030, -0.9262499809, -0.9295535684 -0.9323854446, -0.9347436428, -0.9366261959, -0.9380323887, -0.9389615655 -0.9394137263, -0.9393896461, -0.9388904572, -0.9379178882, -0.9364743829 -0.9345626831, -0.9321863055, -0.9293491840, -0.9260557890, -0.9223110080 -0.9181203246, -0.9134896994, -0.9084255695, -0.9029349089, -0.8970250487 -0.8907034993, -0.8839784265, -0.8768582940, -0.8693521619, -0.8614694476 -0.8532197475, -0.8446131349, -0.8356599212, -0.8263708353, -0.8167568445 -0.8068289757, -0.7965991497, -0.7860788107, -0.7752800584, -0.7642148733 -0.7528960109, -0.7413358092, -0.7295469642, -0.7175422311, -0.7053351402 -0.6929380894, -0.6803644896, -0.6676273942, -0.6547405124, -0.6417166591 -0.6285686493, -0.6153115034, -0.6019562483, -0.5885198116, -0.5750215650 0.5615197420, 0.5478612781, 0.5341838002, 0.5204906464, 0.5067980289 0.4931168854, 0.4794588387, 0.4658361673, 0.4522601366, 0.4387422502 0.4252935350, 0.4119254053, 0.3986486793, 0.3854739666, 0.3724119067 0.3594728410, 0.3466667533, 0.3340034485, 0.3214924335, 0.3091430366 0.2969639599, 0.2849639654, 0.2731511295, 0.2615332901, 0.2501178682 0.2389119864, 0.2279221565, 0.2171545923, 0.2066148520, 0.1963084787 0.1862401515, 0.1764142811, 0.1668347418, 0.1575049609, 0.1484276950 0.1396053135, 0.1310400218, 0.1227331311, 0.1146853194, 0.1068974212 0.0993694067, 0.0921007246, 0.0850901082, 0.0783365741, 0.0718384907 0.0655927584, 0.0595967993, 0.0538481586, 0.0483424664, 0.0430756323 0.0380428955, 0.0332404599, 0.0286619961, 0.0242999699, 0.0201510899 0.0162059069, 0.0124559226, 0.0088928537, 0.0054926532, 0.0023052765 -0.0005515143, -0.0030201224, -0.0052712574, -0.0073737046, -0.0093160523 -0.0111072771, -0.0127562135, -0.0142635731, -0.0156361461, -0.0168790054 -0.0179969221, -0.0189934950, -0.0198726747, -0.0206398536, -0.0212980714 -0.0218509119, -0.0223025978, -0.0226570386, -0.0229178313, -0.0230882075 -0.0231725387, -0.0231746566, -0.0230979007, -0.0229462404, -0.0227237809 -0.0224345829, -0.0220820960, -0.0216706358, -0.0212045144, -0.0206875466 -0.0201238506, -0.0195175279, -0.0188730918, -0.0181944817, -0.0174855441 -0.0167510118, -0.0159947462, -0.0152208358, -0.0144332750, -0.0136361914 -0.0128338682, -0.0120294262, -0.0112272501, -0.0104311826, -0.0096443929 -0.0088709844, -0.0081134979, -0.0073764324, -0.0066623385, -0.0059733889 -0.0053142183, -0.0046856776, -0.0040914025, -0.0035321070, -0.0030089030 -0.0025271603, -0.0020749648, -0.0016621647, -0.0012705614, -0.0008115423 ]; h = h.'; h = h(:); h(1*120+(1:120)) = -h(1*120+(1:120)); h(3*120+(1:120)) = -h(3*120+(1:120)); h(:) = h(end:-1:1); No newline at end of file Loading
scripts/binauralRenderer_interface/matlab_hrir_generation_scripts/SHD_2_ROM.m 0 → 100644 +368 −0 Original line number Diff line number Diff line %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % (C) 2022-2023 Baseline Development Group 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 Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation. All Rights Reserved. % % This software is protected by copyright law and by international treaties. % The Baseline Development Group 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 Corporation, Qualcomm Technologies, Inc., and VoiceAge Corporation retain full ownership % rights in their respective contributions in the software. No license of any kind, including but not % limited to patent license, of any foregoing parties is hereby granted by implication, estoppel or % otherwise. % % 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/or 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 IR_cldfb = SHD_2_ROM( py_path, rom_c_file, sofa_file ) % SHD_2_ROM( python_path, rom_c_file, sofa_file ) % % - converts sphere-sampled Head Related Impulse Responses (HRIRs) given in sofa_file to the Spherical Harmonics domain (SHD) % - converts SHD HRIRs to Complex Low Delay Filter Bank (CLDFB) domain % - writes CLDFB HRIRs to c-code ROM tables. % % Processing chain: % % HRIRs --> SHD --> CLDFB --> ROM % HRIRs --> SHD: Using ./generate_HOA_HRIRs_MOD_lens.m % SHD --> CLDFB: Least squares optimization % CLDFB --> ROM: Using ../param_bin/writeData3L.m [thispath,~,~] = fileparts(mfilename('fullpath')); thispath = [thispath,filesep]; %py_path = 'C:\Users\xxxx\AppData\Local\Programs\Python\Python39\python.exe'; % may look like this if ~exist('sofa_file','var') || isempty(sofa_file) sofa_file = fullfile(thispath,'..','HRIRs_sofa','HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000.sofa'); end %% convert sphere-sampled HRIRs to SHD HRIRs % requires: % python -m pip install sofar % python -m pip install numpy % convert sphere-sampled HRIRs to SHD HRIRs hrir_len = 128; ambi_order = 3; [sofa_path,sofa_name, sofa_ext] = fileparts(sofa_file); IR = generate_HOA_HRIRs_MOD_lens(ambi_order, py_path, sofa_path, [sofa_name,sofa_ext], hrir_len); % load([thispath,'HRIR_128_48000_dolby_SBA3.mat'],'IR'); gives the same data as the above with default sofa file %% SHD -> CLDFB via least squares error optimization [~,num_ears,num_ch] = size(IR); num_cldfb_taps = 3; IR_cldfb = zeros(60,num_cldfb_taps,num_ears,num_ch); % 60 frequency bands eval_flag = 0; % optional, = 1 requires signal processing toolbox (fftfilt) for pos = 1:num_ch disp(['Channel ',num2str(pos),'/',num2str(num_ch)]) for ear = 1:num_ears IR_cldfb(:,:,ear,pos) = fir_to_cldfb_fir( IR(:,ear,pos), num_cldfb_taps, eval_flag ); end end %% CLDFB -> ROM % Note: The following section is copied from generate_HRIR_in_SHD_CLDFB_FASTCONV.m provided by @emerit latency_s = 2.083333333333333e-05; % No added latency from conversion method max_band = 50; % Compute 60 bands, but only use 50 in ROM table IR_cldfb_rom = permute(IR_cldfb, [3 1 4 2]); % after permute: [ears(2), bands(60), chans(16), taps(3)] order_int = (round(sqrt(size(IR_cldfb_rom,3))-1)); if order_int == 1 order = 'FOA'; else order = ['HOA' int2str(order_int)]; end if ~exist('rom_c_file','var') || isempty(rom_c_file) rom_c_file = [thispath,'ivas_rom_binauralRenderer_',order,'.c']; % fullfile(thispath,'..','..','..','lib_rend',['ivas_rom_binauralRenderer_',order,'.c']); end fid = fopen(rom_c_file,'wt'); fprintf(fid, ['const float FASTCONV_' order '_latency_s = %10.9ff;\n'], latency_s); addpath(fullfile(thispath,'..','param_bin')); % for writeData3L() writeData3L(fid, ['const float leftHRIRReal_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], real( squeeze(IR_cldfb_rom(1,1:max_band,:,:)))); writeData3L(fid, ['const float leftHRIRImag_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], imag( squeeze(IR_cldfb_rom(1,1:max_band,:,:)))); writeData3L(fid, ['const float rightHRIRReal_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], real( squeeze(IR_cldfb_rom(2,1:max_band,:,:)))); writeData3L(fid, ['const float rightHRIRImag_' order '[BINAURAL_CONVBANDS][' order '_CHANNELS][BINAURAL_NTAPS_SBA]'], imag( squeeze(IR_cldfb_rom(2,1:max_band,:,:)))); fclose(fid); rmpath(fullfile(thispath,'..','param_bin')); function F = fir_to_cldfb_fir( target_fir, num_cldfb_taps, eval_flag ) [pFilter, D, S, L] = get_cldfb_filter; cldfb_delay = D - S + 1; % processing delay given stride pFilt = sqrt(S)/L * pFilter(:); N = length(pFilt); legacy_flag = 1; % = 1 used to indicate slightly too short buffers as used to generate tested coefficients % Filter Bank analysis / synthesis filters cldfb_mod_mat = exp(1i*((0:N-1)'-D/2) * ((0:L-1)+0.5) * pi/L ); % N x L modulation matrix with alternating sign periodicity of 2*L in time (row) direction cldfb_ansyn_filters = pFilt .* cldfb_mod_mat; % all (L) analysis/synthesis filters target_fir = target_fir(:); fir_length = length(target_fir); %% brute force least squares if legacy_flag num_slots = ceil(fir_length/S) + 10; % used to generate tested IVAS coefficients in CLDFB HRIR ROM with slightly too short buffers inp_len = S * num_slots; len = inp_len - cldfb_delay; idx_opt = cldfb_delay + (1:len); pos_offset = 200; else num_slots = ceil((fir_length + 2 * N - 1) / S); % convolution with target fir, analysis + synthesis filters (of length N each) len = S * num_slots; inp_len = len; idx_opt = (1:len); % use all output samples in the optimization, including the filter bank delay pos_offset = 0; end % Big trial and error Nbins = num_cldfb_taps * L; Vreal = zeros(S*len, Nbins); % all real for all shifts concatenated for each mask bin, Vimag = zeros(S*len, Nbins); % then all imag r = zeros(S*len,1); % right hand side all shifts concatenated x = zeros(inp_len,1); for pos = 1:S % input dirac pulse x(:) = 0; % target impulse response y0 for input pulse at pos out = x; out(pos+pos_offset+(0:fir_length-1)) = target_fir; y0 = [zeros(cldfb_delay,1);out(1:end-cldfb_delay)]; r(len*(pos-1)+(1:len)) = y0(idx_opt); fprintf('.'); % CLDFB analysis for impulse input x(pos+pos_offset) = 1; X = zeros(L,num_slots); frm_idx = (1:N)'; x_tmp = [zeros(N-S,1);x]; for slt = 1:num_slots X(:,slt) = sum(cldfb_ansyn_filters.*flipud(x_tmp(frm_idx))).'; frm_idx = frm_idx + S; end for cldfb_bin = 1:Nbins cldfb_band = rem(cldfb_bin-1,L)+1; cldfb_lag = fix((cldfb_bin-1)/L); % real-valued filter contribution of a single tap frm_idx = cldfb_lag * S + (1:N)'; y_tmp = zeros( num_slots * S + N - S,1); for slt = cldfb_lag+1:num_slots y_tmp(frm_idx) = y_tmp(frm_idx) + real(X(cldfb_band,slt-cldfb_lag) * cldfb_ansyn_filters(:,cldfb_band)); frm_idx = frm_idx + S; end Vreal(len*(pos-1)+1:len*pos,cldfb_bin) = y_tmp(idx_opt); % imaginary-valued filter contribution of a single tap frm_idx = cldfb_lag * S + (1:N)'; y_tmp(:) = 0; for slt = cldfb_lag+1:num_slots y_tmp(frm_idx) = y_tmp(frm_idx) + real(1i*X(cldfb_band,slt-cldfb_lag) * cldfb_ansyn_filters(:,cldfb_band)); frm_idx = frm_idx + S; end Vimag(len*(pos-1)+1:len*pos,cldfb_bin) = y_tmp(idx_opt); end % bin end % pos fprintf('\n'); % solve lsq V = [Vreal,Vimag]; M = V'*V; Mrel = M + 1e-8*norm(M)*eye(size(M)); c = Mrel\(V'*r); % map back to mask locations F = zeros(L,num_cldfb_taps); for cldfb_bin = 1:Nbins F(cldfb_bin) = c(cldfb_bin)+1i*c(Nbins+cldfb_bin); end if eval_flag % evaluation (not needed for table generation, needs signal processing toolbox) get_snr( target_fir, F, cldfb_ansyn_filters, cldfb_delay, S ); end function get_snr( target_fir, cldfb_firs, cldfb_ansyn_filters, cldfb_delay, S ) rng(0); num_samples = 5*48000; x = randn(num_samples,1); % Filter noise in time domain y1 = fftfilt(target_fir,x); % Filter noise in CLDFB domain [N,L] = size(cldfb_ansyn_filters); num_slots = ceil((num_samples + cldfb_delay) / S); % analysis X = zeros(L,num_slots); frm_idx = (1:N)'; x_tmp = [zeros(N-S,1);x;zeros(cldfb_delay,1)]; for slt = 1:num_slots X(:,slt) = sum(cldfb_ansyn_filters.*flipud(x_tmp(frm_idx))).'; frm_idx = frm_idx + S; end % filter X = fftfilt(cldfb_firs.',X.').'; % synthesis frm_idx = (1:N)'; y_tmp = zeros( num_slots * S + N - S,1); for slt = 1:num_slots y_tmp(frm_idx) = y_tmp(frm_idx) + real(sum(X(:,slt).' .* cldfb_ansyn_filters,2)); frm_idx = frm_idx + S; end y2 = y_tmp(cldfb_delay+(1:num_samples)); plot(y1), hold on, plot(y2,'r--'), plot(y1-y2,'k'), hold off set(gca,'xlim',[1,num_samples]) fprintf('SNR: %.1f dB \n', 10*log10(sum(y1.^2)/sum((y1-y2).^2))); legend('time domain','CLDFB domain','difference') title('Filtered noise') function [h, D, S, L] = get_cldfb_filter() % const float LDQMF_60[] in \lib_com\rom_com.c, line 5219 S = 60; % stride L = 60; % frequency bands D = 240 + S - 1; % system delay h = [ 0.0000953076, 0.0001230230, 0.0001279964, 0.0001260533, 0.0001211219 0.0001122123, 0.0001010860, 0.0000876540, 0.0000719883, 0.0000545472 0.0000352143, 0.0000145686, -0.0000074264, -0.0000303788, -0.0000539205 -0.0000782743, -0.0001028838, -0.0001275374, -0.0001520015, -0.0001760167 -0.0001997108, -0.0002226708, -0.0002446725, -0.0002655797, -0.0002852145 -0.0003034996, -0.0003203036, -0.0003356283, -0.0003493345, -0.0003614030 -0.0003719004, -0.0003807641, -0.0003881051, -0.0003939842, -0.0003985357 -0.0004019095, -0.0004041938, -0.0004056677, -0.0004065430, -0.0004069925 -0.0004072535, -0.0004075877, -0.0004083676, -0.0004098394, -0.0004122990 -0.0004160839, -0.0004214063, -0.0004285777, -0.0004378651, -0.0004495422 -0.0004637682, -0.0004806494, -0.0005003878, -0.0005231378, -0.0005489803 -0.0005777747, -0.0006095612, -0.0006443121, -0.0006813223, -0.0007226231 -0.0007722576, -0.0008268412, -0.0008839625, -0.0009417336, -0.0010004630 -0.0010601623, -0.0011206097, -0.0011817788, -0.0012432419, -0.0013045983 -0.0013656860, -0.0014260965, -0.0014855355, -0.0015435946, -0.0015999591 -0.0016543545, -0.0017062968, -0.0017554691, -0.0018015467, -0.0018441341 -0.0018829798, -0.0019177221, -0.0019480695, -0.0019736972, -0.0019943134 -0.0020097434, -0.0020197174, -0.0020240925, -0.0020226294, -0.0020152442 -0.0020017736, -0.0019820682, -0.0019561697, -0.0019240153, -0.0018855907 -0.0018409232, -0.0017900462, -0.0017330211, -0.0016699535, -0.0016009507 -0.0015261442, -0.0014456788, -0.0013597424, -0.0012685407, -0.0011722331 -0.0010710671, -0.0009652392, -0.0008549765, -0.0007405236, -0.0006221444 -0.0005001140, -0.0003745670, -0.0002458634, -0.0001142541, 0.0000199491 0.0001564174, 0.0002949402, 0.0004350246, 0.0005769439, 0.0007203126 -0.0008803223, -0.0010328424, -0.0011841310, -0.0013346316, -0.0014848098 -0.0016343417, -0.0017832819, -0.0019316213, -0.0020790498, -0.0022252349 -0.0023701149, -0.0025136294, -0.0026556554, -0.0027960713, -0.0029348312 -0.0030717771, -0.0032068293, -0.0033399195, -0.0034709862, -0.0035999804 -0.0037267797, -0.0038513308, -0.0039736414, -0.0040935921, -0.0042111278 -0.0043262239, -0.0044388464, -0.0045489701, -0.0046565188, -0.0047614835 -0.0048637423, -0.0049632201, -0.0050599808, -0.0051539382, -0.0052450863 -0.0053333500, -0.0054187514, -0.0055012843, -0.0055808770, -0.0056575472 -0.0057313135, -0.0058021732, -0.0058701355, -0.0059352517, -0.0059975707 -0.0060571772, -0.0061141332, -0.0061685541, -0.0062205540, -0.0062703062 -0.0063179093, -0.0063635921, -0.0064075105, -0.0064498796, -0.0064908965 -0.0065308069, -0.0065698619, -0.0066083665, -0.0066466411, -0.0066849431 -0.0067233290, -0.0067621553, -0.0068021296, -0.0068436749, -0.0068870094 -0.0069324085, -0.0069801519, -0.0070305937, -0.0070840055, -0.0071406048 -0.0072006541, -0.0072644479, -0.0073321410, -0.0074039386, -0.0074799177 -0.0075602704, -0.0076450342, -0.0077342330, -0.0078278277, -0.0079257628 -0.0080279401, -0.0081341872, -0.0082442267, -0.0083577875, -0.0084744738 -0.0085938899, -0.0087156557, -0.0088391500, -0.0089637861, -0.0090888245 -0.0092134504, -0.0093367994, -0.0094579896, -0.0095760096, -0.0096898535 -0.0097982995, -0.0099003557, -0.0099947909, -0.0100801717, -0.0101551116 -0.0102182031, -0.0102678994, -0.0103026126, -0.0103207529, -0.0103206923 -0.0103006857, -0.0102590285, -0.0101939747, -0.0101036867, -0.0099863587 -0.0098401112, -0.0096632261, -0.0094537362, -0.0092098210, -0.0089295702 -0.0086111929, -0.0082527259, -0.0078523541, -0.0074084769, -0.0069190590 0.0063841688, 0.0057985946, 0.0051621343, 0.0044734711, 0.0037309236 0.0029329660, 0.0020781513, 0.0011651339, 0.0001925042, -0.0008409545 -0.0019364181, -0.0030950012, -0.0043176264, -0.0056051607, -0.0069584334 -0.0083780792, -0.0098646237, -0.0114185056, -0.0130400723, -0.0147295250 -0.0164868534, -0.0183120724, -0.0202049762, -0.0221651513, -0.0241921283 -0.0262852497, -0.0284437388, -0.0306666382, -0.0329528190, -0.0353010744 -0.0377098918, -0.0401776619, -0.0427025780, -0.0452826768, -0.0479161367 -0.0506004691, -0.0533332452, -0.0561118126, -0.0589331910, -0.0617944039 -0.0646922663, -0.0676232576, -0.0705836788, -0.0735698044, -0.0765774846 -0.0796026587, -0.0826408863, -0.0856874809, -0.0887378305, -0.0917868018 -0.0948293805, -0.0978601947, -0.1008738130, -0.1038645208, -0.1068264544 -0.1097536832, -0.1126400903, -0.1154794544, -0.1182654947, -0.1209914312 -0.1236500666, -0.1262341589, -0.1287376434, -0.1311538219, -0.1334753036 -0.1356947273, -0.1378047168, -0.1397978216, -0.1416664869, -0.1434033662 -0.1450008005, -0.1464512348, -0.1477471888, -0.1488809884, -0.1498452872 -0.1506324410, -0.1512351334, -0.1516460329, -0.1518578976, -0.1518635303 -0.1516559124, -0.1512281001, -0.1505732536, -0.1496847868, -0.1485562176 -0.1471813470, -0.1455538720, -0.1436681300, -0.1415183097, -0.1390990764 -0.1364052594, -0.1334318966, -0.1301742792, -0.1266280264, -0.1227891371 -0.1186537445, -0.1142183766, -0.1094799563, -0.1044355705, -0.0990828425 -0.0934195668, -0.0874440819, -0.0811550021, -0.0745511875, -0.0676321834 -0.0603975877, -0.0528475679, -0.0449828543, -0.0368040986, -0.0283128861 -0.0195108838, -0.0104003223, -0.0009837818, 0.0087356847, 0.0187546927 0.0290693250, 0.0396753438, 0.0505682528, 0.0617432520, 0.0731955394 -0.0849232078, -0.0969054326, -0.1091460735, -0.1216373071, -0.1343720406 -0.1473424733, -0.1605402082, -0.1739567965, -0.1875831038, -0.2014097124 -0.2154271752, -0.2296251506, -0.2439934313, -0.2585212290, -0.2731975317 -0.2880111337, -0.3029502928, -0.3180032372, -0.3331578076, -0.3484017253 -0.3637222052, -0.3791064322, -0.3945416212, -0.4100143015, -0.4255111217 -0.4410185516, -0.4565227628, -0.4720100164, -0.4874662757, -0.5028775334 -0.5182296634, -0.5335084200, -0.5486994982, -0.5637886524, -0.5787616372 -0.5936041474, -0.6083019376, -0.6228409410, -0.6372069120, -0.6513859630 -0.6653640866, -0.6791275144, -0.6926627755, -0.7059561610, -0.7189947963 -0.7317654490, -0.7442554235, -0.7564523220, -0.7683438063, -0.7799182534 -0.7911639810, -0.8020697832, -0.8126249313, -0.8228194118, -0.8326428533 -0.8420860767, -0.8511404991, -0.8597975969, -0.8680517077, -0.8758881092 -0.8832823634, -0.8902196884, -0.8967157602, -0.9027729034, -0.9083824754 -0.9135394692, -0.9182395935, -0.9224776030, -0.9262499809, -0.9295535684 -0.9323854446, -0.9347436428, -0.9366261959, -0.9380323887, -0.9389615655 -0.9394137263, -0.9393896461, -0.9388904572, -0.9379178882, -0.9364743829 -0.9345626831, -0.9321863055, -0.9293491840, -0.9260557890, -0.9223110080 -0.9181203246, -0.9134896994, -0.9084255695, -0.9029349089, -0.8970250487 -0.8907034993, -0.8839784265, -0.8768582940, -0.8693521619, -0.8614694476 -0.8532197475, -0.8446131349, -0.8356599212, -0.8263708353, -0.8167568445 -0.8068289757, -0.7965991497, -0.7860788107, -0.7752800584, -0.7642148733 -0.7528960109, -0.7413358092, -0.7295469642, -0.7175422311, -0.7053351402 -0.6929380894, -0.6803644896, -0.6676273942, -0.6547405124, -0.6417166591 -0.6285686493, -0.6153115034, -0.6019562483, -0.5885198116, -0.5750215650 0.5615197420, 0.5478612781, 0.5341838002, 0.5204906464, 0.5067980289 0.4931168854, 0.4794588387, 0.4658361673, 0.4522601366, 0.4387422502 0.4252935350, 0.4119254053, 0.3986486793, 0.3854739666, 0.3724119067 0.3594728410, 0.3466667533, 0.3340034485, 0.3214924335, 0.3091430366 0.2969639599, 0.2849639654, 0.2731511295, 0.2615332901, 0.2501178682 0.2389119864, 0.2279221565, 0.2171545923, 0.2066148520, 0.1963084787 0.1862401515, 0.1764142811, 0.1668347418, 0.1575049609, 0.1484276950 0.1396053135, 0.1310400218, 0.1227331311, 0.1146853194, 0.1068974212 0.0993694067, 0.0921007246, 0.0850901082, 0.0783365741, 0.0718384907 0.0655927584, 0.0595967993, 0.0538481586, 0.0483424664, 0.0430756323 0.0380428955, 0.0332404599, 0.0286619961, 0.0242999699, 0.0201510899 0.0162059069, 0.0124559226, 0.0088928537, 0.0054926532, 0.0023052765 -0.0005515143, -0.0030201224, -0.0052712574, -0.0073737046, -0.0093160523 -0.0111072771, -0.0127562135, -0.0142635731, -0.0156361461, -0.0168790054 -0.0179969221, -0.0189934950, -0.0198726747, -0.0206398536, -0.0212980714 -0.0218509119, -0.0223025978, -0.0226570386, -0.0229178313, -0.0230882075 -0.0231725387, -0.0231746566, -0.0230979007, -0.0229462404, -0.0227237809 -0.0224345829, -0.0220820960, -0.0216706358, -0.0212045144, -0.0206875466 -0.0201238506, -0.0195175279, -0.0188730918, -0.0181944817, -0.0174855441 -0.0167510118, -0.0159947462, -0.0152208358, -0.0144332750, -0.0136361914 -0.0128338682, -0.0120294262, -0.0112272501, -0.0104311826, -0.0096443929 -0.0088709844, -0.0081134979, -0.0073764324, -0.0066623385, -0.0059733889 -0.0053142183, -0.0046856776, -0.0040914025, -0.0035321070, -0.0030089030 -0.0025271603, -0.0020749648, -0.0016621647, -0.0012705614, -0.0008115423 ]; h = h.'; h = h(:); h(1*120+(1:120)) = -h(1*120+(1:120)); h(3*120+(1:120)) = -h(3*120+(1:120)); h(:) = h(end:-1:1); No newline at end of file