Category Archives: Electronics

A completely new bass-line, in every track!

I wanted to blog about this back in the beginning of November when I saw something at the American Music Awards that really bugged me, but was only just reminded of it now, so here goes.

TL;DR: Electronic music producers create novel sounds and have performances that move an audience continuously over a period of time greater than an hour and in a way a ‘rock band’ can only dream of – except they don’t have the creative forethought to dream it – too busy sounding like everyone else. It’s shitty to dismiss electronic music producers as cheating with computers – everyone is using new music production methods (based on computers and digital technology). I don’t include Pauly D in the group of electronic music producers. I compare ‘Portugal. The Man’ to Nickelback. This is long. Even the TL;DR is long. You have been warned. 

Music is a funny thing. Ever since reading ‘This is your brain on music’ I’ve become fascinated with music, way beyond its personal hedonistic value. Music, for most people, has an amazing power to tap into and alter our emotional state. How? Well it’s a bit of a mystery. Is it a side-product of language? Is it proto-language? Is it its own thing in its own right? I tend towards the later and maybe one day I’ll write up why.

One thing is for sure and that is, music is personal. What moves you will not move another person. There is a lot of cognition and emotion attached to this. We tend to like what we hear. My mum loved classical music and English church hymns. This is what she listened to as a youngster and learnt to play on piano. Not-so-strangely enough, so do I. Do I listen to classical music a lot? Not really. Do I like English church services? That’s a big no. Am I even a patriotic English boy? Not on your life. But, ‘I vow to thee my country’, will make me cry tears of joy? sadness? I don’t even know. But, Every. Fucking. Time. It’s in me.

I also grew up listening to 80’s new wave and synth-pop. The analog synthesizer, as an instrument, modulated my brain and became in many respects what music is. Certainly a big part of it. The precise saw-wave and square-wave tone with their rich overtones, filtered in imaginative ways, can create sounds and therefore mood in a plethora of ways that simply exceeds what mechanical construction of string (guitar etc) and woodwind (saxophone etc) instruments can. And yet, no matter the source, so long as a sound is composed of even (woodwind) or odd (string) harmonics, the rules of harmony still apply. Major and minor chords and keys still work with electronic sounds.

So what’s the deal with the AMAs 2017? Well, see below:

This flashed up during a performance by ‘Portugal. The Man’. Lets unpack this…

Firstly, let’s go with the obvious. Ummmm, the projection is clearly computer driven. And no matter what you think, the visuals are a big part of a performance. I’d say a massive part of what they were trying to express at this moment. This ought to fling the least sensitive and most mis-calibrated irony meter into orbit. But let’s keep going. The instruments were designed/constructed/built without any aid from computers? Their amplification was entirely driven by analog electronics? The mastering and propagation of their sounds, all done with transistors biased for analog, not digital response? Have they not honed their sound in response to how they sound, using digital circuits? Of course they have. Their sound would not be what it is without them using computers and digital technology. They would sound different without it. No musician, immersed in a modern technology world, is immune to the influence of the digitization of sound and how that informs their sound. I’m not saying they would sound worse without the digital influence, but they would have a different sound. A sound that might even irk some of their fan base. Lastly on this point, I guess their music teachers weren’t up there either so they had no music teachers? Of course they did and I’m sure they would acknowledge that. Ugh, but they show no appreciation for their source of musical inspiration – or they aren’t aware of it.

But lets get a bit deeper and controversial. What really got under my skin is the hubris and arrogance of that computer-generated screen projection. Clearly, what ‘Portugal. The Man’ are saying is that they have real skill in music and performance because they don’t need computers – DJs suck. DJs cheat. DJs don’t have talent.

Ok, lets go over this.

First, lets make a distinction between DJs and Dj/Producers. DJing is beatmixing and paying a little attention to matching and moving between keys. You can teach a misbehaving macaque monkey to DJ. Some talent – presumably beyond what a macaque can handle – goes into making a set move the listener through a 2+hour period of time. So lets put aside that local club DJ who spins tracks between lines of coke and most likely will never grow up – like that macaque. Think DJ Pauly D. Pauly D might compose some tracks, but they are largely panned as shit. He doesn’t have a following beyond girls who want to root him.

