3:54:04 am on 7/30/10
Menu
» Home
» About Scott
» QRSS VD
» Old Stuff
» Archive
» Contact

Categories
» C/C++
» Circuitry
» Dentistry
» DIY ECG
» General
» Linux
» Microcontrollers
» Molecular Biology
» My Website
» PHP
» Prime Numbers
» Python
» Radio
» UCF Lab
» Everything
Writings
» MD Labels
» Streamrip
» AIM Thoughts
» WindowsXP?
» Partitioning
» CD/DVD Repair
» Monitor Info
» CRT Deflection
» Venomcrack
» Flash Thing
» Heart/Brain
» Diabetes
» Triops
» Biomed

Friends
» Fred
» Kyle W
» Nick
» Louis
» Tom
» Kyle H




Archives
» July 2010
» June 2010
» May 2010
» April 2010
» March 2010
» February 2010
» January 2010
» December 2009
» September 2009
» August 2009
» July 2009
» June 2009
» May 2009
» April 2009
» March 2009
» February 2009
» January 2009
» December 2008
» November 2008
» October 2008
» September 2008
» September 2007
» December 2006
» August 2006
» January 2006
» August 2005
» July 2005
» June 2005
» May 2005
» April 2005
» March 2005
» February 2005
» January 2005
» December 2004
» November 2004
» October 2004
» September 2004
» August 2004
» July 2004
» June 2004
» May 2004
» April 2004
» March 2004
» February 2004
» January 2004
» December 2003
» November 2003
» October 2003
» September 2003
» August 2003
» July 2003
» June 2003
» May 2003
» April 2003
» March 2003
» February 2003
» January 2003
» December 2002
» November 2002
» October 2002
» September 2002
» June 2001

Idea: vdFSK modulation
Posted by
Scott on July 22nd, 2010 12:39:54 PM | 854 words | 2 Comments
Filed under: General | Scott was 24.82 years old when he wrote this

My goal is to create a QRPP (extremely low power) transmitter and modulation method to send QRSS (extremely slow, frequency shifting data) efficiently, able to be decoded visually or with automated image analysis software. This evolving post will document the thought process and development behind AJ4VD’s Frequency Shift Keying method, vdFSK.

Briefly, this is what my idea is. Rather than standard 2-frequencies (low for space, high for tone) QRSS3 (3 seconds per dot), I eliminate the need for pauses between dots by using 3 frequencies (low for a space between letters, medium for dot, high for dash). The following images compare my call sign (AJ4VD) being sent with the old method, and the vdFSK method.

Traditional QRSS:
traditional

Again, both of these images say the same thing: AJ4VD, (.- .— ….- …- -..). However, note that the above image has greater than a 3 second dot, so it’s unfairly long if you look at the time scale. Until I get a more fairly representative image, just appreciate it graphically. It’s obviously faster to send 3 frequencies rather than two. In my case, it’s over 200% faster.

vdFSK method:
modulation

This is the code to generate audio files converting a string of text into vdFSK audio, saving the output as a WAV file. Spectrographs can be created from these WAV files.

### generate_audio.py ###
# converts a string into vdFSK audio saved as a WAV file

import numpy
import wave
from morse import *

def makeTone(freq,duration=1,samplerate=5000,shape=True):
    signal = numpy.arange(duration*samplerate)/float(samplerate)*float(freq)*3.14*2
    signal = numpy.sin(signal)*16384
    if shape==True: #soften edges
        for i in range(100):
            signal[i]=signal[i]*(i/100.0)
            signal[-i]=signal[-i]*(i/100.0)
    ssignal=''
    for i in range(len(signal)): #make it binary
        ssignal += wave.struct.pack('h',signal[i])
    return ssignal

def text2tone(msg,base=800,sep=5):
    audio=''
    mult=3 #secs per beep
    msg=" "+msg+" "
    for char in msg.lower():
        morse=lookup[char]
        print char, morse
        audio+=makeTone(base,mult)
        for step in lookup[char]:
            if step[0]==".":
                audio+=makeTone(base+sep,int(step[1])*mult)
            if step[0]=="-":
                audio+=makeTone(base+sep*2,int(step[1])*mult)
            if step[0]=="|":
                audio+=makeTone(base,3*mult)
    return audio

msg="aj4vd"
file=wave.open('test.wav', 'wb')
file.setparams((1, 2, 5000, 5000*4, 'NONE', 'noncompressed'))
file.writeframes(text2tone(msg))
file.close()

