Skip to content

Instantly share code, notes, and snippets.

@NT7S
Last active December 1, 2019 16:50
Show Gist options
  • Save NT7S/b3d1f99abdd1143e02303710375e7496 to your computer and use it in GitHub Desktop.
Save NT7S/b3d1f99abdd1143e02303710375e7496 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import numpy as np
import simpleaudio as sa
import time
# Set up audio params
sample_rate = 8000
sound_length = 5 #seconds
t = np.linspace(0, sound_length, sound_length * sample_rate, False)
# Define CW signal
wave1_freq = 600
wave1_amplitude = 1
wave1_phi = 0
wave1 = wave1_amplitude * np.sin(wave1_freq * t * 2 * np.pi + wave1_phi)
# Create keying waveform
wpm = 40
tx_intv = 1.2 / wpm # keying interval
keying = np.zeros(wave1.size)
for i in range (1, int((keying.size / sample_rate) / (tx_intv)), 2):
keying[int(sample_rate * (i * tx_intv)):int(sample_rate * (i * tx_intv + tx_intv))] = 1
taps = np.array([
-0.005946901269245122, \
-0.0032211059347523195, \
-0.0038440699902768383, \
-0.004335185893314178, \
-0.004618082667223961, \
-0.004610544806595846, \
-0.004229573289430787, \
-0.0033917453968697483, \
-0.002028051041620645, \
-0.0000779737869300087, \
0.0024926916286312248, \
0.00569982765877049, \
0.009534812572960348, \
0.013961833701309628, \
0.018917781140524174, \
0.024309521855601125, \
0.030016610101736976, \
0.03589781173597167, \
0.04180040186066688, \
0.047555344979786536, \
0.05298415762863876, \
0.05792015092713824, \
0.06221396733171683, \
0.06570516590449127, \
0.06829411291109369, \
0.069881349050518, \
0.07041715978547827, \
0.069881349050518, \
0.06829411291109369, \
0.06570516590449127, \
0.06221396733171683, \
0.05792015092713824, \
0.05298415762863876, \
0.047555344979786536, \
0.04180040186066688, \
0.03589781173597167, \
0.030016610101736976, \
0.024309521855601125, \
0.018917781140524174, \
0.013961833701309628, \
0.009534812572960348, \
0.00569982765877049, \
0.0024926916286312248, \
-0.0000779737869300087, \
-0.002028051041620645, \
-0.0033917453968697483, \
-0.004229573289430787, \
-0.004610544806595846, \
-0.004618082667223961, \
-0.004335185893314178, \
-0.0038440699902768383, \
-0.0032211059347523195, \
-0.005946901269245122])
# Execute the filter
filtered_keying = np.zeros(keying.size - taps.size)
for i in range(filtered_keying.size):
filtered_keying[i] = np.inner(taps, keying[taps.size + i:i:-1])
signal = wave1 * keying
output = wave1[taps.size:] * filtered_keying
# Normalize audio level to 16 bit int for simpleaudio
signal *= 32767 / np.max(np.abs(signal))
signal = signal.astype(np.int16)
# Start playback then wait for finish
play_obj = sa.play_buffer(signal, 1, 2, sample_rate)
play_obj.wait_done()
# # Now wait one second
time.sleep(1)
# Normalize audio level to 16 bit int for simpleaudio
output *= 32767 / np.max(np.abs(output))
output = output.astype(np.int16)
# Start playback then wait for finish
play_obj = sa.play_buffer(output, 1, 2, sample_rate)
play_obj.wait_done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment