# Input Data
Pw = input("power (Watts) ? ");
Zi = input("Input impedance (Ohms) ? ");
Zo = input("Output impedance (Ohms) ? ");
f = input("frequency (Hz) ? ");

# Bm = Flux Density Gaus / cm2
# Dc = Current Density A / mm2

# As suggested by Alfonso Martignoni
Bm = 11300;
Dc = 3;

# As suggested by Robert G. Wolpert
# Audio Transform Design Manual
# Bm = 17000;  # 29M6 on page 45
# Dc = 2.63;   # page 47

# tabela de fios AWG
awg = [
  #Fio  Área (mm2)
    40, 0.00487;
    39, 0.00621;
    38, 0.00811;
    37, 0.0103;
    36, 0.0127;
    35, 0.0159;
    34, 0.0201;
    33, 0.0255;
    32, 0.0324;
    31, 0.0401;
    30, 0.0507;
    29, 0.0647;
    28, 0.08;
    27, 0.102;
    26, 0.128;
    25, 0.162;
    24, 0.205;
    23, 0.259;
    22, 0.327;
    21, 0.412;
    20, 0.519;
    19, 0.653;
    18, 0.823;
    17, 1.04;
    16, 1.31;
    15, 1.65;
    14, 2.08;
    13, 2.63;
    12, 3.31;
    11, 4.17;
    10, 5.26;
    9, 6.63;
    8, 8.37;
    7, 10.6;
    6, 13.3;
    5, 16.8;
    4, 21.1;
    3, 26.7;
    2, 33.6;
    1, 42.4;
    0, 53.5;
];

function wire = lookupWire(len, awg)
  wire = -1;
  [n m] = size(awg);
  # printf("lookupWire %f: ", len);
  for i = 1:n
    if (awg(i,2) > len)
      wire = awg(i,1);
      # printf("found AWG %d %f\n", awg(i,1), awg(i,2));
      return;
    endif
  end
  # printf("not found\n");
endfunction

# Cálculo
Vi = sqrt(Pw  * Zi);
Ii = Pw / Vi;

Vo = sqrt(Pw * Zo);
Io = Pw / Vo;

Si = Ii / Dc;
So = Io / Dc;

Wi = lookupWire(Si, awg);
Wo = lookupWire(So, awg);

Sm = 7.5 * sqrt ((1.5 * Pw) / f);
Sg = 1.1 * Sm;

printf("*** Sg min = %f cm2 ***\n", Sg);

l = input("core leg size (cm)? ");
h = input("core   height (cm)? ");


printf("*** Output ***\n", Sg);

Sgo = l * h;
Smo = Sgo / 1.1;
printf("Sgo = %f cm2\n", Sgo);

Ni = round(Vi * 1E8 / (4.44 * Bm * Smo * f));

No = round(Vo / Vi * Ni);

printf("Vi = %d Volts @ %d Amps\n", Vi, Ii);
printf("Ni = %d AWG %d\n", Ni, Wi);
printf("No = %d AWG %d\n", No, Wo);
