Saturday, January 31, 2009

Digital Beethoven

A case: you have to look at a measuring instrument every few seconds for some long minutes but don't want to use stopwatch because you want to concentrate on something else, what then? Also there is a laptop available.

In my case, I made a python script to play a sound file every few seconds to interrupt me from whatever I was reading or discussing, and look at the instrument. The sound file would say something like "take measurement", "take reading", or "look at the meter", just because. I used winsound from python standard library, it's simple. It's a Windows specific thing, but as I'm stuck with windows at work anyway, might as well.

The fun wasn't actually in getting the script work and getting the experiment done more effectively, it was in discovering how easy it is to make a sinusoid beep at (almost) any audible frequency. So, something has to be done during the wait times and it was this: Für Elise in beeps.

import time as t
import winsound as w

#musical scale. Some are off by some hertz, don't protest
middle = {'C': 262,
         'D': 294,
         'd': 312,
         'E': 330,
         'F': 349,
         'G': 392,
         'g': 416,
         'A': 440,
         'B': 494,
         ' ': 37}

#the duration for a quarter note in miliseconds. Arbitrary, don't complain
beat = 498

#the score, believe it
elise = [('E5',0.5),('d5',0.5),
        ('E5',0.5),('d5',0.5), ('E5',0.5), ('A4',0.5), ('D5',0.5), ('C5',0.5),
        ('G4',1.5),('C4',0.5), ('E4',0.5), ('A4',0.5),
        ('B4',1.5),('E4',0.5), ('g4',0.5), ('B4',0.5),
        ('C5',1.5),('E4',0.5), ('D5',0.5), ('C5',0.5),
        ('B4',1.5),('E4',0.5), ('E4',0.5), ('E5',0.5),('E4',0.5),
        ('E5',0.5),('E5',0.5), ('E6',0.5), ('d5',0.5),('E5',0.5),('d5',0.5),
        ('E5',0.5),('d5',0.5), ('E5',0.5), ('d5',0.5),('E5',0.5),('d5',0.5),
        ('E5',0.5),('d5',0.5), ('E5',0.5), ('B4',0.5),('D5',0.5),('C5',0.5),
        ('A4',1.5),('C4',0.5), ('E4',0.5), ('A4',0.5),
        ('B4',1.5),('E4',0.5), ('g4',0.5), ('B4',0.5),
        ('C5',1.5),('E4',0.5), ('E5',0.5), ('d5',0.5),
        ('E5',0.5),('d5',0.5), ('E5',0.5), ('B4',0.5),('D5',0.5),('C5',0.5),
        ('A4',1.5),('C4',0.5), ('E4',0.5), ('A4',0.5),
        ('B4',1.5),('D4',0.5), ('C5',0.5), ('B4',0.5),

#this code plays the score
for elem in elise:
   freq = int(middle[elem[0][0]]*(2**(int(elem[0][1])-4)))
   dura = int(beat*elem[1])

   #this is what makes the sound
   w.Beep(freq, dura)

   #a blink interval between notes, so the same note played twice would
   #still have a pause in between

Go ahead and dissect it, maybe something interesting would come out. To try it out yourself: save the code into '', get python interpreter if you haven't, open your '' in the interpreter, pull your hair out after listening to the robotic piece

Something similar can be done with wave library, which is not windows specific, only I still can't be bothered to read the documentation

Friday, January 30, 2009

Exquisite Breakfast

Sliced bread and peanut butter for breakfast. How ordinary, follow that and maybe you can claim that you comply to some obscure international standards, maybe intergalactic. Then again, who blogs about breakfast these days and age? Would someone please send Ryan the memo saying penguins dislike meal blogging except at a culinary blog?

No way, this is about a discovery, or maybe a rediscovery of common sense. Which of course is not so common and occasionally not making any sense at all. It begins during my internship two years ago, early morning and me weren't in friendly terms and I had to catch a bus before sunrise. Skipping breakfast was a no-no, so I came up with a plan: I'd make breakfast, which was just bread and whatever spread I had handy before sleep, wrap it nicely with tissue papers and enjoy it in the few minutes I had between waking up and falling asleep again (only in bus). Only, 'enjoy' is just not the right word.

Firstly, leaving sliced bread all night makes it dry, so my breakfast tasted more like sponge and chocolate most of the time. Secondly, the minutes there are to enjoy it were just not there, and again that first reason. Well, I braved through it because there wasn't much choice.

It turns out the solution isn't complicated. Not about the time though, just the drying part. If I put the dry bread on top of a mug full of something like milk or milo, and microwave it for 3 minutes, then eat it after dipping into whatever I have in the mug, it actually works fine. Nice even. Now I can push more overhead to the night and have some extra minutes to snore in the morning. Fabulous!

Go ahead and make yourself a sandwich if you just marched through the text.

Tuesday, January 27, 2009

Sleepy Logger

My senior high history teacher once said that he only sleeps 4 hours a day. He said if you sleep 8 hours, you'd spend 1/3 of your life sleeping, and when you sleep, you don't get anything done. That inspired my sleep-pinching ways during university. Though people who know me would testify otherwise and they wouldn't be lying; I managed only to implement it halfway. I stay awake late (to increase uptime) and try to still get up early (to reduce downtime), guess which part always fail.

Along the way, I've compromised that principle and chose to get enough sleep so as not to be on the verge of drop down snoring all day. Yes, having a defined working hours contribute to that. Still, I found out I keep logging into dream world pretty late anyway. So here's an idea: let's be somewhat accountable and log my bedtime and wake up time everyday, maybe some interesting pattern would emerge. besides, looking at some numbers might (significantly) reduce the ambiguity in 'enough sleep' and 'not enough sleep'.

So how's the result? After 3 weeks, I see an entirely not rare pattern. Not much sleep in the weekdays, and quite much during the weekends (and holidays). In numbers:

  • average weekdays: 5.89 hours
  • average weekends: 9.13 hours

How healthy it is, I'll decide when I finally break down.

While I'm at it, I also log what kind of meal I have everyday. Nothing fancy like how much sugar or fat or awesome each have, just what it is called. Two interesting result so far are: I habitually skip breakfast in the weekends, and the later I get off work, the more snack I have.

Sunday, January 18, 2009

Unwelcome Runaway

I hated running. It's tiring, it makes me all sweaty (and that would result in higher pile of laundry) among other things. I thought it's a thing that one do only to chase buses, when getting chased by penguins, and some other isolated cases. Anyway, my opinion was that as long as I can run at decent speed for decent time to occasionally chase buses all is well. I don't expect to be chased by a waddle of penguins anytime soon anyway.

Then, the year turned. I went back from holiday at home stuffed. You see, at home anything could be dessert. Some nights batagor was dessert (don't argue this), other nights pempek was (don't argue this too), and some other nights good ol' ice cream was. Wait, not just nights, day too! Also, always as dessert; I had a proper meal before as far as I can recall.

After all that, I was feeling ...unwieldy. I needed to move, burn things down, stuffs like that. Then idea struck: I always visit NTU every week, why not run while I'm there? The air's good, the scenery's okay, and again I always visit anyway. With the right preparation maybe running is not so bad; right preparation, but what's that?

Running with full gears on—phone, wallet, keys, pens, notepad, etc—would be horrible. Add bag to the picture and it would be doublerrible. It would be like chasing after a double-decker around the university (and never catching up). So the first step is to contact some friends who live in the dorms and try to get one to agree to harbor my equipments for a couple hours [Wowies, Ryan unequipped things: STR -10, DEF -10, AGI +30, call for help, penmanship ability disabled]. Actually, the first step is to patch that iron-burn hole on my old high school training pants, but let's not go into details. Next step is go for it. Good penguins, it's only two steps long!

I decided I would take a long route around the campus first, finish at the running track and do a few run-walk cycle there, closed with a short sprint (as if the bus is actually a race car and I'm waaay late for work). The whole thing would be in the evening, don't want to fall down early in hot weather. First thing I realised: I overlooked NTU's hilly terrain. Any route I take would involve some uphill and downhill. That's easy to cheat: walk uphill, run downhill; don't protest I did admit it's cheating. Second: my run to walk ratio is pretty disastrous. Any worse and I would be obliged to call the exercise "Walking and occasionally simulating a bus chase". Well, I'll see if there'd be improvement over time, but I'd have to have the resolve to do it regularly first. Third: regular glasses get in the way so I leave it with my other stuffs (Oh no, SIGHT -2), I'm a little myopic so places with very low traffic and plenty space for pedestrian is best. Well, NTU is one.

Tell you what, it felt great. It's a time for myself (which I most of the time happy to have). I can have that kind of time just by sitting on an empty bench somewhere, or by walking. The amazing thing is, by running my mind is occupied by keeping breath pace and putting one foot in front of the other, I don't worry about anything else, I can't. Even if anything would come to mind, pain and fatigue from my joints would distract me, it's in a way liberating (I remember being told that by someone, but only now I'm convinced). Simple running can actually be a real running away, cool.

The activity is two weeks old now, with one session a week. I should really wait for more data before saying this, but I can feel myself improving. The run to walk ratio in the second week is better (my feeling, I don't actually quantize it), and I can run around the running track twice before walking for one round. Still it could be environmental and not really an improvement at my side. This reminds me of my Karate training when I was a junior high student, the Senpai would make us run one cycle more each week, no excuse, no walking phase either.

Now for a little story. Most lights were turned off in the center by the time, on the second week I noticed that I could see quite some stars from the track, so I lied down on the side after running to get a closer look. It's a solemn moment, I might keep running so that I could do this after. No photographic evidence, try it out yourself.

In closing, good habits are frail; I don't know how long I can keep this up—hopefully long enough. If frailty is not enough problem, I tend to treat myself to some chocolate afterwards. If you just read the whole thing, it might not hurt to treat yourself to some too!