CAPÍTULO 9 APÉNDICESbibing.us.es/proyectos/abreproy/11191/fichero/Capitulo+9.pdf · CAPÍTULO 9...

22
CAPÍTULO 9 Apéndices 165 CAPÍTULO 9 APÉNDICES En este capítulo se proporciona el código del programa MatLab implementado para estimar el valor de los parámetros de diseño de un circuito eslectrónico. Cada función ha sido comentada, para que el usuario del programa pueda comprender la tarea de las mismas. Las funciones cuyo código se adjunta en este capítulo se van a repartir en dos anexos. En el Anexo Primero se incluye el código de las funciones que el usuario debe usar para cualquier problema, esto es el conjunto de funciones genéricas implementadas. En el Anexo Segundo se incluye el código de las funciones específicas realizadas para los dos casos mostrados en el capítulo de resultados ( capítulo 7 de la presente memoria ), así como el contenido de los ficheros information.txt, config.txt y functions.txt que se han escrito para estos dos problemas planteados.

Transcript of CAPÍTULO 9 APÉNDICESbibing.us.es/proyectos/abreproy/11191/fichero/Capitulo+9.pdf · CAPÍTULO 9...

CAPÍTULO 9 Apéndices

165

CAPÍTULO 9 APÉNDICES

En este capítulo se proporciona el código del programa MatLab implementado para estimar el valor de los parámetros de diseño de un circuito eslectrónico. Cada función ha sido comentada, para que el usuario del programa pueda comprender la tarea de las mismas.

Las funciones cuyo código se adjunta en este capítulo se van a repartir

en dos anexos. En el Anexo Primero se incluye el código de las funciones que el usuario debe usar para cualquier problema, esto es el conjunto de funciones genéricas implementadas. En el Anexo Segundo se incluye el código de las funciones específicas realizadas para los dos casos mostrados en el capítulo de resultados ( capítulo 7 de la presente memoria ), así como el contenido de los ficheros information.txt, config.txt y functions.txt que se han escrito para estos dos problemas planteados.

CAPÍTULO 9 Apéndices

166

Anexo Primero

Código de la función principal optimization.m function optimization % This is the main function. This function change the files input.scs % to a new format. The files ‘infomation.txt’ and ‘config.txt’ are % read here, in order to obtain the necessary information for the % development of the program. Finally, the optimization function % 'fmincon' from the Optimization ToolBox is invoqued: close all; delete results.txt delete errors.txt % It is created a file for saving the value of the variables, the % parameters to optimizice, in each iteration of the objetive % function, simulation.m: fidres=fopen('results.txt','w'); fclose(fidres); % It is created a file for saving the error commited with each % requirement calculated, respect the requiremets imposed by the % designer: fiderr=fopen('errors.txt','w'); fclose(fiderr); % Obtain the value for the global variables, npars and nschematics: info=cell(2,1); B=textread('information.txt','%s'); info{1,1}=char(B(1)); info{2,1}=char(B(2)); npars=str2num(info{1,1}); nschematics=str2num(info{2,1}); % Invoque a function for prepare all the netlists, and create a cell,P % that contain the information given in config.txt: [P]=process_file(npars,nschematics) % It is used a logaritmic normalisation for the parameters, so that % all the parameters move in the same rage of values. This fact will % assure the correct performance of the function 'fmincon'. % The expression for the normalisation of the parameters is: % xi = log(P/Pmin)/log(Pmax/Pmin) % if the the parameters P is P=Pmax -> xi=1, and if P=Pmin -> xi=0. % Define the initial vector x0, normalize: x0=[]; for k=1:npars a=str2num(P{k,4}); b=str2num(P{k,2}); c=str2num(P{k,3}); x0(k)=(log10(a/b))/(log10(c/b)); end % Define the parameters for the optimization function 'fmincon.m': A=[]; b=[]; Aeq=[]; beq=[];

CAPÍTULO 9 Apéndices

167

