diff options
author | bnewbold <bnewbold@robocracy.org> | 2011-08-14 19:47:48 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2011-08-14 19:47:48 -0400 |
commit | b1bce8be26ee4ab2e905703f0f1f14182e26c124 (patch) | |
tree | 9e24b5f2bf1a88bee3e4ae07bd5e26aca639911a | |
download | solar_autonoma-b1bce8be26ee4ab2e905703f0f1f14182e26c124.tar.gz solar_autonoma-b1bce8be26ee4ab2e905703f0f1f14182e26c124.zip |
initial import
-rw-r--r-- | Turing102910.zip | bin | 0 -> 1299093 bytes | |||
-rwxr-xr-x | simple_turing.py | 146 |
2 files changed, 146 insertions, 0 deletions
diff --git a/Turing102910.zip b/Turing102910.zip Binary files differnew file mode 100644 index 0000000..3ff5047 --- /dev/null +++ b/Turing102910.zip diff --git a/simple_turing.py b/simple_turing.py new file mode 100755 index 0000000..9704c6f --- /dev/null +++ b/simple_turing.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python + +import sys +import wave +import random +import struct + +RATE = 22000 +SAMPLE_LEN = RATE * 30 +TSIZE = 10000 # gets doubled + +class Machine(): + def __init__(self, prog=None, verbose=False): + self.verbose = verbose + self.tape = map(lambda x: 0, range(TSIZE*2)) + self.top = 0 + self.bottom = 0 + self.index = 0 + self.state = 'A' + if prog: + self.prog= prog + else: + self.prog = { + 'A': {} + } + + def run(self): + while self.state != 'H': + self.step() + + def step(self, n=1): + if self.state == 'H': + raise Exception("In halt state!") + instr = self.prog[self.state][self.tape[self.index+TSIZE]] + self.tape[self.index+TSIZE] = instr['write'] + self.index += instr['dir'] + if self.index > self.top: + self.top = self.index + if self.index < self.bottom: + self.bottom = self.index + self.state = instr['next'] + if n>1: + self.step(n=n-1) + + def __str__(self): + return "S=%s, [%d:%d]=%s" % (self.state, + self.bottom, + self.top, + ''.join(map(lambda x: str(x), + self.tape[TSIZE+self.bottom: + TSIZE+self.top]))) + +# right is 1, left is -1 +bb_2_2 = { + 'A': { 0: {'next': 'B', + 'write': 1, + 'dir': 1}, + 1: {'next': 'B', + 'write': 1, + 'dir': -1}}, + 'B': { 0: {'next': 'A', + 'write': 1, + 'dir': -1}, + 1: {'next': 'H', + 'write': 1, + 'dir': 1}}, +} + +bb_6_2 = { + 'A': { 0: {'next': 'B', + 'write': 1, + 'dir': 1}, + 1: {'next': 'E', + 'write': 1, + 'dir': -1}}, + 'B': { 0: {'next': 'C', + 'write': 1, + 'dir': 1}, + 1: {'next': 'F', + 'write': 1, + 'dir': 1}}, + 'C': { 0: {'next': 'D', + 'write': 1, + 'dir': -1}, + 1: {'next': 'B', + 'write': 0, + 'dir': 1}}, + 'D': { 0: {'next': 'E', + 'write': 1, + 'dir': 1}, + 1: {'next': 'C', + 'write': 0, + 'dir': -1}}, + 'E': { 0: {'next': 'A', + 'write': 1, + 'dir': -1}, + 1: {'next': 'D', + 'write': 0, + 'dir': 1}}, + 'F': { 0: {'next': 'H', + 'write': 1, + 'dir': -1}, + 1: {'next': 'C', + 'write': 1, + 'dir': 1}}, +} + +def test_fsm(): + m = Machine(prog=bb_2_2) + print m + m.run() + print m + return m + +def scale(val, r_bottom, r_top, amp=32767): + f = 1.0 * (val - r_bottom) / (r_top - r_bottom) + return int(amp * 2.0 * f) - amp + +def go(downsample = 1, prog=bb_6_2, seconds=10, rate=22000): + noise_output = wave.open('turing_test.wav', 'w') + noise_output.setparams((1, 1, RATE, 0, 'NONE', 'not compressed')) + samples = rate * seconds + + m = Machine(bb_6_2) + m.step() + for i in range(0, samples): + #value = random.randint(-32767, 32767) + if i % downsample == 0: + m.step() + value = scale(m.index, m.bottom, m.top, 32767) + packed_value = struct.pack('h', value) + noise_output.writeframes(packed_value) + #noise_output.writeframes(packed_value) + + noise_output.close() + return (m.bottom, m.top) + +if __name__ == '__main__': + out = '' + if len(sys.argv) > 2: + out = go(seconds=int(sys.argv[1]), downsample = int(sys.argv[2])) + elif len(sys.argv) > 1: + out = go(seconds=int(sys.argv[1])) + else: + out = go() + print out |