function convertBFormToStereoUHJ(filename)
% read in B-format IR and convert to stereo UHJ
% https://wiki.xiph.org/Ambisonics
% http://pcfarina.eng.unipr.it/Aurora/B-Format_to_UHJ.htm
% http://www.dsprelated.com/showthread/matlab/5198-1.php

% Read in wavfile specified by filename
[WXYZ,fs] = audioread(filename);

% separate into spearate channels
W = WXYZ(:,1);
X = WXYZ(:,2);
Y = WXYZ(:,3);

% get numel for fft
N = numel(W);

% create p and j
p = 90*pi/180;
j = cos(p) + 1i * sin(p);

L = ifft(0.5*(0.9397.*fft(W,N)+ 0.18568.*fft(X,N) - j.*0.342.*fft(W,N) + j.*0.5099.*fft(X,N) + 0.655.*fft(Y,N)));
R = ifft(0.5*(0.9397.*fft(W,N)+ 0.18568.*fft(X,N) + j.*0.342.*fft(W,N) - j.*0.5099.*fft(X,N) - 0.655.*fft(Y,N)));
 
% make stereo output
stereoOut = real([L R]);

% write to wav
filenameOut = strcat('StereoUHJ_',filename);

audiowrite(filenameOut,stereoOut,fs);

end 