/* fouran.f -- translated by f2c (version of 3 February 1990  3:36:42).
   You must link the resulting object file with the libraries:
	-lF77 -lI77 -lm -lc   (in that order)
*/

#include "f2c.h"

/* Common Block Declarations */

struct {
    integer ielmnt, isbckt, nsbckt, iunsat, nunsat, itemps, numtem, isens, 
	    nsens, ifour, nfour, ifield, icode, idelim, icolum, insize, 
	    junode, lsbkpt, numbkp, iorder, jmnode, iur, iuc, ilc, ilr, 
	    numoff, isr, nmoffc, iseq, iseq1, neqn, nodevs, ndiag, iswap, 
	    iequa, macins, lvnim1, lx0, lvn, lynl, lyu, lyl, lx1, lx2, lx3, 
	    lx4, lx5, lx6, lx7, ld0, ld1, ltd, imynl, imvn, lcvn, nsnod, 
	    nsmat, nsval, icnod, icmat, icval, loutpt, lpol, lzer, irswpf, 
	    irswpr, icswpf, icswpr, irpt, jcpt, irowno, jcolno, nttbr, nttar, 
	    lvntmp;
} tabinf_;

#define tabinf_1 tabinf_

struct {
    integer locate[50], jelcnt[50], nunods, ncnods, numnod, nstop, nut, nlt, 
	    nxtrm, ndist, ntlin, ibr, numvs, numalt, numcyc;
} cirdat_;

#define cirdat_1 cirdat_

struct {
    integer iprnta, iprntl, iprntm, iprntn, iprnto, limtim, limpts, lvlcod, 
	    lvltim, itl1, itl2, itl3, itl4, itl5, itl6, igoof, nogo, keof;
} flags_;

#define flags_1 flags_

struct {
    doublereal atime, aprog[3], adate, atitle[10], defl, defw, defad, defas, 
	    rstats[50];
    integer iwidth, lwidth, nopage;
} miscel_;

#define miscel_1 miscel_

struct {
    doublereal omega, time, delta, delold[7], ag[7], vt, xni, egfet, xmu, 
	    sfactr;
    integer mode, modedc, icalc, initf, method, iord, maxord, noncon, iterno, 
	    itemno, nosolv, modac, ipiv, ivmflg, ipostp, iscrch, iofile;
} status_;

#define status_1 status_

struct {
    doublereal twopi, xlog2, xlog10, root2, rad, boltz, charge, ctok, gmin, 
	    reltol, abstol, vntol, trtol, chgtol, eps0, epssil, epsox, pivtol,
	     pivrel;
} knstnt_;

#define knstnt_1 knstnt_

struct {
    doublereal tstep, tstop, tstart, delmax, tdmax, forfre;
    integer jtrflg;
} tran_;

#define tran_1 tran_

struct {
    doublereal xincr, string[15], xstart, yvar[8];
    integer itab[8], itype[8], ilogy[8], npoint, numout, kntr, numdgt;
} outinf_;

#define outinf_1 outinf_

struct {
    doublereal value[200000];
} blank_;

#define blank_1 blank_

/* Table of constant values */

static integer c__9 = 9;
static integer c__0 = 0;
static integer c__72 = 72;
static integer c__1 = 1;
static integer c__7 = 7;

