Tuesday, November 17, 2015

Writing Audio Files With Python, part 1

There aren't really great Python audio libraries, at least not any that I can find that are targeted for a complete novice.  Python does have the "wave" library which can at least handle the metadata to write audio in some format.

Before we jump into an example, we should note that there are at least two main ways of describing an audio stream:

  • You can describe "these are the notes that are played at this time".  Think sheet music, or a FFT representation.  This is probably how you think about music.
  • You can describe "this is the 'pressure' of the music at this time" (we'll explain how 'pressure' works later).  This is called "sampling" and is how basically all recordings of music work.  The main problem we face is converting from the first representation to the second representation.
Here's a "Hello World" of using the Python WAV writer:

# Copyright 2015
import wave
SAMPLES_PER_SECOND = 4400
w = wave.open("/media/sf_tl-share/test.wav", mode="w")w.setnchannels(1)w.setsampwidth(1)w.setframerate(SAMPLES_PER_SECOND)w.setnframes(SAMPLES_PER_SECOND * 10)for i in xrange(SAMPLES_PER_SECOND * 10):    w.writeframes("a" if (i%10 < 5) else "A")w.close()
Fairly straightforward.  A few notes:
  • CDs normally have 44100 samples per second.  This is enough to fully encompass the human hearing range.  The Nyquist-Shannon theorem explains why this is the case.  To decrease the file size, we're using only 4400 samples per second here.
  • We are setting the "framedata" to be a byte.  Characters are the easiest way to represent bytes in Python, at least for a Hello World example.  It's clearly better to use integers internally and the struct module when writing them. (WAV files are little-endian, and we're currently using 8-bit sound, represented by 8-bit unsigned integers)
  • This is a "square wave".  We would have a "smoother" sound if we used a sine wave to generate the data points.  We'll get into Fast Fourier Transforms and the explanation of why these sound different later.
  • What are these "samples" anyhow?  For lack of a better word, they're sampling "pressure". Pressure is a way that we can describe the sound wave; the pressure is something that alternates like a wave.  Sound waves are theoretically smooth pitches (i.e. a sine wave at 440Hz is concert A).  This definition feels very circular, but I don't know a better representation.
  • For performance reasons, we need to batch the writes for calling writeframes.  
import numpy as np
import struct
import wave
SAMPLES_PER_SECOND = 4410
w = wave.open("/media/sf_tl-share/test.wav", mode="w")
w.setnchannels(1)
w.setsampwidth(1)
w.setframerate(SAMPLES_PER_SECOND)
w.setnframes(SAMPLES_PER_SECOND * 10)
 
def write_one_second(pitch):
  frames = []
  for i in xrange(SAMPLES_PER_SECOND-30):
    pressure = 32 + 16 * np.sin(2 * np.pi * i * pitch / SAMPLES_PER_SECOND)
    frames.append(struct.pack("<B", pressure))
  for j in xrange(30):
    frames.append(struct.pack("<B", 32))
  w.writeframes("".join(frames))
 
for i in xrange(2):
    write_one_second(440)
    write_one_second(261)
    write_one_second(261* 1.06)
    write_one_second(261 * 1.06 * 1.06)
    write_one_second(440 / 1.06)
w.close()

We get a few artifacts, but it mostly resembles what you'd expect the notes A C C# D G# to sound like, and it runs fairly quickly.

A few notes going forward:

  • It would probably be best to store the whole thing in a numpy array and convert it at the end. That's probably overkill for this example, though.  It does let us do more transforms.
  • As a general rule, 44100 will eliminate a lot of noise artifacts.
  • The "write_one_second" function doesn't let us have multiple notes play at once, but that's an obvious change.

Monday, November 16, 2015

