Fighting to Make GTK+, Glade, and Python Play in Windows

These screenshots show me running the Py2EXE-compiled script I wrote last weekend on a Windows 7 machine. Additionally there is a screenshot of the “Add/Remove Programs” window demonstrating which versions of which libraries were required.glade_exeneedToInstall


Python Script with GTK+ GUI Compiled with Py2EXE

Wow, that’s a mouthfull. This is a total hack, but it works — and barely I might add! I spent all night jumping through hoops to get this thing to run on Windows. The problem is that I designed my previous UI in a version of GLADE which is newer than that supported by Windows. It looks like it’s not backward-compatible, so I have to re-design the GUI from scratch using an earlier version of GLADE. I’ll probably stick to GTK version 2.12 and Python version 2.6 because they play nicely on Windows. It’s a quick and dirty script, but I was able to make the following run on Windows as a single EXE file!glade_windows_python



Spectrograph UI Made with Glade

While continuing to investigate my options for the new version of QRSS VD, I re-visited Glade, the GTK GUI designer. In short, it lets you draw widgets (combo boxes, scrollbars, labels, images, buttons, etc) onto windows and then makes it easy to add code to the GUI. I *hated* the old QRSS VD development because of the ridiculously large amount of time I had to spend coding the UI. Hopefully by migrating from TKinter to GTK – while it opens a whole new can of worms – will let me add functionality rapidly without hesitation.glade_python_improving

Here’s a quick screenshot of my running this new version of the software with a GUI I made in less than an hour. The bars for brightness and contrast can be adjusted which modify the spectrograph in real time. The audio is whatever is playing in Pandora. I like the “fantastic plastic machine” radio station!


Fast TK Pixelmap generation from 2D Numpy Arrays in Python

I had TKinter all wrong! While my initial tests with PyGame’s rapid ability to render Numpy arrays in the form of pixel maps proved impressive, it was only because I was comparing it to poor TK code. I don’t know what I was doing wrong, but when I decided to give TKinter one more shot I was blown away — it’s as smooth or smoother as PyGame. Forget PyGame! I’m rendering everything in raw TK from now on. This utilizes the Python Imaging Library (PIL) so it’s EXTREMELY flexible (supports fancy operations, alpha channels, etc).fast_scroll_tk

The screenshot shows me running the script (below) generating random noise and “scrolling” it horizontally (like my spectrograph software does) at a fast rate smoothly (almost 90 FPS!). Basically, it launches a window, creates a canvas widget (which I’m told is faster to update than a label and reduces flickering that’s often associated with rapid redraws because it uses double-buffering). Also, it uses threading to handle the calculations/redraws without lagging the GUI. The code speaks for itself.

import Tkinter
from PIL import Image, ImageTk
import numpy
import time

class mainWindow():

	def __init__(self):
		self.root = Tkinter.Tk()
		self.frame = Tkinter.Frame(self.root, width=500, height=400)
		self.canvas = Tkinter.Canvas(self.frame, width=500,height=400),y=-2)
		self.root.after(0,self.start) # INCREASE THE 0 TO SLOW IT DOWN

	def start(self):
		global data'L', ([1],[0]),'b').tostring()) = ImageTk.PhotoImage(
		if self.times%33==0:
			print "%.02f FPS"%(self.times/(time.clock()-self.timestart))

if __name__ == '__main__':


Detrending Data in Python with Numpy

While continuing my quest into the world of linear data analysis and signal processing, I came to a point where I wanted to emphasize variations in FFT traces. While I am keeping my original data for scientific reference, visually I want to represent it emphasizing variations rather than concentrating on trends. I wrote a detrending function which I’m sure will be useful for many applications:detrend_fft

def detrend(data,degree=10):
	for i in range(degree,len(data)-degree):
	return detrended+[None]*degree

However, this method is extremely slow. I need to think of a way to accomplish this same thing much faster. [ponders]

UPDATE: It looks like I’ve once again re-invented the wheel. All of this has been done already, and FAR more efficiently I might add. Simply:

import scipy.signal

Now I’m looking into scipy.signal.triang()


Insights Into FFTs, Imaginary Numbers, and Accurate Spectrographs

I’m attempting to thoroughly re-write the data assessment portions of my QRSS VD software, and rather than rushing to code it (like I did last time) I’m working hard on every step trying to optimize the code. I came across some notes I made about Fast Fourier Transformations from the first time I coded the software, and though I’d post some code I found helpful. Of particular satisfaction is an email I received from Alberto, I2PHD, the creator of Argo (the “gold standard” QRSS spectrograph software for Windows). In it he notes:

I think that [it is a mistake to] throw away the imaginary part of the FFT. What I do in Argo, in Spectran, in Winrad, in SDRadio and in all of my other programs is compute the magnitude of the [FFT] signal, then compute the logarithm of it, and only then I do a mapping of the colors on the screen with the result of this last computation.

These concepts are simple to visualize when graphed. Here I’ve written a short Python script to listen to the microphone (which is being fed a 2kHz sine wave), perform the FFT, and graph the real FFT component, imaginary FFT component, and their sum. The output is:real_imaginary_fft_pcm

Of particular interest to me is the beautiful complementarity of the two curves. It makes me wonder what types of data can be extracted by the individual curves (or perhaps their difference?) down the road. I wonder if phase measurements would be useful in extracting weak carries from beneath the noise floor?

Here’s the code I used to generate the image above. Note that my microphone device was set to listen to my stereo output, and I generated a 2kHz sine wave using the command speaker-test -t sine -f 2000 on a PC running Linux. I hope you find it useful!

import numpy
import pyaudio
import pylab
import numpy

soundcard=1 #CUSTOMIZE THIS!!!
			input_device_index=soundcard,input=True) #prime the sound card this way
