I have to make a Python algorithm that calculates a .wav file (with a message in Morse code) into the readable text. I have made a sort of operations to normalize the data from file:
import wave
import numpy as np
import matplotlib.pyplot as plt
import sys
import scipy.signal.signaltools as sigtool
from scipy.signal import find_peaks
def normalization(sig):
env = (sigtool.hilbert(sig))
threshold = 200
square_sig = (env > threshold)
return square_sig
wav = wave.open("Python.wav", 'r')
# I got this file from website:
# https://www.meridianoutpost.com/resources/etools/calculators/calculator-morse-code.php?
# just typed 'python' and generated a .wav file
if wav.getnchannels()==2:
print("Stereo files are not supported!")
sys.exit(0)
# to make it easier I am operating only on monofiles
raw = wav.readframes(-1)
raw = list(np.frombuffer(raw, "Int16"))
# made a list with the values of signal
thresh = normalization(raw)
# thresholded values
binom, _ = find_peaks(thresh, height=0)
# and at the end I have converted these values into set of ones
plt.figure('raw')
plt.plot(raw)
plt.figure('threshold')
plt.plot(treshold)
plt.figure('binom')
plt.plot(binom,thresh[binom],'.')
plt.show()
After that I have to count the length of ones and gaps. From the knowledge of Morse code the sequences will mean:
- long set of ones - Morse dash
- short set of ones - Morse dot
- long gap - space between readable characters
- short gap - space between Morse characters
However I don't know how to count the length of these markers. I tried to do it, by counting 1 next to one other until I find a value that equals to 0, but it didn't work (when I ploted this output I got the function y=x).
May you help me how to figure out this problem?
question from:
https://stackoverflow.com/questions/65876920/how-to-count-morse-dashes-and-gaps-in-wav-file-using-python-3 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…