/*<       subroutine fouran >*/
/* Subroutine */ int fouran_()
{
    /* Initialized data */

    static struct {
	char e_1[32];
	doublereal e_2;
	} equiv_37 = { {'f', 'o', 'u', 'r', 'i', 'e', 'r', ' ', 'a', 'n', 'a',
		 'l', 'y', 's', 'i', 's', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 
		' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 0. };

#define fortit ((doublereal *)&equiv_37)

    static struct {
	char e_1[8];
	doublereal e_2;
	} equiv_38 = { {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 0. };

#define ablnk (*(doublereal *)&equiv_38)


    /* Format strings */
    static char fmt_61[] = "(\002 fourier components of transient response\
 \002,5a8///)";
    static char fmt_71[] = "(\0020dc component =\002,1pd12.3/,\0020harmonic \
  frequency    fourier    normalized    phase     normalized\002/,\002    no\
         (hz)     component    component    (deg)    phase (deg)\002//)";
    static char fmt_81[] = "(i6,1pd15.3,d12.3,0pf13.6,f10.3,f12.3/)";
    static char fmt_101[] = "(//5x,\002total harmonic distortion =  \002,f12\
.6,\002  percent\002)";

    /* System generated locals */
    integer i_1, i_2;
    doublereal d_1, d_2;
    complex q_1;

    /* Builtin functions */
    double sin(), cos();
    integer s_wsfe(), do_fio(), e_wsfe();
    double sqrt();

    /* Local variables */
    static doublereal dcco, harm;
    static integer locx, locy, nknt, loct, ipnt, ipos;
    extern /* Subroutine */ int move_();
    static integer iknt;
    static doublereal freq1;
    extern /* Subroutine */ int getm8_(), zero8_();
    static integer j, k;
    static doublereal phase, cosco[9], sinco[9];
    extern /* Subroutine */ int title_();
    static integer jstop;
    static doublereal xnorm, pnorm;
    extern /* Subroutine */ int ntrpl8_();
    static doublereal forfac;
#define nodplc ((integer *)&blank_1)
#define cvalue ((complex *)&blank_1)
    static doublereal forprd;
    static integer kfrout, numpnt;
    static doublereal arg;
    extern /* Subroutine */ int outnam_();
    static doublereal xnharm;
    extern /* Subroutine */ int magphs_();
    static doublereal phasen, thd;
    extern /* Subroutine */ int clrmem_();
    static doublereal yvr;

    /* Fortran I/O blocks */
    static cilist io__22 = { 0, 0, 0, fmt_61, 0 };
    static cilist io__24 = { 0, 0, 0, fmt_71, 0 };
    static cilist io__31 = { 0, 0, 0, fmt_81, 0 };
    static cilist io__35 = { 0, 0, 0, fmt_81, 0 };
    static cilist io__36 = { 0, 0, 0, fmt_101, 0 };


/*<       implicit double precision (a-h,o-z) >*/

/*     this routine determines the fourier coefficients of a transient */
/* analysis waveform. */

/* spice version 2g.6  sccsid=tabinf 3/15/83 */
/*<       common /tabinf/ ielmnt,isbckt,nsbckt,iunsat,nunsat,itemps,numtem, >*/
/*<      1   isens,nsens,ifour,nfour,ifield,icode,idelim,icolum,insize, >*/
/*<      2   junode,lsbkpt,numbkp,iorder,jmnode,iur,iuc,ilc,ilr,numoff,isr, >*/
/*<      3   nmoffc,iseq,iseq1,neqn,nodevs,ndiag,iswap,iequa,macins,lvnim1, >*/
/*<      4   lx0,lvn,lynl,lyu,lyl,lx1,lx2,lx3,lx4,lx5,lx6,lx7,ld0,ld1,ltd, >*/
/*<      5   imynl,imvn,lcvn,nsnod,nsmat,nsval,icnod,icmat,icval, >*/
/*<      6   loutpt,lpol,lzer,irswpf,irswpr,icswpf,icswpr,irpt,jcpt, >*/
/*<      7   irowno,jcolno,nttbr,nttar,lvntmp >*/
/* spice version 2g.6  sccsid=cirdat 3/15/83 */
/*<       common /cirdat/ locate(50),jelcnt(50),nunods,ncnods,numnod,nstop, >*/
/*<      1   nut,nlt,nxtrm,ndist,ntlin,ibr,numvs,numalt,numcyc >*/
/* spice version 2g.6  sccsid=flags 3/15/83 */
/*<       common /flags/ iprnta,iprntl,iprntm,iprntn,iprnto,limtim,limpts, >*/
/*<      1   lvlcod,lvltim,itl1,itl2,itl3,itl4,itl5,itl6,igoof,nogo,keof >*/
/* spice version 2g.6  sccsid=miscel 3/15/83 */
/*<       common /miscel/ atime,aprog(3),adate,atitle(10),defl,defw,defad, >*/
/*<      1  defas,rstats(50),iwidth,lwidth,nopage >*/
/* spice version 2g.6  sccsid=status 3/15/83 */
/*<       common /status/ omega,time,delta,delold(7),ag(7),vt,xni,egfet, >*/
/*<      1   xmu,sfactr,mode,modedc,icalc,initf,method,iord,maxord,noncon, >*/
/*<      2   iterno,itemno,nosolv,modac,ipiv,ivmflg,ipostp,iscrch,iofile >*/
/* spice version 2g.6  sccsid=knstnt 3/15/83 */
/*<       common /knstnt/ twopi,xlog2,xlog10,root2,rad,boltz,charge,ctok, >*/
/*<      1   gmin,reltol,abstol,vntol,trtol,chgtol,eps0,epssil,epsox, >*/
/*<      2   pivtol,pivrel >*/
/* spice version 2g.6  sccsid=tran 3/15/83 */
/*<       common /tran/ tstep,tstop,tstart,delmax,tdmax,forfre,jtrflg >*/
/* spice version 2g.6  sccsid=outinf 3/15/83 */
/*<       common /outinf/ xincr,string(15),xstart,yvar(8),itab(8),itype(8), >*/
/*<      1   ilogy(8),npoint,numout,kntr,numdgt >*/
/* spice version 2g.6  sccsid=blank 3/15/83 */
/*<       common /blank/ value(200000) >*/
/*<       integer nodplc(64) >*/
/*<       complex cvalue(32) >*/
/*<       equivalence (value(1),nodplc(1),cvalue(1)) >*/


/*<       dimension sinco(9),cosco(9) >*/
/*<       dimension fortit(4) >*/
/*<       data fortit / 8hfourier , 8hanalysis, 8h        , 8h         / >*/
/*<       data ablnk / 1h  / >*/


/*<       forprd=1.0d0/forfre >*/
    forprd = 1. / tran_1.forfre;
/*<       xstart=tstop-forprd >*/
    outinf_1.xstart = tran_1.tstop - forprd;
/*<       kntr=1 >*/
    outinf_1.kntr = 1;
/* c    xn=101.0d0 */
/*<       xincr=forprd/npoint >*/
    outinf_1.xincr = forprd / outinf_1.npoint;
/* c    npoint=xn */
/*<       call getm8(locx,npoint) >*/
    getm8_(&locx, &outinf_1.npoint);
/*<       call getm8(locy,npoint) >*/
    getm8_(&locy, &outinf_1.npoint);
/*<       do 105 nknt=1,nfour >*/
    i_1 = tabinf_1.nfour;
    for (nknt = 1; nknt <= i_1; ++nknt) {
/*<       itab(1)=nodplc(ifour+nknt) >*/
	outinf_1.itab[0] = nodplc[tabinf_1.ifour + nknt - 1];
/*<       kfrout=itab(1) >*/
	kfrout = outinf_1.itab[0];
/*<       call ntrpl8(locx,locy,numpnt) >*/
	ntrpl8_(&locx, &locy, &numpnt);
/*<       dcco=0.0d0 >*/
	dcco = 0.;
/*<       call zero8(sinco,9) >*/
	zero8_(sinco, &c__9);
/*<       call zero8(cosco,9) >*/
	zero8_(cosco, &c__9);
/*<       loct=locy+1 >*/
	loct = locy + 1;
/*<       ipnt=0 >*/
	ipnt = 0;
/*<    10 yvr=value(loct+ipnt) >*/
L10:
	yvr = blank_1.value[loct + ipnt - 1];
/*<       dcco=dcco+yvr >*/
	dcco += yvr;
/*<       forfac=dble(ipnt)*twopi/npoint >*/
	forfac = (doublereal) ipnt * knstnt_1.twopi / outinf_1.npoint;
/*<       arg=0.0d0 >*/
	arg = 0.;
/*<       do 20 k=1,9 >*/
	for (k = 1; k <= 9; ++k) {
/*<       arg=arg+forfac >*/
	    arg += forfac;
/*<       sinco(k)=sinco(k)+yvr*dsin(arg) >*/
	    sinco[k - 1] += yvr * sin(arg);
/*<       cosco(k)=cosco(k)+yvr*dcos(arg) >*/
	    cosco[k - 1] += yvr * cos(arg);
/*<    20 continue >*/
/* L20: */
	}
/*<       ipnt=ipnt+1 >*/
	++ipnt;
/*<       if (ipnt.ne.npoint) go to 10 >*/
	if (ipnt != outinf_1.npoint) {
	    goto L10;
	}
/*<       dcco=dcco/npoint >*/
	dcco /= outinf_1.npoint;
/*<       forfac=2.0d0/npoint >*/
	forfac = 2. / outinf_1.npoint;
/*<       do 30 k=1,9 >*/
	for (k = 1; k <= 9; ++k) {
/*<       sinco(k)=sinco(k)*forfac >*/
	    sinco[k - 1] *= forfac;
/*<       cosco(k)=cosco(k)*forfac >*/
	    cosco[k - 1] *= forfac;
/*<    30 continue >*/
/* L30: */
	}
/*<       call title(0,72,1,fortit) >*/
	title_(&c__0, &c__72, &c__1, fortit);
/*<       ipos=1 >*/
	ipos = 1;
/*<       call outnam(kfrout,1,string,ipos) >*/
	outnam_(&kfrout, &c__1, outinf_1.string, &ipos);
/*<       call move(string,ipos,ablnk,1,7) >*/
	move_(outinf_1.string, &ipos, &ablnk, &c__1, &c__7);
/*<       jstop=(ipos+6)/8 >*/
	jstop = (ipos + 6) / 8;
/*<       write (iofile,61) (string(j),j=1,jstop) >*/
	io__22.ciunit = status_1.iofile;
	s_wsfe(&io__22);
	i_2 = jstop;
	for (j = 1; j <= i_2; ++j) {
	    do_fio(&c__1, (char *)&outinf_1.string[j - 1], (ftnlen)sizeof(
		    doublereal));
	}
	e_wsfe();
/*<    61 format(' fourier components of transient response ',5a8///) >*/
/*<       write (iofile,71) dcco >*/
	io__24.ciunit = status_1.iofile;
	s_wsfe(&io__24);
	do_fio(&c__1, (char *)&dcco, (ftnlen)sizeof(doublereal));
	e_wsfe();
/*<    71 format('0dc component =',1pd12.3/, >*/
/*<      1   '0harmonic   frequency    fourier    normalized    phase     no >*/
/*<      2rmalized'/, >*/
/*<      3   '    no         (hz)     component    component    (deg)    pha >*/
/*<      4se (deg)'//) >*/
/*<       iknt=1 >*/
	iknt = 1;
/*<       freq1=forfre >*/
	freq1 = tran_1.forfre;
/*<       xnharm=1.0d0 >*/
	xnharm = 1.;
/*<       call magphs(cmplx(sngl(sinco(1)),sngl(cosco(1))),xnorm,pnorm) >*/
	d_1 = sinco[0];
	d_2 = cosco[0];
	q_1.r = d_1, q_1.i = d_2;
	magphs_(&q_1, &xnorm, &pnorm);
/*<       phasen=0.0d0 >*/
	phasen = 0.;
/*<       write (iofile,81) iknt,freq1,xnorm,xnharm,pnorm,phasen >*/
	io__31.ciunit = status_1.iofile;
	s_wsfe(&io__31);
	do_fio(&c__1, (char *)&iknt, (ftnlen)sizeof(integer));
	do_fio(&c__1, (char *)&freq1, (ftnlen)sizeof(doublereal));
	do_fio(&c__1, (char *)&xnorm, (ftnlen)sizeof(doublereal));
	do_fio(&c__1, (char *)&xnharm, (ftnlen)sizeof(doublereal));
	do_fio(&c__1, (char *)&pnorm, (ftnlen)sizeof(doublereal));
	do_fio(&c__1, (char *)&phasen, (ftnlen)sizeof(doublereal));
	e_wsfe();
/*<    81 format(i6,1pd15.3,d12.3,0pf13.6,f10.3,f12.3/) >*/
/*<       thd=0.0d0 >*/
	thd = 0.;
/*<       do 90 iknt=2,9 >*/
	for (iknt = 2; iknt <= 9; ++iknt) {
/*<       freq1=dble(iknt)*forfre >*/
	    freq1 = (doublereal) iknt * tran_1.forfre;
/*<       call magphs(cmplx(sngl(sinco(iknt)),sngl(cosco(iknt))), >*/
/*<      1   harm,phase) >*/
	    d_1 = sinco[iknt - 1];
	    d_2 = cosco[iknt - 1];
	    q_1.r = d_1, q_1.i = d_2;
	    magphs_(&q_1, &harm, &phase);
/*<       xnharm=harm/xnorm >*/
	    xnharm = harm / xnorm;
/*<       phasen=phase-pnorm >*/
	    phasen = phase - pnorm;
/*<       thd=thd+xnharm*xnharm >*/
	    thd += xnharm * xnharm;
/*<       write (iofile,81) iknt,freq1,harm,xnharm,phase,phasen >*/
	    io__35.ciunit = status_1.iofile;
	    s_wsfe(&io__35);
	    do_fio(&c__1, (char *)&iknt, (ftnlen)sizeof(integer));
	    do_fio(&c__1, (char *)&freq1, (ftnlen)sizeof(doublereal));
	    do_fio(&c__1, (char *)&harm, (ftnlen)sizeof(doublereal));
	    do_fio(&c__1, (char *)&xnharm, (ftnlen)sizeof(doublereal));
	    do_fio(&c__1, (char *)&phase, (ftnlen)sizeof(doublereal));
	    do_fio(&c__1, (char *)&phasen, (ftnlen)sizeof(doublereal));
	    e_wsfe();
/*<    90 continue >*/
/* L90: */
	}
/*<       thd=100.0d0*dsqrt(thd) >*/
	thd = sqrt(thd) * 100.;
/*<       write (iofile,101) thd >*/
	io__36.ciunit = status_1.iofile;
	s_wsfe(&io__36);
	do_fio(&c__1, (char *)&thd, (ftnlen)sizeof(doublereal));
	e_wsfe();
/*<   101 format (//5x,'total harmonic distortion =  ',f12.6,'  percent') >*/
/*<   105 continue >*/
/* L105: */
    }
/*<       call clrmem(locx) >*/
    clrmem_(&locx);
/*<       call clrmem(locy) >*/
    clrmem_(&locy);
/*<   110 return >*/
/* L110: */
    return 0;
/*<       end >*/
} /* fouran_ */

#undef cvalue
#undef nodplc
#undef ablnk
#undef fortit