pcm=numpy.fromstring(, dtype=numpy.int16)

freq=freq*rate/1000 #make the frequency scale

pylab.title("Original Data")
pylab.xlabel("Time (milliseconds)")
pylab.title("Real FFT")
pylab.xlabel("Frequency (kHz)")
pylab.title("Imaginary FFT")
pylab.xlabel("Frequency (kHz)")
pylab.title("Real+Imaginary FFT")
pylab.xlabel("Frequency (kHz)")

After fighting for a while long with a “shifty baseline” of the FFT, I came to another understanding. Let me first address the problem. Taking the FFT of different regions of the 2kHz wave I got traces with the peak in the identical location, but the “baselines” completely different. fft_base2

Like many things, I re-invented the wheel. Since I knew the PCM values weren’t changing, the only variable was the starting/stopping point of the linear sample. “Hard edges”, I imagined, must be the problem. I then wrote the following function to shape the PCM audio like a triangle, silencing the edges and sweeping the volume up toward the middle of the sample:

def shapeTriangle(data):
	return data*triangle

After shaping the data BEFORE I applied the FFT, I made the subsequent traces MUCH more acceptable. Observe:fft_base3

Now that I’ve done all this experimentation/thinking, I remembered that this is nothing new! Everyone talks about shaping the wave to minimize hard edges before taking the FFT. BAH! Another case of me re-inventing the wheel because I’m too lazy to read others’ work. However, in my defense, I learned a lot by trying all this stuff — far more than I would have learned simply by copying someone else’s code into my script. Experimentation is the key to discovery!


Smoothing Window Data Averaging in Python – Moving Triangle Tecnique

While I wrote a pervious post on linear data smoothing with python, those scripts were never fully polished. Fred (KJ4LFJ) asked me about this today and I felt bad I had nothing to send him. While I might add that the script below isn’t polished, at least it’s clean. I’ve been using this method for all of my smoothing recently. Funny enough, none of my code was clean enough to copy and paste, so I wrote this from scratch tonight. It’s a function to take a list in (any size) and smooth it with a triangle window (of any size, given by “degree”) and return the smoothed data with or without flanking copies of data to make it the identical length as before. The script also graphs the original data vs. smoothed traces of varying degrees. The output is below. I hope it helps whoever wants it! moving window triangle smoothing python

import numpy
import pylab

def smoothTriangle(data,degree,dropVals=False):
	"""performs moving triangle smoothing with a variable degree."""
	"""note that if dropVals is False, output length will be identical
	to input length, but with copies of data at the flanking regions"""
	for i in range(degree,len(data)-degree*2):
	if dropVals: return smoothed
	while len(smoothed)<len(data):smoothed.append(smoothed[-1])
	return smoothed

data=numpy.random.random(100) #make 100 random numbers from 0-1
data=numpy.array(data*100,dtype=int) #make them integers from 1 to 100
for i in range(100):
	data[i]=data[i]+i**((150-i)/80.0) #give it a funny trend

pylab.plot(data,"k.-",label="original data",alpha=.3)
pylab.plot(smoothTriangle(data,3),"-",label="smoothed d=3")
pylab.plot(smoothTriangle(data,5),"-",label="smoothed d=5")
pylab.plot(smoothTriangle(data,10),"-",label="smoothed d=10")
pylab.title("Moving Triangle Smoothing")


Simple Python Spectrograph With PyGame

WARNING: this project is largely outdated, and some of the modules are no longer supported by modern distributions of Python.

For a more modern, cleaner, and more complete GUI-based viewer of realtime audio data (and the FFT frequency data), check out my Python Real-time Audio Frequency Monitor project.

While thinking of ways to improve my QRSS VD high-definitions spectrograph software, I often wish I had a better way to display large spectrographs. Currently I’m using PIL (the Python Imaging Library) with TK and it’s slow as heck. I looked into the PyGame project, and it seems to be designed with speed in mind. I whipped-up this quick demo, and it’s a simple case audio spectrograph which takes in audio from your sound card and graphs it time vs. frequency. This method is far superior to the method I was using previously to display the data, because while QRSS VD can only update the entire GUI (500px by 8,000 px) every 3 seconds, early tests with PyGame suggests it can do it about 20 times a second (wow!). With less time/CPU going into the GUI, the program can be more responsivle and my software can be less of a drain.
Simple Spectrograph

