I would like to evaluate some complicated integrals involving the hyperbolic secant, but the extension of the usual contour integration evaluation using the residue theorem isn't clear to me. I've been considering a simple example with a known solution $$\int_{-\infty}^{\infty} \text{Sech}\Big(\frac{\pi s}{2}\Big) \ d s \ = \ 2$$
This question is related as it discusses functions with an infinite number of poles, but there it is assumed that the sum of the residues converges. As is explained in that question, we are only ever actually considering bounded contours and then taking the limit that they grow arbitrarily large but I am not sure how that justifies my approach in this problem.
Suppose I am quite cavalier and I attempt to evaluate this integral by closing the contour along a semi-circle in the upper-half complex plane. Without really checking, I claim that the integral along the circular part of the contour vanishes and thus the integral over the real line is given by $$\sum_{n=0}^{\infty} 2 \pi i \ \text{Res}\Big( \text{Sech}\Big(i\pi\big (n+\frac{1}{2}\big)\Big) = 4-4+4-4 \ . . .$$ which clearly does not converge. However, if we consider the average of the first $m$ partial sums $$\frac{1}{m}(4+0+4+0+ \ . . . ) = \left \{ \begin{array}{lr} 2 & \ m \in \text{evens}\\ 2 + \frac{4}{m}& m \in \text{odds} \end{array} \right.$$ which goes to $2$ as $m \to \infty$. This procedure seems totally ad hoc, even magical, but it predicts the correct answer. The first question is, why does the limit of the average of the partial sums give the correct answer?
Another way of regularizing this problem is to instead compute $$\lim_{\eta \to 0}\int_{-\infty}^{\infty} \text{Sech}\Big(\frac{\pi s}{2}\Big) e^{i \eta \ s} \ d s $$ and then using the same procedure $$\lim_{\eta \to 0}\sum_{n=0}^{\infty} 2 \pi i \ \text{Res}\Big( \text{Sech}\Big(i\pi\big (n+\frac{1}{2}\big) e^{-\eta (2n-1)}\Big) =\lim_{\eta \to 0} \frac{4e^{\eta}}{1+e^{2\eta}} = 2$$ also gives the correct answer. So, there seems to be something to this method.
If I am more careful about the circular piece of the contour, I can show that its contribution is exponentially small almost everywhere. $\text{Sech}(x + iy)$ clearly decreases exponentially when $x$ is large, but what about when $y$ is large and $x$ is small? By rewriting $x + iy = re^{i \theta}$ and taking the absolute value, I can show that when $\theta = \frac{\pi}{2} +\epsilon, \ \left|\epsilon\right|<1$ (ie we are close to the imaginary axis), then $$\left| \text{Sech}(re^{i\theta})\right|\sim e^{-\frac{1}{2}\pi r \left|\epsilon\right| } : \ \ r\to \infty$$ My understanding of the Riemann integral suggests that the single point where $\epsilon = 0$ does not change the value of the integral along the contour.
So, it seems like I do have good reason to argue that the contribution from the circular contour vanishes. Is there a way, then, to justify my regularized sum of the residues as the correct answer in general or is it just an accident for this special case?
Of course we can avoid contours altogether, $$ \begin{align} \int_{-\infty}^\infty\mathrm{sech}(ax)\,\mathrm{d}x &=4\int_0^\infty\frac{e^{-ax}\,\mathrm{d}x}{1+e^{-2ax}}\\ &=4\int_0^\infty\left(e^{-ax}-e^{-3ax}+e^{-5ax}-\dots\right)\,\mathrm{d}x\\ &=\frac4a\left(1-\frac13+\frac15-\dots\right)\\ &=\frac4a\frac\pi4\\ &=\frac\pi{a}\tag{1} \end{align} $$ However, the integral along the contours with large imaginary part cannot be discounted since $$ \cosh(x+iy)=\cosh(x)\cos(y)+i\sinh(x)\sin(y)\tag{2} $$ That is, for any $k\in\mathbb{Z}$, $$ \int_{-\infty}^\infty\mathrm{sech}(ax+ik\pi)\,\mathrm{d}x=(-1)^k\frac\pi{a}\tag{3} $$ The method outlined by Ron Gordon, where we stick close to the real axis, is a good way to approach integrals of this kind.
Why does the average give the correct answer?
Consider the contour $$ \gamma_{k,R}=[-R,R]\color{#A0A0A0}{\cup[R,R+ik\pi]}\cup[R+ik\pi,-R+ik\pi]\color{#A0A0A0}{\cup[-R+ik\pi,-R]}\tag{4} $$ The integrals along the greyed out pieces vanish as $R\to\infty$. Thus, using $(2)$, $$ \begin{align} \lim_{R\to\infty}\int_{\gamma_{k,R}}\mathrm{sech}(x)\,\mathrm{d}x &=\int_{-\infty}^\infty\mathrm{sech}(x)\,\mathrm{d}x -\int_{-\infty+ik\pi}^{\infty+ik\pi}\mathrm{sech}(x)\,\mathrm{d}x\\ &=\int_{-\infty}^\infty\mathrm{sech}(x)\,\mathrm{d}x -(-1)^k\int_{-\infty}^\infty\mathrm{sech}(x)\,\mathrm{d}x\\ &=\left(1-(-1)^k\right)\int_{-\infty}^\infty\mathrm{sech}(x)\,\mathrm{d}x\tag{5} \end{align} $$ Thus, the fact that the average converges to the proper thing is nothing more than the fact that the average of $$ \left(1-(-1)^k\right)\tag{6} $$ tends to $1$.
What I have said above could be made into a valid argument that the average gives the proper value. However, you could just stop at $k=1$ and say that that gives you twice the actual value and divide by $2$. This is just what Ron Gordon does.
Concerning $e^{\large-\frac12\pi r|\epsilon|}$
As noted in $(2)$, $$ \begin{align} |\mathrm{sech}(x+iy)| &=\left|\,\frac1{\cosh(x)\cos(y)+i\sinh(x)\sin(y)}\,\right|\\ &\sim2e^{-|x|}\\ &=2e^{-r\,|\sin(\theta)|}\\ &\sim2e^{-r\,|\theta|}\tag{7} \end{align} $$ where $\theta$ is the angle from the imaginary axis. This estimate is deceptive. Although it is true, inside the strip where $2e^{-r\,|\theta|}\ge1$, the integral along the arc is approximately $$ \int_{-\log(2)}^{\log(2)}\mathrm{sech}(x+iy)\,\mathrm{d}x\tag{8} $$ which can blow up when $y\equiv\frac\pi2\pmod{\pi}$, but whose absolute value is always at least $\frac85\log(2)$ (e.g. when $y\equiv0\pmod{\pi}$).