Skip to content

Instantly share code, notes, and snippets.

@fedden
Last active December 1, 2020 10:54
Show Gist options
  • Save fedden/d06cd490fcceab83952619311556044a to your computer and use it in GitHub Desktop.
Save fedden/d06cd490fcceab83952619311556044a to your computer and use it in GitHub Desktop.
Frequency modulation Python matplotlib visualisation
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()
@r41d
Copy link

r41d commented Nov 5, 2020

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 🤔

@piakos314
Copy link

piakos314 commented Dec 1, 2020

Anyone looking up the code,

  1. Remove the 2.0 * np.pi parts as it is unnecessary

  2. 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