Skip to content

Instantly share code, notes, and snippets.

@akey7
Created June 28, 2020 20:28
Show Gist options
  • Save akey7/728ed195b1991daa19eec53af6155f24 to your computer and use it in GitHub Desktop.
Save akey7/728ed195b1991daa19eec53af6155f24 to your computer and use it in GitHub Desktop.
Writes and amplitude modulated waveform to a .wave file.
# Import libraries
import numpy as np
from scipy.io.wavfile import write
# Properties of the wav
sps = 44100 # DON'T change
carrier_hz = 440.0
modulator_hz = 0.25
ac = 1.0
ka = 0.25
duration_s = 10.0
# Calculate the sine wave
t_samples = np.arange(sps * duration_s)
carrier = np.sin(2 * np.pi * carrier_hz * t_samples / sps)
# Modulate the carrier
modulator = np.sin(2 * np.pi * modulator_hz * t_samples / sps)
envelope = ac * (1.0 + ka * modulator)
modulated = envelope * carrier
# Write the wav file
modulated *= 0.3
modulated_ints = np.int16(modulated * 32767)
write('amplitude-modulated.wav', sps, modulated_ints)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment