Fourier Series Expansion using Mathematica

In summary, the programmer attempted to animate a square wave over many periods by using the Fourier Series function, however ran into difficulties due to the Mathematica treating the iterator as a general variable.
  • #1
Lucid Dreamer
25
0
Hello,

I recently learned about the Fourier Series and how it can be used decompose a periodic signal into a sum of sinusoids. I can calculate all the coefficients by hand, but I wanted Mathematica to do that for me. I attempted to write a code, and it does give the desired output.

I wanted to animate the output, in the sense that Mathematica would display more and more corrections over time. So I want to start by displaying 1 partial sum, and over time display many partial sums. I looked into the Animate function to do this, but ran into some problems as it requires an index to iterate over. Naturally, I chose the number of partial sums included as the iterator. So instead of having the number of partial sums to include as an integer, I make it a variable. But doing so leaves Mathematica in some infinite loop. Below is my code. I tried to include as many comments as possible, and would appreciate any help.

Code:
(*Define basis functions of sin and cosine*)
cosBasis[m_, t_, T_] := Cos[2*Pi*m*t/T];
sinBasis[m_, t_, T_] := Sin[2*Pi*m*t/T];

(*Define inner product*)
fourierIP[f_, g_] := Integrate[f*g, {t, -Pi, Pi}]

(*Define function to calculate coeffecients of sin and cosine, 
depends on (f[t],period,m)*)
fourierSinCoeff[func_, T_, m_] := 
  fourierIP[func[t], sinBasis[m, t, T]]/
   fourierIP[sinBasis[m, t, T], sinBasis[m, t, T]];
fourierCosCoeff[func_, T_, m_] := 
  fourierIP[func[t], cosBasis[m, t, T]]/
   fourierIP[cosBasis[m, t, T], cosBasis[m, t, T]];

(*Compute M^th partial sum of Fourier coeffecients*)
fourierSeries[func_, t_, T_, M_] := 
  Sum[fourierCosCoeff[func, T, m]*cosBasis[m, t, T], {m, 0, M}] + 
   Sum[fourierSinCoeff[func, T, m]*sinBasis[m, t, T], {m, 1, M}];

(*SquareWave*)
const = 0.5;(*height of square wave*)
squareWave[t_] := 
  Piecewise[{{const, 0 < t <= Pi}, {0, 
     Pi < t <= 2*Pi}}];(*construct square wave*)
periodicExtension[func_, nPeriods_] := 
 Sum[func[t + 2*Pi*n], {n, -nPeriods, 
   nPeriods}];(*extend square wave over multiple periods**)
plot1 = Plot[periodicExtension[squareWave, 4], {t, -4*Pi, 4*Pi}, 
  PlotRange -> {{-4*Pi, 4*Pi}, {-1, 1}}, 
  ExclusionsStyle -> Dotted](*display square wave*)
fourierCosCoeff[f, 2*Pi, m];
fourierSinCoeff[f, 2*Pi, m];
output = fourierSeries[squareWave, t, 2*Pi, 
  9];(*include 9 partial sums*)
plot2 = Plot[output, {t, -4*Pi, 4*Pi}];
Show[plot1, plot2](*displays plot of square wave and Fourier wave together*)
(*The above code works just fine, the moment I switch the 
number of partial sums to include into a variable, I get problems*)

(*Now to animate over many partial sums*)
output1 = fourierSeries[squareWave, t, 2*Pi, m];
Animate[output1, {m, 1, 3, 1}]
 
Technology news on Phys.org
  • #2
Never mind, I got it.

The problem seems to be that mathematica treats the iterator as a general variable (ie. one that can be numbers,strings,etc.) and so takes what seems like forever to compute the expression for the Fourier series. By specifying the iterator as an integer, the process takes a few seconds.

Code:
Compile[{m, _Integer}, m];
output1 = fourierSeries[squareWave, t, 2*Pi, m];
Animate[Plot[output1 /. m -> k, {t, -4*Pi, 4*Pi}], {k, 1, 100, 1}]
 

Related to Fourier Series Expansion using Mathematica

1. What is Fourier Series Expansion and how is it used?

Fourier Series Expansion is a mathematical technique used to represent a periodic function as a sum of simple sinusoidal functions. It is often used in signal processing, physics, and engineering to analyze and manipulate periodic data.

2. How does Mathematica compute Fourier Series Expansion?

Mathematica uses the Fast Fourier Transform (FFT) algorithm to compute Fourier Series Expansion. This algorithm efficiently calculates the coefficients of the sine and cosine terms in the expansion.

3. Can Mathematica handle non-periodic functions for Fourier Series Expansion?

No, Mathematica can only compute Fourier Series Expansion for periodic functions. If the function is not periodic, it can be approximated by a periodic function to use this technique.

4. What are the benefits of using Mathematica for Fourier Series Expansion?

Mathematica has built-in functions that make it easy to calculate Fourier Series Expansion, and it can handle both symbolic and numerical calculations. It also has visualization tools to help understand the results and can handle complex functions.

5. How can I check the accuracy of the Fourier Series Expansion using Mathematica?

Mathematica allows for the calculation of the error between the original function and its Fourier Series Expansion. This can help in determining the number of terms needed for an accurate representation of the function.

Similar threads

  • Calculus and Beyond Homework Help
Replies
1
Views
452
  • Calculus and Beyond Homework Help
Replies
2
Views
545
Replies
4
Views
730
Replies
11
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Replies
28
Views
5K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
Replies
1
Views
1K
Back
Top