Questions on ISIL

  1. Why don't we just nuke them all and be done with it?
    • Nuclear bombs don't work that way.  They really aren't useful against military troops spread across hundreds of miles of empty desert.
    • YOU CAN'T HUG YOUR CHILDREN WITH NUCLEAR ARMS!
    • Legally, the US (still) has a policy of not being the first country to use nuclear weapons.
  2. Why don't we just "shock and awe" them all and be done with it?
    • Because that worked so well in Iraq 12 years ago.  Beyond that, from a moral point of view, it's pretty much the same as "nuke them all".
    • We've already been doing that and it hasn't helped.
    • There's not really much of a difference between "a drone bombs a nightclub" and "a suicide bomber bombs a nightclub".
  3. Why doesn't the government of Iraq or Syria do something about it?
    • For all practical purposes, there is no government in Iraq or Syria, definitely not in the desert regions.  The blue regions are about the parts of the country that are controlled by the central government authorities.
    • Southern Iraq (labeled as Basra) is basically running the Iraqi government.  The region also controls most of Iraq's oil.
    • Syria is fighting multiple different rebel groups, both separatist and religious.
      • The Alawite region (possibly including Aleppo and part of Turkey)
      • The Kurdish region (including northern Iraq and part of Turkey - Mosul is actually controlled by ISIL)
      • The "Euphrates" region (eastern Syria and central Iraq - I can't find a good name for this region, "desert" is too general and "ISIL" too specific)
      • IN ADDITION, there are the normal "we hate Assad"-type rebels.  These would probably be described as "pro-democracy advocates" or the like if they're aligned with the US, or "religious groups" if they're not.
  4. What about Iran or Turkey?
    • Both the Alawite and Kurdish rebels have claims for territory inside Turkish borders.  ISIL (as a proto-state, not as a "global whatever") does not.  As long as ISIL is fighting other elements within Iraq and Syria, it more or less aligns with Turkey's interests.
    • There is no real way that the US can work with Iran in any way.  Beyond that, there are also Kurdish regions in Iran that Iran would prefer not join an independent Kurdistan.  ISIL isn't really fighting in the Shiite areas near Basra, as long as those areas are part of an established state there's no real motivation there either.
  5. What about other countries?
    • Russia actually does have an interest in the "inviolate borders of nation-states" rule of international law being weakened.  I don't think the US is ready to "trade the Crimea to Russia to get rid of ISIL".  Russia is also a strong backer of Assad.
    • The Saudi government more or less supports ISIL.
    • Egypt is still in no position to engage in external military actions after the counterrevolutionary coup by Sisi against the Muslim Brotherhood.
  6. Isn't "suggesting that ISIL should have its own country" surrendering?
    • I'm suggesting that they already do have their own country.  The post-1945 facade of "nation-states are exactly what the UN says they are, and apart from limited regionalism no states shall be created or destroyed" is clearly failing in the Middle East.  Various "3-state plans" post-Iraq were discarded as un-workable in 2003.
    • The Ba'ath party was the main vehicle for "pan-Islamic nationalism" in the Middle East.  Any plan for a multi-ethnic state probably has to include some elements of Ba'ath philosophy.  Of course, the two main proponents of the Ba'ath party were Sadaam Hussein and the Hafez al-Assad (Bashar al-Assad's father).  Between the lack of a "cult of personality" for a leader, and being seen as "government allies", it seems almost impossible for a new multi-ethnic movement to arise in the current environment.
  7. Is ISIL a proto-state or an "international movement"?
    • They certainly want to be an international movement.
    • They certainly are a proto-state.  They control territory.  They have an army.  They collect taxes (or at least tribute).  They keep threatening to create a currency.  Nobody else claims to be in control of the territory they claim to control.
    • That said, every "terrorist" in the world wants to be part of a global movement.  I don't think they care if it's ISIL or Al-Qaeda or PETA or the NRA.
  8. What do you do with angry young men who want to fight other angry young men?

Friday, November 13, 2015

So I ran a Google Consumer Survey

So I finally actually ran a GCS survey yesterday.  The question: "Which of these candidates in the Republican Presidential Primary do you most support?" Trump, Carson, Cruz, Rubio, "Other", or "I am not a Republican".

