Updated pySquelch Code
512 words | Posted on June 18th, 2009
Scott was 23.73 years old when he wrote this!
Filed under: General, Python, Radio
While working to improve the python-based frequency activity logger I wrote a couple of entries back, I greatly increased its accuracy. I won’t go into the details other than saying that it simply polls the sound card a few times a second to listen for when a transmission begins and ends. The new data file format appears like this…
1245316480,5 1245316803,3 1245316839,6 1245316848,11 1245316867,5 1245316875,12 1245316893,13
The major change is that the date and time is recorded as seconds since epoch (Unix time) and the duration of the transmission is given after the comma. This greatly simplifies post-processing. Here is the revised python code to poll the sound card and generate such a log file:
##################################
##### UPDATED CODE IS IN A NEWER ENTRY #####
### CHECK THE PYTHON AND RADIO CATEGORIES ###
##################################
import time, random, alsaaudio, audioop
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
inp.setchannels(2)
inp.setrate(1000)
inp.setformat(alsaaudio.PCM_FORMAT_S8)
inp.setperiodsize(100)
addToLog=""
lastLogTime=0
testLevel=False ### SET THIS TO TRUE TO TEST YOUR SOUNDCARD
def getVolEach():
# this is a quick way to detect activity.
# modify this function to use alternate methods of detection.
while True:
l,data = inp.read() # poll the audio device
if l>0: break
vol = audioop.max(data,1) # get the maximum amplitude
if testLevel: print vol
if vol>10: return True ### SET THIS NUMBER TO SUIT YOUR NEEDS ###
return False
def getVol():
# reliably detect activity by getting 3 consistant readings.
a,b,c=True,False,False
while True:
a=getVolEach()
b=getVolEach()
c=getVolEach()
if a==b==c:
if testLevel: print "RESULT:",a
break
if a==True: time.sleep(1)
return a
def updateLog():
# open the log file, append the new data, and save it again.
global addToLog,lastLogTime
#print "UPDATING LOG"
if len(addToLog)>0:
f=open('log.txt','a')
f.write(addToLog)
f.close()
addToLog=""
lastLogTime=time.mktime(time.localtime())
def findSquelch():
# this will record a single transmission and store its data.
global addToLog
while True: # loop until we hear talking
time.sleep(.5)
if getVol()==True:
start=time.mktime(time.localtime())
print start,
break
while True: # loop until talking stops
time.sleep(.1)
if getVol()==False:
length=time.mktime(time.localtime())-start
print length
break
newLine="%d,%d\n"%(start,length)
addToLog+=newLine
if start-lastLogTime>30: updateLog() # update the log
while True:
findSquelch()
To test the functionality of the code visually, you can use this quick and dirty method of graphing the log files output by this program. Keep in mind THIS IS NOT INTENDED TO BE USED other than to simply test the program.
##################################
##### UPDATED CODE IS IN A NEWER ENTRY #####
### CHECK THE PYTHON AND RADIO CATEGORIES ###
##################################
import pylab
import time, datetime
f=open('log.txt')
raw=f.read()
f.close()
raw=raw.split("\n")
pylab.figure()
for line in raw:
if len(line)<5:continue
line=line.split(",")
sec=datetime.datetime.fromtimestamp(int(line[0]))
dur=int(line[1])
sec2=datetime.datetime.fromtimestamp(int(line[0])+int(line[1]))
pylab.fill([sec,sec,sec2,sec2],[0,1,1,0],'k',lw=0,alpha=.5)
pylab.show()
This entry was posted on Thursday, June 18th, 2009 at 8:48 amand is filed under General, Python, Radio. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
