I have a 4 seconds audio sample of someone saying "hello", I managed to load the wav file and show it in a time-amplitude spectrum, my next step was to calculate a AM (Amplitude Modulation) on this sound, I managed to do it on a sine wave I created but doing it on an actual sound is apparently different.
I am getting unexpected result, I am expecting a cos shape with amplitude changes according to the sound, but I'm getting back roughly the same sound!
Here is my full code:
def generateSignalAM(t,data):
TWO_PI = 2 * np.pi
fc = 100
ac = 0.5
carrier_wave = np.cos(t * fc * TWO_PI)
am = carrier_wave * (1 + data/ac)
plt.plot(t,am)
plt.plot(time,data)
plt.xlabel("Time(s)")
plt.ylabel("Amplitude")
plt.legend(['AM Signal', 'Original Signal'])
plt.show()
return am
samplerate, data = scipy.io.wavfile.read("hello.wav")
duration = len(data)/samplerate
time = np.arange(0,duration,1/samplerate) #time vector
generateSignalAM(time,data)
Here is the output:
Following @The Photon I've changed the code to this:
def generateSignalAM(t,data):
#sample rate is 44100 Hz
TWO_PI = 2 * np.pi
fc = 10000
ac = 0.00005
carrier_wave = np.cos(t * fc * TWO_PI)
am = carrier_wave * (1 + data/ac)
plt.plot(t,am)
#plt.plot(time,data)
plt.xlabel("Time(s)")
plt.ylabel("Amplitude")
#plt.legend(['AM Signal', 'Original Signal'])
plt.show()
return am
And got the following result:


You are severely over-modulating your signal. If you want to see a nice envelope on your AM signal, you need to have a modulation depth less than 1. See this answer on Electrical Engineering Stackexchange for an explanation of over-modulation.
Put simply, if your AM signal is
(1 - m(t)) cos( fc t )wherem(t)is the message signal andcos( fc t )is the carrier, you want the magnitude ofm(t)to be less than one at each point in time.In your example, the magnitude of
m(t)reaches nearly 5000 (The input signal reaches near -10000, and you scale it by 0.5).You can adjust the value of
acin your code to scale the message signal to amplitudes less than 1. (You'll need a value less than 0.0001 or so)As a secondary issue, the frequency range of the human voice is typically in the 10 - 8000 Hz range (with a more limited range needed for understandable speech), and you are modulating this signal onto a 100 Hz carrier. That will result in aliasing as the positive and negative frequency images of your AM signal overlap. Try increasing the carrier frequency to at least 10 kHz.
With a higher carrier frequency you might also have to increase your sample rate to make the plot look presentable. You'll also want to zoom the plot in on a just a few cycles of the message signal to make the AM signal appear as expected.