It’s black and its listening!

I’ve been longing for this day (/night) for almost an year now! I’ve finally, finally, got some data from an electret mic and finally got it into a beaglebone black. If you really want to understand the depth of this achievement, please go through what I’ve been doing for the past few months:

 

http://electronics.stackexchange.com/questions/111458/reading-sound-with-beaglebone-black

http://electronics.stackexchange.com/questions/111969/understanding-a-circuit-diagram

http://electronics.stackexchange.com/questions/106204/connect-electret-mics-to-computer

http://electronics.stackexchange.com/questions/77160/microphone-time-delay-estimation

http://dsp.stackexchange.com/questions/11399/signal-processing-using-panda-board

http://dsp.stackexchange.com/questions/11561/sound-card-for-recording-audio

http://dsp.stackexchange.com/questions/11472/audio-interface-or-external-sound-card

 

That’s a lot of questions. And I got a lot of answers. Though I’m no where near completing the actual project, at least getting it started is a relief. I’ve tried making my own amplifier, tried an audioboard from hrc, tried another opamp IC for amplifier, and finally hit upon this :

Click to access hbmic.pdf

The previous amplifiers I made might have been working fine – I just might not have read the values properly. Anyways, its working now and that’s all I care about.

Ain't she a (black) beauty?
Ain’t she a (black) beauty?

 

Components : Everything that was in the previous link containing the circuit diagram. I did not have 10 uF capacitors so I used 22 uF instead.

Beaglebone black : The INR 4200 beast. This board is being sold out faster than it is being produced. Lucky we could order one!

The process is pretty straightforward now that it works. The beaglebone black has a set of analog pins that can read analog inputs. I connect the mic (which produces voltage variations in the range of 20mv) to a preamplifier (built using the circuit diagram in the link and 741 opamp) and the output of the preamp (in range of 1 v) is given to the analog input of the BBB. When the sound hits the electret mic, it produces a voltage that is proportional to the amplitude of the sound. This is the signal we then amplify and finally measure.

Is this enough to do some signal processing and thus continue the project? Now that requires more experimenting and is a matter for another post.

I made the mistake of not connecting the ground of beaglebone black to the ground rail in the breadboard and cursed many perfectly working amplifier circuit because it BBB did not read.

Now we have to read this value using beaglebone.

First, enable the input output pins :

echo cape-bone-iio > /sys/devices/bone_capemgr.*/slots
If that step went well, you should not see any messages.

Then I wrote a small python script to read the analog pin 1 continously :


import os
import time

while 1:
os.system("cat /sys/devices/ocp.2/helper.14/AIN1")
time.sleep(0.1)

Replace AIN1 with your analog pin (for eg: AIN2 or AIN3) and the value in time.sleep with your desira
ble delay between reads. Saved it as test.py and python test.py does the job.

BBB reading values. Only positive values can be read. I should have given a DC bias.
BBB reading values (look hard. They’re there). Only positive values can be read. I should have given a DC bias.

The beaglebone black can read only voltages till 1.8 volts on its analog pins. Also, it can only read DC. That is why there are a lot of 0s in my screenshot – that’s where the negative voltages should go. The solution is to give a DC bias to the analog signal. Say we give 1v bias. Then the readings would be around 1V and will go below 1v when a negative peak occurs.

GSoC : Unit tests, Merges and Travis

Its been two weeks since community bonding got over and I’ve been making slow but steady progress. A couple of new bugs has surfaced and I will be fixing them before moving on to my main task.

Also, varnam project has been moved to github owing to gitorious being too unstable/unreliable.

I walked (crawled would be more appropriate) into some new territory the past week :

1. Unit tests : Every software project need unit tests. Unit tests make sure that all the tiny little parts (units) of the software function the way they are supposed to function and that you did not accidentally break anything with your new commit. I had heard of them, but I never thought I’d have to do them in c. Varnam uses check for unit testing. I wrote a patch that checks for the validity of the suggestions file libvarnam receives and wrote a test case for it. I learned to do stat calls to a path, rather than using fopen() to check if the file exists. Being the beginner I am, I did break something because the test fails at 70%. Should look into it.

