【发布时间】:2017-03-25 05:00:17
【问题描述】:
我一直在尝试创建一个摩尔斯电码音频解码器,这将通过麦克风输入摩尔斯电码音频并使用 python 将其解码为英文。现在我的首要任务是能够识别嘀嘀嘀嘀。我尝试处理这部分解码器的方法如下:
import pyaudio
import struct
import math
from datetime import datetime
thresh = 0.002
sample_period = 0.01
RATE = 44100
sample_cycles = int(RATE*sample_period)
SHORT_NORMALIZE = (1.0/32768.0)
CHANNELS = 2
FORMAT=pyaudio.paInt16
def rms(sample):
count = len(sample)/2
format = "%dh"%(count)
shorts = struct.unpack( format, sample )
sum_squares = 0.0
for i in shorts:
n = i * 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 = sample_cycles)
thresh_final=thresh
list1=""
counter=0
for i in range(1000):
try:
sample=stream.read(sample_cycles)
except IOError:
print("Error Recording")
amp=rms(sample)
if amp>thresh:
list1+="1"
else:
list1+="0"
list1=list1.split("0")
print(list1)
for i in range(len(list1)):
if len(list1[i])==45:
print ("Dah")
elif len(list1[i])==15:
print("Dit")
代码/目标的快速总结:
- 以 0.01 秒的间隔进行音频采样
- 通过 RMS 获取样本的幅度
- 如果 0.01s 样本的幅度大于阈值(设置仅用于识别是否有哔声),则在字符串中添加“1”,否则为“0”
- 然后通过零将字符串拆分为一个列表(因为这些将是哔声之间的间隙)
- 如果子列表的长度 = 15(15*0.01s=0.15s,这是我用于 dahs 的时间)打印 'dit',并且如果子列表的长度 = 45 打印,则遍历列表'dah'
正如您所见,这不是很有效,所以关于我如何处理摩尔斯电码音频解码的任何建议?
受另一个帖子影响的代码:Detect tap with pyaudio from live mic
【问题讨论】:
标签: python audio morse-code