# Square Wave function.

1. Aug 26, 2012

### back2square1

for a square wave function,

f(x)= { -1, -∞ ≤ x ≤ 0; +1, 0 ≤ x ≤ ∞

Expanding it in fourier series gives a function like,

f(x) = (4/π) * Ʃn=0( (sin ((2n+1)x) / (2n+) )

Plotting a graph of the equation gives something like this, http://goo.gl/vFJhL
which obviously doesn't look like a square wave. Can anyone tell me where have I gone wrong? What am I missing?

P.S
Fourier co-efficients
an=0
a0=0

2. Aug 26, 2012

### Mute

The way you have written this, f(x) is not a square wave. It's a signum function.

A fourier series is a series representation of a periodic function. If you take the fourier series of a non-periodic function on a finite interval [a,b], then the fourier series matches your function on that domain, but repeats the function shape with period b-a.

What you have written, however, has an infinite domain, so a fourier series cannot be used - you would have to use a fourier transform instead. However, because you mention a square wave, what you probably want to do is define your function on a finite domain, such as

$$f(x) = \left\{\begin{array}{c}{-1,~-1 \leq x < 0 \\ +1,~0 < x \leq 1}\end{array}\right.$$

Then, calculate the fourier series for that function on the domain [-1,1]. The resulting fourier series should be a square wave. Note also that it is odd about x = 0, so I would expect only sine terms in the series, and in fact you should get the series you quoted.

As for why your plots aren't working, it looks to me like an order of operations issue. When you write the sum you are writing terms like "4sin(5x)/5pi". The plotter is interpreting this as "(4*sin(5x)/5)*pi". Write it like "4sin(5x)/5/pi" or "4sin(5x)/(5pi)" and you will get the result you want.

Last edited: Aug 26, 2012
3. Aug 26, 2012

### back2square1

Thank you very much, Mute. It worked really nice. You're a wonderful guy. As you said, I added some extra parenthesis to my function and I can see a nice square wave, like this: http://goo.gl/9nu8V
Thank you once again. And yeah, I meant to write it like this,
f(x)= { -1, -π ≤ x ≤ 0; +1, 0 ≤ x ≤ π
I wrongly clicked on ∞ instead of π on the side bar.
And hey, please tell me how did you write that function in such a nice layout?

4. Aug 26, 2012

### Bacle2

Use the 'Quote' button on Mute's post, and it will show you the formatting used.