Friday, July 28, 2017

Olde Skool Antenna Design with NEC2 on OpenBSD

The Numerical Electromagnetics Code (NEC) is a powerful and efficient program for the analysis of the electromagnetic properties of antennas and other objects. It is a Method of Moments type of successive approximation program and dates back to the time of the dinosaurs.  NEC was developed on a CDC 7700 in the mid 1970s to early 80s at Lawrence Livermore by Burke and Poggio for the US Navy and it was paid for by the friendly US tax payers - free for use by anyone now.

Dipole Antenna Example

CDC - Control Data Corporation, became Computing Devices Canada, which became General Dynamics Canada - I worked there through all the name changes - but you don't need to go hunt in a museum for a CDC 7700 to run NEC - an Intel PC will do.   The NEC documentation amusingly still refers to stacks of punch cards, but these have been replaced with simple text files.  I learned programming with punch cards on a Sperry-Univac, so the batch processing way that NEC works is not strange to me, but it does bring back some scary memories of the good old, bad old days of iron age, hollow state computing...

NEC was considered a large program, requiring a powerful mainframe to execute.  However, what used to require a big room full of computing equipment, can now be done on a laptop computer.  Just to prove the point, I installed NEC on my Lenovo S10e netbook running OpenBSD.

The original FORTRAN code is still available, but it has been rewritten a few times into C and C++, to improve the efficiency and ability to handle very large numbers of wire elements. The new versions can handle tens of thousands of elements and can be used to model a whole aircraft for example, but then you would need something with a wee bit more RAM than my netbook.

Here is another similar method of moments approach:

As usual with high tech things, there is absolutely no correlation between price and quality.  You can spend $100,000 on an antenna design program from ANSYS or others, or you can use NEC or Puma EM - for Free.

The difference is that the $100,000 package comes with training and support, whereas with NEC, you have to figure it out yourself - but figuring it out yourself over a few days, is rather less than $100,000 me thinks and even with the commercial support and training session, you still need to sit for a few days to figure it out anyway.  If you have big money burning your pocket, then spending it on RF test equipment may be a better idea.

Getting Started With NEC2 Antenna Modelling

The place to start is here:

Download the manuals, and read them:

The first manual describes how NEC works and the third manual describes how to use NEC to model an antenna.  There are many examples, from dipoles to Yagis and helixes - it is all there.  To get started, you could find a suitable example and tweak it to suit your purpose.

Download the NEC example files from the American Radio Relay League:

Download an antenna handbook or two and clue up on antennas, so that you can tell when your simulations are going wrong:

If you are broke, get this one - the author passed away before he could complete it and his legacy is a free antenna book: 

NEC on OpenBSD

If you are a very recent convert and don't have an OpenBSD laptop computer yet:

Assuming that you have a machine which is minimally working, first set up the OpenBSD ports system.  Look for a file named ports.tar.gz on the mirrors.

$ cd /tmp
$ ftp$(uname -r)/{ports.tar.gz,SHA256.sig}
$ signify -Cp /etc/signify/openbsd-$(uname -r | cut -c 1,3) -x SHA256.sig ports.tar.gz

Untar this file in the /usr directory, to create /usr/ports and all the directories under it.

# su -
# cd /usr
# tar xzf /tmp/ports.tar.gz

Look for nec2 based antenna design software:
# cd /usr/ports
# make search key="nec2"
  Port:   xnecview-1.36p0
  Port:   necpp-1.2.3p3

Build necpp:
# cd cad/necpp
# make install
... looong wait ...
# make clean

Build xnecview:
# cd cad/xnecview/
# make install
... looong wait ...
# make clean