DJ/Producers (just producers or electronic producers from now on) on the other hand are musicians that compose predominantly, although not exclusively, in the medium of computers and entirely synthetic sounds or samples. It should be noted that producers will use ‘conventional’ instruments, particularly guitars, piano and vocalists, from time to time. I assert that electronic producers are far more open to the potential of instruments and novel sounds than any other musician, by many orders of magnitude. And this is what I love about the medium. I’m never bored. Every new track (from a talented producer – the kind I listen to) has so many new elements to its sound. You just have to be open to it to hear it all.

Let me explain all this by example – not even a good example – just what I have at hand this minute. Right now I’m listening to a set by Oliver Smith from ABGT250 (I was there live for the performance!) . Its an hour long set with 11 different songs in it. The transition from ‘Solina’ to ‘On the Moon’ introduces a completely different base-line sound. He jumps from E major to B minor through a percussive part (otherwise the keys would clash). Whats so impressive? Well for starters, new base-line sound!!! Im sorry but your typical 4 string bass guitar sounds the fucking same in every track. Its boring. F-G-C-C-F-G-C-C chords over and over. But the availability of different bass sounds, not the chords, it’s timbre, tailored to the different chord pattern of each of the tracks in Oliver Smiths set is genuinely creative. There is a completely new-sounding bass-line in every track! Now if you don’t follow all that or it just doesn’t make sense to you or you don’t care, I don’t hate you. I get it. You haven’t trained your brain to understand the complexity. I doubt the complexity of northern Australian didgeridoo music means as much to you as, well, the natives of northern Australia. But that doesn’t mean there is no talent or intricate detail in their music. The creative elements that go into Oliver Smiths performance were put down when crafting the bass line for each song, along with all other elements from melody to percussion through to the decision to mix these two tracks together (after production of each track), knowing full well how to transition would work and what mood it would produce.

And to be completely honest now, a computer/robot could play a bass guitar in a rock band. An electronic producer plays a computer. Who is the well trained macaque?

None of this is to say that electronic music and producers work in a fantastic world without its own limitations and problems. Certainly, live improvisation is a problem. I’d love to see it, and I think it is possible. But to be honest I don’t think most people want it, which is a bit of a shame. Most people want their band to sound like they do on their phone. Part of the emotional aspect of music is the understanding and recognition of what you are listening to – its why modern western music for the last 500 years has been using the same chords and key signatures and largely the same kind of instruments. A poorly appreciated element of music is the element of surprise but I haven’t had a rock band surprise me musically either. Well, not since Nirvana. I was about 18 then and all music was still new to my developing brain. ‘Portugal. The Man’ sound like a piss weak Nirvana – couldn’t quite make it to the bottom of the bottle so they sound just like everyone else who couldn’t go all out. No edge. I’ve heard it before.

So fuck that ‘no computers up here’ message. It’s arrogant and dismissive – and simply wrong. Would ‘Portugal. The Man’ be so dismissive of Kabuki or Carnatic (south Indian) music? I doubt it. But do they understand it? I doubt it. Do they understand how much they sound like Nickelback? I hope they never find out.

Random Number Generation Part II – Some Data Analysis

Following on from my last post, I’ve been trying to read the voltage values that are generated from the random number generator I put together. I was initially using an arduino unit so I could control the voltage output down to below 1 volt. Ultimately I want to read the values with an ESP8266 unit running micropython so I can upload the values to an internet based data logger and the ESP8266 is only 1 volt tolerant on its analog-to-digital pin. So, the voltage has to be controlled and I didn’t mind blowing up one of my arduinos (which are 5 volt tolerant). The voltage divider is now composed of a 10k resistor from positive rail to analog out and a 1k resistor from analog out to ground.

The ardunio was giving me readings up to about 180 unit out of 1024 which gives about 180/1024 * 5V = 0.8789 volts… nice and safe.

Reading these voltages with the ESP8266 gave some interesting results. The code to do this was very simple:

from machine import ADC

import time
adc = ADC(0)            # create ADC object on ADC pin

for i in range(1000000):
    print(adc.read(), end=',')
    time.sleep_ms(10)

This reads a voltage every 10 milliseconds from the analog pin and reports one million of them. It took about 2.8 hours and the output was redirected to a file of type csv with this command on my mac:

ampy --port /dev/tty.SLAB_USBtoUART run readanalog.py > avalanche_noise.csv

‘ampy’ is a nice program from Adafruit, which you can learn more about here.

I then analyzed this data in python using the jupyter web interface. A histogram plot of the values looks like this:

