1

I'm trying to get the fourier series function(s) to work for a square wave with duty cycles other than 50% ie rectangular wave.

squareWave[t_, period_, duty_] := UnitBox[Mod[t/period, 1.]/(2. duty)]    
xx[t_] := squareWave[t, 10, 0.8]    
Plot[xx[t], {t, -10, 10}, Background -> Gray]
curvexx = FourierTrigSeries[xx[t], t, 10];  
Plot[curvexx, {t, -10, 10}, Background -> Gray]

that's my code, set up squarewave, check by plotting (alright so far), take fourier series, check by plotting and it's wrong but why where did I go wrong?

onepound
  • 343
  • 1
  • 9
  • 1
    Your code is incomplete. squareWave is not defined. Please post complete code: it should be possible to just copy and paste to test it. – Szabolcs Jun 16 '14 at 15:26
  • 3
    @Öskå Maybe. But that's not what the OP wrote. Not posting complete code and not posting the same code that produced the error already wastes too much time on this site. – Szabolcs Jun 16 '14 at 15:27
  • forgot to paste squareWave[t_, period_, duty_] := UnitBox[Mod[t/period, 1.]/(2. duty)] but really it aint a crime – onepound Jun 16 '14 at 15:43
  • @user15970 - FourierTrigSeries doesn't accept the FourierParameters-Option. Eliminate that and you get at least a nice image :) – eldo Jun 16 '14 at 15:47
  • @eldo Actually it does. The red colouring is not correct. – Szabolcs Jun 16 '14 at 15:52
  • @user15970 Please explain what result you get, what result you expect, and why you think that the result you get is incorrect. You may be looking for FourierParameters -> {1, Pi/5}. Why did you choose FourierParameters -> {1, π/2}? – Szabolcs Jun 16 '14 at 15:52
  • okay I just got rid of FourierParameters – onepound Jun 16 '14 at 15:53
  • its wrong because the curvexx plot is not the same period as the xx[t] plot – onepound Jun 16 '14 at 15:55
  • @user15970 Please read the documentation of FourierTrigSeries carefully, especially under the Details section where it is precisely defined what this function does, and how FourierParameters affect the result. You'll see that your choice of FourierParameters must match the period. – Szabolcs Jun 16 '14 at 15:57
  • okay with FourierParameters -> {1, Pi/10} its okay ie sorted without as edited above its wrong – onepound Jun 16 '14 at 15:58
  • curvexx = FourierTrigSeries[xx[t], t, 20, FourierParameters -> {1, Pi/10}]; Plot[curvexx, {t, -10, 10}, Background -> Gray] does the trick many thanks to Szabolcs – onepound Jun 16 '14 at 15:59
  • @user15970 FourierTrigSeries essentially truncates the function to the interval $(-\pi, \pi)$. By using FourierParameters -> {1, Pi/5} this is extended to $(-5, 5)$. Just make sure the interval width is equal to the period (or possibly an integer multiple of it). My original suggestion of Pi/10 makes it 2x larger, so Pi/5 is better. – Szabolcs Jun 16 '14 at 16:02

2 Answers2

7

FourierTrigSeries essentially truncates the domain of the function to the interval $(-\pi,\pi)$. In other words, it assumes that the period of the function is $2\pi$. If this is not the case, you need to use FourierParameters to change this interval. FourierParameters -> {1,b} uses $(-\pi/b, \pi/b)$, so for a function of period p you need to use FourierParameters -> {1, 2Pi/p}, i.e. in your case use FourierParameters -> {1,Pi/5}.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
4

I'm not well-versed in Fourier analysis, but your problem seems to stem from the choice of period. FourierTrigSeries appears to be assuming a period of 2 π

squareWave[period_, duty_] := UnitBox[Mod[t/period, 1.]/(2. duty)]
xx[t_] := squareWave[2 Pi, 0.8]
Plot[xx[t], {t, -10, 10}]
curvexx = FourierTrigSeries[xx[t], t, 8];
Plot[curvexx, {t, -10, 10}]

square

approx

m_goldberg
  • 107,779
  • 16
  • 103
  • 257