/*   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    */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

main()
{
       HideCur();
       while(1)
       {
               clrscr();
               printf("J-Pole design calculations by N3GO Gary O'Neil\n");
               printf("----------------------------------------------\n\n");

               printf("(1) Antenna design calculations\n");
               printf("(2) Antenna adjustment calculations\n");
               printf("(3) Velocity factor calculations\n\n");
               printf("(Q) Quit\n");
               switch(bioskey(0) & 0xff) {
                       case '1': design();break;
                       case '2': tuning();break;
                       case '3': velocity();break;
                       case 'q':
                       case 'Q': ShowCur();
                                                exit(0);
               }
       }
}


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;
char s[20];

       clrscr();

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

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

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

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

       input("                     Velocity factor: ",s,&v);

       while(1)
       {
               clrscr();

/*  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("   Frequency 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\n\n");
               printf("Impedances used for this calculation are as follows: \n");
               printf("\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);
               printf("\n\n\n");
               input("New Velocity factor (enter zero to quit): ",s,&v);
               if(v == 0) return;
       }

errexit:
       printf("Use 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;
char s[20];

       clrscr();
       printf("\n\n\n");
       input("       Frequency (MHz) where minimum VSWR is desired: ",s,&f2);
       printf("\n");

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

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

               if(! dl) break;

               if(dl > 0)
                       printf("    Reduce the length of the halfwave element by: %f cm\n",dl * 12 * 2.54);
               else
                       printf("  Increase the length of the halfwave element by: %f cm\n",fabs(dl) * 12 * 2.54);

               printf("\n\n");
       }
       clrscr();
       printf("\n\n\n          Good Job!!!\n");
       getch();
}


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


velocity()
{
double f1,f2,v,l,l1,l2,dl;
char s[20];
char c;

       clrscr();
       printf("\n\n\n");
       input("              Desired operating frequency (MHz): ",s,&f1);
       printf("\n");
       input("      Estimate the velocity factor of the transmission line: ",s,&v);
       printf("\n");
       printf("              Cut an electrical half wavelength sample.\n");

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

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

       while(1)
       {
               input("            Frequency (MHz) where VSWR is lowest: ",s,&f2);
               clrscr();
               printf("\n\n");

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

               v = l1 * f2 / 491.78;

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

/*  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("      Adjusted length of %f MHz sample: %f\n", f1, l2 * 12 * 2.54);
               printf("\n\n\n");
               printf("         Does the VSWR at %.4f MHz equal that of the dummy load?", f2);

               c = bioskey(0) & 0xff;
               if((c == 'y') || (c == 'Y')) break;

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

               l1 = l2;
       }

       clrscr();
       printf("\n\n");
       input("        Measured physical length (cm): ",s,&l);
       l = l / 2.54 / 12;

/*  Equation C3 in Appendix C */

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


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


input(char *p, char *s, double *v)
{
       printf("%s",p);
       ShowCur();
       gets(s);
       HideCur();
       *v = atof(s);
}

HideCur()
{
       asm{
               push ax
               push cx

               mov ah,01h
               mov ch,00100110b
               mov cl,00000111b
               int 10H

               pop cx
               pop ax
       }
}

ShowCur()
{
       asm{
               push ax
               push cx

               mov ah,01H
               mov ch,00000110b
               mov cl,00000111b
               int 10H

               pop cx
               pop ax
       }
}