2. Merging : I finally understood what merging is in git, though a bit painfully. Somehow I couldn’t digest the fact that git could simply “merge” all the changes that I made and all the changes (possibly overlapping) that some one else made into something that works. The other guy could have completely removed the stuff I’ve been working on from his commit! I just found out that doing so results in conflicts, and git won’t merge the branches until the conflicts are resolved. Bloody mess. Thankfully, there are wonderful tools like meld that makes resolving conflicts a lot less painful. Besides, the conflicts I had was of a less “violent” nature. So there I go, making my very first (meaningful) merge and a pull request

3. Travis : Just when I thought that the day is done, Travis CI reported that the build failed. Travis? Travis is a continous integrations tool integrated into github. This means, every time I push into a repository, Travis builds (does cmake . , and make) the project after merging my pull request and then runs the test cases to see if anything is broken. And yes, I had broken quite a few things. My new patch completely fails a few assertions.

****************I called it a day and went to sleep*****************

I spent almost the entire day debugging and the tests continued to fail. Apparently some files that should have been generated are simply not there. I created a quick hack, and put together a new pull request. At least the tests are proceeding better now. Travis still fails. Perhaps my mentor can help me with that.

The pull request : https://github.com/varnamproject/libvarnam/pull/47

KDE, Gnome, Unity and cinnamon – why should you care?

You shouldn’t.

It really doesn’t matter. But geeks and fanboys support their favorite desktop environments with religious fervor out there. If you’re ever in doubt, just pick one and go. The next n paragraphs are on how and why I switched from KDE to cinnamon.

(Desktop environments in linux are more less like themes in windows – and more. Get started with linux here)

I was running a linux mint 15 KDE on my computer till now. It was not a stable version (based on ubuntu 13.10) and had quite a few broken things in it. Worse – I couldn’t get the latest (and the greatest) version of firefox from the repositories – the newest version that was available was firefox 27. So I decided to upgrade my OS and since there was a general idea that KDE was heavy on resources, I thought I’d give cinnamon a try. But a quick glance at the forums made it clear that KDE is not as heavy as it used to be. It boots like a snail, true. But once you actually start doing something KDE seems to use less memory than ubuntu’s unity.

So, I installed linux mint 17 cinnamon without any problems. I was disappointed to see that it took as much time as KDE to log in, even though it was supposed to be lighter (I was wrong, cinnamon is faster, read on!). However, cinnamon was super snappy when I started clicking things, and to my surprise, had everything I wanted! It is true that you can’t tweak cinnamon to the extend you can tweak KDE. But cinnamon had just the right things for me. The default wallpapers are no good though, and I quickly went in search of wallpaper packs.

Desktop
Desktop : You will have a harder time finding wallpapers that go with the default green icon set

Switching workspaces is sweet, and considerably faster than the 3D cube in KDE (you can turn this off, but I couldn’t resist it while I was using KDE). Pressing ctrl+alt+ quickly takes you between workspaces with a swish. Yes, an audible, annoying “swish”. Also, it displays “workspace 1” or “Workspace 2” when you switch into the corresponding workspace. I quickly changed the workspace labels to “Home” and “Development”. I haven’t decided what to do about the “swish” yet. To my disappointment, it is not possible to have different wallpapers for the different workspaces. This is a complete let down. But a quick search revealed that only KDE has implemented this feature so far. Moving on, I configured ‘hot corners’ – Move your mouse to the upper left corner and all the active windows are lined up in a nice grid. If unity had these (supported out of the box), maybe I would have never moved from stock ubuntu. Move mouse to the lower right and it shows the desktop. These are configurable, of course.

Meanwhile, the slow startup time was driving me mad. I did not wipe out my previous OS for this! I got so pissed I downloaded and installed the 64 bit version of Mint 17 cinnamon. What a relief! Faster and snappier and me happier! I now remember that my previous KDE was 64 bit. On comparison, cinnamon 64 bit is definitely faster.

Window switching
Window switching : When I go to the upper right corner

Oh, and now I have nvidia drivers by default now. Optimus do not work yet, but prior linux distros I tried didn’t even detect my card, and installing nvidia drivers almost always crashed my system. Nvidia nouveu was selected as default (no hardware acceleration) and proprietary drivers from Nvidia were listed as well. However, switching to proprietary drivers crashed cinnamon, and kept crashing until I switched back to Nouveu. This was expected since I have an optimus card. Might have worked if I was on a desktop.