print 'file written'

And the other file needed…

### morse.py ###
# library for converting between text and Morse code
raw_lookup="""
a.- b-... c-.-. d-.. e. f..-. g--. h.... i.. j.--- k-- l.-.. m--
n-. o--- p.--. q--.- r.-. s... t- u.- v...- w.-- x-..- y-.-- z--..
0----- 1.---- 2..--- 3...-- 4....- 5..... 6-.... 7--... 8---.. 9----.
..-.-.- =-...- :---... ,--..-- /-..-. --....-
""".replace("\n","").split(" ")

lookup={}
lookup[" "]=["|1"]
for char in raw_lookup:
    """This is a silly way to do it, but it works."""
    char,code=char[0],char[1:]
    code=code.replace("-----","x15 ")
    code=code.replace("----","x14 ")
    code=code.replace("---","x13 ")
    code=code.replace("--","x12 ")
    code=code.replace("-","x11 ")
    code=code.replace(".....","x05 ")
    code=code.replace("....","x04 ")
    code=code.replace("...","x03 ")
    code=code.replace("..","x02 ")
    code=code.replace(".","x01 ")
    code=code.replace("x0",'.')
    code=code.replace("x1",'-')
    code=code.split(" ")[:-1]
    #print char,code
    lookup[char]=code

Automated decoding is trivial. The image above was analyzed, turned into the image below, and the string (AJ4VD) was extracted:
produced

The code to do this:

### decode.py ###
# given an image, it finds peaks and pulls data out
from PIL import Image
from PIL import ImageDraw
import pylab
import numpy

pixelSeek=10
pixelShift=15

def findPeak(data):
	maxVal=0
	maxX=0
	for x in range(len(data)):
		if data[x]>maxVal:
			maxVal,maxX=data[x],x
	return maxX

def peaks2morse(peaks):
	baseFreq=peaks[0]
	lastSignal=peaks[0]
	lastChange=0
	directions=[]
	for i in range(len(peaks)):
		if abs(peaks[i]-baseFreq)<pixelSeek:
			baseFreq=peaks[i]
		if abs(peaks[i]-lastSignal)<pixelSeek and i<len(peaks)-1:
			lastChange+=1
		else:
			if abs(baseFreq-lastSignal)<pixelSeek:c=" "
			if abs(baseFreq-lastSignal)<pixelSeek:c=" "
			if abs(baseFreq-lastSignal)<pixelSeek:c=" "
			directions.append([lastSignal,lastChange,baseFreq,baseFreq-lastSignal])
			lastChange=0
		lastSignal=peaks[i]
	return directions

def morse2image(directions):
	im=Image.new("L",(300,100),0)
	draw = ImageDraw.Draw(im)
	lastx=0
	for d in directions:
		print d
		draw.line((lastx,d[0],lastx+d[1],d[0]), width=5,fill=255)
		lastx=lastx+d[1]
	im.show()

im=Image.open('raw.png')
pix=im.load()
data=numpy.zeros(im.size)
for x in range(im.size[0]):
	for y in range(im.size[1]):
		data[x][y]=pix[x,y]

peaks=[]
for i in range(im.size[0]):
	peaks.append(findPeak(data[i]))

morse=peaks2morse(peaks)
morse2image(morse)
print morse

» 2 Comments!



High Altitude Balloon Transmitter
Posted by
Scott on July 14th, 2010 8:05:46 AM | 2,558 words | 10 Comments
Filed under: C/C++, Circuitry, General, Microcontrollers, Radio | Scott was 24.80 years old when he wrote this

SUMMARY: A small group of high school students taking an AP class for college credit launched a high-altitude weather balloon with a small payload. In addition to a video transmitter and GPS transmitter, they decided to include a simple transmitter built from scratch. This is the story of the project, with emphasis on the simple transmitter’s design, construction, implementation, and reception (which surprised me, being detected ~200 miles away and lasting the entire duration of the flight!) [sample.ogg]

6/16/2010 – TRACKING

I’m completely amazed at how well the transmitter/receiver worked! For only a few milliwatts, I was able to track that thing all the way from takeoff to landing in Gainesville, FL a few hundred miles away. Here is the data assembled in a special, annotated way!

CLICK HERE to view the signal tracked from Gainesville, FL
balloon_track

