# Wishful Coding

Didn't you ever wish your computer understood you?

## Hybrid Headphone Valve Amplifier

In university we currently learn about MOSFETs and BJTs, but valves(vacuum tubes) are completely absent from the material. Of course valves are essentially obsolete, and I’ve seen before that they try to avoid letting students work with hundreds of volts, but valves interest me.

I mean, how could you not be interested in glowing tubes with vacuum. The other part is that some audiophiles claim they produce superior sound. But sometimes audiophiles are full of bullshit, so I’d better verify all those claims for myself.

### The plan

I might eventually actually use hundreds of volts on my valves, but for now the plan is to operate them at a low voltage and use modern transistors to create an output buffer.

I draw inspiration from this 12v headphone amp and this document about triodes at low voltages.

The point is that you can operate valves at low voltages, but they just provide very low current. So you need a separate output stage to drive anything.

The referenced designs use an A-class source-follower for the output. Some claim that even though they have terrible efficiency, can have less distortion than AB-class output stages. This will be tested of course.

For MOSFETs and BJTs I learned how to use mathematical models to design amplifier stages. For valves, I could not find any such models. I asked the professor, and he referred me to the MOSFET equations in ohmic mode:

But with K and $V_{th}$ not known, this is not very useful. I asked around a bit, and it seems that people just take the graphs from the datasheet and use them for biasing. But as said in the linked PDF, voltages in the datasheet range from 50 to 200 volt usually, so we’ll have to make our own graphs.

To do so, I used the NI MyDAQ arbitrary waveform generator, oscilloscope, and an XY-plot to plot the relation between grid voltage and anode current at several anode voltages.

In this graph I plotted the anode current for anode voltages from 1v to 10v and grid voltages from -1v to 1v. Current never exceeds 2.2mA for 10v.

But valves are always biased with negative voltages, even though a positive voltage clearly gives more current. To see why, we need to look at grid current.

As can be seen, for positive grid voltages, the control grid starts to leak current to the cathode, which is undesirable. But as described in the linked PDF, this current itself can be used to bias the valve.

### Class war

I read a lot about the advantages and disadvantages of A-class, B-class and AB-class amplifiers, but most of it is not very scientific.

One thing is for sure, A-class uses a lot more power and is at best 25% efficient. When the output is at rest, a constant current is just dissipated as heat. When the output is low, that is the amount of current that can be drawn. But when the output is high, it needs to supply double that current, of which half is just burned and the other half flows into the output.

A more interesting and vague topic is distortion. It is claimed that distortion in A-class amps is mainly even harmonics and B-class distortion is mainly odd harmonics. Why this is the case and why the one would be superior is not frequently explained.

Why even harmonics are more pleasing is understandable. First, it is important to understand that harmonics drop off in amplitude very quickly, so you mostly hear the first few. If you look at the first harmonics, you see that the even ones are octaves, while the odd ones are fifths and thirds.

Octaves are just the same note, but twice as high. It seems reasonable to assume this sounds more pure and full than odd harmonics, which are other notes altogether.

But why, how, and by how much?

The voltage-current relationship of a BJT is

If you squint a bit, you could say that the output current is an e-power of the input voltage. Taking a cosine for the input, we can formulate equations for A-class and B-class amps.

To take a look at the harmonics we can calculate the Fourier series for these functions. Except the indefinite integral of this equation does not exist. So instead I kindly asked my CPU to perform the following integral numerically for me. (the sine coefficients are trivially 0 since $f(x)=f(-x)$)

Harmonics 1 2 3 4 5 6
A-class 1.1303 0.2714 0.0443 0.0054 0.0005 0
B-class 1.1303 0 0.0443 0 0.0005 0

From this you can clearly see that odd B-class harmonics are identical to the A-class harmonics. The interesting part is that the even harmonics are gone. They simply cancel out.

Of coures, you can build either class with very low distortion, but for the same biasing/distortion level, B-class will have less overall distortion, but concentrated entirely in odd harmonics. Meanwhile, A-class has a more balanced harmonic distribution, which might sound more pleasing if the overall distortion is low enough.

### Initial design

Initially, I’m mostly going to use the design of the linked headphone amp and iterate based on that. There is one major change though.

While transistors operated in the saturated region don’t depend on drain/collector voltage, valve anode current depends linearly on anode voltage.

As described, the linked headphone amplifier uses a variable resistor that is tuned such that at rest the output voltage sits at the center of the available range, meaning there is only 6v across the valve instead of 12v.

Instead I used a current mirror so that there is a constant 11.4v across the valve. The current is then mirrored to the other BJT, where it drops over the resistor connected to the buffer to create the output signal.

Normal cathode biasing is used for now, but grid-leak biasing will also be tested later.

### AB-class design

The gain stage for this design is unchanged, but an AB-class output stage is added.

It turns out the input impedance of the output stage is $1k//1k=500\Omega$, which created a nice current divider with the current mirror. So I had to add another follower stage as a buffer.

