# Python- FM Modulation

I am trying to Frequency modulate a sine signal using Python. I have written the following code that should do it:

def generateSignalFM(time_vector,data):
TWO_PI = 2 * np.pi
fc = 100
b = 15
data = np.sin(TWO_PI * 1 * time_vector)
fm = np.sin(TWO_PI * (fc + b * data) * time_vector)

plot_graph2d(time_vector,data,time_vector,fm)

def plot_graph2d(t1,data1,t2,data2):

plt.plot(t2,data2)
plt.xlabel("Time(s)")
plt.ylabel("Amplitude")
plt.plot(t1,data1)
plt.legend(['FM Signal', 'Original Signal'])
plt.show()


However this is the result I'm getting back:

As you can see it does work, but it is not synchronized, the lowest frequency of the sine wave does not appear below the lowest frequency of the FM signal.

Can anyone explain why this is happening?

Tying to FM an audio file:

1. Input to sine should be phase, not frequency
2. The * t is only to apply to fc per 1

Corrected:

### Code

import numpy as np
import matplotlib.pyplot as plt

#%% Generate #################################################################
t = np.linspace(0, 1, 2048, 0)
fc = 200
b = 15
data = -np.cos(2*np.pi * 1 * t)
phi = fc*t + b * data
fm = np.sin(2*np.pi * phi)

#%% Plot #####################################################################
plt.plot(t, fm)
plt.xlabel("Time(s)")
plt.ylabel("Amplitude")
plt.plot(t, data)
intended_freq_approx = np.hstack([0, *np.diff([data])])
intended_freq_approx *= np.abs(data).max() / np.abs(intended_freq_approx).max()
plt.plot(t, intended_freq_approx)
plt.legend(['FM Signal', 'Original Signal', '~Intended Freq'])
plt.show()

#%% Bonus ####################################################################
from ssqueezepy import ssq_stft
from ssqueezepy.visuals import imshow

Tx = ssq_stft(fm)[0][::-1]
imshow(Tx, abs=1, title="abs(SSQ_STFT)", ylabel="frequency", xlabel="time",
ticks=0)

• Thank you, I also tried loading a wav file and FM it, I added the results in the question, do you think it works?, FC=20KHz Jul 21 at 15:19
• @yarinCohen You're using an audio signal as phase, this will not yield meaningful results (imagine time vs frequency looking like the audio signal). How to FM properly is its own question; I can imagine a multi-component approach. Jul 21 at 16:17
• Dang I thought I can use the same formula for the audio signal as well, as it's just bunch of cos and sine waves, is there a special name for FM an audio signal? because I can't find anything on then web. Jul 21 at 17:20
• @yarinCohen "How to frequency modulate an audio signal", is how I'd ask on this network. Can't just feed to sin since it blends multi-component phases and yields nonsense. Jul 21 at 21:35
• Granted, it's not an unambiguous question, would need clarification on the precise goal. Jul 21 at 21:58