ANALYSIS: the text on the image describes most if it, but one of the most interesting features is the “multipathing” during the final moments of the descent, where the single carrier signal splits into two. I believe this is due to two Doppler shifts: (1) as the distance between the falling transmitter and the receiver is decreasing, producing a slight in increase in frequency, and (2) a signal reflected off of a layer of the atmosphere above the craft (the ionosphere?) before it gets to the receiver, the distance of which is increasing as the craft falls, producing a decrease in frequency. I’ll bet I can mathematically work backwards and determine how high the craft was, how fast it was falling, and/or how high the layer of the reflecting material is – but that’s more work than this dental student is prepared to do before his morning coffee!

HERE IS SOME AUDIO of some of the strongest signals I received. Pretty good for a few milliwatts a hundred miles away! [beeps.ogg]

6/16/2010 – THE FLIGHT

The launch:

This is the design team:
DSC_7127

Walking the balloon to its launch destination at NASA with an awesome rocket (Saturn 1B – identified by Lee, KU4OS) in the background.
DSC_7210

The team again, getting ready for launch. I’ve been informed that the reason their hands are up is to prevent the balloon from tilting over too much. I’d imagine that a brush with a grass blade could be bad news for the project!
DSC_7232

Last minute checks – you can see the transmitter and battery holders for it taped to the Styrofoam.
DSC_7248

The transmitter in its final position. Note the coil of yellow wire. That serves as a rudimentary “ground” for the antenna’s signal to push off of. I wasn’t very clear on my instructions on how to make it. I meant that it should be a huge coil wrapped around the entire payload (as large as it can be), which would have probably produced a better signal, but since I was able to capture the signal during the whole flight it turned out to be a non-issue.
DSC_7250

The antenna can be seen dropping down as a yellow wire beneath the payload. (arrow)
DSC_7253

Awesome photo.
DSC_7279

Launch! Look how fast that balloon is rising!
DSC_7294

It’s out of our hands now. When I got the text message that it launched, I held my breath. I was skeptical that the transmitter would even work!
DSC_7297

One of the students listening to my transmitter with QRSS VD software (score!)
DSC_7365

Video capture from an on-board camera was also attempted (900MHz), but from what I hear it didn’t function well for very long.
DSC_7334

6/15/2010 – IMPROVED BUILD

Here you can see me (center arrow) showing the students how to receive the Morse code signal sent from the small transmitter (left arrow) using a laptop running QRSS VD (my software) analyzing audio from and an Icom706 mkII radio receiver attached to a dipole (right arrow).DSC_7082

I amped-up the output of the oscillator using an octal buffer chip (74HC240) with some decent results. I’m pleased! It’s not perfect (it’s noisy as heck) but it should be functional for a 2 hour flight.
72hc240_qrp_amplifier

Closeup of the transmitter showing the oscillator at 29.4912 MHz, the Atmel ATTiny44a AVR microcontroller (left chip), octal buffer 74HC240 (right chip), and some status lights which blink as the code is executed.01_closeup

This is my desk where I work from home. Note the styrofoam box in the background – that’s where my low-power transmitter lives (the one that’s spotted around the world). All I needed to build this device was a soldering iron. 02_workstation

Although I had a radio, it is not capable of receiving 29MHz so I was unable to test the transmitter from home. I had to take it to the university to assess its transmitting capabilities.03_room

At UF I used an oscilloscope to measure the waveform of the transmitter. 04_measure

I connected the leads to the output of the transmitter, shorted by a 39ohm resistor. By measuring the peak-to-peak voltage of the signal going into a resistor, we can measure its power.04_measure2

Here’s the test setup. The transmitter is on the blue pad on the right, and the waveform can be seen on the oscilloscope on the upper left.05_lab

Here’s a closer view.
06_scope

With the amplifier off, the output power is just that of the oscillator. Although the wave should look like a sine wave, it’s noisy, and simply does not. While this is unacceptable if our goal is a clean radio signal with maximum efficiency, this is good enough to be heard at our target frequency. The PPV (peak-to-peak voltage) as seen on the screen is about 100mV. Since I’m using a x10 probe, this value should be multiplied by 10 = 1V. 1V PPV into 39 ohms is about 3 milliwatts! ((1/(2*2^.5))^2/39*1000=3.2). For the math, see this post07_no_amp

With the amplifier, the output is much more powerful. At 600mV peak-to-peak with a 10x probe (actually 6V peak-to-peak, expected because that’s the voltage of the 4xAAA battery supply we’re using) into 39 ohms we get 115 millivolts! (6/(2*2^.5))^2/39*1000=115.38. 08_amp

