Fourier Series Expansion using Mathematica

Click For Summary
SUMMARY

The discussion focuses on using Mathematica to compute and animate the Fourier Series expansion of a square wave function. The user successfully implemented the Fourier coefficients calculation using custom functions for sine and cosine basis functions. However, they encountered performance issues when attempting to animate the output with a variable iterator for the number of partial sums. The solution involved specifying the iterator as an integer, which significantly improved computation time.

PREREQUISITES
  • Understanding of Fourier Series and its application in signal processing.
  • Familiarity with Mathematica programming, specifically functions and plotting.
  • Knowledge of sine and cosine basis functions in the context of Fourier analysis.
  • Experience with Mathematica's Animate function and its requirements for iterators.
NEXT STEPS
  • Explore advanced features of Mathematica's Animate function for dynamic visualizations.
  • Learn about optimizing performance in Mathematica, particularly with iterative computations.
  • Investigate the mathematical theory behind Fourier Series and its convergence properties.
  • Study other signal types and their Fourier Series representations, such as triangular and sawtooth waves.
USEFUL FOR

Mathematics students, signal processing engineers, and software developers interested in visualizing Fourier Series expansions using Mathematica.

Lucid Dreamer
Messages
25
Reaction score
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
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}]
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
28
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K