IPython is more than just a command line.

So I just didn’t get it.

It’s embarrassing. I launched ipython and all it did was this:

ipython0

It’s the same as the immediate window in Spyder. I didn’t see the use in typing in immediate code.

It was nice. Made a good immediate execution window in Spyder, but where is this notebook people were talking about?

Just in case you also aren’t getting it or reading the one page on the one website that realizes not everyone got the memo:

C:\Users\Kal-El\Documents> ipython notebook

So there you are.

 

Posted in DSP, Mathy Stuff

What’s all this Numpy stuff anyway?

I always jump in with both feet. Having never used Python before doesn’t faze me; programming things hasn’t since my first job out of college when i was “challenged” to programmed boot code into core memory with toggle switches. (I won beer, but that’s a different topic.)

KL_CoreMemory

This is magnetic core memory. Those are little magnetic donuts with wires fished through them by people with little tiny fingers. One bit per donut. These things were the first memory devices in space, and they still work despite lots of hard radiation Programming them isn’t hard after you read the manual: you use toggle switches.

I needed some kind of editor and apparent needed to download this NumPy thing into my Windows computer. And a working Python installation. Mine was broken, and I never really remembered installing it — it was probably there from some other thing I installed.

These are the toggle switches. Just in case you were trying to picture them.

This is a picture of the toggle switches, just in case you were trying to picture them.

Apparently I’d had an old broken version of Python on my laptop from some software that I installed then deleted because it sucked. (92% chance)

A couple of Googlings later and I found Anaconda. They brag that they did all the work for me, installing Python AND this NumPy thing. 195 seems like a lot of things to get for free, but I wasn’t too impressed because my Ubuntu machines all have thousands of things.

It includes over 195 of the most popular Python packages for science, math, engineering, data analysis.

One 366 MB download later and I had this:

C:\

Wait. 366 MB? Not 36? What’s in this thing anyway? Probably should have read the website. My laptop had 250G free, so I didn’t really check that out. (I should have.)

Instead I looked for an included IDE, and found Spyder, one of the 195.

C:\spyder

That’s what you do, and you get this:

spyder stuff

Yup, it’s an IDE kinda similar to every other IDE everywhere, since like the 90s. It has an output window, a code window with pretty colors, an object inspector, etc.

But I want to stop the story right here. See all those python files in the little window? I took some time to write them (mostly cut and paste at first) to test out what I could do. That was a fun learning exercise and I learned a lot doing it.

Don’t waste your time like I did. The cool kids WILL laugh at you. Instead, use notebooks.

Don’t be a dope like I was.

Posted in DSP, Mathy Stuff

Pink noise found.

The following python code makes white, violet, brown, and pink noise. I’ve heard of Python – all the cool kids seem to be playing it.

So what is this NumPy thing? The website compares itself to Matlab. I’ve seen this before in the early days of the internets, like a couple of years ago. Octave didn’t quite cut it.

And what kind of signal processing stuff can I get for it? Can I remember signal processing? It’s been a while.

To make this pink noise thing useful, I need to generate a stream of numbers and put it into the computer’s speaker.

Charts and graphs are nice too.

And how random is randn() anyway? My computer backgammon game is not very random, how do I know if it’s working?

"""
Allan deviation tools
Anders Wallin (anders.e.e.wallin "at" gmail.com)
v1.0 2014 January

Functions for generating noise
AW 2014

See: http://en.wikipedia.org/wiki/Colors_of_noise

This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import numpy
# import math


def white(N):
    """ white noise has constant PSD (up to the nyquist frequency) """
    return numpy.random.randn(N)


def violet(N):
    """ violet noise with f^2 PSD """
    return numpy.diff(numpy.random.randn(N))


def brown(N):
    """ Brownian or random walk (diffusion) noise with 1/f^2 PSD
        not really a color... rather Brownian or random-walk
    """
    return numpy.cumsum(numpy.random.randn(N))


def pink(N, depth=80):
    """ N-length vector with (approximate) pink noise
    pink noise has 1/f PSD """
    a = []
    s = iterpink(depth)
    for n in range(N):
        a.append(s.next())
    return a


def iterpink(depth=20):
    """Generate a sequence of samples of pink noise.

    pink noise generator
    from http://pydoc.net/Python/lmj.sound/0.1.1/lmj.sound.noise/

    Based on the Voss-McCartney algorithm, discussion and code examples at

