Integration of sawtooth, square and triangle wave functions

Context

After a discussion about how to plot the results of a frequency modulation between two signals on Stack Overflow, I understood that I need to find the time-integral of the following wave functions before using them in the general FM formula (as illustrated in the first answer).


Research

Integrating a sine wave function is indeed easy, but things gets a lot complicated when it comes to other waveforms. Here follow the equations I'm using to display the waveforms:

  • Sawtooth wave:

    $ f(x) = \bmod(f_c x, 1.0); $

  • Square wave:

    $ f(x) = \operatorname{sign}(\cos(f_c x)); $

  • Triangle wave:

    $ f(x) = \frac{1}{f_c}|\bmod(x, f_c) - \frac{1}{2}f_c|$

These functions looks right, but as I don't have any particular background in mathematics or calculus I won't be surprised if I made some bad mistakes. Please be patient.


Questions

  1. Is there a better way to describe mathematically the wave functions above?
  2. If these are right, what is the correct time-integral?

Updates

Thanks to the the functions with period $T$ in the form Rahul suggested I get:

$$\begin{align}\operatorname{sawtooth}(x) = \int_0^x \frac{2x}T-1 \ \mathrm dx &= \frac{x(x - T)}T \end{align}$$

$$\begin{align} \operatorname{square}(x) &= \int_0^x \begin{cases}1&\text{if } x<T/2\\-1&\text{if }x\ge T/2\end{cases} \ \mathrm dx &= \begin{cases}x&\text{if } x<T/2\\-x&\text{if }x\ge T/2\end{cases} \end{align}$$

$$\begin{align} \operatorname{triangle}(x) &= \int_0^x \begin{cases}\frac{4x}T-1&\text{if } x<T/2\\3-\frac{4x}T&\text{if }x\ge T/2\end{cases} \ \mathrm dx &= \begin{cases}x(\frac{2x}T-1)&\text{if } x<T/2\\x(3-\frac{2x}T)&\text{if }x\ge T/2\end{cases} \end{align}$$

By using a modulo operator it's easy to make them periodic $f(x) = \operatorname{sawtooth}(x \% T)$ and they all work as expected when placed as modulators in the frequency modulation equation: $$\begin{align} f(x) = \cos\left(2\pi f_c x + 2\pi f_\Delta \int_0^xg(x)\,\mathrm dx\right) \end{align}$$


Solution 1:

Well, in principle there's nothing wrong with the definitions you have. Mathematically, functions are defined extensionally, so two different-looking functions that have the same output on all inputs are actually the same function, just written in different forms.

That said, when dealing with custom user-specified functions that are periodic, I think it's easier to define them on an interval spanning one period, and then extend them to the rest of the real line using periodicity. That is, you define $g\colon [0,T)\to\mathbb R$ whatever way you like, where $T$ is the period of your waveform, and then let $f(x) = g(x\bmod T)$ for any $x\in\mathbb R$. For your examples, I'd define $$\begin{align} g_{\text{sawtooth}}(x) &= \frac{2x}T-1, \\ g_{\text{square}}(x) &= \begin{cases}1&\text{if } x<T/2,\\-1&\text{if }x\ge T/2,\end{cases}\\ g_{\text{triangle}}(x) &= \begin{cases}\frac{4x}T-1&\text{if } x<T/2,\\3-\frac{4x}T&\text{if }x\ge T/2.\end{cases} \end{align}$$ These are slightly different from the ones in your question. They all have amplitude $1$ and mean $0$, and the square and triangle waves behave sort of like $\sin$ and $-\cos$ respectively.

Now you want to integrate these. There's a nice way to integrate a periodic function, by breaking the interval of integration into periods: $$\begin{align} \int_0^xf(t)\,\mathrm dt &= \int_0^Tf(t)\,\mathrm dt+\int_T^{2T}f(t)\,\mathrm dt+\cdots+\int_{(n-1)T}^{nT}f(t)\,\mathrm dt+\int_{nT}^xf(t)\,\mathrm dt \\ &= n\int_0^Tf(t)\,\mathrm dt+\int_0^{x-nT}f(t)\,\mathrm dt. \end{align}$$ If you pick $n=\lfloor x/T\rfloor$, and use the fact that $f(t)=g(t)$ when $t\in[0,T)$, this becomes $$\int_0^xf(t)\,\mathrm dt=nT\bar g+\int_0^{x\bmod T}g(t)\,\mathrm dt,$$ where $\bar g$ is the mean value of $g$ over $[0,T)$. (This is one reason why I made it zero in the above examples, so this term drops out.) So all you really need to find analytically is the indefinite integral of $g$ over $[0,T)$. I imagine you can do that, especially with the simple definitions above.

Solution 2:

I am going to focus on your sawtooth example:

We usually write mod like this

$$ f(x)\equiv f_cx\mod{ 1 } $$

or this

$$ f(x)\equiv f_cx\pmod{ 1 } $$

in mathematics.

Another issue is that normally I would only use the mod operator for integers. I'm not saying what you are doing is strictly wrong, but it strikes me as unnatural mathematically, where the mod operator is used on integers to build arithmetics like rings. See Wikipedia for more on that. You will also run into problems on computers where the mod operator is usually defined for integers only.

I would say your functions don't have a particular "best" description, but some descriptions are better than others. The "best" for implementation on a computer would probably be something with a lot of conditions, like this:

$$ f(x)=\left\{ \begin{matrix} ...\\ c(x+1)&, -1<x\le0\\ cx&, 0<x\le1\\ c(x-1)&, 1<x\le2\\ ... \end{matrix}\right. $$

This representation also shows what you intend, so there is really nothing wrong with it.

Mathematically, other "good" representations would have to do with infinite expansions, either Taylor series or Fourier series. I'm not sure there would be a proper Taylor series expansion of these functions, and since these are waves, the Fourier series is appropriate anyway. It so happens that the Forier series expansion of the sawtooth function is given on Wikipedia:

$$ f(x)=2\sum_{n=1}^\infty {(-1)^{(n+1)} \over n} \sin(nx), x - \pi \notin 2 \pi \mathbb{Z} $$

If you wanted the peaks to apear at multiples of 1 instead of $\pi$, that would just be a matter of:

$$ f(x)=2\sum_{n=1}^\infty {(-1)^{(n+1)} \over n} \sin(n(2 \pi x + 1)), x \notin \mathbb{Z} $$

Now, both of this and the conditional form are pretty easy to integrate.

The square wave is also very easy to integrate: start by thinking of it as a constant function. You should get a triangle wave of the same period. Note that at the end of each "cycle" you should get 0.

The triangle wave is best integrated in sections in the conditional form. You should get segmented parabolas. Again, the end of each cycle should give you 0.