float xR[nX], xI[nX];
float yR[nY], yI[nY];
float dd[xyCount], d[xyCount];

float x0[maxSig],  xfw[maxSig], xwHz[maxSig];
float pAX[maxSig], pBX[maxSig], pCX[maxSig];
float pAXMult[maxSig], pBXMult[maxSig], pCXMult[maxSig];

float y0[maxSig],  yfw[maxSig], ywHz[maxSig];
float pAY[maxSig], pBY[maxSig], pCY[maxSig];
float pAYMult[maxSig], pBYMult[maxSig], pCYMult[maxSig];

float adj[maxSig*16], amp[maxSig], ampMult[maxSig], aA[maxSig], aAMult[maxSig];

(void) vsSet( yList, xyCount, 0.0 );
(void) vsSet( dd,    xyCount, 0.0 );
(void) vsSet( d,     xyCount, 0.0 );

/***/
/* jActive xPos xWidth xPassive1 xPassive2 xPassive3
/* jHi     yPos yWidth yPassive1 yPassive2 yPassive3
/***/

n = 0;
j = 0;
k = 0;

for( i = 0; i < sigCount; i++ )
   {
    aA[i]     = aList[j++];
    adj[k]    = aList[j++];
    aAMult[i] = aList[j++];
    if (adj[k++]) {aA[i] = pList[n++];};

    x0[i]  = aList[j++];
    adj[k] = aList[j++];
    mult   = aList[j++];
    if (adj[k++]) {x0[i] = pList[n++];};

    xwHz[i] = aList[j++];
    adj[k]  = aList[j++];
    mult    = aList[j++];
    if (adj[k++]) {xwHz[i] = pList[n++];};
    xfw[i]  = xwHz[i]*tSizeX/swX;

    pAX[i]     = aList[j++];
    adj[k]     = aList[j++];
    pAXMult[i] = aList[j++];
    if (adj[k++]) {pAX[i] = pList[n++];};
    pBX[i]     = aList[j++];
    adj[k]     = aList[j++];
    pBXMult[i] = aList[j++];
    if (adj[k++]) {pBX[i] = pList[n++];};
    pCX[i]     = aList[j++];
    adj[k]     = aList[j++];
    pCXMult[i] = aList[j++];
    if (adj[k++]) {pCX[i] = pList[n++];};

    amp[i]     = aList[j++];
    adj[k]     = aList[j++];
    ampMult[i] = aList[j++];
    if (adj[k++]) {amp[i] = pList[n++];};

    y0[i]  = aList[j++];
    adj[k] = aList[j++];
    mult   = aList[j++];
    if (adj[k++]) {y0[i] = pList[n++];};

    ywHz[i] = aList[j++];
    adj[k]  = aList[j++];
    mult    = aList[j++];
    if (adj[k++]) {ywHz[i] = pList[n++];};
    yfw[i]  = ywHz[i]*tSizeY/swY;

    pAY[i]     = aList[j++];
    adj[k]     = aList[j++];
    pAYMult[i] = aList[j++];
    if (adj[k++]) {pAY[i] = pList[n++];};
    pBY[i]     = aList[j++];
    adj[k]     = aList[j++];
    pBYMult[i] = aList[j++];
    if (adj[k++]) {pBY[i] = pList[n++];};
    pCY[i]     = aList[j++];
    adj[k]     = aList[j++];
    pCYMult[i] = aList[j++];
    if (adj[k++]) {pCY[i] = pList[n++];};

    (void) vsSet( d,  xyCount, 0.0 );

    (void) vsSet( xR, fSizeX, 0.0 );
    (void) vsSet( xI, fSizeX, 0.0 );

    (void) vsSet( yR, fSizeY, 0.0 );
    (void) vsSet( yI, fSizeY, 0.0 );

    xd = 0.001*xDelay/(1.0/swX);
    yd = 0.001*yDelay/(1.0/swY);

    a  = amp[i]*ampMult[i];

    (void) vSim1D( xR, xI, tSizeX, fSizeX, x0[i], xfw[i], a,   xP0, xP1 );
    (void) vSim1D( yR, yI, tSizeY, fSizeY, y0[i], yfw[i], 1.0, yP0, yP1 );

    (void) apod( xR, tSizeX, q1X, q2X, q3X, swX, qcX ); 
    (void) apod( xI, tSizeX, q1X, q2X, q3X, swX, qcX ); 

    (void) apod( yR, tSizeY, q1Y, q2Y, q3Y, swY, qcY ); 
    (void) apod( yI, tSizeY, q1Y, q2Y, q3Y, swY, qcY ); 

    xR[0] *= 0.5;
    xI[0] *= 0.5;

    yR[0] *= 0.5;
    yI[0] *= 0.5;

    p = aAMult[i];

    if (xActive)
       {
        for( ii = 0; ii < p; ii++ )
           {
            f = aA[i]/swX;  (void) jMod( xR, tSizeX, f, xd*PI*f + 0.0 );
            f = aA[i]/swX;  (void) jMod( xI, tSizeX, f, xd*PI*f + 0.0 );
           }
       };

    p = pAXMult[i];

    for( ii = 0; ii < p; ii++ )
       {
        f = pAX[i]/swX; (void) jMod( xR, tSizeX, f, xd*PI*f + PI/2.0 );
        f = pAX[i]/swX; (void) jMod( xI, tSizeX, f, xd*PI*f + PI/2.0 );
       }

    p = pBXMult[i];

    for( ii = 0; ii < p; ii++ )
       {
        f = pBX[i]/swX; (void) jMod( xR, tSizeX, f, xd*PI*f + PI/2.0 );
        f = pBX[i]/swX; (void) jMod( xI, tSizeX, f, xd*PI*f + PI/2.0 );
       }

    p = pCXMult[i];

    for( ii = 0; ii < p; ii++ )
       {
        f = pCX[i]/swX; (void) jMod( xR, tSizeX, f, xd*PI*f + PI/2.0 );
        f = pCX[i]/swX; (void) jMod( xI, tSizeX, f, xd*PI*f + PI/2.0 );
       }

    (void) fft( xR, xI, fSizeX );

    p = aAMult[i];

    if (yActive)
       {
        for( ii = 0; ii < p; ii++ )
           {
            f = aA[i]/swY;  (void) jMod( yR, tSizeY, f, yd*PI*f + 0.0 );
            f = aA[i]/swY;  (void) jMod( yI, tSizeY, f, yd*PI*f + 0.0 );
           }
       };

    p = pAYMult[i];

    for( ii = 0; ii < p; ii++ )
       {
        f = pAY[i]/swY; (void) jMod( yR, tSizeY, f, yd*PI*f + PI/2.0 );
        f = pAY[i]/swY; (void) jMod( yI, tSizeY, f, yd*PI*f + PI/2.0 );
       }

    p = pBYMult[i];

    for( ii = 0; ii < p; ii++ )
       {
        f = pBY[i]/swY; (void) jMod( yR, tSizeY, f, yd*PI*f + PI/2.0 );
        f = pBY[i]/swY; (void) jMod( yI, tSizeY, f, yd*PI*f + PI/2.0 );
       }

    p = pCYMult[i];

    for( ii = 0; ii < p; ii++ )
       {
        f = pCY[i]/swY; (void) jMod( yR, tSizeY, f, yd*PI*f + PI/2.0 );
        f = pCY[i]/swY; (void) jMod( yI, tSizeY, f, yd*PI*f + PI/2.0 );
       }

    (void) fft( yR, yI, fSizeY );

    (void) vvProd( xR, yR, d, xSize, ySize );
    (void) vvAdd( yList, d, xyCount ); 

    if (!xActive && !yActive)
       {
        (void) fprintf( " %.3e ", amp[i] );
       }
    else
       {
        (void) fprintf( " %.3f ", aA[i] );
       };
   }

sum    = vSum( yList, xyCount );
sumAbs = vSumAbs( yList, xyCount );
avgAmp = vSum( amp, sigCount );

(void) vvCopy( d, yList, xyCount );
(void) vvSub( d, dList, xyCount );
(void) vvCopy( dd, d, xyCount );
(void) vvDiv( d, nList, xyCount );
(void) vvMult( d, d, xyCount );

avgAmp /= sigCount;

rms = vSum( d, xyCount );
rms = sqrt( rms/xyCount );

(void) fprintf( "   %.3f\n", rms );

if (reportFlag)
   {
    fd = dOpen( simName, "w" );

    (void) dWrite( fd, fdata, wordLen*FDATASIZE );
    (void) dWrite( fd, yList, wordLen*xyCount );
    (void) dClose( fd );

    fd = dOpen( difName, "w" );

    (void) dWrite( fd, fdata, wordLen*FDATASIZE );
    (void) dWrite( fd, dd,    wordLen*xyCount );
    (void) dClose( fd );

    fd = dOpen( tabName, "w" );

    (void) printf( fd, "VARS   SIGNAL VARNAME ADJUSTABLE VALUE\n" );
    (void) printf( fd, "FORMAT %%d    %%s     %%d        %%f\n\n" );

    j = 0;

    for( i = 0; i < sigCount; i++ )
       { 
        (void)fprintf( fd, "%.0f jActive   %.0f %f\n", i+1, adj[j++], aA[i] );
        (void)fprintf( fd, "%.0f xPos      %.0f %f\n", i+1, adj[j++], x0[i] );
        (void)fprintf( fd, "%.0f xWidth    %.0f %f\n", i+1, adj[j++], xwHz[i] );
        (void)fprintf( fd, "%.0f xPassive1 %.0f %f\n", i+1, adj[j++], pAX[i] );
        (void)fprintf( fd, "%.0f xPassive2 %.0f %f\n", i+1, adj[j++], pBX[i] );
        (void)fprintf( fd, "%.0f xPassive3 %.0f %f\n", i+1, adj[j++], pCX[i] );
        (void)fprintf( fd, "%.0f jHi       %.0f %f\n", i+1, adj[j++], amp[i] );
        (void)fprintf( fd, "%.0f yPos      %.0f %f\n", i+1, adj[j++], y0[i] );
        (void)fprintf( fd, "%.0f yWidth    %.0f %f\n", i+1, adj[j++], ywHz[i] );
        (void)fprintf( fd, "%.0f yPassive1 %.0f %f\n", i+1, adj[j++], pAY[i] );
        (void)fprintf( fd, "%.0f yPassive2 %.0f %f\n", i+1, adj[j++], pBY[i] );
        (void)fprintf( fd, "%.0f yPassive3 %.0f %f\n", i+1, adj[j++], pCY[i] );
       }

    (void)fprintf( fd, "%.0f rms       %.0f %f\n",   0, 0, rms );
    (void)fprintf( fd, "%.0f N         %.0f %.0f\n", 0, 0, xyCount );
    (void)fprintf( fd, "%.0f sum       %.0f %f\n",   0, 0, sum );
    (void)fprintf( fd, "%.0f sumAbs    %.0f %e\n",   0, 0, sumAbs );
    (void)fprintf( fd, "%.0f avgAmp    %.0f %e\n",   0, 0, avgAmp );
    (void)fprintf( fd, "%.0f fitFlag   %.0f %.0f\n", 0, 0, 1 );

    (void) dClose( fd );
   };
