# Is this formula for a square wave described anywhere?

This comes with a bit of a backstory. I was exploring in a graphing application and several steps involved were pure intuition, so I hope it's an interesting read. Skip below to the bold header to see the formula:

I was playing around with the equation for the upper-half-circle of radius $a$:

$$\sqrt{a^2-x^2}$$

I wanted to keep its max value at 1, so:

$$\frac{\sqrt{a^2-x^2}}{a}$$

I then wanted to make it more rectangular, so I raised the power of x:

$$\frac{\sqrt{a^2-x^8}}{a}$$

But I noticed this meant the domain was no longer $[-a, a]$

So, I multiplied the $x$ term by $\frac{1}{a^6}$:

$$\frac{\sqrt{a^2-\frac{1}{a^6}x^8}}{a}$$

Which brought the domain back to being $[-a,a]$. The generalized form, allowing me to make it as "sharp-cornered" as possible, was:

$$\frac{\sqrt{a^2-\frac{1}{a^{(2(d-1))}}x^{2d}}}{a}$$

Where $d \in \mathbb{Z}^+$. This made a single rectangle-approximating pulse centered at $0$ when $d$ was sufficiently large.

I then wanted this shape to repeat, so I replaced $x$ with $\sin{(x)}$:

$$\sqrt{1-\frac{1}{a^{\left(d-2\right)}}\sin \left(x\right)^d}$$

This yielded what looked like a square wave with $\approx 100\%$ duty cycle. I thought I could introduce a more substantial "off-time" by adding an offset of the sin term, removing $\frac{1}{a^{\left(d-2\right)}}$ since I no longer care about the "radius" of the "upper half circle":

$$\sqrt{1-\left(\sin \left(x\right)-\sin \left(\left(x-\frac{\pi }{2}\right)\right)\right)^d}$$

The problem was that this was undefined where it should be $= 0$. Looking at the behaviour of the functions $\sqrt{x}$ and $e^{x}$, I realized it would be acceptable to substitute $e^{x}$ for $\sqrt{x}$ since the problem was that $\sqrt{x}$ is undefined when $x < 0$, and even thought the asymptotic behaviour is quite divergent for $x > 1$, this wouldn't matter since the input terms are bounded, being $\sin$ functions.

This yielded the following, which was pretty good:

$$e^{\left(-\left(\left(\sin \left(\frac{\pi }{2}x\right)\right)-\sin \left(\frac{\pi }{2}\left(x-1\right)\right)\right)^d\right)}$$

After quite a bit of playing around, I arrived at the following:

If you skipped the above, this is the real meat of the question:

$c$ controls the wavelength and $p \in [0, 1]$ is the duty cycle:

$$e^{-\left(\left(\cos \left(\frac{\pi }{2c}x\right)\right)-\left(\frac{\sqrt{1-\left(2p-1\right)}}{\sqrt{1+\left(2p-1\right)}}\right)\cos \left(\frac{\pi }{2c}\left(x-c\right)\right)\right)^d}$$

To simplify, with wavelength $1$ and duty cycle $50\%$, we have:

$$e^{-\left(\left(\cos \left(\frac{\pi }{2}x\right)\right)-\cos \left(\frac{\pi }{2}\left(x-1\right)\right)\right)^d}$$

Where $d = 2n$ for some $n \in \mathbb{Z}^+$.

My question:

Is this formula for a square wave written about anywhere, or can it be derived from another form? How does it relate to the infinite series expansion which we see so much in textbooks about fourier series?

Thanks for your patience, and please understand I'm not an expert student of math by any means. I'm not implying I've made some independent discovery, I'm quite ignorant of what might be out there. I'm just wondering if anybody can point out how this monster I've found relates to more familiar territory for a novice student?

• Approximating piecewise differentiable functions by smooth ones is not unheard of... Commented Aug 29, 2016 at 20:03
• @Calculon is there any way that the exponential function could show up as a result of taking the limit of the traditional fourier series? I'm wondering if even the possible solution to my question might be way over my head. I don't see how this function could derive from the more familiar forms... thanks! Commented Aug 29, 2016 at 20:12
• The step function can be approximated by en.wikipedia.org/wiki/Sigmoid_function, which contains the exponential. With the Fourier series, you can use it to approximate a step function, but the approximation will not be uniform: you will get the Gibbs phenomenon. Not sure if I answered the question, but hope so.
– avs
Commented Aug 29, 2016 at 20:21
• I hope I see it correctly but the exponent in your function is just $\sin(\pi x/2 )^d$. If you then consider the Taylor series expansion of your exponential function, you will get something similar to the Fourier series expansion of a square wave. Commented Aug 29, 2016 at 20:21
• @Calculon Thanks for continuing to reply! The function $\sin \left(\frac{\pi x}{2}\right)^{d}$ seems to produce a square wave with duty cycle $100\%$, though. I thought the classic square wave is at $50\%$? Also thanks for the tip on the taylor series expansion, I'll check that out now and see what happens. Commented Aug 29, 2016 at 20:30

So, thanks to the two people who answered, there seems to be some kind of basic understanding of why this works:

1. Sigmoid functions composed with $\sin$

If we take

$$s\left(x\right)=\frac{1}{1+e^{-d\left(\sin \left(\pi x\right)\right)}}$$

, we get a square wave with duty cycle $50\%$. By inverting the polarity of one copy of the wave and summing them (and taking the absolute value after subtracting $1$), the phase shift of the inverted wave becomes the duty cycle parameter:

$$|\left(s\left(x\right)+s\left(-x+2p\right)\right)-1|$$

Similarly, we can use another sigmoid function, $\tanh{(x+1)}$:

$$s\left(x\right)=\tanh \left(\left(\sin \left(\pi x\right)+1\right)^d\right)$$

1. The Taylor Series of the function

There seems to be an intuition that if we find the taylor series of the monster, it should approximate the familiar taylor series for the square wave. This should be easier if we simplify the function by removing $p$, $c$, and letting the frequency be a multiple of $\pi$:

$$e^{-\left(\sin \left(x\right)-\cos \left(x\right)\right)^d}$$

... and yet, it's still too difficult for me, as I've sat here quite a bit attempting to derive the increasingly-complex derivatives, and it seems there's no clear way this will converge to an equivalent series to the traditional square wave series. And yet, it seems logical that of course it should!

Anyways, thanks for the help everyone. It seems realizing that the original function is basically a sigmoid with a periodic input clarifies how we get a square wave!