The values range from 135 to 674 with a mean of ~292.6. This distribution is obviously skewed too so it’s not gaussian distributed – maybe Poisson?  But the most striking thing is the lines of empty space (no blue) – is this a plotting problem or are certain values skipped during the reads? Well lets zoom in around the top of the distribution.

So its true, some values are just never recorded. Its hard to believe the voltages from the circuit would do this. Also, its clear from the numbers that every 12th value is skipped. I think this must be an error in the way micropython reads the register so I’ll probably post this somewhere in the micropython forums and see if this is true or if I’m just doing something wrong.

Next I tried to fit this distribution to a gaussian curve. It failed. Here is the python code:

import math
import numpy as np
import matplotlib.pyplot as plt
import scipy as scipy
import scipy.stats as stats
from scipy.optimize import curve_fit
from scipy.misc import factorial
from scipy.stats import norm
%matplotlib inline          # needed for plotting in jupyter

data_p = data # not really needed

fig = plt.figure(figsize=(20, 8))
entries, bin_edges, patches = plt.hist(data_p, bins=(int(np.amax(data_p)-np.amin(data_p))), range=[np.amin(data_p),np.amax(data_p)], normed=True)
bin_middles = 0.5*(bin_edges[1:] + bin_edges[:-1])

# poisson function, parameter lamb is the fit parameter
def gauss(x, *p):
 A, mu, sigma = p
 return A*np.exp(-(x-mu)**2/(2.*sigma**2))

# fit with curve_fit
p0 = [1., 260., 30.] # initial guesses for height, mean and SD

parameters, cov_matrix = curve_fit(gauss, bin_middles, entries, p0 = p0) 
print parameters # print results
print np.sqrt(np.diag(cov_matrix)) # print errors of fit

# plot poisson-deviation with fitted parameter
x_plot = np.linspace(0, np.amax(data_p), 1000)
plt.plot(x_plot, gauss(x_plot, *parameters), 'r-', lw=4)
plt.show()

Output:

This doesn’t fit very well. But it does reveal the skew present in the data. It does in some ways look poisson distributed (you can read more about this distribution here) and electrical noise is typically poisson distributed because of the discrete nature of electrical charge (read about this and shot noise here).

Mathematical aside:

One way I like to think of the Poisson distribution and Poisson processes is as follows. They arise from discrete events that must always have a positive value – this is not true for Gaussian distributions. So, in our circuit, current (electrons) flow or do not flow. When they flow, a discrete (integer) number of them flow. There is a lower limit on the number that can jump the gap. That number is zero. The gaussian distribution is the limit of a binomial distribution as the number of events goes to infinity. The binomial distribution is based on the idea that an event either happens or does not. (N.B. this is different from the Poisson case because in that case, events either happen 1 or more times or do not happen). If we accumulate binomial events, there is no limit to how many ‘yes’ or ‘no’ events can happen so some of the distribution must extend as far as the number of events recorded – for gaussian this limit goes to infinity and negative infinity. The Poisson distribution doesn’t act this way. If we try and model the Poisson distribution as an infinite binomial distribution we quickly realize that while we can get an infinite number of ‘zero value’ events as well (with low probability) there is more than one other alternative. So the distribution must take into account these many possible values which stretches the distribution in the positive direction while there is a still a hard limit at zero. We can shift the Poisson distribution so ‘N’ zero-value trials will be plotted at ‘-N’ (like we would for a binomial distribution) but on the positive size, the curve would extend past ‘+N’ because some of those trials can have a value of more than 1.

Enough qualitative description of distributions…

Using a poisson function instead like this:

def poisson(k, lamb):
 return (lamb**k/factorial(k)) * np.exp(-lamb)

and we get this:

It doesn’t converge at all.

Yikes, whats going on?

Well my thought was that the numbers I’m reading don’t match the number of electrons actually flowing. This signal is amplified by the transistors and then quantized, not in nature, but by the ADC converter in the ESP8266. My hope is that this signal is proportional to the number of electrons that flowed during signal acquisition. But this signal is not the same as the number of electrons which will be behaving as a Poisson process. But the recorded numbers should be proportional to the number of electrons. So if we divide these values by some constant, can we get the fit to work, and at what optimal division factor.

Long story short, if I divide the 1000000 million points by 23.9 I get an optimal fit in terms of the error reported for the fitted parameter. That parameter, which is the mean value, is ~6.558336. Does this mean, that on average 6.5 electrons pass through the transistor while the ESP8266 is taking an ADC measurement? I think it might be! Here is the fit:

