Jump to content
  • 0

Sample code for FM signal generation


Umar

Question

3 answers to this question

Recommended Posts

  • 0

I'm no expert, but I'll guess at an answer that might offer some insight. 

One thing that might not be obvious at first is that to do true FM, your oscillator will need to have a state variable, such as its instantaneous phase offset.  I say it may not be obvious at first because you might instead try to just calculate the oscillator's output as a function of time, but then you would need to solve for the integral of everything that has led up to that moment in time to know exactly where the phase should be, else if you use a naive calculation then the modulation magnitude would increase with time and you get nonsense out. 

For each "next sample", you advance that phase offset by the appropriate amount of phase advance, which is the computed instantaneous frequency (the carrier frequency plus a small portion of the amplitude of the modulation signal at that same sample, + or -) optionally multiplied by 2π if you're going to use an actual sin() or cos() function (otherwise consider it normalized to the proportion of a full cycle, as when you're going to also multiply it by a table size when doing a table lookup) and divided by the sample rate.  The output is then the sine (or cosine) at that updated phase offset, for which a table lookup is common for efficiency (though there are also fast approximations available).  And you'll want to modulo the full cycle out of the phase offset to avoid loss of resolution when the phase offset gets too big. 

Sometimes it is acceptable and easier to just use phase modulation instead of frequency modulation, since you just add a portion of the modulation signal to the phase offset, along with the carrier frequency's own contribution.  Oh, and you might find the music DSP mailing list archive helpful, such as here:  Phase Modulation vs Frequency Modulation (YMMV).

(if you see some random PI characters below, ignore them -- they keep being added back in each time I edit this to delete them)

 

image.png

image.png

Edited by zimbot
Link to comment
Share on other sites

  • 0

While it doesn't supply source HDL code you might find this project interesting: https://forum.digilentinc.com/topic/20299-fun-with-phasors/

It might have some insight for you. There are a lot of ways to implement complex functionality into logic designs, and the brute force methods are rarely appropriate. Analig Devices has a lot of good basic material for students of DSP concepts and methodologies.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...