/*   From the feeble mind of N3GO - Gary O'Neil - March 17, 1995  */
/*               End-fed Zepp/J-pole design calculations          */
/*                                                                */
/*   Converted from BASIC to C by SM5SXL Mats Petersson 950407    */
/*   Ncurses conversion 970319                                    */

#include <config.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
#endif
#include <math.h>


input(char *p, double *v)
{
       printf("%s", p);
       while (! scanf(" %lf", v))
               getchar();
}


design()
{
/*   ZA = Antenna Feedpoint Impedance, ZF = Feedline Impedance, and */
/*   Z0 = the impedance of the matching transmission line.          */
/*   ZA = 3500 to 5000 ohms for end fed dipole.                     */
/*   ZF = 50 ohms in my analysis                                    */
/*   Z0 = 300 Ohms nominal ** Note:  Maximum = sqrt(ZA*ZF)          */

       double za, z0, zf, zmax, zp, rho, yf, b, l1, l2, x1, x2, dipole, series,
       shunt, r,f,v;
       static char fs[21], vs[21];
       char tmp[82];

       za = 5000;
       zf = 50;
       z0 = 300;

       zmax = sqrt(za * zf);
       if (z0 > zmax) goto errexit;

       printf("\nFeedline Impedance (Ohms): %.0f\n",zf);
       printf("Matching Transmission line Impedance: %.0f\n",z0);
       input("\nFrequency (MHz): ", &f);

/*  V = velocity factor of transmission line used for matching network */

       input("\nVelocity factor: ", &v);

       while (1) {
/*  ZP = the antenna impedance ZA normalized to the stub impedance Z0 */

               zp = za/z0;                                                             /* Equation A1 */

/*  RHO = the antenna reflection coefficient with respect to the matching
                 stub impedance */

               rho = (zp-1)/(zp+1);                    /* Equation A2 */

/*  YF = the feedline admittance 1/ZF normalized to the stub admittance 1/Z0 */

               yf = z0/zf;                             /* Equation A3 */

/*  B =  the normalized susceptance of the shorted stub section */

               b = sqrt((pow(rho,5) * pow(yf+1,2) - pow(yf-1,2)) /
                       (1-pow(rho,2)));                /* Equation A4 */

/*  X2 = the normalized reactance of the shorted stub section */

               x2 = 1/b;                               /* Equation A5 */
               l2 = atan(x2) * (180/M_PI);             /* Equation A6 */
               r = yf/(pow(yf,2)+pow(b,2));            /* Equation A7 (Real) */
               x1 = b/(pow(yf,2)+pow(b,2));            /* Equation A8 (Imag) */
               l1 = 90 - (atan(x1) * (180/M_PI));

/*  The constant 5606 is 95 per cent of the speed of light in inches
       for a half wavelength at 1 MHz */

               dipole = 5606 / f;                      /* Equation A9 - A */

/*  The constant 32.78 is the speed of light in inches/degree/MHz */

               series = 32.78 * v * (l1/f);            /* Equation A9 - B */
               shunt = 32.78 * v * (l2/f);             /* Equation A9 - C */

               printf("\nFrequency in MHz  = %f\n",f);
               printf("Wavelength in air = %f cm\n",(299.7*39.37/f) * 2.54);
               printf("Length of stub = %f wavelengths\n",(l1+l2)/360);
               printf("Height of 'Tap' = %f wavelengths\n",l2/360);
               printf("Dipole Length = %f cm\n",dipole * 2.54);
               printf("Series Trans. Line = %f cm\n",series * 2.54);
               printf("Shunt Trans. Line = %f cm\n",shunt * 2.54);
               printf("Stub Length = %f cm\n",series * 2.54 + shunt * 2.54);
               printf("Overall Length = %f cm\n",series * 2.54 + shunt * 2.54 + dipole * 2.54);
               printf("\n");
               printf("Impedances used for this calculation are as follows:\n\n");
               printf("Impedance of End-fed Dipole: %.0f Ohms\n",za);
               printf("Antenna System Feedline Impedance: %.0f Ohms\n",zf);
               printf("Matching Section Transmission line Impedance: %.0f Ohms\n",z0);
               printf("Maximum Usable Matching line Impedance: %.0f Ohms\n",zmax);
               printf("Velocity Factor of Matching Section: %f\n",v);
               input("\nNew Velocity factor (enter zero to quit): ", &v);
               if (v == 0) return;
       }

errexit:
       printf("\nUse matching transmission line impedance less than %f Ohms\n",zmax);
       printf("Change term in data statement to reflect a lower impedance.\n");
}