lb=zeros(1,npars); % the lower bound for each variable is 0. ub=ones(1,npars); % the lower bound for each variable is 1. nonlcon=[]; options=optimset('fmincon'); optnew=optimset(options,'DiffMinChange', 1e-3,'MaxFunEvals',2500,'TolFun',1e-08,'TolX',1e-08,'TolCon',1e-08); save P P; save npars npars; save nschematics nschematics tic; [x,fval,exitflag,output]=fmincon(@simulation,x0,A,b,Aeq,beq,lb,ub,nonlcon,optnew); toc save x x; % printf the information that contain resuls.txt and errors.txt, create by the program in the optimization process: print_results;

Código de la función process_file.m function [P]=process_file(npars,nschematics) % Copy to the work space the file input.scs % of each schematic realized in CADENCE % create a cell with useful information P=createcell_P(npars); for i=1:nschematics % copy the original input.scs to the space work: comand=sprintf('cp netlist%d/input.scs input%d.scs',i,i); unix(comand); % open the inputi.scs, for read it: file=sprintf('input%d.scs',i); fid=fopen(file,'r'); A=textread(file,'%s'); allparameters(A); prepareInput(fid,P,npars) % copy the original input.scs to the space work: comand2=sprintf('mv -f inputaux.scs input%d.scs',i); unix(comand2); delete parameters.txt end Código de la función createcell_P.m function P=createcell_P(npars) % Function that create a cell of elements, called P. % In the cell created, P, the program find the name of % each varible, its lower and upper bound, and the initial starting % value. P=cell(npars,4); % read the file that contain the information needed, config.txt.

CAPÍTULO 9 Apéndices

168