import pygame
import numpy
import threading
import pyaudio
import scipy
import scipy.fftpack
import wave
rate=12000 #try 5000 for HD data, 48000 for realtime
overlap=5 #1 for raw, realtime - 8 or 16 for high-definition
def graphFFT(pcm):
	global currentCol, data
	ffty=scipy.fftpack.fft(pcm) #convert WAV to FFT
	ffty=abs(ffty[0:len(ffty)/2])/500 #FFT is mirror-imaged
	#ffty=(scipy.log(ffty))*30-50 # if you want uniform data
	print "MIN:t%stMAX:t%s"%(min(ffty),max(ffty))
	for i in range(len(ffty)):
		if ffty[i]<0: ffty[i]=0
		if ffty[i]>255: ffty[i]=255
	if len(scooter)<6:return
	if currentCol==windowWidth: currentCol=0

def record():
	p = pyaudio.PyAudio()
	inStream =,channels=1,rate=rate,
	while True:
		pcm=numpy.fromstring(, dtype=numpy.int16)

pal = [(max((x-128)*2,0),x,min(x*2,255)) for x in xrange(256)]
print max(pal),min(pal)
#data=Numeric.array(data) # for older PyGame that requires Numeric
pygame.init() #crank up PyGame
pygame.display.set_caption("Simple Spectrograph")
world=pygame.Surface((windowWidth,fftsize/2),depth=8) # MAIN SURFACE
t_rec=threading.Thread(target=record) # make thread for record()
t_rec.daemon=True # daemon mode forces thread to quit with program
t_rec.start() #launch thread
while 1:
	for event in pygame.event.get(): #check if we need to exit
		if event.type == pygame.QUIT:pygame.quit();sys.exit()
	pygame.surfarray.blit_array(world,data) #place data in window
	screen.blit(world, (0,0))
	pygame.display.flip() #RENDER WINDOW
	clk.tick(30) #limit to 30FPS


I've Tried the Windows Thing Again, and I Give Up!

After spending nearly my entire teen-age life bitterly rejecting any Microsoft products (none of the computers in my room had any MS software on any of them for years), I gradually eased back into the Windows life. My wife got a new PC, it ran Windows, she was happy, I was happy. She’s since gotten a better PC, and I settled into using her old one. Windows (Vista) was junky as heck, so I replaced it with XP, and never got around to loading Linux to dual boot with. While I wasn’t a fan of Windows, it seemed easy enough, and I tolerated it. Today I come home after a long day of dental school and the darn thing won’t boot. It tries to boot, and goes into some weird endless reboot cycle. After getting mad and just letting it reboot on its own a bunch of times, it finally managed to boot — but with no icons, and explorer.exe wouldn’t load. I booted in safe mode, consolidated all the files I wanted into a single folder, then booted from a USB drive I carry with me at all times which has an Ubuntu LiveCD on it. I’m installing Linux as I write this. [snap] i_hate_windows

There are several things ironic about this:
1.) I’m surfing the net / blogging WHILE installing Ubuntu – try being productive while Windows is installing!
2.) The Internet works. Even with windows installed, the Internet doesn’t work with this USB wireless adapter – it requires drivers. In fact, it doesn’t work on Windows 7 AT ALL! And here it is running out of the box.
3.) I can determine my graphics cards with a single command: lspci – try doing that with Windows. MAYBE you’ll see “UNKNOWN DISPLAY ADAPTER” in the Device Manager at best. Here I have drivers up and ready to go.
4.) I’m not even a fan of Linux. At this point, I’m more disappointed in Microsoft. I don’t care what software I run – I just want it to work, and I feel I’m getting let down again and again. Even if my problem were caused by a virus, it begs the question of why it can happen in the first place. All I do is browse the web! Are you kidding me?

Bah! I hate computers -_-

update: Kyle Walker posted a comic I had to share. Thanks Kyle!hitler_windows


Simplest Case PyGame Example

I’m starting to investigate PyGame as an alternative to PIL and K for my QRSS VD spectrograph project. This sample code makes a box bounce around a window.

import pygame, sys
pygame.init() #load pygame modules
size = width, height = 320, 240 #size of window
speed = [2, 2] #speed and direction
screen = pygame.display.set_mode(size) #make window
s=pygame.Surface((100,50)) #create surface 100px by 50px
s.fill((33,66,99)) #color the surface blue
r=s.get_rect() #get the rectangle bounds for the surface
clock=pygame.time.Clock() #make a clock
while 1: #infinite loop
        clock.tick(30) #limit framerate to 30 FPS
        for event in pygame.event.get(): #if something clicked
                if event.type == pygame.QUIT: #if EXIT clicked
                        sys.exit() #close cleanly
        r=r.move(speed) #move the box by the "speed" coordinates
        #if we hit a  wall, change direction
        if r.left < 0 or r.right > width: speed[0] = -speed[0]
        if < 0 or r.bottom > height: speed[1] = -speed[1]
        screen.fill((0,0,0)) #make redraw background black
        screen.blit(s,r) #render the surface into the rectangle
        pygame.display.flip() #update the screen