% WFS

files = { ...
    'wfs_positioning_m2.wav'; ...
    'wfs_positioning_m1.wav'; ...
    'wfs_reference.wav'; ...
    'wfs_positioning_p1.wav'; ...
    'wfs_positioning_p2.wav'; ...
};
outfiles = { ...
	'01-positioning_foreground_m2_dB.csv'; ...
 	'02-positioning_foreground_m1_dB.csv'; ...
	'03-positioning_foreground_ref_dB.csv'; ...
	'04-positioning_foreground_p1_dB.csv'; ...
	'05-positioning_foreground_p2_dB.csv'; ...
};
% directions of the loudspeakers
ls_directions = pi/2:-(2*pi)/56:-(2*pi)+pi/2+(2*pi)/56;

for ii=1:length(files)
    sig = audioread(files{ii});
    % Switch signal to correct order
    sig(:,2:56) = sig(:,56:-1:2);
    db_values(ii,:) = db(rms(sig(:,1:56)));
end

% Normalize
db_values = db_values - max(db_values(:));

for ii=1:length(outfiles)
    fid = fopen(outfiles{ii},'w');
    fprintf(fid,'# Loudspeaker ID, phi / rad, magnitude / dB\n');
    fclose(fid);
    dlmwrite(outfiles{ii},[[1:56]' ls_directions' db_values(ii,:)'],'delimiter',',','-append');
end
