Skip to content

Commit

Permalink
fixes bug in hrtf interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcht67 committed Jun 22, 2024
1 parent bc26bfe commit 46fedf5
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 323 deletions.
36 changes: 34 additions & 2 deletions Matlab/MCA_Interpolation/MCAInterpolation.asv
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
% COORDINATE STATES

% 1. HUTUBS SOFA FILE (azim: [0, 360], elev [-90, 90])
% 2. sparseHRIRdataset_SOFA (azim: [0, 360], elev [-90, 90]) [retrieved by sparseHRIRdataset.SourcePosition]
% 3. hrtfDataset (azim: [0, 360], elev [0, 180]) [retrieved by hrtfDataset.samplingGrid]
% 4. denseSamplingGrid (azim: [0, 360], elev [0, 180])
% 5. interpHRTF (azim: [0, 360], elev [0, 180]) [retrieved by interpHRTF.samplingGrid]
% 6. SOFAobj (azim: [0, 360], elev [-90, 90]) [retrieved by SOFAobj.SourcePosition]


% CONCLUSIONS

% supdeq_sofa2hrtf transforms elevation to colatitude
% Code Snippet:
% 0110 %Transform samplingGrid to SH coordinate system
% 0111 samplingGrid(:,1) = mod(samplingGrid(:,1),360);
% 0112 samplingGrid(:,2) = 90-samplingGrid(:,2);

% supdeq_writeSOFAobj re-transforms colatitude to elevation
% Code Snippet:
% 0091 %Transform grid to SOFA format
% 0092 samplingGrid(:,2) = 90-samplingGrid(:,2);

% Get new sampling grid
N = 44;
denseSamplingGrid = supdeq_lebedev([], N);
denseSamplingGrid(:,3) = [];

denseSamplingGrid = supdeq_g


% 1. import hrirs
sparseHRIRdataset_SOFA = SOFAload('pp2_HRIRs_measured.sofa');
hrtfDataset = supdeq_sofa2hrtf(sparseHRIRdataset_SOFA, N);
sparseSamplingGrid = hrtfDataset.samplingGrid;

% MCA Interpolation
headRadius = 0.0875;

interpHRTF_sh = supdeq_interpHRTF(hrtfDataset, denseSamplingGrid,'None','SH', nan, headRadius);
interpHRTF_sh = supdeq_interpHRTF(hrtfDataset, sparseSamplingGrid,'None','SH', nan, headRadius);
interpHRTF_sh_timealign = supdeq_interpHRTF(hrtfDataset, denseSamplingGrid,'None','SH', nan, headRadius);
%interpHRTF_mca_limited = supdeq_interpHRTF(hrtfDataset, denseSamplingGrid,'SUpDEq','SH', inf, headRadius);
interpHRTF_mca = supdeq_interpHRTF(hrtfDataset, denseSamplingGrid,'SUpDEq','SH', inf, headRadius);

% Write SOFA file
fs = 44100;
earDistance = 0.165;
sourceDistance = 1.47;

% adjust sampling grid to SOFA azimuth (-90,90)
%denseSamplingGrid(:,2) = denseSamplingGrid(:,2)-90;

HRIR_L = permute(interpHRTF_sh.HRIR_L,[2 1]);
HRIR_R = permute(interpHRTF_sh.HRIR_R,[2 1]);
SOFAobj = supdeq_writeSOFAobj(HRIR_L, HRIR_R, denseSamplingGrid, fs, earDistance, sourceDistance);
SOFAobj = supdeq_writeSOFAobj(HRIR_L, HRIR_R, sparseSamplingGrid, fs, earDistance, sourceDistance);
SOFAsave('pp2_HRIRs_interpolated_sh.sofa', SOFAobj);

HRIR_L = permute(interpHRTF_sh_timealign.HRIR_L,[2 1]);
Expand Down
33 changes: 28 additions & 5 deletions Matlab/MCA_Interpolation/MCAInterpolation.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
% COORDINATE STATES

% 1. HUTUBS SOFA FILE (azim: [0, 360], elev [-90, 90])
% 2. sparseHRIRdataset_SOFA (azim: [0, 360], elev [-90, 90]) [retrieved by sparseHRIRdataset.SourcePosition]
% 3. hrtfDataset (azim: [0, 360], elev [0, 180]) [retrieved by hrtfDataset.samplingGrid]
% 4. denseSamplingGrid (azim: [0, 360], elev [0, 180])
% 5. interpHRTF (azim: [0, 360], elev [0, 180]) [retrieved by interpHRTF.samplingGrid]
% 6. SOFAobj (azim: [0, 360], elev [-90, 90]) [retrieved by SOFAobj.SourcePosition]


% CONCLUSIONS

% supdeq_sofa2hrtf transforms elevation to colatitude
% Code Snippet:
% 0110 %Transform samplingGrid to SH coordinate system
% 0111 samplingGrid(:,1) = mod(samplingGrid(:,1),360);
% 0112 samplingGrid(:,2) = 90-samplingGrid(:,2);

% supdeq_writeSOFAobj re-transforms colatitude to elevation
% Code Snippet:
% 0091 %Transform grid to SOFA format
% 0092 samplingGrid(:,2) = 90-samplingGrid(:,2);