Notes about power: First of all, the actual power output isn’t 115mW. The reason is that the math equations I used work only for pure sine waves. Since our transmitter has multiple waves in it, less than that power is going to produce our primary signal. It’s possible that only 50mW are going to our 29MHz signal, so the power output assessment is somewhat qualitative. Something significant however is the difference between the measured power with and without the amplifier. The 6x increase in peak-to-peak voltage results in a 36x (6^2) increase in power, which is very beneficial. I’m glad I added this amplifier! A 36 times increase in power will certainly help.

The final schematic is here:
balloon_transmitter_final

6/14/2010 – THE BUILD

Last week I spoke with a student in the UF aerospace engineering department who told me he was working with a group of high school students to add a payload to a high-altitude balloon being launched at (and tracked by) NASA. We tossed around a few ideas about what to put on it, and we decided it was worth a try to add a transmitter. I’ll slowly add to this post as the project unfolds, but with only 2 days to prepare (wow!) I picked a simplistic design which should be extremely easy to understand by everyone. Here’s the schematic:

balloon_transmitter

The code is as simple as it gets. It sends some Morse code (”go gators”), then a long tone (about 15 seconds) which I hope can be measured QRSS style. I commented virtually every line so it should be easy to understand how the program works.

#include <avr /io.h>
#include <util /delay.h>

char call[]={2,2,1,0,2,2,2,0,0,2,2,1,0,1,2,0,2,0,2,2,2,0,1,2,1,0,1,1,1,0,0};
// 0 for space, 1 for dit, 2 for dah

void sleep(){
  _delay_ms(100); // sleep for a while
  PORTA^=(1<<PA1); // "flip" the state of the TICK light
}

void ON(){
 PORTB=255; // turn on transmitter
 PORTA|=(1<<PA3); // turn on the ON light
 PORTA&=~(1<<PA2); // turn off the ON light
}

void OFF(){
 PORTB=0; // turn off transmitter
 PORTA|=(1<<PA2); // turn on the OFF light
 PORTA&=~(1<<PA3); // turn off the OFF light
}

void ID(){
        for (char i=0;i<sizeof(call);i++){
                if (call[i]==0){OFF();} // space
                if (call[i]==1){ON();} // dot
                if (call[i]==2){ON();sleep();sleep();} // dash
    sleep();OFF();sleep();sleep(); // between letters
        }
}

void tone(){
 ON(); // turn on the transmitter
 for (char i=0;i<200;i++){ // do this a lot of times
  sleep();
 }
 OFF();sleep();sleep();sleep(); // a little pause
}

int main(void) // PROGRAM STARTS HERE
{
    DDRB = 255; // set all of port B to output
 DDRA = 255; // set all of port A to output
 PORTA = 1; // turn on POWER light

 while (1){ // loop forever
  ID(); // send morse code ID
  tone(); // send a long beep
 }
}

I’m now wondering if I should further amplify this signal’s output power. Perhaps a 74HC240 can handle 9V? … or maybe it would be better to use 4 AAA batteries in series to give me about 6V. [ponders] this is the schematic I’m thinking of building.

UPDATE

This story was featured on Hack-A-Day! Way to go everyone!
hackaday_swharden


» 10 Comments!



Coder’s Block
Posted by
Scott on July 12th, 2010 10:36:03 PM | 240 words | No Comments (yet)
Filed under: General, Radio | Scott was 24.80 years old when he wrote this

It’s inexplicable, yet undeniable. I simply can’t code anything useful right now. I’m currently memorized by the idea of writing a truly powerful set of tools for scientific frequency analysis (more than just turning audio into images), and I keep starting over re-coding things from scratch. I develop too much, too quickly, and half way in I get overwhelmed and mentally blocked. I do it to myself. I’ve taken about a week off and will continue to take a few more days off to reset my mind. I’m trying to improve my coding by reading books (e-books) about advanced Python programming. Perhaps when it’s time to return, I’ll write gorgeous and functional code. I always seem to have one or the other, but never both [sigh]qrss_aj4vd_belgium

The photo above is the signal of my (AJ4VD) little homemade transmitter in Gainesville, Florida, USA (using a 20-ft piece of wire inside my apartment as an antenna) detected by ON5EX in Belgium. It makes me happy. It reminds me that some of the projects I work on succeed, which gives me motivation to continue pursuing the ones which currently challenge me.


