%====================================================
%  MB2000 SENSOR
%
%****************************************************
%   Fonctions de transfert Microbaromètre CTBT
%****************************************************
% filtre passe bas de démodulation
bdm = [0 0 (2*pi*40)^2];
adm = [1 sqrt(2)*2*pi*40 (2*pi*40)^2];
% filtre passe haut
bph = [1 0];
aph = [1 2*pi/100];
% filtre passe bas de sortie
bpb = [0 2*pi*33];
apb = [1 2*pi*33];
pdm = roots(adm);
zdm = roots(bdm);
pph = roots(aph);
zph = roots(bph);
ppb = roots(apb);
zpb = roots(bpb);
polesMB = [pdm;pph;ppb];
zerosMB = [zdm;zph;zpb];
A0MB = 0.02*(2^24)/32;
A0MB = 0.02; % V/Pa

%*********************************************
% tracé de la réponse des MB2000
fmin = 3*20/4096;
df   = 20/4096;
fmax = 10;
f = (fmin:df:fmax);
HMB = freqs(poly(zerosMB),poly(polesMB),f*2*pi);
HMB20 = freqs(poly(zerosMB),poly(polesMB),[pi/10 pi/5]);
AMB10 = abs(HMB20(2)); % facteur de normalisation
mgnMB = A0MB.*abs(HMB)./AMB10; % en V/Pa
phaseMB = (180/pi).*unwrap(angle(HMB)); % en degrés
% tracé des résultats 
figure
subplot(2,1,1)
loglog(f,mgnMB.*1);
grid
axis([fmin fmax 0.1 50]);
hold off
title('MB2000 Pressure response');
ylabel('mV/Pa');
subplot(2,1,2)
semilogx(f,phaseMB);
grid
axis([fmin fmax -180 90]);
title('Phase response');
xlabel('Frequency Hz');
ylabel('deg');

%********************************************************
% Création du fichier fap MB2000
%
fid = fopen(strcat('MB2000.fap'),'w');
fprintf(fid,'# MB2000 theoretical response \n');
fprintf(fid,'# in V/Pa \n');
fprintf(fid,'#  \n');
fprintf(fid,'theoretical  0	instrument	fap	Derivation from PAZ -DASE-DIG \n');
fprintf(fid,'#Number of FAP \n');
n = length(mgnMB);
fprintf(fid,'%d \n',n);
fprintf(fid,'#Frequency \t');
fprintf(fid,'Amplitude \t');
fprintf(fid,'Phase \t \t');
fprintf(fid,'AmEr \t');
fprintf(fid,'PhEr \n');

for i=1:n,
fprintf(fid,'%f \t',f(i));
fprintf(fid,'%f \t',mgnMB(i));
fprintf(fid,'%f \t',phaseMB(i));
fprintf(fid,'%1.1f \t',0);
fprintf(fid,'%1.1f \n',0);
end

fclose(fid);





