[Math] Integration of sawtooth, square and triangle wave functions

calculusintegrationsignal processing

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}$$

Best Answer

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.

Related Question