Pythonで、モールス符号の復号

$ ~/anaconda3/bin/python test4.py ~/40wpm.wav 0 500000

-...-
-. --- .--
....- -----
.-- .--. --
-...-
- . -..- -
.. ...
..-. .-. --- --
.--- .- -. ..- .- .-. -.--

$

import sys
import numpy as np
import matplotlib.pyplot as plt
import soundfile as sf
from scipy.signal import hilbert

filename = sys.argv[1]
nstart   = int(sys.argv[2])
nframes  = int(sys.argv[3])

wav, fs  = sf.read(filename,start=nstart,frames=nframes)
analytic_signal = hilbert(wav)
env = np.abs(analytic_signal)

th = 0.5*max(env)
env[env<th] = 0
env[env!=0] = 1

t_up = 0
t_down = 0
up = []
down = []
string = ''
t_ws = 5000
t_ls = 2500
t_dd = 2500

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_ws:
			print(string)
			string = ''
		elif t_down_duration > t_ls:
			string = string + ' '
	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('')

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.