http://www.firstpr.com.au/dsp/pink-noise/

    depth: Use this many samples of white noise to calculate the output. A
      higher  number is slower to run, but renders low frequencies with more
      correct power spectra.

    Generates a never-ending sequence of floating-point values. Any continuous
    set of these samples will tend to have a 1/f power spectrum.
    """
    values = numpy.random.randn(depth)
    smooth = numpy.random.randn(depth)
    source = numpy.random.randn(depth)
    sumvals = values.sum()
    i = 0
    while True:
        yield sumvals + smooth[i]

        # advance the index by 1. if the index wraps, generate noise to use in
        # the calculations, but do not update any of the pink noise values.
        i += 1
        if i == depth:
            i = 0
            smooth = numpy.random.randn(depth)
            source = numpy.random.randn(depth)
            continue

        # count trailing zeros in i
        c = 0
        while not (i >> c) & 1:
            c += 1

        # replace value c with a new source element
        sumvals += source[i] - values[c]
        values[c] = source[i]


def pinknoise_to_file(N=10000, filename="pinknoise_frequency.txt"):
    n = pink(N, depth=80)
    f0 = 10e6  # imagined carrier, 10 MHz
    n = [x + f0 for x in n]  # add noise and carrier
    fr = [(x - f0) / float(f0) for x in n]  # fractional frequency
    with open(filename, 'w') as f:
        f.write("# simulated oscillator with pink frequency noise\n")
        f.write("# fractional frequency data \n")
        f.write("# number of datapoints %d\n" % len(fr))
        for ff in fr:
            f.write("%0.6g\n" % ff)
        #print sum(n)/float(len(n))


if __name__ == "__main__":
    #pinknoise_to_file()
    print "Nothing to see here."

 

Posted in DSP

In search of pink noise

I had a lot of free time after back surgery, so I spent some time looking into a project a guy at work was interested in.

I can’t talk about that project because it’s a big secret (in the event that it’s worth something), but it had pink noise in it. Dr. Burrus never talked about pink noise, I’m fairly sure, so I went to Wikipedia for answers.

The first thing it talks about is power spectrum, but the real thing I wanted to know is why pink? (because that’s what it looks like as a color)

Pink Noise

The red is pink and the orange is white (obviously). The enormous DC value is apparently not an issue for floating point numbers, but I’m pretty sure it’ll make PCM saturate.

So you’d think that Wiki was the final word, but it turns out that there isn’t a lot of actual research into pink noise; just a bunch of research that compares things to pink noise.

Using Wikipedia’s formula doesn’t help much.

So I happen to know that white noise is Gaussian noise, (because of DSP in grad school). In sampled signals, each subsequent number is completely independent of the last one and can be from zero to infinity, but more likely zero-ish.

I happen to know these Gaussians are pretty because an expert told me.

Pink noise, not so much. In fact, it’s hard to calculate. Random number generators give us perfectly fine white noise, but none of them generate the right mix of numbers for pink noise.

So some guys on the internet worked out a method.

1. Take the fft of a signal, then multiply the output spectrum by a slanted line, then make inverse fft. There are a dozen reasons this is wrong – don’t do it.

Slanted line.

Some other guys used a plain old FIR filter set up to make more or less a slanted line, but with math that can be proven.

Those same guys came up with a fairly efficient algorithm based on additive random number generators at different octaves. Other guys wrote python code for it.

Pink Noise Found

Posted in DSP

Antimatters

Antimatters

Posted in Everything

In Archive