# What is the algorithm to do frequency modulation?

I'm familiar with audio synthesis, oscillators etc. I'm now trying to implement my own software algorithm to do FM as described by https://en.wikipedia.org/wiki/Frequency_modulation.

What I have implemented outputs the following (spectrogram produced with sox):

This was produced by my algorithm set with a property of: A 1200hz sine wave modulated by a 12hz sine wave.

However, I don't think this is actually 'correct' in what FM should do. I'm assuming I would see a 'signal' increasing and decreasing in frequency.

Is this correct? Or am I doing something wrong?

The way I am calculating the amplitude for each time point (code is written in Haskell):

fmSine :: Frequency -> Signal' -> Signal'
fmSine (Frequency hz) (Signal sng) = Signal (\t -> sin ( (sng t) + (hz* 2 * pi * t)))


Essentially I'm doing sin ((amplitudeOfModulationFrequency t) + hz * 2pi * t) Where hz is the frequency and amplitudeOfModulationFrequency t is the amplitude at time t of the modulating frequency.

What your plot shows is noise from varying size discontinuities in the waveform.

When you change a modulating frequency, you have to make sure the resulting FM waveform does not have discontinuities. You have to do this by incrementally changing the phase of the argument to the sin() function, not by naively multiplying the modulating frequency by time, which will jump the phase by some (psuedo)random angle when the frequency changes.

• Note for people who may have forgotten 1st year calculus: frequency is proportional to the 1st derivative of phase. Jul 29 '18 at 19:49

It sounds like you are asking how to demodulate FM. This involves the use of a Frequency Discriminator which converts Frequency to Magnitude. A simple discriminator can be done either in the digital or analog form with a delay and multiply as demonstrated in the graphic below. The output is also sensitive to the amplitude of the input, so it is important to first hard limit the signal to remove any incidental AM components. Also a bandpass filter centered on the signal spectral occupancy prior to demonduation in front of this is recommended to limit the sensitivity to your signal of interest. (The bandpass filter would be done prior to hard limiting).

At the output of the delay is a replica of the input signal with a phase shift that is proportional to the frequency of the input. The multiplier combined with a low pass filter is a classical phase detector:

This is shown from the relationship

$cos(\alpha)cos(\beta) = \frac{1}{2}cos(\alpha+\beta) + \frac{1}{2}cos(\alpha-\beta)$

$cos(\omega_1 t + \phi)cos(\omega_1 t) = \frac{1}{2}cos(2\omega_1 t + \phi) + \frac{1}{2}cos(\phi)$

The low pass filter removes the doubled frequency component such that we are left with the term $\frac{1}{2}cos(\phi)$

The sensitivity (slope) of the discriminator is set by the duration of the delay, and the discriminator is centered such that the phase between the two signals when the carrier is unmodulated is 90 degrees (where the slope is most linear and maximum).

There is a closed form expression for the Fourier Transform of frequency modulation by a sine wave

https://physics.stackexchange.com/questions/69468/fourier-series-of-single-tone-modulated-wave

It's the second answer (that only got one vote) with all the Bessel Functions. It checks with the derivation found in

Stark, Henry, and Franz B. Tuteur. Modern electrical communications: theory and systems. Prentice Hall, 1979.

You can also find it https://en.wikipedia.org/wiki/Frequency_modulation_synthesis

What you are asking about is instantaneous frequency which is different than a Fourier transform. You are showing an STFT which seems not really short enough to see the instantaneous frequency or long enough to show the harmonics.