» Be the first to comment!



Fighting to Make GTK+, Glade, and Python Play in Windows
Posted by
Scott on June 28th, 2010 9:46:26 AM | 106 words | No Comments (yet)
Filed under: General | Scott was 24.76 years old when he wrote this

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


» Be the first to comment!



Python Script with GTK+ GUI Compiled with Py2EXE
Posted by
Scott on June 27th, 2010 11:16:05 PM | 159 words | No Comments (yet)
Filed under: General, Python | Scott was 24.76 years old when he wrote this

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

WHAT A NIGHTMARE


» Be the first to comment!



Spectrograph UI Made with Glade
Posted by
Scott on June 26th, 2010 7:03:05 PM | 192 words | No Comments (yet)
Filed under: General, Python | Scott was 24.75 years old when he wrote this

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!


» Be the first to comment!



Fast TK Pixelmap generation from 2D Numpy Arrays in Python
Posted by
Scott on June 24th, 2010 9:29:29 PM | 361 words | No Comments (yet)
Filed under: General, Python | Scott was 24.75 years old when he wrote this

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():
	times=1
	timestart=time.clock()
	data=numpy.array(numpy.random.random((400,500))*100,dtype=int)

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

	def start(self):
		global data
		self.im=Image.fromstring('L', (self.data.shape[1],\
			self.data.shape[0]), self.data.astype('b').tostring())
		self.photo = ImageTk.PhotoImage(image=self.im)
		self.canvas.create_image(0,0,image=self.photo,anchor=Tkinter.NW)
		self.root.update()
		self.times+=1
		if self.times%33==0:
			print "%.02f FPS"%(self.times/(time.clock()-self.timestart))
		self.root.after(10,self.start)
		self.data=numpy.roll(self.data,-1,1)

if __name__ == '__main__':
    x=mainWindow()

» Be the first to comment!



Detrending Data in Python with Numpy
Posted by
Scott on June 24th, 2010 8:38:52 AM | 216 words | No Comments (yet)
Filed under: General, Python, Radio | Scott was 24.84 years old when he wrote this

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):
	detrended=[None]*degree
	for i in range(degree,len(data)-degree):
		chunk=data[i-degree:i+degree]
		chunk=sum(chunk)/len(chunk)
		detrended.append(data[i]-chunk)
	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
ffty=scipy.signal.detrend(ffty)

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


» Be the first to comment!



Insights Into FFTs, Imaginary Numbers, and Accurate Spectrographs
Posted by
Scott on June 23rd, 2010 10:21:00 PM | 877 words | No Comments (yet)
Filed under: General, Python, Radio | Scott was 24.75 years old when he wrote this

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

### RECORD AUDIO FROM MICROPHONE ###
rate=44100
soundcard=1 #CUSTOMIZE THIS!!!
p=pyaudio.PyAudio()
strm=p.open(format=pyaudio.paInt16,channels=1,rate=rate,\
			input_device_index=soundcard,input=True)
strm.read(1024) #prime the sound card this way
pcm=numpy.fromstring(strm.read(1024), dtype=numpy.int16)

### DO THE FFT ANALYSIS ###
fft=numpy.fft.fft(pcm)
fftr=10*numpy.log10(abs(fft.real))[:len(pcm)/2]
ffti=10*numpy.log10(abs(fft.imag))[:len(pcm)/2]
fftb=10*numpy.log10(numpy.sqrt(fft.imag**2+fft.real**2))[:len(pcm)/2]
freq=numpy.fft.fftfreq(numpy.arange(len(pcm)).shape[-1])[:len(pcm)/2]
freq=freq*rate/1000 #make the frequency scale

