brainwagon "There is much pleasure in useless knowledge." — Bertrand Russell

26Feb/142

Products of Primes and the Primorial Function…

A friend of mine was working on a programming exercise, and it turns it out was based on a chunk of math which I thought I should have seen before, but either have not seen or have forgotten. It's basically that the products of all primes less than some number n is less than or equal to en and in the limit converges to precisely en. First of all, I wrote a chunk of code to test it out, at least for primes less than a million. Here's the code I wrote (I swiped my rather bizarre looking prime sieve code from a previous experiment with different sieving algorithms):

from random import randint
from math import sqrt, ceil, floor, log
import time
import sys
from math import *

def sieveOfErat(end):
    if end < 2: return []

    #The array doesn't need to include even numbers
    lng = ((end/2)-1+end%2)
    
    # Create array and assume all numbers in array are prime
    sieve = [True]*(lng+1)

    # In the following code, you're going to see some funky
    # bit shifting and stuff, this is just transforming i and j
    # so that they represent the proper elements in the array
    
    # Only go up to square root of the end
    for i in range(int(sqrt(end)) >> 1):
            
        # Skip numbers that aren't marked as prime
        if not sieve[i]: continue

        # Unmark all multiples of i, starting at i**2
        for j in range( (i*(i + 3) << 1) + 3, lng, (i << 1) + 3):
            sieve[j] = False
            
    # Don't forget 2!
    primes = [2]

    # Gather all the primes into a list, leaving out the composite numbers
    primes.extend([(i << 1) + 3 for i in range(lng) if sieve[i]])

    return primes


sum = 0. 
for p in sieveOfErat(1000000):
        sum += log(p)
        print p, sum/p

Most of the code is just the sieve. In the end, instead of taking a very large product, we instead take the logarithm of both sides. This means that the sum of the logs should be nearly equal to n. The program prints out the value of the prime, and how sum compares to the value of p. Here's a quick graph of the results:

sieve

Note, it's not monotonically increasing, but it does appear to be converging. You can run this for higher and higher values and it does appear to be converging to 1.