% Get new sampling grid
N = 44;
denseSamplingGrid = supdeq_lebedev([], N);
denseSamplingGrid(:,3) = [];

% remove weights
%denseSamplingGrid(:,3) = [1];

% 1. import hrirs
sparseHRIRdataset_SOFA = SOFAload('pp2_HRIRs_measured.sofa');
hrtfDataset = supdeq_sofa2hrtf(sparseHRIRdataset_SOFA, N);
hrtfDataset = supdeq_sofa2hrtf(sparseHRIRdataset_SOFA, 12);
sparseSamplingGrid = hrtfDataset.samplingGrid;

% MCA Interpolation
headRadius = 0.0875;
Expand All @@ -20,9 +46,6 @@
earDistance = 0.165;
sourceDistance = 1.47;

% adjust sampling grid to SOFA azimuth (-90,90)
%denseSamplingGrid(:,2) = denseSamplingGrid(:,2)-90;

HRIR_L = permute(interpHRTF_sh.HRIR_L,[2 1]);
HRIR_R = permute(interpHRTF_sh.HRIR_R,[2 1]);
SOFAobj = supdeq_writeSOFAobj(HRIR_L, HRIR_R, denseSamplingGrid, fs, earDistance, sourceDistance);
Expand Down
Binary file modified Matlab/MCA_Interpolation/pp2_HRIRs_interpolated_mca.sofa
Binary file not shown.
Binary file modified Matlab/MCA_Interpolation/pp2_HRIRs_interpolated_sh.sofa
Binary file not shown.
Binary file not shown.
Binary file removed assets/pp2_HRIRs_interpolated_mca.sofa
Binary file not shown.
Binary file removed assets/pp2_HRIRs_interpolated_mca_time_aligned.sofa
Binary file not shown.
Binary file not shown.
Binary file removed assets/pp2_HRIRs_interpolated_sh.sofa
Binary file not shown.
Binary file modified assets/pp2_HRIRs_interpolated_sh_time_aligned.sofa
Binary file not shown.
Binary file removed assets/pp2_HRIRs_interpolated_sh_timealign.sofa
Binary file not shown.
Binary file modified assets/pp2_HRIRs_interpolated_sh_timealign_time_aligned.sofa
Binary file not shown.
Binary file removed assets/pp2_HRIRs_measured.sofa
Binary file not shown.
Binary file removed assets/pp2_HRIRs_measured_interpolated.sofa
Binary file not shown.
Binary file modified assets/pp2_HRIRs_measured_time_aligned.sofa
Binary file not shown.
51 changes: 32 additions & 19 deletions python/inspect_sofa_files.ipynb

Large diffs are not rendered by default.

64 changes: 51 additions & 13 deletions python/sofaTransformCoord.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 1,
"id": "c3665469-1050-44fe-8fb6-3034c12c1e36",
"metadata": {},
"outputs": [],
Expand All @@ -29,7 +29,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 2,
"id": "54b8817c-19e2-4b00-a750-d85688778026",
"metadata": {},
"outputs": [],
Expand All @@ -42,15 +42,15 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 3,
"id": "7cb6c188-f063-4e33-a372-5696670d97d7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0 180.0\n"
"-90.0 90.0\n"
]
}
],
Expand All @@ -61,7 +61,26 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 4,
"id": "b4a4709e-f04b-4743-8f7a-8bb5cfa644f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0 350.0\n"
]
}
],
"source": [
"azimuths = [coord[0] for coord in sofa_measured.SourcePosition]\n",
"print(np.min(azimuths), np.max(azimuths))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9b2ec01d-2ce6-4e6e-b4ac-64a9143319dc",
"metadata": {},
"outputs": [
Expand All @@ -80,41 +99,60 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 9,
"id": "3d36fc0b-8194-42ec-8db8-3715b74eefcf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0 358.81618306613007\n"
]
}
],
"source": [
"azimuths = [coord[0] for coord in sofa_interpolated_mca.SourcePosition]\n",
"print(np.min(azimuths), np.max(azimuths))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7bdfb7ee-a11e-4bc7-bd42-1cf3c23a98c0",
"metadata": {},
"outputs": [],
"source": [
"sofa_measured.SourcePosition = [ [ coord[0], coord[1] + 90, coord[2] ] for coord in sofa_measured.SourcePosition]"
"#sofa_measured.SourcePosition = [ [ coord[0], coord[1] + 90, coord[2] ] for coord in sofa_measured.SourcePosition]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 7,
"id": "d7575700-075f-4884-b528-5e6aa874e4f5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0 180.0\n"
"-90.0 90.0\n"
]
}
],
"source": [
"elevations = [coord[1] for coord in sofa_measured.SourcePosition]\n",
"print(np.min(elevations), np.max(elevations))"
"#elevations = [coord[1] for coord in sofa_measured.SourcePosition]\n",
"#print(np.min(elevations), np.max(elevations))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 8,
"id": "544e2fc9-5864-4d2a-8b37-29670387bd1e",
"metadata": {},
"outputs": [],
"source": [
"sf.write_sofa(sofa_measured_path, sofa_measured)"
"#sf.write_sofa(sofa_measured_path, sofa_measured)"
]
},
{
Expand Down
Loading

0 comments on commit 46fedf5

Please sign in to comment.