% Simulation of loudspeaker A with ZOH D/A
% 100 realizations, 25 scales

%   CBVK Toolbox
%   JHMG, PMSB 2016

clear

arqin='inputs_r100';
%arqout='../../../mat/LoudspeakerA/outputs_Azoh_r100_s25';
arqout='outputs_Azoh_r100_s25';
diff='diffloudAzoh';
                       
scales=10.^([-20:1.5:10 12.5:2.5:20]/20);
fs=5000;

R=100;                  % Number of realizations
ini=1;                  % Initial realization
x0 = [0;0;0];           % Initial state
                        
%---------------------------------------

[f,g,h]=dloudA_com;      % Regenerate model to be sure

if matlabpool('size')==0
  matlabpool
end

S=length(scales);


outputs_r = cell(S,1);
outputs = cell(R,1);

for s=1:S
    PP{s}.s = 0;
end

P0.fs=fs;


load(arqin);

inisim=tic;

% Realization loop 
for r=ini:ini+R-1
    
    u0 = inputs{r};    
    tspan=(0:length(u0)-1)/fs;  % Realization lengths may differ
    P0.u = u0;
  
    % Scale loop
    parfor s=1:S
        PP{s}.s = scales(s);
        y = odedev(diff,P0,PP{s},h,tspan,x0);    
        outputs_r{s} = y;        
        
        disp(['Run = ' num2str(r) ' : Scale ' num2str(s) ' computed.']);
    end;
    
    outputs{r} = outputs_r;
    
    save(arqout,'outputs','scales' );
    
    %disp(['>>>>>> Run ' num2str(tmc) ' took ' num2str(toc/60) ...
     % ' minutes <<<<<<<'])
end;

tsim=toc(inisim);

%save tsim_Azoh_r100_s25 tsim