This seems like a rather remarkable thing to me. The relationship between e and primes seems (to me) completely unobvious, I wouldn't have any idea how to go about proving such a thing. A quick search on Wikipedia reveals this page on the primorial function but similarly gives little insight. Recalling Stirling's approximation for ordinary factorials suggests that these large products are related to exponentials (Stirling's approximation not only has a factor of e in it, but also the square root of two times pi as well), but the idea that the product of primes would precisely mimic powers of e seems deeply mysterious...

Any math geniuses out there care to point me at a (hopefully simple) explanation of why this might be? Or is the explanation far from simple?

Filed under: Math 2 Comments
15Feb/140

The Minima — A General Coverage Transceiver

A while ago, Bill Meara from Soldersmoke brought Ashar Farhan's new design, the Minima to my attention. The Minima is a general coverage transceiver which has a lot of cool features. It's a superhet design which is Arduino based (actually, it incorporates a bare bones Arduino, which is little more than an Atmel ATMega328 chip.) Farhan is the designer of the popular BitX design, and this design has a lot of cool features, and yet seems rather straightforward.

Some versions of this are beginning to appear in the wild. Mark, G0MGX seems to have done the best at documenting his build on his blog. Here's his video demonstrating the receiver:



Filed under: Amateur Radio No Comments
10Feb/140

Raspberry Pi Camera NoIR…

I've been playing around with the Raspberry Pi Camera for a number of different purposes, but one thing is pretty apparent right off: while the quality overall is quite good, it's not very good in low light. Because at least part of my potential application is watching the night-time activities of wildlife (most likely my cat, but perhaps including foxes that cruse around yard) I decided to order the version of the Raspberry Pi Camera which had no IR blocking filter, called the Raspberry Pi NoIR. It arrived today, and at the same time I ordered an inexpensive IR illuminator to serve as a light source. Addendum: The illuminator died after less than 12 hours of use. Do not buy this one. It's rubbish.

It arrived today!

Out with the old camera, in with the new, power on the illuminator (if you order the same one, note that it does not come with a wall-wart to power it) and voila:

Screen Shot 2014-02-10 at 8.35.00 PM

Scrappy Cam!

Okay, a couple of quick notes. The illuminator is just not that strong. Here, the illuminator was a little under five feet from the the couch. For stuff that is more distant, it's clear that the illuminator just isn't good enough to reach into the corners of the room. Check out...

Screen Shot 2014-02-10 at 8.33.32 PM

You can see me standing to the side. Obviously, the color balance is all wonky, it's going from magenta to purple. The frame rate is still quite low, which in my streaming application manifests itself as a pretty long delay. Still, seems pretty cool! More experiments soon...

Filed under: Raspberry Pi No Comments
6Feb/141

Streaming Video From the Raspberry Pi Camera…

First of all, let me get this off my chest: video over the web is a hideous Tower of Babel.

With that basic complaint, let me start by saying that this project started with a Raspberry Pi and the Raspberry Pi Camera. Previously, I had used the Pi with a USB webcam and had connected it to my wireless router and run the "motion" program to serve as a kind of cat cam. But to be honest, I wasn't really very happy with the results. The videos it recorded were low frame rate. The quality of the camera was pretty low. It was good enough to allow me to monitor my cat while I was on vacation, and have some assurance that he was still alive, but it left something to be desired.

After picking up some new $8.88 TP-Link Wifi dongles, I decided to see what else I could do. The trick with making this work was trying to find a way to leverage the video compression hardware that already runs on the raspberry pi, and to do as little as possible to it, but still allow it to be streamed to standard web browsers and devices.

I briefly went through experimenting with mjpegstreamer which works, but didn't really offer the quality that I was after.

Then, I stumbled on this great article on using nginx-rtmp to stream live video from the Raspberry Pi. It looked like just what I wanted. It took me an evening (mostly spent waiting for nginx and ffmpeg to recompile) but I have it working now. Check it out:

Screen Shot 2014-02-06 at 8.56.16 AM

I'm currently able to stream 720x486 wide video at 25fps directly from the Pi at around 25fps, using somewhere around 13% of the available cpu on the Pi. It can be accessed by both desktop browsers and my iPad/iPhone. Seems really good!

With a couple of caveats. Remember that Tower of Babel I mentioned? Yeah, that. To stream to desktop browsers, they must run Flash. That is because nginx-rtmp uses RTMP, which is a proprietary streaming video protocol. But, I'm sure you say "how does this work on the iPad/iPhone?" The answer is it uses a different protocol for those devices, the HTTP Live Streaming protocol, which is also transparently supported by the RTMP server. Why can't you just use HLS on the desktop? Because most desktop browsers don't support HLS. Sigh. HLS also has increased latency compared to the RTMP protocol.

Sigh.

But anyway, it works! I'm awaiting the arrival of an Raspberry Pi camera with the IR filter removed and an IR illuminator, and I'll be doing more experiments. I'll maybe write up more explicit instructions for this when i get the entire thing working. If anyone wants to give this a try and has trouble, don't hesitate to sing out with questions.

Filed under: Raspberry Pi 1 Comment
2Feb/141

An $8.88 WiFi adapter for my Raspberry Pi…

g1I was out running errands the other day, and found myself at Fry's Electronics. I needed to pick up a VGA extension cable to replace one that had inexplicably become bad, and as I often do while wandering around, found myself doing a bit of window shopping. (Not Windows shopping, I've had enough of that.)

While doing so, I found myself staring at a variety of wireless network dongles, including the exceptionally tiny TP-Link WN725N for a mere $8.88. I wondered if it might be a good match to a couple of the network-less Raspberry Pis that I had lying around. A little of judicious searching on my phone indicated that some people had used them for this purpose, and that they worked well even without a hub, so I bought two of them and brought them home.

I was hopeful that if I just jammed them in, they would be detected automatically and then a short bit of network config later, I'd have a working adapter. Sadly, it did not go that smoothly. It appears that while v1 of the hardware worked out of the box with Raspbian, the v2 hardware (which I had) does not. So, the google search begins...

Ill skip to the end, so you won't have to do the search. Go to this website, it has all the information. Basically, you have to find the right version of the kernel model to match your version, and install it in the right place, and rerun depmod -a. They even have a link to a script that can find the right version to download. Once I got the driver installed, I had no further issues. The dongle appears to work quite well, and my raspberry pi no longer has a tail connecting it to my router. Awesome! I haven't stress tested it, but it appears to work as well as any other adapter I've tried. I consider the experiment a success.