I'm currently working on a radio astronomy project where I need to monitor the amplitude of an audio signal over time.
I've used the simplified Python code suggested by user1405612 here Detect tap with pyaudio from live mic which takes the mic input and works out the RMS amplitude and I've added a part to simply log the value to a CSV file. This is working very well, and thanks must got ouser1405612 for it!
However is there a way I can implement a simple frequency filter to this code. For example I am interested in the RMS amplitude of frequency 19.580khz (in reality I would want to look at the range of say 19.4hkz to 19.6hkz)?
Is there a way to do this with PyAudio using the code in the link above by looking at the raw stream data for example, or any other way? I don't want anything complex like graphs, spectrum analysis etc, just a simple frequency filter. Unfortunately a band pass filter before the mic input is not possible, so it needs to be done on the computer.
Thanks in advance!
Update - 31/12/14 - her is my current code:
# source https://stackoverflow.com/questions/4160175/detect-tap-with-pyaudio-from-live-mic
import pyaudio
import struct
import math
import datetime
FORMAT = pyaudio.paInt16
SHORT_NORMALIZE = (1.0/32768.0)
CHANNELS = 1
#RATE = 44100
RATE = 48000
INPUT_BLOCK_TIME = 1
INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
filename = 'Data.CSV'
def get_rms(block):
count = len(block)/2
format = "%dh"%(count)
shorts = struct.unpack( format, block )
# iterate over the block.
sum_squares = 0.0
for sample in shorts:
# sample is a signed short in +/- 32768.
# normalize it to 1.0
n = sample * SHORT_NORMALIZE
sum_squares += n*n
return math.sqrt( sum_squares / count )
pa = pyaudio.PyAudio()
stream = pa.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = INPUT_FRAMES_PER_BLOCK)
errorcount = 0
for i in range(1000):
try:
block = stream.read(INPUT_FRAMES_PER_BLOCK)
except IOError, e:
errorcount += 1
print( "(%d) Error recording: %s"%(errorcount,e) )
noisycount = 1
amplitude = get_rms(block)
print amplitude
#writeCSV
i = datetime.datetime.now()
f = open(filename,"a")
f.write("{},{}\n".format(i,amplitude))
f.close()