Class blog 2/13
ECE 3340 - Han Q. Le (r)
1. Make a Fourier-series coefficient function
We did this in class. To avoid error, we USE variable name Λ for period instead of 2. Why? Even an experienced programmer will MAKE mistakes when having to change numerical values from one problem to the next.
In[35]:=
Out[35]=
Substitute
In[36]:=
Substitute again:
In[37]:=
Out[37]=
We still have to solve the problem of m=0:
In[38]:=
Out[38]=
We know that it should be zero as expected for an odd function.
Npw, we define this function in class (but here we include period Λ
In[41]:=
But to get an array we have to do this:
In[42]:=
Out[43]=
What we have
done here is that we allow Λ to be an unknown variable. The
expression:
coefcube[#,Λ]
&
is known as an anonymous function, a most seminal concept in the history of computer programming development. Read about it in wikipedia here. Anonymous function.
If you are not ready to learn it, it is OK for now,
don’t worry. If you alreaydy know about lambda function in Python,
then this is the syntax in Mathematica. For those using MATLAB,
look it up.
You will see that I use anonymous function every in my codes. Example, see the highlight below:
In[1]:=
Programmers can’t live without it.
For now, if you don’t understand, just skip and
don’t worry about it. Just treat the whole expression
(coefcube[#,Λ] &) → a blackbox function”
One student in the class wanted to know how to put a whole array in coefcube function. We can redesign the function:
In[44]:=
It is a more versatile function, because we can put in just a number or a list of number, it will be OK:
Out[46]=
Or:
In[49]:=
Out[50]=
See the above? input an array, you get an array, input a number, you get a number. Kind for kind.
By now, hope you appreciate the capability of Mathematica to handle symbolic manipulation. We don’t have a value for Λ, but it works just fine. We can’t do that in most other languages.
2. Use Mathematica built-in function
In[51]:=
Out[51]=
|
|
The problem? you have to be aware that the period is -π to π. Look it up:
The n
coefficient in the Fourier series expansion of f(t)
is by default given by
.
Too often, people forget to scale their function period to {-π to π}. Just like I forgot to scale our period to 2 in the class. Why, because I had chosen period = 1 (-0.5 to 0.5) for so many examples that when we change to [-1, 1], I forgot to insert the period. This is the reason to always use a variable name, not a numerical value of that variable. I forgot to change from 1 -> 2, it cost some time to find out. If we use Λ from the beginning, the mistake would not have happened.
3. Try your hand at it
Below are the 4 codes of 4 common waveforms, taken from one of my tutorial files in Fourier “Lect_3340_LTI_FFT_utilities.nb”
Take a shot and create your own function, see if
you can modify the code below (just substitute your function
Fourier coefficient in function xFcoeff below). and obtain a demo
that you enjoy.
In section 4, I show some results with half-moon bridge.
5.1.2 For computation - Square wave
Demo only. You SHOULD extract, modify, adapt, do whichever needed for your own work.
Out[2
8]=
5.1.2 For computation - Sawtooth
Demo only. You SHOULD extract, modify, adapt, do whichever needed for your own work.
Code
Out[29]=
5.1.2 For computation - Triangle ramp (symmetric ramp)
Demo only. You SHOULD extract, modify, adapt, do whichever needed for your own work.
Code
Out[71]=
5.1.2 For computation - Exponential
Demo only. You SHOULD extract, modify, adapt, do whichever needed for your own work.
Code
Out[72]=
4. Half-moon bridge
Out[52]=
In[67]:=
In[69]:=
Out[69]=
In[70]:=
Out[70]=
It’s nice to learn about BesselJ function. The two coefficients are:
In[71]:=
Out[71]=
In[80]:=
In[89]:=
Out[96]=
Code
Out[98]=