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() |
I'm also under the impression that the output signal is modulated by the slope of the base-band signal, rather than by it's amplitude, and think it should be the other way around 🤔
Anyone looking up the code,
-
Remove the
2.0 * np.pi
parts as it is unnecessary -
change
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))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great for demonstration purposes! One thing I'm curious about; however, is that it appears that the frequency of the carrier is modulated by the slope of the base-band signal, rather than by it's amplitude. The max and min frequencies in the output signal appear to coincide with the PGT and NGT zeroes in the base-band signal. I was always under the impression it should be the opposite.