Laplace transform of a square wave function What is the right way to find the Laplace transform of this function:


The thing I noticed was:
$$f(t)=\text{A}\space\space\space\space\space\space\space\space\space\space 0\le t<\frac{\text{T}}{2}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space f(t)=-\text{A}\space\space\space\space\space\space\space\space\space\space \frac{\text{T}}{2}<t<\text{T}$$
But how do find the complete Laplace transform of this function?

 A: We can write the square wave function as
$$
f(t)=A\sum_{k=0}^{\infty} \left[u\left(t-kT\right)-2u\left(t-\frac{2k+1}{2}T\right)+u\left(t-(k+1)T\right)\right]
$$
where $u(t)$ is the Heaviside's function. So the Laplace transform of $f(t)$ is
\begin{align}
F(s)&=A\sum_{k=0}^{\infty} \frac{1}{s}\left[\mathrm e^{-kTs}-2\mathrm e^{-\frac{2k+1}{2}Ts}+\mathrm e^{-(k+1)Ts}\right]\\
&=A\sum_{k=0}^{\infty} \frac{\mathrm e^{-kTs}}{s}\left[1-\mathrm e^{-\frac{Ts}{2}}\right]^2\\
&=\frac{A}{s}\left(1-\mathrm e^{-\frac{Ts}{2}}\right)^2\sum_{k=0}^{\infty} \mathrm e^{-kTs}\\
&=\frac{A}{s}\left(1-\mathrm e^{-\frac{Ts}{2}}\right)^2\frac{1}{1-\mathrm e^{-sT}}\\
&=\frac{A}{s}\frac{1-\mathrm e^{-\frac{Ts}{2}}}{1+\mathrm e^{-\frac{Ts}{2}}}\\
&=\frac{A}{s}\tanh\left(\frac{sT}{4}\right)
\end{align}
A: $\newcommand{\lap}{\mathscr{L}}\newcommand{\dd}{\mathrm{d}}$
Let us use $A=1$.
Approach #1 (involves integration)
Function $f$ is periodic with period $T$.
In fact, it suffices to defive this function over the interval $[0, T]$
\begin{equation}
  f(t) = \begin{cases}
          1,&\text{ for } 0 \leq t < \tfrac{T}{2}
          \\
          -1, &\text{ for } \tfrac{T}{2} < t \leq T
         \end{cases}.
 \end{equation}
As a result, the Laplace transform of $f$ exists and it is given by
\begin{align*}
  \lap\{f\}(s) {}={}& 
  \frac{1}{1-e^{-Ts}}\int_0^{T}e^{-s\tau}f(\tau)\dd\tau
  \\
  {}={}&
      \frac{1}{1-e^{-2Ts}} \left(\int_0^{\tfrac{T}{2}}e^{-s\tau}\dd\tau - \int_{\tfrac{T}{2}}^{T}e^{-s\tau}\dd\tau \right)
  \\
  {}={}&
      \frac{1}{1-e^{-2Ts}} 
 \left(
   \left.-\tfrac{1}{s}e^{-s\tau}\right|_0^{\tfrac{T}{2}} 
   {}+{} 
   \left.\tfrac{1}{s}e^{-s\tau}\right|_{\tfrac{T}{2}}^{T} 
 \right)
  \\
  {}={}&
      \ldots
  \\    
  {}={}& \frac{1}{s}\frac{e^{\tfrac{Ts}{2}}-1}{e^{\tfrac{Ts}{2}}+1}
  {}={}
      \frac{1}{s}\tanh\left(\tfrac{Ts}{4}\right).
 \end{align*}
I like @alexjo's answer, but we need to use the dominated convergence theorem to guarantee that we can interchange the Laplace transform and the series.
Approach #2 (no integration needed)
We can determine the Laplace transform of a periodic function without the need to compute any integrals. In fact, the Laplace transform of a periodic function boils down to determining the Laplace transform of another function [1, Thm. 4.25].
Firstly, note that we can write $f(t)=1 -2 H_{\frac{T}{2}}(t)$ for $t\in[0, T]$.
We can employ Theorem 4.25 from [1] (I am the author of this book), according to which
$$
(\lap f)(s) = \frac{(\lap f_0)(s)}{1-e^{-sT}},\tag{$*$}
$$
where $f_0(t) = f(t)(1-H_T(s)).$ We have
\begin{aligned}
f_0(t) {}={}& \left(1 -2 H_{\frac{T}{2}}(t)\right)(1-H_T(t)) \\ {}={}&
1-H_T(t)-2 H_{\frac{T}{2}}(t)+2H_{\frac{T}{2}}(t)H_T(t) \\ {}={}&
1-H_T(t)-2 H_{\frac{T}{2}}(t)+2H_T(t)\\ {}={}&
1+H_T(t)-2 H_{\frac{T}{2}}(t).
\end{aligned}
We can easily determine the Laplace transform of $f_0$ and then apply Equation ($*$).
The same trick can be applied to other periodic functions as well. You can have a look at [1, Example 4.13] where we deal with $f(t) = |\sin t|$ or Exercises 4.7 - 4.10.
[1] P. Sopasakis, Control systems: an introduction, Applied Mathematix Press, 2023
