VIDEO
$ ~/anaconda3/bin/python test6.py ~/straightKey8kHz.wav 0 -1 77 0.75
RETURNED
FM
P.
C.
BEACH,FL
IN
30?30
FT
BRAVE
M/H
AND
NW
BACK
HOME
BUT
STILL
IN
M/H
HEE
AGE
HR
6
4
UND
BEEN
HAM
SINCE
61
^
?1961<BT>RUNNING
ICOM
735
AND
STRAIGHT
KEY
OLDJ44HEE
SOHWNW
WB0SND/MW5BIB/M
import sys
import numpy as np
import matplotlib.pyplot as plt
import soundfile as sf
from scipy.signal import hilbert
from scipy.signal import medfilt
from scipy import signal
morse ={
"" : "",
"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" : "--..",
"1" : ".----",
"2" : "..---",
"3" : "...--",
"4" : "....-",
"5" : ".....",
"6" : "-....",
"7" : "--...",
"8" : "---..",
"9" : "----.",
"0" : "-----",
"." : ".-.-.-",
"," : "--..--",
":" : "---...",
"?" : "..--..",
"'" : ".----.",
"-" : "-....-",
"/" : "-..-.",
"@" : ".--.-.",
"<SN>" : "...-.",
"<BT>" : "-...-",
"<KN>" : "-.--."
}
morse_inv = dict((v,k) for (k,v) in morse.items())
filename = sys.argv[1]
nstart = int(sys.argv[2])
nframes = int(sys.argv[3])
nmedian = int(sys.argv[4])
th_amp = float(sys.argv[5])
print(filename, nstart, nframes, nmedian, th_amp)
wav, fs = sf.read(filename,start=nstart,frames=nframes)
wav_len = wav.shape[0]
trig_pos = 0
analytic_signal = hilbert(wav)
env1 = np.abs(analytic_signal)
env2 = medfilt(env1, 81)
env_max = max(env2)
th = 0.5*env_max*th_amp
print('env_max =', env_max, 'th_amp =', th_amp, 'th =', th)
env = np.empty_like(env2)
for i in range(len(env-1)):
if env2[i] > th:
env[i] = 1
else:
env[i] = 0
trig = 0
t_up = 0
t_down = 0
up = []
down = []
for t in range(1, len(env-1)):
if env[t-1] == 0 and env[t] == 1:
if trig == 0:
trig = 1
trig_pos = t
t_up = t
t_down_duration = t - t_down
down.append(t_down_duration)
if env[t-1] == 1 and env[t] == 0 and trig == 1:
t_down = t
t_up_duration = t - t_up
up.append(t_up_duration)
print('trig_pos =', trig_pos)
print('down =', down)
print('up =', up)
plt.figure(1, figsize=(18,10))
plt.subplot(611)
trig_pos = 0
disp_pos1 = max([0,trig_pos-int(fs/100)])
disp_pos2 = disp_pos1 + 10*int(fs)
print('disp_pos1 =', disp_pos1, 'dsip_pos2 =', disp_pos2)
plt.plot(wav[disp_pos1:disp_pos2], color='green')
plt.title("input signal")
plt.subplot(612)
plt.plot(env1[disp_pos1:disp_pos2], color='green')
plt.title("input signal")
plt.subplot(613)
plt.plot(env2[disp_pos1:disp_pos2], color='green')
plt.title("input signal")
plt.subplot(614)
plt.plot(env[disp_pos1:disp_pos2], color='green')
plt.title("input signal")
plt.subplot(615)
hist_down, bin_edges_down, patches_down = plt.hist(down, bins=50, color="blue")
dotspace_arg = np.argmax(hist_down)
dotspace_len = bin_edges_down[dotspace_arg]
print('dotspace_len =', dotspace_len)
print('hist_down =', hist_down)
print('bin_edges_down =', bin_edges_down)
plt.title("Spaces Histogram")
plt.grid(True)
plt.subplot(616)
hist_up, bin_edges_up, patches_up = plt.hist(up, bins=50, color="orange")
print('hist_up =', hist_up)
print('bin_edges_up =', bin_edges_up)
plt.title("Dot/Dash Histogram")
plt.grid(True)
t_dd = 1000 # oh no!
t_ls = 1000
t_ws = 3300
string = ''
for t in range(1, len(env-1)):
if env[t-1] == 0 and env[t] == 1:
t_up = t
t_down_duration = t - t_down
down.append(t_down_duration)
if t_down_duration > t_ls:
print(morse_inv.get(string, '^'), end='')
string = ''
if t_down_duration > t_ws:
print(' ')
if env[t-1] == 1 and env[t] == 0:
t_down = t
t_up_duration = t - t_up
up.append(t_up_duration)
if t_up_duration < t_dd:
string = string + '.'
else:
string = string + '-'
print('')
plt.subplots_adjust(hspace=0.5, wspace=0.35)
plt.show()