### Execution

I built the first design and after making sure it looks okay on the oscilloscope I connected it to an electric guitar and an old pair of headphones, and it worked!

Initially I used a cheap wall wart to power the amplifier, but this gave a lot of noise and hum. Now I switched to an old PC power supply, which is not perfect either, but at least the 50Hz hum is gone.

For the A-class circuit the THD is about 1% to 2% depending on god knows what.

The AB-class circuit does not have the nice space-heater output stage, and performs slightly worse than the A-class circuit with a THD of around 2% to 3%.

Interestingly, the harmonics are about the same. Both have strong even harmonics and nearly no odd harmonics. Whether these harmonics are caused by the gain or the output stage remains to be seen. So much for theory I guess.

To be continued…

## If Everyone Would Just...

While everyone was making new years resolutions for themselves, I was making new years resolutions for the world. Or rather, thinking how change happens.

It seems people are hardly able to change themselves, even if they set out to do so. So how can we even begin to think about changing humanity as a whole? It seems we just keep doing whatever is easiest/best/most comfortable, and change only happens when something easier/better/more comfortable comes around.

Sometimes you read about these civilizations that seemed to be really advanced for their time, but then just sort of died out. Looking back it is often obvious why, and you wonder if they did not see it coming. But what if they saw it coming all along, but were unable to change their ways?

The only mechanism we have for global change is governance. But as Douglas Adams explains

One of the many major problems with governing people is that of whom you get to do it; or rather of who manages to get people to let them do it to them: It is a well known fact, that those people who most want to rule people are, ipso facto, those least suited to do it. Anyone who is capable of getting themselves into a position of power should on no account be allowed to do the job.

Often times I have an idea to make something somewhere a little better. No grand plans to solve world hunger, just small things. Often by means of sharing or exchanging information or goods. Without exception, these ideas include the sentence “If everyone would just…” and you quickly learn about the Network Effect.

I’m just back from 32c3, where I saw a number of interesting art projects. Many of these projects used technology not to provide utility, but to convey a message. Thinking about all the above, I built The World Improvement Server; a caricature of global change.

Th World Improvement Server runs a program that speaks the SUN-RPC rwall protocol, listening for your world improvement ideas. These ideas are then broadcast over the QOTD protocol to anyone who cares to listen.

To start improving the world, simply run rwall 37.247.53.27 and type your idea, followed by EOF.

To learn more about ways to improve the world, simply telnet 37.247.53.27 17, which as of 2 January 2016 returns

\$ telnet 37.247.53.27 17
Trying 37.247.53.27...
Connected to 37.247.53.27.
Escape character is '^]'.

If everyone would just [...]
the world would be a much better place!

Submit global world improvement using rwall.

Remote Broadcast Message from pepijn@pepijn-Latitude-E6420
(/dev/pts/0) at 18:19 ...

If everyone would just use this service to make the world a better place
the world would be a better place.

Connection closed by foreign host.


## The relation between harmonics and parallel resistance

I’m building a simple synth on a breadboard, where the frequency is defined as $f=\frac{1}{RC}$, so by using different resistors with a row of buttons, different tones can be made. But what if you press two buttons simultaneously? You get 2 parallel resistors, giving:

I stumbled on this while playing with it, and the combined tones seem to be nice intervals sometimes. So I started to wonder if there was a connection.

In 12-tone equal temperament, the ratio of the frequency between two notes is $\sqrt[12]{2}$, so we could just plug it in and see what comes out. Take a series of notes:

And a series of a combined frequencies:

Plotting these in Matlab gives the following result

As can be seen, some combined tones are indeed quite close, but most not exactly. But then equal temperament does not have exact harmonics either. So we still do not know if we are generating actual harmonics, or just frequencies that happen to be close.

So what if instead of starting with $\sqrt[12]{2}$, we start with integer multiples of the base frequency and fold then back into one octave. This gives:

Plugging that into the parallel resistance equation, we can begin to search for exact harmonics giving exact harmonics.

 Combined Harmonic Name Button ratio Name 17 Minor second 9 Major second 9 Major second 5 Major third 19 Minor third 11 Tritone 5 Major third 3 Fifth 11 Tritone 7 Minor seventh 3 Fifth 2 Octave 2 Octave 1 Prime

And we can indeed verify that two resistors with a 2:1 ratio give a fifth (3:2):

Likewise a resistor ratio for a fifth gives a major third (5:3)

Math, music, physics. So beautiful.

Update:

As pointed out by Darius Bacon, this might not be a complete surprise, as there is a striking similarity between parallel resistance and the harmonic mean.

Wikipedia also has the following to say about the harmonic series:

Its name derives from the concept of overtones, or harmonics in music: the wavelengths of the overtones of a vibrating string are 1/2, 1/3, 1/4, etc., of the string’s fundamental wavelength. Every term of the series after the first is the harmonic mean of the neighbouring terms; the phrase harmonic mean likewise derives from music.