% /// ASAR-MARA Research Group
% 
% Cologne University of Applied Sciences
% Technical University of Berlin
% University of Rostock
% Deutsche Telekom Laboratories
% WDR Westdeutscher Rundfunk
% IOSONO GmbH
% 
% SOFiA sound field analysis
% 
% sofia_mtxToGIXEL: MTXdata to GIXEL viewer file format, R13-01-05
%
% Copyright (C)2013 by bBrn - benjamin Bernschütz   
%                             rockzentrale 'AT' me.com
%                             +49 171 4176069 Germany  
% 
% This file is part of the SOFiA toolbox under GNU General Public License
% 
%  
% [] = sofia_mtxToGIXEL(mtxData, fileName, comment, compress)
% ------------------------------------------------------------------------     
% mtxData   SOFiA 3D-matrix-data in 1° steps, generated by makeMTX()           
% fileName  Name of the produced output file [string]
% comment   Comment shown in the GIXEL viewer window [string]
% compress  Compression Base: Reduction of the color scale dynamics.
%
% The function generates a csv file that is readable by the GIXEL viewer. 
% Download and information: http://www.audiogroup.web.fh-koeln.de


% CONTACT AND LICENSE INFORMATION:
%
% /// ASAR Research Group 
%  
%     [1] Cologne University of Applied Sciences
%     [2] Technical University of Berlin 
%     [3] University of Rostock 
%     [4] WDR Westdeutscher Rundfunk 
%     [5] Deutsche Telekom Laboratories 
%
% SOFiA sound field analysis
%
% Copyright (C)2013 bBrn - benjamin Bernschütz [1,2] et al.(§)   
%
% Contact ------------------------------------
% Cologne University of Applied Sciences 
% Institute of Communication Systems
% Betzdorfer Street 2
% D-50679 Germany (Europe)
%
% phone       +49 221 8275 -2496 
% cell phone  +49 171 4176069 
% mail        rockzentrale 'at' me.com 
% --------------------------------------------
% 
% This file is part of the SOFiA sound field analysis toolbox
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% 
%
% (§) Christoph Pörschmann [1]   christoph.poerschmann 'at' fh-koeln.de
%     Sascha Spors         [3]   sascha.spors 'at' uni-rostock.de  
%     Stefan Weinzierl     [2]   stefan.weinzierl 'at' tu-berlin.de


function sofia_mtxToGIXEL(mtxData, fileName, comment, compress)

if isempty(strfind(fileName,'.csv'))
   fileName=[fileName,'.csv'];    
end

if nargin < 4
 compress = 3;
end

disp('SOFiA MTX to GIXEL file R13-01-05');

compressionScale = compress^(log10(max(max(abs(mtxData)))));

mtxData = abs(mtxData)./max(max(abs(mtxData)));
mtxData = abs(mtxData)*compressionScale;

fieldSizeV = size(mtxData,1);
fieldSizeH = size(mtxData,2);

newlinetype = 'pc';

imageResolution = [num2str(fieldSizeH),'X',num2str(fieldSizeV)];

dlmwrite(fileName, imageResolution, 'delimiter','','newline', newlinetype)

if nargin > 2
   dlmwrite(fileName, comment,'delimiter','','-append','newline', newlinetype)  
else
   dlmwrite(fileName, 'SOFiA Sound Field Analysis','delimiter','','-append','newline', newlinetype)  
end

dlmwrite(fileName,mtxData,'delimiter',',','-append', 'precision', '%7.6f','newline', newlinetype)
