Last active
December 1, 2020 10:54
-
-
Save fedden/d06cd490fcceab83952619311556044a to your computer and use it in GitHub Desktop.
Frequency modulation Python matplotlib visualisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
modulator_frequency = 4.0 | |
carrier_frequency = 40.0 | |
modulation_index = 1.0 | |
time = np.arange(44100.0) / 44100.0 | |
modulator = np.sin(2.0 * np.pi * modulator_frequency * time) * modulation_index | |
carrier = np.sin(2.0 * np.pi * carrier_frequency * time) | |
product = np.zeros_like(modulator) | |
for i, t in enumerate(time): | |
product[i] = np.sin(2. * np.pi * (carrier_frequency * t + modulator[i])) | |
plt.subplot(3, 1, 1) | |
plt.title('Frequency Modulation') | |
plt.plot(modulator) | |
plt.ylabel('Amplitude') | |
plt.xlabel('Modulator signal') | |
plt.subplot(3, 1, 2) | |
plt.plot(carrier) | |
plt.ylabel('Amplitude') | |
plt.xlabel('Carrier signal') | |
plt.subplot(3, 1, 3) | |
plt.plot(product) | |
plt.ylabel('Amplitude') | |
plt.xlabel('Output signal') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anyone looking up the code,
Remove the
2.0 * np.pi
parts as it is unnecessarychange
product[i] = np.sin(2. * np.pi * (carrier_frequency * t + modulator[i]))
to
product[i] = np.sin(carrier_frequency * time - modulation_index * np.cos(modulator_frequency * time))