/****************************************************************************/


tuning()
{
/*     Antenna adjustment calculations by N3GO  03/24/95   */

       double f1, f2, dl;
       static char f1s[21], f2s[21];
       char tmp[82];

       input("\nFrequency (MHz) where minimum VSWR is desired: ", &f2);

       while (1) {
               input("\nMeasured minimum VSWR frequency (MHz): ", &f1);
               if (! f1) return;

               dl = (491.78 * (f2-f1)) / (f1*f2);      /* Equation B1 */

               if (! dl) break;

               if (dl > 0)
                       printf("\nReduce the length of the halfwave element "
                              "by: %f cm\n", dl * 12 * 2.54);
               else
                       printf("\nIncrease the length of the halfwave element "
                               "by: %f cm\n", fabs(dl) * 12 * 2.54);
       }
       printf("\nGood Job!!!\n");
}


/****************************************************************************/


velocity()
{
       double f1, f2, v, l, l1, l2, dl;
       static char f1s[21], f2s[21], vs[21], ls[21];
       char c, tmp[82], yesno;

       input("\nDesired operating frequency (MHz): ", &f1);
       input("\nEstimate the velocity factor of the transmission line: ",
               &v);
       printf("\nCut an electrical half wavelength sample.\n");

       l1 = (v * 491.78) / f1;                         /* Equation C1 */

       printf("\nCut sample length to: %f cm\n",l1 * 12 * 2.54);

       while (1) {
               input("\nFrequency (MHz) where VSWR is lowest: ", &f2);

/*  Compute new velocity factor estimate based on VSWR measurement result */

               v = l1 * f2 / 491.78;

               printf("\nCurrent velocity factor estimate: %f\n",v);

/*  Compute new sample length based on new velocity factor estimate */

               l2 = 491.78 * v / f1;

/*  Compute length change required for desired frequency sample */

               dl = l2 - l1;

               printf("\nAdjusted length of %f MHz sample: %f\n",
                       f1, l2 * 12 * 2.54);
               printf("\nDoes the VSWR at %.4f MHz equal that of the "
                       "dummy load? ", f2);
               while(! scanf(" %c", &yesno))
                       getchar();
               if (yesno == 'y')
                       break;

               if (dl <= 0)
                       printf("\nReduce the stub length by "
                       "%f cm\n", fabs(dl) * 12 * 2.54);
               else
                       printf("\nIncrease the stub length by "
                       "%f cm\n", dl * 12 * 2.54);

               l1 = l2;
       }

       input("\nMeasured physical length (cm): ", &l);
       l = l / 2.54 / 12;

/*  Equation C3 in Appendix C */

       printf("\nThe velocity factor is: %f\n", l * f2 / 491.78);
}


/***************************************************************************/


main()
{
       char c;

       printf("\nJ-Pole design calculations by N3GO Gary O'Neil\n");
       printf("----------------------------------------------\n");

       while (1) {
               printf("\n1  Antenna design calculations\n");
               printf("2  Antenna adjustment calculations\n");
               printf("3  Velocity factor calculations\n");
               printf("Q  Quit\n");
               while (! scanf(" %c", &c))
                       getchar();
               switch(c) {
                       case '1':
                               design();
                               break;
                       case '2':
                               tuning();
                               break;
                       case '3':
                               velocity();
                               break;
                       case 'q':
                       case 'Q':
                               exit(0);
               }
       }
}