Since I have the name sake Nvidia drivers now, decided to try steam on linux. Opened up software centre to install steam, and uh oh, its less bloated and more snappy than its ubuntu counterpart. For the first time in my life, I’m using software manager rather than the command line apt-get to install things I need. It gave me great app suggestions and I found myself installing minitube, a native youtube client for streaming content from youtube (which crashed cinnamon – reported by multiple users).

Mint 17 came with it usual share of bundled applications and proprietary codecs to get me started. Torrent client was working (though it took me somewhile to get it to capture magnet links) and steam is downloading monochroma demo.

Verdict? Comfortable, convenient, way thinner (on the memory) than KDE, and way better than everything except KDE (tied).

GSoC : Community bonding

The community bonding period of this year’s google summer of code is nearing an end. Its been a rather busy week, and I had to juggle time between exam preps and GsoC. I cannot say that I have made much progress. However, an IRC meeting with the mentor turned out to be very fruitful. It was about setting up the right development environment, and I did learn a lot!

1. ctags/etags : I was complaining how hard it is to find function definitions in the libvarnam codebase. There are a lot of header files. That’s when I heard about ctags. I had to install the ctags package from the ubuntu repositories, and configure it to catalogue the libvarnam folder. Then I got myself the sublime text editor and installed the plugin for ctags. Now all I have to do is press ctrl+t+t when I encounter a function call and sublime will open the the definition of that function in a separate tab! Productivity multiplied – ten fold!

Another convenient way (though not as convenient) would be to use grep -iR. The -iR argument makes grep list the files from which the pattern matches were found.

2. Nemiver : I have used the gnu debugger (gdb) in my lab before. The programs I wrote then were rather small and I could live without a debugger. But mentor says no. Nemiver is a rather neat front end to gdb and I don’t have to look up line numbers to insert break points anymore – I click on the line instead. Also, nemiver makes the print command in gdb quite obsolete. Nemiver shows the values of all the variables in the scope as a list.

3. Sample project : My first task. In order to get myself familiar with libvarnam and learn some debugging in the process, the mentor asked me to write a sample project. My sample program, found here, would convert all the string literals in a python program into their corresponding Malayalam equivalent. Simple and buggy. But I did learn how to make nemiver branch into the libvarnam API and do some transliteration.

Now that I’m getting a few days gap before the last exam, I must fix a bug or two. I hope I’ll be able to start working on the stemming algorithm starting May 20th.

Google Summer of Code!

I’m excited to announce that I’ve been selected to this year’s google summer of code. My mentoring organization is SMC – Swathantra Malayalam Computing and I will be working on the varnam project.

Varnam means ‘colors’. Varnam is a transliterator for indic languages. My task is to improve the learning capability of varnam by coming up with a stemmer algorithm for indic languages. A stemmer algorithm returns a base word when it is supplied a complex word. In english, supplying ‘retirement’ to the porter stemmer algorithm will trim it down to ‘retire’ and subsequently return ‘retir’. I have to do the same thing with malayalam words. The trick is to design the whole thing in such a way that stemming support for other languages can be easily added. The stemming rules will differ from language to language. Though I will be laying down the rules for malayalam, I should provide room for someone else if she decides to add support for another language. In short, my algorithm should be designed to read a ‘rule file’.

The varnam project can be found here. Why use varnam when you have, say, google input tools? For one, google input tools work only in windows. Two, I’m not sure if you can use it in your own programs. I guess not. Three, it is not open source which means google won’t let you take a peek inside. Four, varnam can render the whole linux shell in malayalam if need be (and if you are willing to put in the effort)! To be frank, seeing small round malayalam alphabets on my desktop konsole was quite unexpected!

I’m so grateful to SMC for letting me work on this and even more grateful to google for the upcoming paycheck ;). SMC requires us to keep the blog updated on a weekly basis, so I guess everyone will be hearing an awful lot from me 😀

Getting machines to listen

I’ve been wanting to do this sound localization project for almost an year now. Its simple – have a few microphones ready, yell at it, and display the direction of sound on a screen. And being the electronics newbie I am, I had spent a considerable amount of time wondering how to connect an electret mic to my board/computer.

However, the last few days have been extremely productive and now I understand what exactly goes on when you yell at your laptop mic.

The Task : Record something using the built in mics (or any mic), store it in RAW format, access it using a python program. Understand sound.

The tool : Ladies and Gentlemen, meet ALSA – Advanced Linux Sound Architecture
Another tool : pyaudioalsa. Helps us do the necessary stuff without resorting to the C API.