### GRAPH THIS STUFF ###
pylab.subplot(411)
pylab.title("Original Data")
pylab.grid()
pylab.plot(numpy.arange(len(pcm))/float(rate)*1000,pcm,'r-',alpha=1)
pylab.xlabel("Time (milliseconds)")
pylab.ylabel("Amplitude")
pylab.subplot(412)
pylab.title("Real FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq,fftr,'b-',alpha=1)
pylab.subplot(413)
pylab.title("Imaginary FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq,ffti,'g-',alpha=1)
pylab.subplot(414)
pylab.title("Real+Imaginary FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq,fftb,'k-',alpha=1)
pylab.show()

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):
	triangle=numpy.array(range(len(data)/2)+range(len(data)/2)[::-1])+1
	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!


» Be the first to comment!



Smoothing Window Data Averaging in Python – Moving Triangle Tecnique
Posted by
Scott on June 20th, 2010 10:12:03 PM | 370 words | 1 Comment
Filed under: General, Python | Scott was 24.74 years old when he wrote this

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"""
	triangle=numpy.array(range(degree)+[degree]+range(degree)[::-1])+1
	smoothed=[]
	for i in range(degree,len(data)-degree*2):
		point=data[i:i+len(triangle)]*triangle
		smoothed.append(sum(point)/sum(triangle))
	if dropVals: return smoothed
	smoothed=[smoothed[0]]*(degree+degree/2)+smoothed
	while len(smoothed)<len(data):smoothed.append(smoothed[-1])
	return smoothed

### CREATE SOME DATA ###
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

### GRAPH ORIGINAL/SMOOTHED DATA ###
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")
pylab.grid(alpha=.3)
pylab.axis([20,80,50,300])
pylab.legend()
pylab.show()

» 1 Comment so far...



Simple Python Spectrograph With PyGame
Posted by
Scott on June 19th, 2010 9:53:25 PM | 506 words | No Comments (yet)
Filed under: General, Python | Scott was 24.73 years old when he wrote this

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 scipy.io.wavfile
import wave
rate=12000 #try 5000 for HD data, 48000 for realtime
soundcard=2
windowWidth=500
fftsize=512
currentCol=0
scooter=[]
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%s\tMAX:\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
	scooter.append(ffty)
	if len(scooter)<6:return
	scooter.pop(0)
	ffty=(scooter[0]+scooter[1]*2+scooter[2]*3+scooter[3]*2+scooter[4])/9
	data=numpy.roll(data,-1,0)
	data[-1]=ffty[::-1]
	currentCol+=1
	if currentCol==windowWidth: currentCol=0

def record():
	p = pyaudio.PyAudio()
	inStream = p.open(format=pyaudio.paInt16,channels=1,rate=rate,\
						input_device_index=soundcard,input=True)
	linear=[0]*fftsize
	while True:
		linear=linear[fftsize/overlap:]
		pcm=numpy.fromstring(inStream.read(fftsize/overlap), dtype=numpy.int16)
		linear=numpy.append(linear,pcm)
		graphFFT(linear)

pal = [(max((x-128)*2,0),x,min(x*2,255)) for x in xrange(256)]
print max(pal),min(pal)
data=numpy.array(numpy.zeros((windowWidth,fftsize/2)),dtype=int)
#data=Numeric.array(data) # for older PyGame that requires Numeric
pygame.init() #crank up PyGame
pygame.display.set_caption("Simple Spectrograph")
screen=pygame.display.set_mode((windowWidth,fftsize/2))
world=pygame.Surface((windowWidth,fftsize/2),depth=8) # MAIN SURFACE
world.set_palette(pal)
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
clk=pygame.time.Clock()
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

» Be the first to comment!



I’ve Tried the Windows Thing Again, and I Give Up!
Posted by
Scott on June 15th, 2010 6:47:21 PM | 483 words | 4 Comments
Filed under: General | Scott was 24.72 years old when he wrote this

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


» 4 Comments!



Simplest Case PyGame Example
Posted by
Scott on June 15th, 2010 8:29:03 AM | 216 words | No Comments (yet)
Filed under: General, Python | Scott was 24.84 years old when he wrote this

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.
example_pygame

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 r.top < 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

» Be the first to comment!



Insulated MEPT = Stable Signal
Posted by
Scott on June 12th, 2010 9:18:14 AM | 98 words | No Comments (yet)
Filed under: General, Radio | Scott was 24.72 years old when he wrote this

While it may not be perfect, it’s a whole lot better. Below is a capture from this morning of my signal (the waves near the bottom). Compare that to how it was before and you should notice a dramatic improvement! The MEPT is inside a metal box inside a 1-inch-thick Styrofoam box. Very cool!
stable


» Be the first to comment!



Failing O-Scope! Bah!
Posted by
Scott on June 11th, 2010 5:12:36 PM | 220 words | No Comments (yet)
Filed under: General | Scott was 24.71 years old when he wrote this

As if I didn’t ALREADY have enough against me, my oscilloscope decided to die on my RIGHT as I finally was able to view my 10MHz waveform. (I used a piece of coax with a load at the connector to the o-scope, and ran the coax to my test points.) It was beautiful! … and lasted about 30 seconds. The culprit seems to be a failing “focus” knob. My images had been getting blurrier by the day, and now it’s completely black unless I twist pretty hard on the focus knob. I’d stick a small POT in there, but I have no idea how much voltage/current is being regulated. I’m sure the schematics are posted somewhere, but for now I’m going to try to clean out the potentiometer manually and see if the situation improves. Here are some photos of the circuitry inside this old scope – too bad they don’t make stuff like this anymore!panelfrontron


» Be the first to comment!



QRSS Receiver Works… Barely
Posted by
Scott on June 10th, 2010 11:27:40 PM | 346 words | No Comments (yet)
Filed under: Circuitry, General, Radio | Scott was 24.71 years old when he wrote this

I completed work on my first RF receiver, and for what it is it seems to work decently. It should be self-explanatory from the photos. It’s based around an SA602. As with everything, I don’t plan on posting schematics until the project is complete because I don’t want people re-creating junky circuits! It’s stationed at the University of Florida’s club station W4DFU and its spectrograph can be viewed in real time from the QRSS VD – Web Grabber – W4DFU page. Back to work!IMG_3475IMG_3482IMG_34792dc_qrsscapture

And some music to complete the day!


» Be the first to comment!



Minimalist Radio Receiver
Posted by
Scott on June 9th, 2010 11:42:00 PM | 262 words | No Comments (yet)
Filed under: Circuitry, General, Radio | Scott was 24.71 years old when he wrote this

Now that my minimalist QRSS transmitter is mostly functional, I’m shifting gears toward building a minimalist receiver. These are some early tests, but I’m amazed I managed to hack something together that actually works! Once it’s finished I’ll post schematics. For now, here are some photos. This receiver is based upon an SA602 and although there *IS* an op-amp on the board, I actually bypassed it completely! The SA602 seems to put out enough juice to make my PC microphone jack happy, and those cheap op-amps are noisy anyway, so awesome! Go minimalism!

Here it’s pictured with its power supply:DSCN0833

Here’s a close-up. Remember, the op-amp is there but NOT used!DSCN0832

Here’s the output from 7.040 MHz. Conditions are pretty bad right now, and I’m at my apartment using my crazy indoor antenna [pic1] [pic2]recvbig


» Be the first to comment!



QRSS VD Image Assembler
Posted by
Scott on June 7th, 2010 11:20:18 PM | 356 words | No Comments (yet)
Filed under: General, Python, Radio | Scott was 24.70 years old when he wrote this

This minimal Python script will convert a directory filled with tiny image captures such as this into gorgeous montages as seen below! I whipped-up this script tonight because I wanted to assess the regularity of my transmitter’s embarrassing drift. I hope you find it useful.

full-size output:assembled

10x squished output:assembled-squished

Script to assemble a folder of images into a single, large image:

import os
from PIL import Image

x1,y1,x2,y2=[0,0,800,534] #crop from (x,y) 0,0 to 800x534
squish=10 #how much to squish it horizontally

### LOAD LIST OF FILES ###
workwith=[]
for fname in os.listdir('./'):
    if ".jpg" in fname and not "assembled" in fname:
        workwith.append(fname)
workwith.sort()

### MAKE NEW IMAGE ###
im=Image.new("RGB",(x2*len(workwith),y2))
for i in range(len(workwith)):
    print "Loading",workwith[i]
    im2=Image.open(workwith[i])
    im2=im2.crop((x1,y1,x2,y2))
    im.paste(im2,(i*x2,0))
print "saving BIG image"
im.save("assembled.jpg")
print "saving SQUISHED image"
im=im.resize((im.size[0]/10,im.size[1]),Image.ANTIALIAS)
im.save("assembled-squished.jpg")
print "DONE"

Script to download every image linked to from a webpage:

import urllib2
import os

suckFrom="http://w1bw.org/grabber/archive/2010-06-08/"

f=urllib2.urlopen(suckFrom)
s=f.read().split("'")
f.close()
download=[]

for line in s:
    if ".jpg" in line and not line in download and not "thumb" in line:
        download.append(line)

for url in download:
    fname = url.split("/")[-1].replace(":","-")
    if fname in os.listdir('./'):
        print "I already downloaded",fname
    else:
        print "downloading",fname
        output=open(fname,'wb')
        output.write(urllib2.urlopen(url).read())
        output.close()

» Be the first to comment!



New Transmitter, New Spots!
Posted by
Scott on June 7th, 2010 8:47:04 PM | 435 words | No Comments (yet)
Filed under: General, Radio | Scott was 24.84 years old when he wrote this

These should speak for themselves. Obviously I’m the crazy person who thinks it’s funny to merge molecular biology with amateur radio.

Belgium JO10UX:
belgium JO10UX

England G4CWX:
england G4CWX

France JN39AB:
france JN39AB

Massachusetts W1BW:
mass W1BW_2jpg

Nevada KK7CC:
NE KK7CC

Netherlands JO22DA:
netherlands JO22DA

Alaska KL1X:
alaska

Italia I2NDT:
Italia I2NDT

New Zealand ZL2IK:New Zealand ZL2IK

Germany DL4MGM:
NewZealand


» Be the first to comment!



Continuing Minimalist QRSS
Posted by
Scott on June 6th, 2010 7:15:58 PM | 421 words | 1 Comment
Filed under: General | Scott was 24.70 years old when he wrote this

I’m still working on this project, and although progress is slow I’m learning a lot and the circuit is getting better with time. I’m still not yet ready to post the schematics, but you can get an idea of what’s going on from the picture. It can handle 255 levels of frequency shift and has the ability to turn the tone on and off. 6 capacitors, 3 resistors, 4 transistors, a single inductor, and a micro-controller. Boom!
DSCN0776
OnOffDNA

… and yeah, that’s a double helix

UPDATE I spotted myself on W4BHK’s Grabber about 300 miles away…
dnareport

#include <avr /io.h>
#include <util /delay.h>

char dotlen=5; // ultimately the speeed of transmission
char call[]={0,1,1,1,2,0,2,1,1,0}; // 0 for space, 1 for dit, 2 for dah

void setfor(char freq, char ticks){
	OCR1A=freq;
	while (ticks>0){
		sleep();
		ticks--;
	}
}

void sleep(){
	for  (char i=0;i<dotlen ;i++){
		_delay_loop_2(65000);
	}
}

void slideto(char freq, char ticks){
	freq=freq+30;
	char step=1;
	if (OCR1A>freq){step=-1;}
	while (OCR1A!=freq){
		OCR1A+=step;
		setfor(OCR1A, 1);
	}
	setfor(freq, ticks);
}

void DNA(){
	char a[]={4,5,5,6,6,6,7,7,7,7,8,8,8,8,8,7,7,7,7,6,6,6,5,5,4,3,3,2,2,2,1,1,1,1,0,0,0,0,0,1,1,1,1,2,2,2,3,3};
	char b[]={1,1,1,1,2,2,2,3,3,4,5,5,6,6,6,7,7,7,7,8,8,8,8,8,7,7,7,7,6,6,6,5,5,4,3,3,2,2,2,1,1,1,1,0,0,0,0,0};
	for (char i=0;i<sizeof (a);i++){
		//slideto(a[i]*4,2);
		//slideto(b[i]*4,2);
		setfor(a[i]*2+5, 10);
		setfor(b[i]*2+5, 10);
	}
}

void ID(){
	for (char i=0;i<sizeof(call);i++){
		setfor(10,50);
		if (call[i]==0){setfor(10,100);}
		if (call[i]==1){setfor(15,100);}
		if (call[i]==2){setfor(15,250);}
		setfor(10,50);
	}
}

void ID2(){
	for (char i=0;i<sizeof(call);i++){
		if (call[i]==0){ampOFF();setfor(10,50);}
		if (call[i]==1){ampON();setfor(10,100);}
		if (call[i]==2){ampON();setfor(13,100);}
		ampOFF();setfor(OCR1A,30);
	}
	ampON();
}

void ampON(){PORTA|=(1<<PA7);PORTA|=(1<<PA0);PORTA&=~(1<<PA1);_delay_loop_2(10000);}
void ampOFF(){PORTA&=~(1<<PA7);PORTA|=(1<<PA1);PORTA&=~(1<<PA0);_delay_loop_2(10000);}

int main(void)
{
    DDRA = 255;
	OCR1A = 75;TCCR1A = 0x81;TCCR1B = 1;
	while (1){
		ID2();
		ID();
		for (char i=0;i<3;i++){
			DNA();
		}
	}

}

» 1 Comment so far...



« Earlier Posts |
copyright © 2006 swharden@gmail.com