Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

signal processing - How to count Morse dashes and gaps in .wav file using Python 3

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()

Set of matplotlib outputs

After that I have to count the length of ones and gaps. From the knowledge of Morse code the sequences will mean:

  1. long set of ones - Morse dash
  2. short set of ones - Morse dot
  3. long gap - space between readable characters
  4. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...