You already have ALSA if you are using any of the major Gnu/linux distributions. First get pyalsaaudio and install it on your computer.

The pyalsaaudio page have some nice examples as to how to record sound. Go through it. By default, your recordings are in RAW PCM format. PCM stands for pulse code modulation. The output of your recording is actually a set of values that denote the amplitude of the input sound at various points in time. Just go through and try running the examples that come with pyaudioalsa source – record.py and playback.py.

Your recording is saved as a PCM RAW file. Try opening it in a text editor and you will see junk values.

But where are the amplitude values?

Exactly. Before that, we will try playing it back. Of course you can do it using the example program playback.py. But there’s another way. Try this on the terminal :

aplay -r 44100 -f S16_LE -c 1

That command will make ALSA play back the recording for you. 44100 is the sampling rate of the recording. How did we know that? Look inside record.py and you will see that the recording was sampled at 44.1KHz. S16_LE denotes that there are 16 bits per sample in our recording stored in little endian format? Now how did we know that?. Again, check record.py. The ‘-c 1’ tells ALSA that our recording is mono.

So that’s how you playback raw PCM files.

But what if you want to do some signal processing? What if you want to draw one of those spectrums or waveforms and other seemingly complicated stuff? Then you will need to extract the data from the RAW PCM. It sounds complicated, I know. Have no fear, python (and numpy) is here:

import numpy
data = numpy.memmap("test.pcm", dtype='h', mode='r')
print "VALUES:",data

Finally, something human readable!
Why don’t we draw a graph?
import numpy, pylab
data = numpy.memmap("test.pcm", dtype='h', mode='r')
print data
pylab.plot(data)
pylab.show()

Note : I’m very grateful to this guy for showing me how to do this.

So what do we have here? We have successfully made use of the RAW PCM data. So? There are many algorithms in Scipy and numpy that can do amazing things with that data – cross correlation,fft,convolution. Thank God (and Guido Van Rossum) for python!

S-tall-man the tall man

SPACE – Society for the Promotion of Alternative Computing Environments celebrated their 10 years of existence today here at Trivandrum and they invited none other than Richard Stallman to do the talking. I was 13 or 14 years old when my father first told me about Richard Stallman and about the ‘big things’ he were doing with computers. The only impression I got from his photo was that he was a particularly huge man with huge beard and loooong hair. Today, I saw this guy ‘for real’. Mr.Stallman has had a rather warm relationship with the government of Kerala and he had visited the state several times before. After all, this guy had convinced our government to get rid of the windows PCs and move to free software. Thanks to Stallman – I’m not using that retarded Turbo c++ compiler to compile my c++ programs.

Not so huge from where I am sitting!
Not so huge from where I am sitting!
Shake hands for freedom
Shake hands for freedom.

The talk was semi-boring. The people on the dias dozed off while Stallman was buzy giving us a lecture on digital freedom. Neverthless, he did come up with a few new things to say. There was the usual tandrum about security and privacy on the internet and unjust surveillance. He explained that he has never owned a portable (mobile) phone and never will. Said he would rather not use that piece of technology than let someone spy on him. A friend brought to my attention the rather unusual similarity between Stallman’s way of talking and the flavorless, emotionless voice used by Microsoft windows to read things aloud. If I were blind, I would have (could have, to be more precise) mistaken Stallman’s lecture for a windows machine reading text aloud!

Then he moved on to his forte – free software. As usual, he specifically portrayed Microsoft and the ‘ithings’ as evil and what you and me call open source as a lesser evil. Open source was not enough for him, he wanted things to be ‘free’. This raised a few eyebrows, and I think it raised my eyebrows the farthest. But I already knew this from the wikipedia entry on him so I was more or less feigning surprise. Also, as he had been doing for years, he emphasized the necessity and the importance of referring to linux as GNU/linux (pronounced GNU slash linux or GNU plus linux) instead of just linux. He gave a convoluted philosophical explanation to it but it was bull shit. It seems the man has a problem with Linus Torvalds.

