I've started to learn about FM and I followed this guide to FM a sine wave.
I have managed to do it and here is the result:
And the code:
import numpy as np
import scipy.integrate as integrate
import matplotlib.pyplot as plt
BasebandFrequency = 10e3
CarrierFrequency = 100e3
SamplingFrequency = 1e7
BufferLength = 2000
modulation_index = 4
t = np.arange(0, 2000, 1 / BufferLength)
BasebandSignal = np.sin(2*np.pi*t / (SamplingFrequency/BasebandFrequency))
CarrierSignal = np.sin(2*np.pi*t / (SamplingFrequency/CarrierFrequency))
BasebandSignal_integral = -np.cos(2*np.pi*t / (SamplingFrequency/BasebandFrequency))
ModulatedSignal_FM = np.sin((2*np.pi*t / (SamplingFrequency/CarrierFrequency)) + (modulation_index * BasebandSignal_integral))
plt.plot(t, ModulatedSignal_FM)
plt.plot(t, BasebandSignal)
plt.show()
I tried following his other guide about FM an Audio file but I can't understand how to do it,
here is the audio file I'm using
This is what I came up with so far:
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as integrate
import statistics
def generateSignalFM(time_vector,data):
TWO_PI = 2 * np.pi
fc = 80000
b = 25
fm = np.sin(TWO_PI * (fc + b * data) * time_vector)
return fm
def normalizeAudio(data):
return np.float32(data / max(data))
def averageAudio(data):
return np.float32(data / statistics.mean(data))
def main():
SAMPLE_FOR = 1 # in seconds
time_vector, data, sample_rate = readAudioFile('Recording.wav',SAMPLE_FOR,5)
split_data = data[0:2000]
split_time = time_vector[0:2000]
audio_integrated = []
for i in range(2000):
audio_integrated.append(integrate.trapezoid(split_data[0:i]))
audio_integrated = averageAudio(audio_integrated)
audio_integrated = normalizeAudio(audio_integrated)
fm = generateSignalFM(split_time,audio_integrated)
plot_graph2d(split_time,split_data,split_time,fm)
And the result:
I am honestly lost, I managed to almost fully understand how to FM a sine wave but how do I do it with an Audio Signal?