A=textread('config.txt','%s'); tamA=length(A); m=1; j=1; while (j<=tamA) % copy to the cell P the parameter's name. nombre=char(A(j)); P{m,1}=char(nombre); % copy to the cell P the parameter's lower bound. cotaInf=char(A(j+1)); P{m,2}=char(cotaInf); % copy to the cell P the parameter's upper bound. cotaSup=char(A(j+2)); P{m,3}=char(cotaSup); % copy to the cell P the parameter's initial value. inicio=char(A(j+3)); P{m,4}=char(inicio); j=j+4; m=m+1; clear nombre; clear cotaInf; clear cotaSup; clear inicio; end Código de la función allparameters.m function allparameters(A) % Function that create a text file, parameters.txt, where % are writen all the parameters that contain a file input.scs. % Condition for finishing. exit=0; f=1; while (exit==0) flag1=strcmp(A(f),'parameters'); flag2=strcmp(A(f),'include'); flag3=strcmp(A(f),'\'); if flag1 == 1 startpar=f+1; % Position where the parameters begin. end if flag2==1 endpar=f-1; % Position where the parametes end. exit=1; end f=f+1; end f=0; fi=fopen('parametros0.txt','w+'); for f=startpar:endpar aux=char(A(f)); fprintf(fi,'%s',[aux 32 10]); end

CAPÍTULO 9 Apéndices

169

fclose(fi); % It is has been copied all that is in the lines of parameters, also % the carater ‘\’. With the following sentences the caracters ‘\’ is % eliminated: i=1; B=textread('parametros0.txt','%s'); fi2=fopen('parameters.txt','w'); tam=length(B); while i<=tam line=char(B(i)); flag=strcmp(line,'\'); if flag==0 % copy to parameters.txt fprintf(fi2,'%s',[line 13]); end i=i+1; end fclose(fi2); delete parametros0.txt Código de la función prepareInput.m function prepareInput(fid,P,npars) % function that prepares the file input.scs. Copy to an auxiliar file, % inputaux.scs, the sentences that contain the file input.scs that % receives in the identifier fid, except the line where the parametes % are given. Copy to inputaux.scs all the parameters that are not % going to change in the optimization process, in the same line, and % in different lines all those parameters that chage along the % process. % auxiliar file to write in it. fidaux=fopen('inputaux.scs','w'); exit=0; flag1=0; flag2=0; fseek(fid,0,-1); while flag1==0 tline = fgets(fid); if tline(1:10)=='parameters' flag1=1; end if flag1==0 % copy to inputaux.scs, if we did not reach the parameters line fprintf(fidaux,'%s',tline); end end % Now, the parameters are writen to the auxiliar file. % First, the parematers that are not optimiziced are writen:

CAPÍTULO 9 Apéndices

170

linea1='parameters'; fprintf(fidaux,'%s',linea1); %-------------------------------------------------------------------- A=textread('parameters.txt','%s'); tamA=length(A); j=1; flag2=0; copia=0; while (j<=tamA && copia<(tamA-npars)) par=char(A(j)); tam=length(par); i=1; flagnombre=0; %----------------------------------------------------------------- while (i<tam && flagnombre==0) flagnombre=strcmp(par(i),'='); % veamos hasta donde es el nombre del paramtro if flagnombre==0 nombre(i)=par(i); % copia la parte que corresponde al nombre del parametro. end i=i+1; end parametro=char(nombre); %----------------------------------------------------------------- C=textread('config.txt','%s'); tamC=length(C); flag2=0; c=1; while (c<=tamC && flag2==0) flag2=strcmp(parametro,C(c)); c=c+1; end if (flag2==0) copia=copia+1; if copia==(tamA-npars) linea1=[char(par) '\' char(13) char(10)]; else linea1=char(par); end fprintf(fidaux,'%s',[32]); fprintf(fidaux,'%s',linea1); end j=j+1; end % The sentences above have copied to inputaux.scs all the parameters % that are no optimizad, that to say, they were in the text file % parameters.txt, but were not found in the cell P. %--------------------------------------------------------------------- % Now, the parameters that are written in the cell P are copied to % inputaux.scs, in separate lines. All those will be optimized by % the program. for i=1:npars fprintf(fidaux,'%s',[32 32 32 32]); fprintf(fidaux,'%s=%s',P{i,1},P{i,4}); if i==npars %In the last one, don´t write the carácter ‘\’ fprintf(fidaux,'%s',[13 10]); else

CAPÍTULO 9 Apéndices

171

fprintf(fidaux,'%s',[32 92 13 10]); end end % Jump the parameter´s section in the file input.scs: flagInclude=0; while flagInclude==0 tline = fgets(fid); str=tline(1:7); flagInclude=strcmp(str,'include'); if flagInclude==1 fprintf(fidaux,'%s',tline); end end % Continue copy until it´s reached the end of the file input.scs % feof(fid)=1 indicates the end of the file input.scs while feof(fid)==0 tline=fgets(fid); fprintf(fidaux,'%s',tline); end fclose(fid); fclose(fidaux); Código de la función objetivo, simulation.m function error=simulation(x) % this is the second main function or the objetive function % it can recive one input argument, the parameters vector 'x'. close all; error=0; % dernormalize the parameters vector x % p(j)=pmin(j)*exp(x(j)*log(pmax/pmin)) load P; load npars; load nschematics; p=[]; for k=1:npars p(k)=str2num(P{k,2})*exp(x(k)*log(str2num(P{k,3})/str2num(P{k,2}))); end %--------------------------------------------------------------------- % it is called the function for obteining the requirements of the % circuit: allrequirements=call_spectre(P,p,npars,nschematics); %--------------------------------------------------------------------- % it is called the function that returns the variable to minimizice % error: [error]=function_error(allrequirements); %--------------------------------------------------------------------- % Copy to the file results.txt the values for the parameters and the % requirements calculated in this iteration of the objetive function:

CAPÍTULO 9 Apéndices

172

tam=length(allrequirements); fidres=fopen('results.txt','a'); for i=1:npars fprintf(fidres,'%d ',p(i)); end for i=1:tam fprintf(fidres,'%d ',allrequirements(i)); end fprintf(fidres,'%s',[13 10]); fclose(fidres); Código de la función call_spectre.m function allrequirements=call_spectre(P,p,npars,nschematics) % function that invoque to SPECTRE with a new value for the variables. % After the simulations with SPECTRE with all the netlist, files % inputi.scs this function also uses the text file 'functions.txt',in % order to eval all the specific functions created for obtaing all the % requirements needed. %--------------------------------------------------------------------- % Modify all the inputi.scs such as we insert the new values for the % parameters: for i=1:nschematics file=sprintf('input%d.scs',i); % open the inputi.scs, for read it. fid=fopen(file,'r'); modify(fid,P,p,npars) comand2=sprintf('mv -f inputaux.scs input%d.scs',i); unix(comand2); end %--------------------------------------------------------------------- % Invoque to SPECTRE, with all the file inputi.scs: vector=[]; k=1; for i=1:nschematics

spectre=sprintf('/cad/CADENCE_04/IC_5.0.33/tools/spectre/bin/spectre -env artist4.4.6 -format psfascii -raw ./psf%d +param /cad/AMS/kit_3.40/spectre/ams_range.lmts input%d.scs',i,i);

s=unix(spectre); vector(k)=s; k=k+1; end %--------------------------------------------------------------------- % Eval the functions for calculating the requirements: tamVector=length(vector); AA=textread('functions.txt','%s'); tamAA=length(AA); % if all the simulatios were successful: if tamVector==nschematics for j=1:tamAA-2 funcion=AA(j); comand=char(funcion);

CAPÍTULO 9 Apéndices

173

eval(comand); end req=AA(tamAA-1); requiremts=char(req); eval(requiremts); else % If the requirements were not calculated, because there were problems % with SPECTRE, then, we asign extrain values to the requirements: req=AA(tamAA); requiremts=char(req); eval(requiremts); end %--------------------------------------------------------------------- Código de la función modifyInput.m function modifyInput(fid,P,p,npars) % Function that modifies the file inputi.scs. Copy in the parameters’ % lines the new values for the variables( the parameters to optimize) fidaux=fopen('inputaux.scs','w'); exit=0; flag1=0; flag2=0; fseek(fid,0,-1); while flag1==0 tline = fgets(fid); fprintf(fidaux,'%s',tline); if tline(1:10)=='parameters' flag1=1; end end % Now we introduce the new values for the parameters %-------------------------------------------------------------------- for i=1:npars fprintf(fidaux,'%s',[32 32 32 32]); fprintf(fidaux,'%s=%d',P{i,1},p(i)); if i==npars %En el ultimo par ya no hay que poner el \ fprintf(fidaux,'%s',[13 10]); else fprintf(fidaux,'%s',[32 92 13 10]); end end % Jump the parameters´s section in the file input.scs: for i=1:npars tline=fgets(fid); end % Copy until it reached the end of the file: while feof(fid)==0 tline=fgets(fid); fprintf(fidaux,'%s',tline); end

CAPÍTULO 9 Apéndices

174

fclose(fid); fclose(fidaux); - Código de las funciones realizadas para la obtención de los datos de los ficheros ac.ac, dc.dc, tran.tran; estas funciones son útiles para cualquier fichero. Codigo de la funcion getdata_ac.m function [freq,mod,fase]=getdata_ac(fidac,A,nodo1) % Function that obtein the frecuency response of one node. % The input argument nodo1, is an string, that specifies the % node that has to be found. freq=[]; mod=[]; fase=[]; re=[]; im=[]; f=1; j=1; if fidac ==-1 sprintf('No se ha podido abrir fichero') else tam=length(A); while(f<=tam) flag=0; % Look the Word ‘VALUE’. Alter VALUE are the nodes’ values

flag=strcmp(A(f),'VALUE'); if flag == 1 indVALUE=f; k=f; while(k<=tam) flag=0; % Copy the values for the frequency node:

flag=strcmp(A(k),'"freq"'); if flag == 1 indfreq=k+1; freq(j)=str2double(A{indfreq}); j= j + 1; end k=k+1; end end f=f+1; end end fseek(fidac,0,-1); % Now, the function search the node's values. k=1; v=indVALUE; while v<=tam flag=0; flag=strcmp(A(v),nodo1); if flag == 1

CAPÍTULO 9 Apéndices

175

indREVout=v+1; indIMVout=v+2; % Copy to a vector, re[], the node's real part values: real=strrep(A{indREVout},'(',''); re(k)=str2double(real); % Copy to a vector, re[], the node's real part values: imag=strrep(A{indIMVout},')',''); im(k)=str2double(imag); k=k+1; end v=v+1; end mod=abs(re+i*im); % node's magnitud fase=phase(re+im*i)*180/pi; % node's phase fclose(fidac); Codigo de la funcion getdata_acPSRR.m function [freq,vout,vdd,vss]=getdata_acPSRR(fidac,A,nodo1,nodo2,nodo3) % Function that find the nodes nodo1, nodo2, nodo3. It returns tree % vectors conteining the node's values along the frequency. It also % returns the frequency’s values in a vector, freq[]. % Variables that returns the function. freq=[]; vout=[]; vdd=[]; vss=[]; % Variables that uses the function. re=[]; im=[]; revdd=[]; imvdd=[]; revss=[]; imvss=[]; f=1; j=1; if fidac ==-1 sprintf('No se ha podido abrir fichero') else tam=length(A); while(f<=tam) flag=0; % Look for the work 'VALUE'.After VALUE are the node's values: flag=strcmp(A(f),'VALUE'); if flag == 1 indVALUE=f; k=f; while(k<=tam) flag=0; flag=strcmp(A(k),'"freq"'); if flag == 1 indfreq=k+1;

CAPÍTULO 9 Apéndices

176

freq(j)=str2double(A{indfreq}); j= j + 1; end k=k+1; end end f=f+1; end end fseek(fidac,0,-1); % Obtain nodo1' s real and imaginary part. k=1; v=indVALUE; while v<=tam flag=0; flag=strcmp(A(v),nodo1); if flag == 1 indREVout=v+1; indIMVout=v+2; real=strrep(A{indREVout},'(',''); re(k)=str2double(real); imag=strrep(A{indIMVout},')',''); im(k)=str2double(imag); k=k+1; end v=v+1; end k=1; h=1; v=indVALUE; % Obtain nodo2's and nodo3's real and imaginary part. while (v<=tam) flag1=0; flag1=strcmp(A(v),nodo2); flag2=0; flag2=strcmp(A(v),nodo3); if flag1 == 1 indREvdd=v+1; indIMvdd=v+2; realvdd=strrep(A{indREvdd},'(',''); revdd(k)=str2double(realvdd); imagvdd=strrep(A{indIMvdd},')',''); imvdd(k)=str2double(imagvdd); k=k+1; end if flag2 == 1 indREvss=v+1; indIMvss=v+2;

CAPÍTULO 9 Apéndices

177

realvss=strrep(A{indREvss},'(',''); revss(h)=str2double(realvss); imagvss=strrep(A{indIMvss},')',''); imvss(h)=str2double(imagvss); h=h+1; end v=v+1; end vdd=revdd+i*imvdd; vss=revss+i*imvss; vout=re+i*im; fclose(fidac); Codigo de la funcion getdata_dc.m function [vin,voffset,vout]=getdata_dc(fiddc,B,nodo1,nodo2) % Function that find the nodes nodo1, nodo2. It returns two % vectors conteining the DC node's values. It also returns % the value for the input node, 'Vin', that it always appears % in the file dc.dc vin=[]; voffset=[]; vout=[]; f=1; % index for B j=1; % index for Vin g=1; % index for nodo1 h=1; % index for nodo2 if fiddc ==-1 sprintf('No se ha podido abrir fichero') else tam=length(B); while(f<=tam) flag=0; flag=strcmp(B(f),'VALUE'); if flag == 1 indVALUE=f; end f=f+1; end end k=indVALUE; while(k<=tam) flag=0; flag=strcmp(B(k),'"Vin"'); if flag == 1 indvin=k+1; vin(j)=str2double(B(indvin)); j=j+1; else flag=strcmp(B(k),nodo2);

CAPÍTULO 9 Apéndices

178

if flag == 1 indvoffset=k+1; voffset(g)=str2double(B{indvoffset}); g=g+1; else flag=strcmp(B(k),nodo1); if flag == 1 indvout=k+1; vout(h)=str2double(B{indvout}); h=h+1; end end end k=k+1; end fclose(fiddc); Codigo de la funcion getdata_tran.m function [time,vin,vout]=getdata_tran(fidtran,A,nodo1,nodo2) % Function that find the nodes nodo1, nodo2. It returns two % vectors conteining the transien node's values. It also returns % the value for the node 'time', that it always appears % in the file tran.tran % Variables that returns the function: time=[]; vin=[]; vout=[]; f=1; % index for A j=1; % index for the vector time[] if fidtran ==-1 sprintf('No se ha podido abrir fichero') else tam=length(A); while(f<=tam) flag=0; % Look the word 'VALUE'. After VALUE are the nodes' values. flag=strcmp(A(f),'VALUE'); if flag == 1 indVALUE=f; k=f; while(k<=tam) flag=0; flag=strcmp(A(k),'"time"'); if flag == 1 indtime=k+1; time(j)=str2double(A{indtime}); j= j + 1; end k=k+1; end end f=f+1; end end

CAPÍTULO 9 Apéndices

179

fseek(fidtran,0,-1); v=indVALUE; g=1; % Index for nodo2 h=1; % Index for nodo1 while (v<=tam) flag=0; flag=strcmp(A(v),nodo2); if flag == 1 indvin=v+1; vin(g)=str2double(A(indvin)); g=g+1; else flag=strcmp(A(v),nodo1); if flag == 1 indvout=v+1; vout(h)=str2double(A{indvout}); h=h+1; end end v=v+1; end fclose(fidtran);

CAPÍTULO 9 Apéndices

180

Anexo Segundo

Codigo de la funcion function1.m function [av,gbwu,pm,offset]=funcion1(nodo1,nodo2,nodo3) % Function that calculate the values for the characteristics Av, GB, % PM and offset in a opamp’ s schematic. This function were developped % for the schematic called ‘montaje1’. % The variables that the function uses are defined. freq=[]; mod=[]; fase=[]; vin=[]; vout=[]; voffset=[]; flag=0; dim=0; A=[]; B=[]; av=0; offset=1; fidac=fopen('psf1/ac.ac','r'); % Open the file ac.ac for read it. fiddc=fopen('psf1/dc.dc','r'); % Open the file dc.dc for read it. A=textread('psf1/ac.ac','%s'); B=textread('psf1/dc.dc','%s'); % It gets the data of the AC simulation: [freq,mod,fase]=getdata_ac(fidac,A,nodo1); % It gets the data of the DC simulation: [vin,voffset,vout]=getdata_dc(fiddc,B,nodo1,nodo2); [dim,flag]=dimensiones(freq,mod,fase,vin,vout,voffset); if flag==1 av=mod(1); % Diferencial Gain, in V. offset=voffset(1); % Offset tension. % It is calculated the unity BandWitch and phase margin: if (mod(1)>1 && mod(dim)<1) j=1; while (j<=dim && mod(j)>1) j=j+1; end if mod(j)==1 gbwu=freq(j); pm=180+fase(j); else % Interpolation for de GBu: % y=ln(mod) ; x=ln(freq) x1=log(freq(j-1)); x2=log(freq(j)); y1=log(mod(j-1)); y2=log(mod(j));

CAPÍTULO 9 Apéndices

181

% The ecuation for the interpolation is: % (y-y1)/(x-x1)=(y2-y1)/(x2-x1) % -> x=(y-y1)*(x2-x1)/(y2-y1) + x1 % So that, if y=log(1)=0 then x is igual to: % x=ln(gbwu)=x1-y1*(x2-x1)/(y2-y1) % Despejando: gbwu=exp(x1-y1*(x2-x1)/(y2-y1)); % Lineal interpolation for the phase: % y=fas ; x=ln(freq) y1=fase(j-1); y2=fase(j); % En este caso despejamos la 'y' % y=y1+(y2-y1)*(x-x1)/(x2-x1) % y habria que evaluarla para x=ln(gbwu) pm=180+y1+(y2-y1)*(log(gbwu)-x1)/(x2-x1); end else sprintf('Salimos del programa. Respuesta no deseada') av=0; gbwu=0; offset=-1; pm=0; end end Codigo de la funcion function2.m function [slewrate]=function2(nodo1,nodo2) % Function that calculate the value for the characteristic Slew Rate % in a opamp’ s schematic. This function were developped for the % schematic called ‘montaje2’. % Define the variables for the function: time=[]; vin=[]; vout=[]; A=[]; % open the file tran.tran for read it: fidtran=fopen('psf2/tran.tran','r'); A=textread('psf2/tran.tran','%s'); % It gets the transient simulation data: [time,vin,vout]=getdata_tran(fidtran,A,nodo1,nodo2); tam=length(time); % It calculate the Slew Rate as the pendient of a curve v=1; while ((time(v))<=0.00000005 && v<=tam) v=v+1; end x1=time(v); y1=vout(v); while ((time(v))<=0.00000015 && v<=tam) v=v+1; end x2=time(v); y2=vout(v); slewrate=(y2-y1)/(x2-x1);

CAPÍTULO 9 Apéndices

182

Codigo de la funcion function3.m function [acm]=function3 % Function that calculate the value for the characteristic Acm % in a opamp’ s schematic. This function were developped for the % schematic called ‘montaje3’. % Define the variables for the function: freq=[]; mod=[]; fase=[]; A=[]; flag=0; dim=0; acm=0; fidac=fopen('psf3/ac.ac','r'); % Open the file ac.ac for read it A=textread('psf3/ac.ac','%s'); % AC simulation data: [freq,mod,fase]=getdata_ac(fidac,A,nodo1); acm=mod(1); % In vots. Codigo de la funcion function4.m function [PSRR_pos,PSRR_neg]=function4 % Function that calculate the values for the characteristics % positive and negative PSRR in a opamp’ s schematic. This function % were developped for the schematic called ‘montaje4’. % Define the variables for the function: freq=[]; vout=[]; vdd=[]; vss=[]; A=[]; %flag=0; %dim=0; fidac=fopen('psf4/ac.ac','r'); A=textread('psf4/ac.ac','%s'); % AC simulation data: [freq,vout,vdd,vss]=getdata_acPSRR(fidac,A,nodo1,nodo2,nodo3); psrr_posi=vdd./vout; psrr_nega=vss./vout; mod_psrr_posi=abs(psrr_posi); mod_psrr_nega=abs(psrr_nega); PSRR_pos=(mod_psrr_posi(1)); PSRR_neg=(mod_psrr_nega(1));

CAPÍTULO 9 Apéndices

183

Codigo de la funcion function_error.m( para el circuito del opamp ) function [error]=function_error(allrequirements) % Function that calculate the variable to minimize, ‘error’. This % variable indicates how far the requirements calculated by % call_spectre.m ( given in the input vector all_requirements) are % from the desired requirements that impose the user. error=0; % Extract the information from the vector 'requirements': av=allrequirements(1); gbwu=allrequirements(2); pm=allrequirements(3); offset=allrequirements(4); slewrate=allrequirements(5); acm=allrequirements(6); PSRR_pos=allrequirements(7); %Especificaciones que se imponen: avsp=10000; %Voltios/Voltios gbwusp=10e006; %Hertzios pmsp=80; %Grados offsetsp=5e-003; %Voltios slewratesp=8e006; %Voltios/us acmsp=5e-003; %Voltios/V psrrsp=10000; p_av=1; p_gbwu=1; p_pm=1; p_offset=1; p_sr=1; p_acm=1; p_psrr=1; p_total=p_av + p_gbwu + p_pm + p_offset + p_sr + p_acm + p_psrr; % Calculemos el error con cada una de las especificaciones if abs(offset)<=offsetsp off_error=0; else off_error=((abs(offset)-offsetsp)/offsetsp)^2; if off_error>1 off_error=1; end end if av<=0 av_error=1; else av_error=((log10(av)-log10(avsp))/log10(avsp))^2; end if gbwu<=0 gbwu_error=1; else gbwu_error=((log10(gbwu)-log10(gbwusp))/log10(gbwusp))^2; end

CAPÍTULO 9 Apéndices

184

if pm>=80 pm_error=0; else pm_error=((pm-pmsp)/(pmsp))^2; if pm_error>=1 pm_error=1; end end acm_error=((log10(acm)-log10(acmsp))/log10(acmsp))^2; if acm_error>1 | av<=0 acm_error=1; end if slewrate<=0 | av<=0 sr_error=1; else sr_error=((log10(slewrate)-log10(slewratesp))/log10(slewratesp))^2; end psrrpos_error=((log10(PSRR_pos)-log10(psrrsp))/log10(psrrsp))^2; if psrrpos_error>1 | av<=0 psrrpos_error=1; end error=(p_offset*off_error + p_av*av_error + p_gbwu*gbwu_error + p_pm*pm_error + p_sr*sr_error + p_acm*acm_error + p_psrr*psrrpos_error)/p_total; fiderr=fopen('errors.txt','a'); fprintf(fiderr,'%d %d %d %d %d %d %d %d %s',av_error,gbwu_error,pm_error,off_error,sr_error,acm_error,psrrpos_error,error,[13 10]); fclose(fiderr);

CAPÍTULO 9 Apéndices

185

Codigo de la funcion read_one.m function [time_rise]=read_one % Definir las variables que usa la funcion para calcular % las caracteristicas medidas time=[]; vin=[]; vout=[]; A=[]; fidtran=fopen('psf1/tran.tran','r'); % Abre fichero de la simulacion tran para poder leerlo A=textread('psf1/tran.tran','%s'); [time,vin,vout]=getdattran(fidtran,A); % captamos los datos de la simulacion tran j=find(vin>0); % encuentra el indice donde empieza el cambio del pulso. k=j(1); x1=time(k); tam=length(vin); final_value=vin(tam); valor=0.9*final_value; l=find(vout<valor); ind=length(l); ultimo=l(ind); x2=time(ultimo); time_rise=(x2-x1); Codigo de la funcion read_zero.m function [time_fall]=read_zero % Definir las variables que usa la funcion para calcular % las caracteristicas medidas time=[]; vin=[]; vout=[]; A=[]; fidtran=fopen('psf0/tran.tran','r'); % Abre fichero de la simulacion tran para poder leerlo A=textread('psf0/tran.tran','%s'); [time,vin,vout]=getdattran(fidtran,A); % captamos los datos de la simulacion tran j=find(vin>0); % encuentra el indice donde empieza el cambio del pulso. k=j(1); x1=time(k); tam=length(vin); final_value=vin(tam); valor=0.1*final_value; l=find(vout>valor); ind=length(l);

CAPÍTULO 9 Apéndices

186

ultimo=l(ind); x2=time(ultimo); time_fall=(x2-x1); Codigo de la funcion function_error.m ( para el circuito de la celda SRAM) function [error]=funcion_error(time_rise,time_fall) error=0; %Especificaciones que se imponen: time_risesp=0.6e-009; %segundos time_fallsp=0.5e-009; %segundos p_trise=1; p_tfall=1; p_total=p_trise + p_tfall; % Calculemos el error con cada una de las especificaciones if time_rise <=0 trise_error=1; else trise_error=((log10(time_rise)-log10(time_risesp))/log10(time_risesp))^2; end if time_fall <=0 tfall_error=1; else tfall_error=((log10(time_fall)-log10(time_fallsp))/log10(time_fallsp))^2; end error=(p_trise*trise_error + p_tfall*tfall_error)/p_total; fiderr=fopen('errores.txt','a'); fprintf(fiderr,'%d %d %d %s',trise_error,tfall_error,error,[13 10]); fclose(fiderr);