OK, so which programs do we have now?
$ ls /usr/local/bin/*nec*
  nec2++              nec2diff            xnecview

Apparently we have nec2++ for antenna design and xnecview to plot the results.  A text editor and some RTFMing is all that is needed to create the input files.  Mousepad or even vi will do.

Helical Antenna Model

Let's look at a helical antenna model.  There is one in the file from ARRL.  The NEC manual has two parts, part 1 describes the program and part 3 describes how to use it, with exhaustive examples.  A volunteer is needed to write part 2!   A helical antenna card stack is described on page 20 of part 3 of the manual.

Helix - CocoaNEC
A helical antenna definition is extremely simple, since it is only one wire.  Nevertheless, you need to refer to page 20 of the third manual to know what exactly the Geometry Helix (GH) line means:

GH 1,60, .05,.5,.1,.1,.1,.1,.00001

$ man nec2++
$ man xnecview

Let's put the card stack in the hopper and run it to see what happens.  Watch out for the continuous paper spewing out of the high speed line printer...

$ nec2++ -iHELIX.NEC -ohelix.out

Let's view it:
$ xnecview HELIX.NEC helix.out

To make the original example useful, one has to add definitions for the EX signal source and RP antenna pattern cards.

With EX 0 1 1 0 1 and RP 0 1 360 360 1000 0 0 1 1,  I eventually got something to plot, but it didn't look pretty and an attempt to calculate an admittance matrix caused the netbook to run out of RAM and the program was terminated.

Eventually, I got the NEC syntax sorted and the plots made sense once I used a very large number of elements (500 or more).  As the elements are straight sections, if you specify too few, then the simulation of the spiral becomes complete nonsense.

CM Helix, 900 MHz, 15 loops, 1.15 meter
CM Herman Oosthuysen, July 2017
CM Wave length = 2.998x10^8 / 900MHz = 0.333 m
CM Use Kraus formula:
CM Radius = 116.2 / 2 = 58.1 mm
CM Loops = 15
CM Spacing = .23 = 76.6 mm
CM Length = 1150 mm
CM GH 1,200,.0766,1.150,.0581,.0581,.0581,.0581,.0066
CM EX element 1 has voltage source of 1V
CM EX 0,1,1,0,1,0,0,0,0,0,0
CM FR Freq response 800 to 1000 MHz, 5 points
CM FR 0,5,0,0,800,50
CM RP 72 points x 5 = 360 degrees
CM RP XNDA = 0 (max pwr gain) works best
CM RP 0,1,72,72,0,0,0,5,5,0,0
CM The simulation needs a very large number of elements, 500 or more
GH 1,500,.0766,1.150,.0581,.0581,.0581,.0581,.0066
EX 0,1,1,0,1,0,0,0,0,0,0
FR 0,5,0,0,800,50,,,,,
RP 0,1,72,72,0,0,0,5,5,0,0

Helix 900 MHz

The little netbook can calculate up to about 5 frequencies in a sweep.  For more accurate rendering, a better computer is required!

Equations from Kraus:
G= 10.8 + 10*log10(  (C/lambda)2*N*(S/lambda) )    (Note1)
Z= 150/sqrt(C/lambda) Ohm
D= lambda /PI
S= C/4
HPBW= 52/( (C/lambda)*sqrt(N*(S/lambda)) ),  Half power beamwidth.
BWFN= 115/( (C/lambda)*sqrt(N*(S/lambda)) ),  Beamwidth first nulls.
Ae= D*lambda2/(4*PI)

Where C is circumference, which is about one wavelength for axial mode.

What exactly the gain of this antenna will be, is not clear - somewhere between 7 and 15 dBi - but the simulation at least shows that it radiates in axial mode.

Log Periodic Antenna Model

Just for fun, I tried a few more example files and a log periodic design plotted beautifully.

Logperiodic Example Plots

Therefore the design software is shown to be working fine and now the real work can start, to make a new antenna from scratch.  This becomes complicated when you add all the parasitic things: Stubs, ground plane, loading...

Vertical Dipole Model

Here is something simple that I created with xnec2c on Fedora Linux.  The picture is at the top of the page.

CM 900 MHz, WL = 0.333 m, 1/4 WL = 0.083 m
CM Limitations of xnec2c visualization program RP card:
CM Cannot plot a 360 degree sweep with ground plane (need to remove GN cards)
CM The XNDA field must be 0
GW     1     9  0.00000E+00  0.00000E+00  0.00000E+00  0.00000E+00  0.00000E+00  8.30000E-02  1.00000E-03
FR     0     0     11     0  8.00000E+02  2.00000E+01  0.00000E+00  0.00000E+00  0.00000E+00  0.00000E+00
EX     0     0     5      0  1.00000E+00  0.00000E+00  0.00000E+00  0.00000E+00  0.00000E+00  0.00000E+00
RP     0    72    72      0  0.00000E+00  0.00000E+00  5.00000E+00  5.00000E+00  0.00000E+00  0.00000E+00



A helical antenna is the general case of a wire antenna.  If you squash it up, it becomes a loop and if your stretch it out, it becomes a long wire.

Helical antennas can radiate in two modes (and everything in between...) Normal, or Axial.  Normal mode occurs when the helix circumference is smaller than 1/2 wavelength (0.2 nominal) and Axial mode occurs when it is larger than 0.8 wavelength (1 nominal). (See John D. Kraus, The Helical Antenna, Proceedings of the IRE, 1949).

As you can see in this graph from Kraus, there is almost no right or wrong way to make a helical antenna, provided that you stay close to the two D lambda curves.  To increase the gain, use more wire and reduce the spacing.  To make it more wide band, make the loops bigger.  To match the impedance to your radio, you can either add a tuner, or bend the first loop a little bit!

Axial mode is good for a ground antenna pointing at a small aircraft just like a Yagi-Uda, but the circular polarization will ensure minimal changes in signal strength when the aircraft banks in turns.

Normal mode is good when a retired radio amateur tries to fit a 160 meter HF dipole antenna into the attic of a small cottage.  If one uses two curly cords then a dipole becomes much smaller - any size you want it to be really, so it is a guaranteed fit, whether in your roof or outside in the yard.  Wrap the dipole wire around a plastic drain pipe, stretch it out as much as you can and stick a tuner on it to match it to your radio for whatever frequency you want to work at - as easy as borscht.

Axial mode concerns me at the moment and the normal mode will eventually!

While playing with example designs from the web in order to get going with the modelling programs, I noticed that many published designs and 'antenna calculators' are quite dreadful.  I even found some Yagis that radiate more backwards than forwards, so you really should model an antenna before you build it.


Heretic geeks can buy EZNec for Windows and Linux cowboys can do dnf install nec2c xnec2c, or apt install nec2c xnec2c.  For Mac users, there is CocoaNEC.  All these programs work the same, since the underlying engine is the same, but they do have different bugs.

For example, xnec2c has trouble with the radiation pattern plot when there is a ground plane (remove the GN card) and the RP XNDA field must be 0.  Despite this, xnec2c seems to make better plots than xnecview as it handles edge cases better (where things go to zero).  CocoaNEC creates the best graphics by far, but it also struggles with a helical antenna (it won't display the helix structure, but it can display the radiation pattern) and CocoaNEC always inserts its own RP card - which ensures that it always works.

Different RP Card Handling:
  • xnec2++:     RP 0,1,72,72,1000,0,0,5,5,0,0
  • xnec2c:        RP 0,72,72,0,0,0,5,5,0,0,0
  • CocoaNEC: RP 0,91,120,1000,0,0,2,3,5000
So, you just have pick one and play with it a bit till you figured it out - they all work - eventually.

However, the fancy GUIs only implement a small subset of what NEC can do.  Therefore, to do anything special, you need to stick your nose into the manual and edit the source card stack file with a text editor, the old fashioned way.  This is not easy and fancy GUIs cannot make it so.

La voila!


Monday, July 3, 2017

Cube Satellites

For a few years, it has been possible to launch tiny educational satellites - for free - to fill up some of the unused payload space on big rockets.

This opportunity has been used by many universities and radio amateur groups:

A few months ago, the American University in Sharjah also launched a cubesat called NAYIF-1:

It has been passing overhead many times and I was a bit miffed that I did not know about it before the launch, had no idea how to connect to it and to my knowledge, the radio amateur law in the UAE did not allow foreigners to operate.

The final straw was when I learned that Russia and Slovakia launched three cubesats in the same week:  

It turns out that in preparation for all this, the UAE radio amateur law was changed last year, a radio amateur society (EARS) was founded in Sharjah and expats are welcome:

NAYIF-1 Cubesat

The NAYIF-1 has a Funcube transponder, which has a beacon and a bent pipe.  Anyone can listen to the beacon, but to send something through the transponder would require a ham license:

Cubesat Ground Station

The Funcube project is based in the UK:

Educational material is posted here:

To connect to the NAYIF-1 (or other) satellite, you need to know when it is passing: 

To see all the cubesats currently flying, try this:  There are hundreds.  Which ones are actually working is another matter though.

To send data through a cubesat transponder, you would need a V/UHF radio, which could be an off the shelf SSB ham radio, or a Software Defined Radio specially built for this purpose:

In practice, you need to know exactly when the satellite will pass overhead and then you have one or two minutes (depending on the height of the orbit) to bounce a message through the transponder before it disappears over the other horizon and then you may not see it again for several hours.

Ham Equipment

A new Yaesu/Kenwood/Icom/Xiegu/Alinco transceiver will cost around $800 to $8000, but you could buy a radio kit for $200 and assemble it yourself and end up with a similar high quality transceiver.

For example:


If you design the whole toy from scratch like I usually do, then it may take years to complete, it will likely end up costing $500 or more and the quality will be much inferior, but it will be a whole lot more fun to do and you could then say: This is a total piece of junk, it barely works, but I built it myself!

You could follow a somewhat easier path and build a ham radio kit, some of which are amazing quality and will look exactly the same as a factory built unit, but you could buy a second hand rig on Ebay for much less.

Absolutely amazing high quality factory built or DIY radio kits:

The best value kit which was refreshed recently in 2016, is BITX40, a 25 Watt, SSB, digitally controlled transceiver, built and tested PCB for only $60:
Due to the present low sun spot cycle, the 40 meter (7 MHz) HF band is best for long distance work and the BITX40 is ideal for that.  With the BITX40, you can chat to someone on the other side of the globe on SSB if you have a proper antenna (With a 15 meter flag pole and a tuner you can look very patriotic too!).

Very simple fun kits (which perform much worse and could cost more than the above!):

There are also crazy kits for ancient vacuum tube sets (for old fogeys like me), or little transceivers using only 7 parts that barely work, or radios built entirely from 2N2222 NPN bipolar transistors just because...:

Of course, someone then went and did the same with a radio built entirely from small signal PNP transistors!

A simple little radio can be much fun to chat to people around your city, but they are usually unstable and the transmit frequency will drift.  A good radio will lock on and follow a bad radio, so you can have fun provided that whoever you try to talk to has a good quality rig.  It will also help a lot if you run your fun radio off a battery and let it warm up and settle down for 15 minutes before you transmit.

A 7 Ah motorcycle battery is stable and has no noise and will make your toy work much, much better than a noisy unregulated wall wart and then you may be able to rag with someone hundreds of kilometers away on less power than a child's night light.

Ham Software

The best ham resources are probably the DXzone and ARRL web sites.

Here is a laundry list of Linux ham software and what they are good for:

and this is for Macintosh:

All the radioham utilities are available on Fedora, Ubuntu, Slackware, OpenBSD, Mac...  but you can also try Knoppix or Shackbox if you want to run off a USB stick without actually installing Linux on a machine.

Morse Code???

Everybody associates ham radio with Continuous Wave Morse code and if you listen to the HF bands, then you will hear a lot of it.  CW with Morse was the first digital telegraph modem about 200 years ago.  However, nobody learns to send Morse any more.  I was in the Corps of Signals in 1982 and didn't learn Morse - it was already long obsolete.

However, if you want two data link radios to connect to each other, then they have to use the same modem waveform on both sides of the link and Morse is the 'lowest common denominator', which can even work with a friendly doddering OM (old man) or XYL (ex young lady) clutching a pencil on the other side...

There are a multitude of modem programs that run on common garden variety computers that send data via the sound adaptors.  The most common ham modem modes are RTTY, PSK31 and CW Morse - there are many, many others, but it doesn't help if you are the only one trying to use it, since it will make you feel very lonely.

The best modem program at the moment, which works on Windows, Linux, BSD and Mac, is FLDigi:

However, as with most software things, you need at least a half decent computer and any half decent computer works better with Linux or BSD than with Windows. The Windows scheduler is not good for real-time operation and therefore cannot do encoding and decoding very reliably since it stutters too much.  This is why you will see many disparate reviews of the same amateur radio software, with one bloke saying it works great and the next bloke saying it doesn't.  If you don't want any hassle, use a Macintosh (which runs a kind of BSD).

FLDigi and Satscape are the most fun you can have with your geek hat on...

Best of all, you don't even need a radio to try it.  You can install FLDigi and play through your sound system to hear first hand what it does, or even let two computers side by side, talk to each other over the air, never mind the ether!

--. .-. --- ..- -. -.. / -.-. --- -. - .-. --- .-.. / - --- / -- .- .--- --- .-. / - --- -- / -.-. --- -- -- . -. -.-. .. -. --. / -.-. --- ..- -. - -.. --- .-- -. --..-- / . -. --. .. -. . ... / --- -.

Have fun with the Funcubes!


Wednesday, June 28, 2017

Cold Cathode Display Tubes - Nixies

A hobby project needs to be simple, so that I can sit down, stare at the circuit for a few seconds and carry on without having to read a manual and consult six data sheets, because then it feels like work.  Consequently, I found that thermionic valves make ideal hobby toys, since the circuitry necessarily has to be simple.  One can use a double handful of ICs and transistors in a hair brained design, but with tubes, two or three are the practical limit and this forces one to think carefully about what one wants to do and keep it elegant.

Here is my olde skool guitar amplifier: It works a treat.

There is nothing quite like the mystical glow of a Nixie Tube.  The Nixie is a special kind of neon lamp.  The Neon glows in space surrounding the almost invisibly thin cathode wires.  The wire itself is cold - it is the gas around it that glows and the glowing area is much wider than the wires.

 Four Nixie Tubes with 10 Cathode Drivers

Nixies have lately become very popular amongst bored, or retired ex-hippies and their younger unemployed hipster children.  There is even now a factory for brand new super huge Nixie tubes in an old castle at Brno in the Czech Republic:

Controlling and powering a Nixie is not hard, but it requires high voltages and nowadays all electronics use very low voltages.  This causes a safety problem, as designers try to cut corners to reduce the cost of the resulting complexity.

Safety First

The web is awash with simple Nixie supply schematics that rectify the mains directly for the high voltage supply and use series capacitors to provide a low voltage supply for the silicon logic.  The argument is that since a Nixie needs a dangerous high voltage anyway, one can just as well hook it directly to the mains and since you are doing that, you can generate the low voltage the same way through a buck regulator series capacitor.

The problem with that argument is many fold:
  • The mains wiring is not guaranteed to be correct and many power plugs can be inserted any which way, so there is no certainty that a particular supply wire is Live or Neutral.  It can be either way around.
  • The mains supply is dirty.  Mains power consists of 115 or 230 Volt 50 or 60 Hz sine wave, plus a couple thousand volts of high frequency noise.  This noise will pass straight through a capacitor.
  • Without an isolation transformer, the high frequency 2 kilovolt noise spikes will run into your Arduino or Raspberry computer that you use to control your Nixie tubes.  It will run into your expensive Oscilloscope that you use to develop the electronics and it will run into your laptop computer that you use to debug the Arduino or Raspberry.  It can also run straight into you.
So, after a little while of happy debugging - POOF!
For the cost of a replacement oscilloscope or laptop computer, you could buy a lifetime supply of little mains isolation transformers and if you don't, then your lifetime may not be very long...

Power Consumption, Transformers and Fuses

Nixies do not draw much power, typically 2.5 mA @ 125 V = 0.312 Watt for a large one.

A little Arduino also does not draw much either. A Teensie 2.0 dissipates 27.3 mA 16 MHz 5V = 0.136 Watt. More modern ones draw half as much.

Therefore a widget with four tubes and one Arduino will consume 1.4 Watt or less.

You can get a tiny mains isolation transformer at Digikey or Mouser, for example the Triad Magnetics F-367P:
  • Power: 1.5 VA
  • Size: 34.93mm x 28.58mm x 28.58mm
  • Price: $10.32
Then, put a MOV (Littelfuse V10H250P) and polyfuse on the transformer (Littelfuse 250R080Z), rectify the 230V for the high voltage supply (1N4004 or 1N4007 Diodes), use a 1 uF (Self healing film, 600V AC, Kemet PHE450PR7100JR02R06L2) capacitor to feed the low voltage supply (BZX85C5V6 zener diode) and crucially, ground the secondary for safety of life, limb and test equipment.

The MOV and transformer will absorb the high voltage spikes and will ensure that your circuit will run happily for many years, without needing repairs after every thunder storm and the polyfuse will ensure that your bad soldering will not burn your house down...

Nixie Data Sheet

The common IN8-2 looks like this:

Computer I/O Control

You can control the Nixies using DIP opto-couplers such as the Toshiba TLP627-4F, or you can use MPSA42 (for the cathodes) and MPSA92 (for the anodes) transistors: 

A few high voltage DIP opto-isolator packages can be more compact, but a double handful of transistors and 10k resistors can be much more fun to wire up.  The trannies are also a little cheaper, but with this ridiculously expensive hobby, cost is not really an issue - if it is, then you got to find something else to do!

Nixie Anode Voltage, Metal Sputter and Resistors

If you start with 115 V rms AC and rectify it, you supposedly get a high voltage supply of 162 Volt DC.  However, the actual mains voltage is usually about 120 V rms and rectifies to 120 V x 1.414 = 170 V DC, which is equal to the strike voltage of a Nixie tube, so a tube can usually work off a 115 V rms mains supply, but sometimes it won't, leaving you scratching your beard.  This is why most 115 V supplies use a voltage doubler to ensure that there is sufficient tension to reliably strike the lamps.

Once lit, the resulting Neon plasma has a lower resistance and the Nixie Anode needs 125 Volt to sustain the glow and the current should then be limited to about 2 mA, to keep the electron energy to a reasonable level, otherwise metal atoms can be knocked out of the Anode, causing them to sputter on the inside of the glass, making the tube dark.  The manufacturer recommended maximum current rating for a Nixie tube is about 3.5 to 4 mA.

Therefore, a 230 V Anode supply is better and it rectifies to 230 V x 1.414 = 325 V DC.  At that voltage, the Nixie will definitely strike.  Note that you don't need to put a capacitor on the Anode supply:  The Nixie will happily strike and glow on each half cycle (100 Hz), which is too fast for the eye to see, so it will look like a continuous glow.

If the DC is smoothed with a capacitor, then the current limiting resistor should be (325V - 125V) / 2 mA = 100 kilohm, but if you don't use a capacitor, then you would need to use about 38 k to 47 k to get a decent average glow from a rectified unsmoothed sine wave, but then you may get some sputter and reduced Nixie life from the peak current.  High voltage capacitors are expensive and bulky - so it is your call whether you want to buy a capacitor, or a cup of Starbucks coffee.

Note that common garden variety resistors are rated at 100 V, so the Nixie Anode resistor which will drop 200 V, should be made from two resistors in series, or it may eventually carbonize and smoke.  The wattage of the resistor should be 200 V x 2mA = 0.4 W, so use a 1 W or 2 W resistor to avoid burning a fingertip if you would touch it.

Clock PSU With Battery Backup

If you are building a hoosammawatzit with a little processor to control the Nixies, then you will find that the slightest power interruption will cause the thing to lose the time of day and resetting it every few days will get annoying.

The battery needs to be a higher voltage than the logic supply for the regulator to work, but it also needs to be a lower voltage than the supply, else it will power the circuit until it is run down.  To handle this conundrum, regulate the 230 V AC from the isolation transformer secondary down to 12 V DC, put a 9 V battery with a blocking diode, then regulate that down to 5 V.

Errata: I have to upload a new PSU sketch.  The high voltage supply needs to be half wave rectified with a 470nF capacitor for a little smoothing, to be able to use a common ground with the low voltage supply!

Due to the high voltage, the 1M resistor should be made from two 470k resistors in series.  The buck capacitor should be self healing film, 600V AC, Kemet PHE450PR7100JR02R06L2.

It is not necessary to backup the Nixie supply - they will simply glow again once the mains power comes back, so a 9V alkaline battery will be good for a year.

Arduino Teensie2 Clock Sketch

Here is a basic clock control routine that should get you started.  You'll have to tweak the TIMEBASE to get it reasonably accurate, but don't expect high precision from a 4 MHz garden variety crystal!

// Teensie 2 - Nixie Clock Controller
// Copyright reserved under GPL version 2, Herman Oosthuysen, 2017

// Includes for interrupt routine handlers
#include <avr/io.h>
#include <avr/interrupt.h>

// Teensie 2.0 Nixie Clock Pinout
// USB: Serial Port, ppogram and test
// Power: Gnd, +5V (next to USB socket)
// Pin 0: CATHODE1, output
// Pin 1: CATHODE2, output
// Pin 2: CATHODE3, output
// Pin 3: CATHODE4, output
// Pin 4, PWM: CATHODE5, output
// Pin 5, PWM, INT0: PWM Time Base, output, 976.56 Hz
// Pin 6, INT1: INT1 Time Base, input
// Pin 7, RX, INT2: CATHODE6, output
// Pin 8, TX, INT3: CATHODE7, output
// Pin 9, PWM: CATHODE8, output
// Pin 10, PWM: CATHODE9, output
// Pin 11, A10: CATHODE0, output
// Pin 12, A9, PWM: ANODE1, output
// Pin 13, A8: ANODE2, output
// Pin 14, A7, PWM: ANODE3, output
// Pin 15, A6, PWM: ANODE4, output
// Pin 16, A5: HOURS, input
// Pin 17, A4: MINUTES, input
// Pin 18, A3: ENTER, input
// Pin 19, A2: NC
// Pin 20, A1: NC
// Pin 21, A0: NC
// Pin 22: LED Seconds, output
// Pin 23: SPEAKER (Via 330R, 100UF)

// Example: digitalWrite(LED, LOW);  // LED on
// Example: digitalWrite(LED, HIGH); // LED off
// Example: val = analogRead(HOURS);

#define CATHODE1         0
#define CATHODE2         1
#define CATHODE3         2
#define CATHODE4         3
#define CATHODE5         4
#define CLOCK             5
#define INTERRUPT1        6
#define CATHODE6        7
#define CATHODE7        8
#define CATHODE8        9
#define CATHODE9        10
#define CATHODE0        11
#define ANODE1            12
#define ANODE2            13
#define ANODE3            14
#define ANODE4            15
#define HOURS            16
#define MINUTES            17
#define ENTER            18
#define NC1                19
#define NC2                20
#define NC3                21
#define LED                22
#define SPEAKER            23

unsigned int cathodes[10] =

unsigned int anodes[4] =

// PWM is used as a time base for the clock:
// PWM pins: 4, 5, 9, 10, 12, 14, 15
// PWM is controlled with the analogWrite(pin, value) function.
// analogWrite(5, 128);
// The value is between 0 to 256, 128 is 50% duty cycle.

// The PWM signals are created by hardware timers:
// Timer 0: Pin 5 = 976.56 Hz (1024 us)
// Timer 1: Pin 4, 14, 15 = 3921.57 Hz
// Timer 3: Pin 9 = 3921.57 Hz
// Timer 4: Pin 10, 12 = 3921.57 Hz

// Timer 0, Pin 5 is the slowest, use it for the Time Base
// and link it to Pin 6 INT1, next to it.
#define TIMEBASE    1024

// Interrupt Inputs:
// INT0: Pin 5
// INT1: Pin 6
// INT2: Pin 7
// INT3: Pin 8

// Speaker:
// Beep the speaker with the tone function.
// tone(pin number, frequency in Hz, duration in ms);
#define BEEP 440
#define SHORT 250
#define LONG 1000
// Example: tone(SPEAKER, BEEP, SHORT);

// USB Serial Port:
#define BAUD  9600

// Variables
unsigned int    hours = 0;
unsigned int    minutes = 0;
unsigned int    seconds = 0;
unsigned int    deciseconds = 0;
unsigned int    microseconds = 0;

// Initialization:
void setup()
    // Configure the USB serial port
    Serial.begin(BAUD); // The USB port is 12 Mbit/sec
    // All digital pins default to input mode.
    // Set the mode of all output pins
    pinMode(CLOCK, OUTPUT);
    pinMode(SPEAKER, OUTPUT);
    pinMode(LED, OUTPUT);
    pinMode(CATHODE1, OUTPUT);
    pinMode(CATHODE2, OUTPUT);
    pinMode(CATHODE3, OUTPUT);
    pinMode(CATHODE4, OUTPUT);
    pinMode(CATHODE5, OUTPUT);
    pinMode(CATHODE6, OUTPUT);
    pinMode(CATHODE7, OUTPUT);
    pinMode(CATHODE8, OUTPUT);
    pinMode(CATHODE9, OUTPUT);
    pinMode(CATHODE0, OUTPUT);
    pinMode(ANODE1, OUTPUT);
    pinMode(ANODE2, OUTPUT);
    pinMode(ANODE3, OUTPUT);
    pinMode(ANODE4, OUTPUT);

    // Set all Nixie outputs off
    digitalWrite(ANODE1, LOW);
    digitalWrite(ANODE2, LOW);
    digitalWrite(ANODE3, LOW);
    digitalWrite(ANODE4, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);
    digitalWrite(CATHODE1, LOW);

    // Set the clock PWM time base
    analogWrite(CLOCK, 128);

    // Print startup message on USB to computer screen
      Serial.println("Nixie Clock, GPL v2, Herman, 2017.");
      tone(SPEAKER, BEEP, SHORT);

// Flash each digit at 50 Hz refresh rate
// Each digit is on for about 5 ms and off for about 15 ms
#define FLASH    5   
unsigned int    a = 0;
unsigned int    digits[4];

// Main program loop
void loop()
    // Recalculate the digits
    digits[0] = minutes / 10;
    digits[1] = minutes - digits[0];
    digits[2] = hours / 10;
    digits[3] = hours - digits[2];

    // Refresh the display
    for (a = 0; a < 4; a++)
        digitalWrite(anodes[a], HIGH);       
        digitalWrite(anodes[a], LOW);

// Clock Control Interrupt Routine
// Interrupt period is 1024 us
    microseconds += TIMEBASE;
    if (microseconds >= 9999)
        // 1/10 second
        deciseconds += 1;
        microseconds -= 10000
        if (deciseconds >= 99)
            // second
            seconds += 1;
            deciseconds = 0;
            if (seconds >= 59)
                // minute
                minutes += 1;
                seconds = 0;
                if (minutes >= 59)
                    // hour
                    hours += 1;
                    minutes = 0;
                    if (hours >= 23)
                        hours = 0;
            // Once a second, flash the LED
            digitalWrite(LED, LOW);  // LED on
        if (deciseconds >= 2)
            // 200 ms flash
            digitalWrite(LED, HIGH); // LED off

This project, when controlled by an Arduino Teensie, is about as simple and low cost as I can make it.  With a little bit of additional programming, one can make the thing display the time, ring an alarm, roll the numbers, scroll, flash and go crazy every once in a while, just to break the monotony!

- .. -- . / .. ... / --- -. / --- ..- .-. / ... .. -.. .

Have fun, but be safe!


Saturday, May 13, 2017

WannaCrypt, WannaCry, NotPetya

It is a big Microsoft security waste of time, again and again...

The quality of computer software bears no relationship to its price.  
In fact, the best software tends to be free.

I usually tell friends and family that I don't do Windows.  They then assume that I am one of those weird Mac users.  They are correct of course - I am weird and I use a Mac too - amongst many other types of machines, including those most annoying Windows machines...

Long ago, I used to tell people that they can use Microsoft software:
It is rather buggy, but it is cheap.  
Nowadays however, it is still buggy and very expensive,
especially so when you factor in the maintenance cost.

If you are one of those weird people who insists on wasting money on expensive bad quality software, then you should immediately read this:

and this:

and now this:

Unless you are a medical doctor and it is a matter of life or limb, please don't ask me to help you fix your messed up Windows machine, unless you want OpenBSD or Linux installed on it.  BSD and Linux can do everything that Windows does and more, doesn't cost anything and doesn't break all the time - a good deal if ever there was one.

As O'l Bob Marley said:  
"No Windows, No Cry..."

Note that most Windows malware worms spread through the File and Print server (SMB/CIFS). Therefore the main thing with Windows is to never, ever, ever enable 'File and Printer Sharing', since that starts up a server process and once running, it is a hassle to disable it again. You can disable File and Printer sharing in Network and Sharing Center, but it will *still* run - you need to stop it manually also.

After disabling File and Printer Sharing in the Network Sharing Center, you can stop the server with this command:
c:\> net stop server

Of course, you need to run the Windows command shell as administrator to do the above.

Note that data that isn't backed up, doesn't really exist...

Don't forget to send Thank You letters to Bill Gates, Steve Balmer and Satya Nadella.  They are ultimately responsible for this mess, for creating and aggressively marketing computer software that is not fit for purpose

-. --- / .-- .. -. -.. --- .-- ... --..-- / -. --- / -.-. .-. -.--



Thursday, April 20, 2017

Stochastic Systems Analysis

Engineers will recognize that this post is about trading, others may wonder what is a stochastic system.

Market price data is a time series of mostly random data, a stochastic system.  So a Day Trader is a Stochastic Systems Analyst - now doesn't that sound fancy!

Investopedia Simulator
The one thing I learned over the years is that if you want to make money from trading stocks or futures, then you need to have a simple and well defined system, with easy to calculate entry and exit criteria, otherwise you will eventually lose your shirt.

As Douglas Adams, the famous galactic economist of Bistromatics fame said:  When it is infinitely improbably that something will ever happen, it will happen almost immediately.

Price data is mostly random.  In retrospect it seems to follow a trend and engineers and computer programmers realize that they can easily devise a system to do automated trading based on historical and real-time data with some Digital Signal Processing applied to detect a signal in the noise.  I tried it too, but downloading price data and adjusting for stock splits and companies changing names and ticker symbols is a huge problem and if you then devise an automated trading system it may work for a while, but eventually it will diverge and you can lose your shirt.   

An automated trading system requires constant work, which defeats the original idea!

Mutual Funds

The only people that make money from mutual funds are the people that manage the mutual funds.  In Canada, they make money twice - when you buy and when you sell - a total rip-off.   

So instead of investing in a mutual fund of a bank, rather buy the stock of the bank, pocket the dividends and stop complaining about how they rip people off.

Long Plays

One problem is that any trade also influences the market - it is a feedback loop.  One can get price volatility with a purchase of only a few hundred shares in a blue chip company, if nobody is selling at that nanosecond instant.  Now imagine trying to manage a pension fund and buying and selling tens of thousands of shares.  This is why there is always room for the little guy - us.

To build real wealth, you need to be in the market for the long haul - decades.  The pension funds periodically 'balance their portfolios' - usually in March and October and that can cause wild volatility and crashes.  So a good long strategy is to buy in March and sell in September, then buy in November and sell in February.  If you are in cash, then you can buy the crash.  If it doesn't crash, just buy it all back again a month later.

I just bought four blue chip stocks in my RRSP  (my buy in March entry) and within a few minutes lost $200 - bah, humbug.  If you trade long, don't look at the real-time graphs! 

To control the risk, buy at least 5 stocks for your RRSP and set stop loss orders 5% to 7% down - but you need to keep resetting them, since they expire after a few days.  Effectively, you can run your own mutual fund and keep the management fees.

Day Trading

Day trading is for making a little money to live on now.  Investing is to make a lot of money to retire on in 30 years.  It requires different strategies.

Day trading is also a nice mental exercise to keep your brain from atrophying while you wait for the  market grass to grow.

There are few millionaire day traders.  There are however many people who sell books and training courses on day trading.  Selling books and training courses is clearly more profitable and less risky!

You have to read two or three books (No more than three. Stop and practise instead), to learn the lingo and methods, just don't believe in it as the one true gospel - try to find the idea behind it

Bear in mind that if someone sounds like a second hand car salesman selling a lemon, he probably is. 

Day trading is limited by what a human being can manage.  People can juggle 3 to 11 complicated things at the same time in their head.  The result is that a futures trading system is best limited to a pot of $5000 to $15000 and trades limited to 1 to 5 per day.

As Lazarus Long, the famous American futurologist said: The odds are terrible, but if you don't play, you can't win.

If you trade with E-mini, buy and sell one or two contracts at a time, then you could make $50 to $200 a day.  Buy a contract and sell when it is up by 8 ticks or down by 3 ticks.  On average you should win some - not much, but enough to pay the bills.  If you don't know what you are doing yet, you can loose $50 to $200 a day - so you have to practise in a simulator.

The methods are the same for day trading stocks, but you would need a bigger pot of money - about $25,000 to play with -

In general, don't trade with more than 1% of your pot of money to keep the risk down and test your system in a simulator for a week or two before turning loose on live trades.  If you only make one trade a day, then two weeks of practise is only 10 dummy trades - precious little.  If you cannot make money in a simulator, then real live trading will be even worse and you will join the crowd of suckers at the bottom of the heap.

If you are good, then you should invest half your monthly profit for the long haul in blue chips.

Trading Software

Trading software programs now are a dime a dozen - there is no need to write your own anymore.  Ninjatrader is popular for futures trading, but your own bank probably has a system that is good enough - a fancy expensive program will not magically make you a better trader if you only do one trade per day.

TD Bank for example has four trading systems that I know of, the default Webbroker system, a more advanced Java based Advanced Dashboard program that you can download from the Webbroker site which provides real-time data and also ThinkOrSwim and the related TD Ameritrade -

The lowest cost reputable broker that I know of is Interactive Brokers.  If you do one trade per day of a few hundred stocks/futures, then they may be the best, but figuring out exactly how much it will cost you is difficult, since they have a complex pricing schedule -

Depending on how much you trade and how much you have in your account, a trading platform will cost about $60 per month.  So the cost is not ridiculous if you are dedicated to trading and not just doing it for fun.  Also write down the help phone number of your trading system, so that you can call them when you have a computer or network problem and cancel your trades.

Historical Analysis vs Reality

If you find an elevator stock that goes up and down, then you can trade it and make money every few hours or days simply by sitting and staring at the real-time price graph, but eventually something will change.  There will be a war, a plane crash, a court case, a dishonest CFO... and you may loose your shirt.

It is therefore important to use simple statistics and stop loss orders to control the risks and identify opportunities, but you should steer clear from very complex systems, since finely tuned systems will only work in the short term.   

By the time that you buy the book of a successful trader, he is already retired and canoeing in the Caribbean and his lucky streak has long since ended.

You can learn from the past, but the past will never be repeated exactly the same way.

Simple Statistical Analysis

The Greek philosopher Democritus said that a thing is worth whatever someone is prepared to pay for it and the stock market to me resembles a pack of lemmings running over a cliff, with traders trying to outbid each other before the inevitable market crash.

The first book I read on stock trading was clearly written by a trader who was just lucky (or had inside information).  His 'head and shoulders system' was impossible to recreate and everything worked only in retrospect, not with real data.  Successful plays almost always resemble a 'head and shoulders' graph - after the fact.   It is very easy to predict that you should have bought a stock last week.

Much later, I encountered a book by Burton Malkiel,  'A random walk down Wall Street', which rigorously proved with Montecarlo analysis that most hedge and mutual fund traders were just lucky and will blow up eventually.  It is a very dispiriting book, but it proves that one cannot predict the market by looking at price data only - you need to have inside information also.  Ordinary mortals don't have inside information, but big traders and institutions do (the managers sit on multiple company boards and are all golf buddies) and they are the ones leading the pack of lemmings.

The front running big lemmings will never admit to randomness explaining their strings of successful trades, since that will make them look dumb and they will not admit to having inside information, since that is supposed to be illegal.

Successful trading systems for small time investors invariably boil down to a price moving average and a standard deviation calculation, plus a volume indicator.  A Bollinger band is very good to see what the price is doing, while a trading volume indicator (there are many), can identify what the other lemmings are doing and the trick then, is to run with the pack just a little bit and stop, before they fall off the cliff.

The Lemming Effect

Successful trading strategies look not only at price, but also at volume and track price volatility to get a handle on the Lemming Effect.  The big lemmings out in front of the market are the institutional investors with inside information - successful small trading systems calculate how to follow the fat lemmings for a little while.

Bulls make money, and bears make money, but pigs get slaughtered.

When the market is stable (going sideways) or going up (bullish), then you can buy low and sell high and make money, a little bit at a time.

However, when the market is stable or going down (bearish), then you need to reverse things to (short) sell high and buy low.  It is exactly the same operation, just reversed in time.

As Shane, the famous American cowboy said: If god didn't have a sense of humour, then he would be a very lonely man.

Here is a good write-up on Bollinger Band trading -

So how do you know when to buy long or sell short?  Look at the long term trend - where are the lead lemmings going.  Is the market going up, or going down and if you get a losing streak of five dud trades - reverse what you are doing - don't fight the market - flip your strategy around - ping-pong -

Practise Makes Perfect

It all sounds easy until you try to place a trade, then you wonder what amount to enter at the stop buy, what is the ADR and the profit target and what to enter in the stop loss and would the profit be big enough to cover the trading fees - and by the time you figured it all out, the opportunity has passed.  

Therefore you have to make a spread sheet or prepare a programmable calculator with your formulas so that you can enter and place a trade quickly.  You need to keep a spread sheet of your trades anyway, since come tax time, you will need it.

How do you find a stock to day trade?  Well, that is the boring part.  Here are some clues:
  • Daily volume between 500K->25M shares
  • Stock price < $30
  • Range between hi & low to be > 50c
  • Look for a 20c gain on 1000 shares, for a $200 day.
There are not all that many.  You could play with Bank of America in a simulator to get started.    

Keep it simple, and practise in a simulator or for a few weeks before you risk real hard earned moolah.

La voila!


Monday, March 13, 2017

Troubleshooting Video Streaming

Casting About

Video multicast routing usually 'Just Works'.  Occasionally it doesn't work at all, or the video streams for a few minutes only and then one is left scratching one's head...

To find out what is wrong, one has to know how to analyze a playback streaming problem when it happens.

Here is something to read on streaming, multicasting and the Internet Group Management Protocol (IGMP):


Multicast Version

Sometimes is is necessary to use a specific version of IGMP to get things to work. One can force the IGMP version in the /proc/sys/net/ipv4/conf/eth0/force_igmp_version to 0 (auto), 2 or 3.

Multicast Routing

The multicast address subnet is the 224.x.y.z address range.  For routing to work at all, you need a multicast route, set with a command like this:
# route add -net netmask dev eth0

That is if your ethernet port is eth0.  Mine is enp0s26u1u1 at the moment - I'm using a USB ethernet device on this machine, as the RJ45 socket has a solder problem, resulting in a Welsh device name.

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         UG    1024   0        0 enp0s26u1u1   U     0      0        0 enp0s26u1u1 UGH   1      0        0 enp0s26u1u1       U     0      0        0 enp0s26u1u1

The last line above is the multicast route and that being missing/wrong is 99.999% of the time the actual problem.


Multicast Groups

For multicasting to work, your machine needs to be a member of the multicast group.  This is set at start-up using the IGMP protocol if your route is correct. 

You can list group memberships with netstat:

$ netstat -gn

IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1
em1             1
wlp3s0          1
enp0s26u1u      1
enp0s26u1u      1
lo              1      ff02::1
lo              1      ff01::1
em1             1      ff02::1
em1             1      ff01::1
wlp3s0          1      ff02::1
wlp3s0          1      ff01::1
enp0s26u1u1     1      ff02::1:ff00:10e
enp0s26u1u1     1      ff02::1
enp0s26u1u1     1      ff01::1

Group Subscription

For streaming to work, the client (video player) has to subscribe to the stream with an IGMP Join message.  The routers listen to the IGMP messages (IGMP snooping) and use the data to send multicast traffic only to members of the group.  Otherwise the LAN will be flooded with streaming data.

The network switches keep track of the MAC addresses used by multicast subscribers and this list expires after 500 to 600 seconds.  The subscription therefore needs to be renewed periodically by  IGMP Query and IGMP Report messages, otherwise the switch will stop forwarding the stream and the video player will stop.

To confirm that the subscription is happening and that the video player is configured right, start tcpdump on the server and then start the video player.  You should see something like this:

# tcpdump -i eth0 igmp
tcpdump: listening on eth0
00:20:09.007094 switch-a.stage > ALL-SYSTEMS.MCAST.NET: igmp query v2 [max resp time 20] [ttl 1]
00:20:09.241946 > igmp v2 report (DF) [tos 0xc0]  [ttl 1]
00:20:10.472159 > igmp v2 report (DF) [tos 0xc0]  [ttl 1]

Multicast Ping

You can also ping all members of the group, if the machines are configured to respond to broadcast/multicast pings:

$ ping
PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=64 time=2.03 ms
64 bytes from icmp_seq=1 ttl=64 time=2.03 ms (DUP!)
64 bytes from icmp_seq=2 ttl=64 time=2.05 ms
64 bytes from icmp_seq=2 ttl=64 time=2.05 ms (DUP!)

The /proc filesystem variables /proc/net/ipv4/igmp* are useful to see the groups that your host is currently subscribed to. So you can go and dig there yourself if you don't have netstat installed.

The proc File System

Broadcast ping responses are usually turned off, to prevent Denial Of Service attacks.  Ping responses are controlled via /proc as well and can be turned on with:

echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all

echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

or on machines with systemd:
sysctl net.ipv4.icmp_echo_ignore_broadcasts=0

Next time you experience a problem, check your IP device and route setup, check the groups and do a ping to see what is going on.

Phun With Streaming

Now let's have some fun with other streaming tools, ffmpeg and gstreamer.

Record Your Laptop PC Camera With ffmpeg

There are many ways to do this, so let's explore one of the roundabout ways and record the camera to a file, while also playing it to see what is going on.

Use ffmpeg to record the camera:
$ ffmpeg -f v4l2 -s 800x600 -i /dev/video0 -r 30 -f matroska -vcodec h264 /tmp/camera.mkv &

Give the recorder a head start, wait a few seconds then play the file:
$ ffplay /tmp/camera.mkv

Now you have a slow mirror - yeah, da...

To stop, run 'killall ffmpeg' and 'killall ffplay'.

Create a Video Test Pattern With Gstreamer

It can be very useful to generate a test pattern, just to get started with gstreamer:
$ gst-launch-0.10 videotestsrc ! ffmpegcolorspace ! autovideosink

You can make a test pattern with gstreamer as explained above, or you can play a video. Here is the jackpot with every test pattern you may ever want and few more: 

Send and Receive Audio with Gstreamer

Streaming audio between two machines can be very useful for an intercom or radio over ethernet.  Here is something to get you started, by sending and receiving audio on the same machine.

Gstreamer transmit in one terminal:
$ gst-launch-0.10 -v audiotestsrc do-timestamp=false ! audioconvert ! audio/x-raw-int,channels=1,depth=16,width=16,rate=8000 ! rtpL16pay ! udpsink host=localhost port=1234

Gstreamer receive in another terminal:
$ gst-launch-0.10 -v udpsrc port=1234 ! "application/x-rtp,media=(string)audio, clock-rate=(int)8050, width=16, depth=16, encoding-name=(string)L16, encoding-params=(string)1, channels=(int)1, channel-positions=(int)1, payload=(int)96" ! rtpL16depay ! audioconvert ! alsasink sync=false

Talking to yourself gets boring quickly though.

To ensure that two different machines will work together without an ever increasing time delay due to slight clock differences, make the consumer slightly faster than the producer by using a clock rate of 8050 instead of 8000 Hz.

To send audio between two machines, use the other machine IP address instead of localhost and to make an intercom, you need to make both streams bidirectional - by combining the two examples using another ! to concatenate them.

La voila!