I am working on a project that uses the tangent law to localize a phantom source in the typical stereo setup (two loudspeakers at 60º angle). Since the tangent law shows a relation between the angle of the phantom image and the gain of the loudspeakers, my aim is to plot the amplitude response in dB of one loudspeaker at the different angles of the phantom source, here is the code that works fine with the source positioning:
%-----stereo panning------%
%variables:
Fs = 44100;
theta = 25; %virtual source
phi = 30; %loudspeaker
g(1) = 1; %L gain
g(2) = -(tan(theta)-tan(phi)) / (tan(theta)+tan(phi));
%sum of gains has to be normalized
g = g/sqrt(sum(g.^2));
signal=mod([1:20000]',200)/200; %signal
loudsp_sig=[signal*g(1) signal*g(2)]; %panning
soundsc(loudsp_sig,Fs); %play audio
I tried creating a theta vector and tried plotting (theta, 20*log(g(2)), also using table but either it gives me an error or plots only one point, I'm clearly missing something here.
g(2)
can only be one value. It is a single index. A vector cannot be stored in one memory location. If theta
is a vector then the resulting expression
tan θ – tan ϕ
– ——————————————
tan θ + tan ϕ
should also be a vector calculated for each element in theta
and therefore element-wise division will be needed (as pointed out by Wolfie).
So, use the same number of indices as required by theta
to store all the gains g
:
g(2:numel(theta)+1) = -(tan(theta)-tan(phi)) ./ (tan(theta)+tan(phi));
Also note that the tan
function calculates the tangent of argument in radians. If you want to put the angle in degrees, use tand
i.e.
g(2:numel(theta)+1) = -(tand(theta)-tand(phi)) ./ (tand(theta)+tand(phi));