Why did I do it that way?

  • Apparently the version with a screening question cost 20 times as much per response.  Even with the "I am not a Republican" option getting over 50%, this was still 10 times cheaper (and almost free with a coupon).
    • The pricing has definitely gotten more complicated.  At launch time, if I remember correctly a 2-question survey with a (>10% response) screening question was 50 cents per response.  Apparently responses work such that you're better off either having a long series of questions, or having a single question (which is as easy to answer as it is to close the window).  So I think that for exploratory polling, the single-question approach will almost always be better.
  • There's a limit of 7 answers for GCS responses.  I suspect that the clown-car listing of GOP candidates is distorting the results of GOP polls.  Even if nobody supported Rand Paul, he might get 2% just from hearing his name.  (the "Deez Nuts" phenomenon)
  • There is no "decline to state" since I assume those people won't answer, or will volunteer it in the "Other" box.
  • It's very hard to tell what the impact of partial responses are.  Is "14.4%" a good response rate?  Most of the example surveys have 15-20% responses on the first screening question, so there apparently isn't too large a response bias.  But there is some.  Hopefully it's non-voters who were more likely to not engage.
  • Also, there wasn't a separate option for "I am not a voter" and "I am a Democratic voter".  So it's hard to tell how many of the 52% are non-citizens or non-voters or simply Hillary Clinton supporters.  Or how many of the Hillary supporters also support Trump.  This is a poll of Americans, not of Republicans.
  • The difference between "this is a result that is statistically significant even if you didn't know it going in" and "this is a result that is statistically significant if you had a reason to be testing this specific reason going in" is very difficult to communicate.
  • The difference between "the error we know we have from having a biased sample" and "the error we expect to have based on taking a sample at all" is also difficult to communicate.
  • A tool "to get a statistically significant sample on this cross-tab result, you would need a survey with about 4000 responses" would also be an interesting tool to have.  (or even "4000 responses - +/- 4%.  6000 responses - +/- 3%)
  • Polls are iterative things.  You have to see what types of interesting results you can get before you know what the interesting results mean.  In particular, it would be interesting to prompt with a bunch of "moderate" second-tier candidates (Bush / Kasich / Christie / Fiorina) and see how many people volunteer the front-runners.  Alternatively, a bunch of "conservative" second-tier candidates (Jindal / Santorum / Huckabee / Paul).

Sunday, November 1, 2015

On the Philosophy of Science

https://coraifeartaigh.wordpress.com/2011/02/11/kuhn-vs-popper-the-philosophy-of-lakatos/ is a good background to basic Lakatos' theory.

The first problem of the philosophy of science is "How do we know anything exists at all?"  The traditional answer here is Descartes' "I think, therefore I am".  However, there are two significant problems with that statement:
  1. The Zen koan "What was your original face before your mother and father were born?"  Specifically, if your current existence is the proof that the universe exists, did the universe then not exist before you were born, before your parents were born?  Did your birth somehow change the universe?  This falls into the grue/bleen paradox, and also does not answer the question of "if the universe didn't exist before you were born, how were you born"?
  2. The contrapositive of the statement is "I do not exist, therefore I do not think".  Why should we believe that to be true?  Do we know what the nature of existence is?  Do the angels in heaven exist?  If they do not, does that make them inherently un-thinking?  For that matter, what does it mean to exist, or to think?
Wittgenstein attempts a different approach to the same question, stating "The world is that which is the case."  That statement is phrased as an equality rather than as an assertion.  It defines the world as that which is true, and truth as that which exists in the world.  And then Wittgenstein attempts to build a philosophy on top of this statement, similar to Descartes'.  But while Descartes appears to accept the fallacious ontological argument (and I claim that The Simpsons "Could Jesus microwave a burrito so hot that he himself could not eat it." fully captures the credibility and logical correctness of the argument), Wittgenstein arrives at some form of incompleteness of language and logic, that the ladder of pure logic becomes useless once you have reached the end, and that "What one cannot speak of, one must pass over in silence."

Fortunately, science offers us a third approach (logic and religion being, approximately, the first two).  We can accept some fundamental principles about ourselves and the universe on pure faith(*), but yet construct those principles with their rejection in mind.  We can accept that "gravity makes things fall towards Earth", and then reject it in the case of the sun.  (the pedantic argument that "gravity makes the earth and the sun fall towards each other" is irrelevant, because that statement already assumes that we accept that "gravity is a fundamental force that follows inverse square laws").

And science itself is indeed "falsifiable".  The claim that "your senses are an accurate representation of what is happening in the world" can be proved or disproved in any number of ways.  As far as science in the world is concerned, if the sun failed to rise tomorrow morning or if Bunsen burners started making things colder, it would clearly disprove science.  You may claim that both of these are preposterous and impossible.  But if not for science, why would you think they are preposterous or impossible?

-------

Pascal's Mugging ( http://www.nickbostrom.com/papers/pascal.pdf ) is a more modern, slightly more believable version of the traditional Pascal's wager.  Both of these ask "what is the value of some incredibly unlikely outcome"?  And the answer, by necessity, is "zero".  Nothing.  Nada.  Nilch.

The more formal statement is "If I do something impossible, then something possible will happen".  Which, I claim, is a false statement.  If it has never been done before, then it is impossible, and nothing about the later outcome matters.  If it has been done before, then it is, in fact, possible.  And once it is possible, only then do we have to consider what it means.

I suppose that promises of the afterlife, of immortality, of re-incarnation, have been historically very successful in promoting the social order.  But if you promise me a thousand million trillion eternities, an infinite multiverse of eternities, all I will hear is a pile of words until you have some undeniable evidence of any of your claims.

-------

(*) faith is the necessary word here.  once you have summarized everything you know, the summary must be something you do not know.  sets that contain themselves are inherently subject to paradoxes and contradictions.  so, for now, i take it on faith that the basic most fundamental principles of knowledge must be taken on faith.  ... the theologicians (sic) of old would tell you to be bold and large in your faith.  this is misguided.  one should never take two things on faith when one can take one thing instead.