If I take these same numbers and try and fit a Gaussian curve, it doesn’t do as well.

Conclusions? The electrons that flow across the junction in reverse bias are behaving as a Poisson process as expected. The distribution is not flat. I’ve seen some discussion on the net where people seem to assume this would be the case. It does seem to be random! One of the next steps is to convert these numbers to a flat distribution or at least make it generate a binary sequence. It seems to be that XORing  or Von Neumann filtering will not do a good job of removing the biasing that the Poisson distribution will introduce.

Random Number Generation Part I – The Hardware

Intro:

The notion of randomness has consistently intrigued me, so I have always wanted to build a random number generator and play with it. Just how easy is it to generate truly random numbers as opposed to pseudo-random numbers? First of all, pseudo-random numbers are based on an algorithm so computationally they are easy to generate but also easy to copy or determine the nature of the sequence. They also ‘repeat’ their pattern eventually, even if the repeat cycle might be very large. No, no, no. I want to generate random numbers from an unpredictable natural source such as radioactive decay, cosmic rays, radio noise or ‘avalanche noise’ (hint: not the noise of snow falling down a mountain).

The Source:

In short, avalanche noise is the noisy current flow when a diode is reverse biased (voltage applied the wrong way), once that voltage is high enough to make electrons jump over the semiconductor gap the wrong way.  The nifty thing is transistors have these diode junctions and so current can flow, for example, from the base to the emitter in an NPN transistor once it is reverse biased with high enough voltage. So what many circuits do to generate random noise is set up a transistor in this way, and then amplifying the current that flows, which for ‘quantum energy gap’ reasons is noisy.

An Example Circuit:

Browsing the net I came across Rob Seward’s attempts at doing this and set up his circuit since I had all the components on hand. The circuit is below:

This is my understanding of this circuit: Here, Q1 is reverse biased with 12 V of EMF via the 4.7k resistor. Q2 is forward biased and so it should conduct with a small voltage drop (~0.9 V) across its base to emitter junction so the reverse bias in Q1 is actually ~11.1 volts through the 4.7k resistor. This should be enough to jump the gap as outlined in this great summary of this phenomenon by Giorgio Vazzana. If current randomly jumps the gap in Q1, this current will flow into the base of Q2 where it will be amplified by a common emitter setup and passed into the 0.1 uF capacitor. These spikes in current will pass through the capacitor and into the base of Q3, which is highly biased by the 1.5M resistor. I think whats going on here is this transistor is set up to be a switch – this high bias turns the collector to emitter current essentially off, so any current that flows into its base will turn it on. Thus a voltage appears at its collector (its also in common emitter mode). This voltage is divided by the two resistors of 10k and 4.7k and presented as an analog out signal.

I put this together and applied the analog out and a ground to some earbuds I had lying around. Faint noise!

I quickly wired up an arduino to take sample measurements of the voltage via an analogRead(). The values were peaking out at around 260 out of 1023 where 1023 would be a voltage of 5V. So Im seeing peak voltage here of about 1.25V. Ultimately I want to read these voltages with an ESP8266 unit or a raspberry pi which can only safely sample an analog voltage of 1V so I needed to play with the voltage divider. I’m not entirely sure why this works but I replaced the 10k resistor with a 22k resistor and the 4.7k resistor with a 10k resistor, which in turn dropped the analog reads to maximum reads of about 110 – or a little over half a volt. This is nice and safe.

The code I used for the arduino is here:

/*
 Read analog signal on pin A0

*/

const int analogInPin = A0; // Analog pin that noise is fed at

int sensorValue = 0; // Inital sensor value
 int low = 30; // Set a low value to be surpassed
 int high = 40; // Set a high value to be surpassed
 void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 }

void loop() {
 // read the analog in value:
 sensorValue = analogRead(analogInPin);

// if value is higher than ever recorded, lets note it
 if (sensorValue > high) {
 Serial.print("high: ");
 Serial.println(sensorValue);
 high = sensorValue;
 }
 // if value is lower than ever recorded, lets note it
 if (sensorValue < low) {
 Serial.print("low: ");
 Serial.println(sensorValue);
 low = sensorValue;
 }

// wait 2 milliseconds before the next read
 // thats 500 samples per second
 delay(2);
 }

Whats next:

The next step before making an extensive set of analog reads is to add an OP amp so I can drive a speaker and listen to the noise out loud and make some recordings for here. Until then…

A quick heads up (I have a cold and sound awful):