Skip to content

Instantly share code, notes, and snippets.

@akey7
Created June 28, 2020 20:26
Show Gist options
  • Save akey7/46f88ce0fffadaf91d0426440a6efb24 to your computer and use it in GitHub Desktop.
Save akey7/46f88ce0fffadaf91d0426440a6efb24 to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.io.wavfile import write
import sounddevice as sd
import time
# Samples per second
sps = 44100
# Duration
duration_s = 5.0
# ac: amplitude of the carrier. Should be kept at 1.0 in this script
# you would modify it if you were micing it with, or modulating other,
# waveforms.
# carrier_hz: Frequency of the carrier
# fm_hz: Frequency of the frequency modulator
# k_p: deviation constant
carrier_amplitude = 1.0
carrier_hz = 440.0
fm_hz = 220.0
k = 25.0
# Our final waveform is going to be calculated as the cosine of carrier and
# frequency modulated terms.
# First, define our range of sample numbers
each_sample_number = np.arange(duration_s * sps)
# Create the term that create the carrier
carrier = 2 * np.pi * each_sample_number * carrier_hz / sps
# Now create the term that is the frequency modulator
modulator = k * np.sin(2 * np.pi * each_sample_number * fm_hz / sps)
# Now create the modulated waveform, and attenuate it
waveform = np.cos(carrier + modulator)
waveform_quiet = waveform * 0.3
# Adjust amplitude of waveform and write the .wav file.
waveform_integers = np.int16(waveform_quiet * 32767)
write('fm-out.wav', sps, waveform_integers)
# Play the waveform out the speakers
sd.play(waveform_quiet, sps)
time.sleep(duration_s)
sd.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment