Fourier Series Expansion using Mathematica

AI Thread Summary
The discussion centers on using Mathematica to animate the Fourier Series representation of a square wave by displaying an increasing number of partial sums over time. The user successfully created a code to calculate Fourier coefficients and plot the square wave alongside its Fourier series approximation. However, they encountered issues when trying to animate the output with a variable number of partial sums, leading to performance problems as Mathematica treated the iterator as a general variable. The solution involved specifying the iterator as an integer, which significantly improved computation speed. The final code allows for smooth animation of the Fourier series as the number of partial sums increases from 1 to 100.
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}]
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top