# A Not Quite Random Number Generator (NQRNG)

September 13, 2010
By

(This article was first published on BioStatMatt » R, and kindly contributed to R-bloggers)

I connected the instrumentation amplifier described in an earlier post to a piezoelectric transducer (buzzer) and made recordings at 5000 gain. The plot below shows 1000 such measurements over 1.0 seconds. There is a 4.0 second (at 1000Hz) sample of the data here piezo.csv. There is a clear sinusoidal signal in these data of about 60Hz. These findings were baffling at first, but apparently result from capacitative coupling between the measurement device and 60Hz household AC power lines.

I fit simple periodic mean model to these data: $y = a + b sin( lambda ( 2pi t + phi ) ) + e$, where $y$ is the measured ADC (Analog to Digital Converter) value, $t$ is time in seconds, $a$ is the mean ADC value, $b$ is the peak amplitude, $lambda$ is the frequency in Hertz, $phi$ is the phase in radians, and $e$ is a zero-mean error term. The blue curve in the plot represents the least-squares fit to these data.

The least-squares estimates are as follows:

• $a$ – 577 ADC units
• $b$ – 7.75 ADC units
• $lambda$ – 59.9 Hz
• $phi$ – 4.80 rad

The estimated frequency ($lambda$) is very close to the theoretical value (60). The ADC of the ATmega168 has 10 bit precision. That is, values can range from $0$ to $2^{10}-1 = 1023$. The Atmel manual gives the following formula for back-calculating the voltage at the ADC pin: $V = A * V_r / 1024$, where $A$ is the ADC value and $V_r$ is the reference voltage ($5.0V$ in my setup). In addition, the instrumentation amplifier provides $5000V/V$ gain. Accounting for gain, the voltage measured between the instrumentation electrodes is $V_i = V/5000 = A * 5.0 / 1024 / 5000$. The estimated peak amplitude in ADC units was $b = 7.75$, so $V_i = 7.75 * 5.0 / 1024 / 5000 = 7.5mu V$. That is, household AC coupling induced a $7.5mu V$ peak potential between the instrumentation electrodes. In reality, the gain of the amplifier is less than $5000$, and $7.5mu V$ is a lowball estimate.

Assuming there are no other signals in these data (which is likely incorrect), the residuals in this regression ought to resemble independent random variates. The histogram for the residuals looks good (bell shaped, no noticeable bias or skew), however, there is obvious residual autocorrelation. The figure below is a plot of the autocorrelation within the residuals.

It’s clear that this method isn’t quite ready to replace the pseudo-RNGs. As time permits, I plan to try some signal processing with these data, in hopes to isolate the independent noise. If the only signal is that induced by capacitative coupling, then a high pass filter (at 60Hz) ought to do the trick.

A recent blog post at Xi’An’s mentioned some tests for randomness, including the Marsaglia Diehard Battery and the NIST Test Suite. Maybe if I get something that isn’t obviously nonrandom, I’ll try out some of these tests.

To leave a comment for the author, please follow the link and comment on his blog: BioStatMatt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , ,

Comments are closed.