Then he said something really meaningful. He said that people have the right to use proprietery software and it is actually OK to do so. “Even though they are hurting themselves”, said Stallman, “they are not doing harm to the society”. But, governments and states are established for the purpose of serving man. Institutions and frameworks with public benefit in mind should never ever use proprietary software. Doing so puts the “national security at risk”. This makes sense. Now that he has said it, I feel victimized when I remember that the only operating system available on our school computers were Microsoft Windows. I feel that me, along with a bunch of other kids, were forced to use commercial software that was just popular but not universal. Since we were learning to program and not to ‘use’ computers it would have been better if we were trained on a *nix machine – at least it is free and can be modified at will. Using only Microsoft windows at school is like having to eat only one flavour of ice cream while you are at school. States should not have the option of using proprietery software. The source code should be able to be examined to make sure that there are no backdoors or security threats than can be engineered to be used for malicious purposes. Good thinking Mr.Stallman 🙂

There was another revelation in there for me – Stallman claimed that Facebook monitors a lot of people – people who don’t even have a Facebook account. The logic was this. When your browser loads a website that contains the ‘Share via Facebook button’, your browser requests the Facebook server to provide it with the blue and white ‘F’ image. Your browser tells the Facebook server that it is for your computer (your ip) and for using at this website. Hurray, Zuckerberg knows which site you are surfing!

As for me, I would go with the open source guys. Stallman and software that is ‘truly liberated’ is an extreme, and I don’t like extremes. Not because extremes are difficult to live with, but because such extremes almost always have little practical value and their advocates are often fanatics who refuse to be convinced otherwise. Neverthless, people like you and me are indebted to Richard Stallman for things like GNU, and drum rolls please – gcc (GNU C Compiler).

P.S – Mr.Stallman held an auction at the hall and sold a ‘gnu’ doll – and I came to know that there is an animal called gnu and it lives in africa. The doll was sold for rs.2500.

Back with some brains!

Its been quite some time since I posted, I admit. The good news is, I’m back. Another good news is, I found something cool!

Gone are the days when the word artificial intelligence suddenly pulls up neural networks to your mind. Actually, gone are the days of artificial intelligence it seems. Cognitive computing is going to be the norm, or at least I hope so. Simply put, cognitivie computing is mimicing the way we humans think and making computers do the same.

“But I thought that was what artificial intelligence was all about”

Yes and no.

Though cognitive computing might actually qualify as a way of implementing intelligent machines, conventional artificial intelligence was problem specific. There was usually a separate “learning phase” where we have to feed tons of data to the supposedly intelligent machine. Cognitive computing is a significant improvement on this considering that these machines can learn online. That is, there is no separate learning phase. The machine learns as it work, just like we humans do.

Numenta is a company that deals with the above said “stuff”. They have built a platform (or is it a software?) called nupic (numenta platform for intelligent computing) which implements something known as hierarchichal temporal memory (HTM). And it uses a cortical learning algorithm (CLA) to mimic human brain. Basically, the nupic functions more or less like how we do.

Enough boring theory.

Visit numenta and nupic here : http://numenta.org/

The nupic is open source and you can get the source on github :

https://github.com/numenta/nupic

A warning though. The nupic has a steep learning curve. So get your hands dirty only if you have some time and patience. I couldn’t run the tests on the build (to check if nupic installed correctly) successfully and is still asking around for solutions (the mailing list is great).

And they have some awesome videos of previous hackathons and example programs that clearly demonstrates the power of nupic. Here’s a link.

Coloring the canvas – the processing way

I thought it was time somebody started painting on the canvas. Its been a couple of months since I wrote this program and it was lying in a corner of my hard disk all along. Realized kevinkoder.tk is pretty low on contents and decided to add it to the site.

http://www.kevinkoder.tk/paint7.htm

And the source code can be viewed by pressing ctrl+u (in chrome). But I must warn you though – I’ve put absolutely zero effort into making the code readable. I’m pretty sure even I can’t make sense of it right now. I was in a hurry to do something with processing and thought “Hey, lets make a paint program”. The development came to an abrupt stand still when i tried to implement the ‘fill bucket’ tool using the flood fill algorithm – the damn thing simply refused to work the way I wanted it to.

Anyways, the program proves beyond all doubt that a lot of amazing things are simply doable with processing.

And I had to choose not to include a lot of additional libraries that might have made it a lot easier to develop the program (like a java library that can add additional drawing layers) because 3rd party processing java libraries are incompatible with processing.js , ie, they wont work when you convert the processing code into javascript.

A few handy ‘features’ were added to compensate for the ultimate pathetic-ness of the program. More info can be found in the documentation.

And the documentation : http://www.kevinkoder.tk/paint_doc.htm

.