As promised in the first part of the Fourier series we will now demonstrate a simple example of constructing a periodic signal using the, none other then, Fourier series.If you had to remember two formulas from the last post let them be these two:The first one is the exponential form of the Fourier series and the second one is used to compute its coefficients. Our job is to first obtain the expression for the coefficients which we will later plug into the series formula. We hope to get a nice looking expression for the function as a series since our intention is to plot a few graphs with MATLAB.The function, or signal if you must, we will use for this example will be a classical square wave function shown in the figure below (click to enlarge).The function is periodic with, which is perfect because we don’t have to scale time and we can use the two formulas given above as they are. Coefficients are computed as followsOk, so now we have an expression for the Fourier coefficients of the square wave functionNotice that if we plug we get a form, but have no fear! If we go back to our definition of and plug directly into the exponential, under the integral sign, we get.
This results makes sense and it agrees with the statement from Part 1 that says that the zeroth coefficient corresponds to the average value of the function over one period. Again, we could have deduced that directly from the graph of the waveform.Now, plugging the expression for into the general formula for the Fourier series we arrive at the followingand that is our Fourier series representation of the square wave function. Notice that the above expression contains complex numbers but our square wave signal is real. We either made a mistake in our calculation (we haven’t) or the imaginary parts of individual terms will somehow “magically” disappear. Let’s just try and use this series in MATLAB and plot the result.
May 03, 2015 Fourier Series plotting in matlab. Learn more about plotting, matlab function. Toggle Main Navigation. How do you plot the first ten harmonics of a fourier series that has a dc a0 component of 1 and a bn component of (4/npi)sin(npi/2t) and no an component. This code is giving me the value of the first ten harmonics but I don't know how to. GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together. Sign up Matlab codes for Fourier series, harmonic series, complex Fourier series. These code contains graphs.
Fourier Series of a Square Wave with N=1000As we can see, the more harmonics (sinusoids whose frequency is an integer multiple of the fundamental frequency) we take into account the better the approximation of the square wave function is by the Fourier series. We can also notice from these figures that the “spikes” near the discontinuities don’t reduce in height, well maybe a little at first but it soon converges as increases. This phenomena is called the and it occurs whenever you try to reconstruct a function with jump discontinuities using the Fourier series.So, how come we get a real function out of the complex series? As mentioned before there must be some cancellation and simplification involved in the series for the function to turn out real and upon a closer look at the series expression one can see that the term is equal to zero for every even.
![Series Series](/uploads/1/2/3/9/123919531/172005522.png)
This means that the amplitude of every even harmonic is equal to zero. This fact can be verified using the code given above. Just try and plot the series for and, you will notice that they are the same. The harmonic didn’t contribute to the sum since its amplitude is zero. For any odd the term simplifies to which further simplifies the calculation. Now we are left with the following seriesWe can combine the positive and negative terms of the series in hopes of even more simplification and we getusing Euler’s formula and substituting we get the final, commonly used, form of the serieswhere is not the number of harmonics, but the number of non zero terms of the series ( goes from to which makes a total of terms).
It is clear now why we get a real function out of the series, because the last expression is real!Here’s the MATLAB code for computing the series using the “new” series:t = linspace(-2,2,10000);% timef = 0.t;% creates a zero valued functionM=5;% number of non-zero termsfor k=0:1:M-1fk = (4/pi).(1/(2.k+1)).sin(2.pi.(2.k+1).t);% computes the k-th term of the seriesf = f + fk;% adds the k-th term to fendplot(t, f, 'red');grid on;xlabel('t');ylabel('f(t)');title(strcat('Fourier synthesis of the square wave function with M=', int2str(M), ' non-zero terms.' ));Let’s also plot the amplitudes of the individual coefficients. First we will plot them for the complex Fourier series. We need to be careful here, we are plotting complex numbers so we will actually plot the magnitude of the complex coefficients. Here is the plot for harmonics (click to enlarge)You can clearly see in this plot that all even numbered coefficients (harmonics) have a magnitude equal to zero. We could have plotted the same graph with frequency as our variable.
Then we would see directly “how much” of each frequency component we need to have in the sum to get the result as shown in the upper plot. Heck, internet is free, let’s plot it.And there it is. We didn’t change much, just the variable to. This plot is commonly referred to as the Fourier Series magnitude spectrum. Notice that we didn’t have to scale the frequency axis with respect to the axis from above. This is true because we chose such a signal with the fundamental frequency equal to. You can also notice that the magnitude spectrum is symmetric with respect to the vertical axis.
![Plot fourier series in matlab Plot fourier series in matlab](https://gsegon.files.wordpress.com/2013/08/complex_coefficients_f.jpg)
This is true for every real signal, and this fact is a direct consequence of the fact thatwhich we mentioned in the part 1. You can of course plot the same kind of graph for other forms of the Fourier series but we usually plot the spectrum for either the exponential form or the compact trigonometric form.
Plotting the magnitude spectrum for the “usual” trigonometric form would actually involve two graphs, one for coefficients and one for coefficients. In our case with the square wave example we can just plot the coefficients since all coefficients are zero.
We can see that they are zero since, looking at the second series we derived, the series only involves sinusoids. Even so, we will convert the series to compact trigonometric form and then plot the magnitude spectrum. A keen eye can immediately see that we would get the same graph as if we were actually plotting just the coefficients (it follows from the conversion formulas from part 1). Actually let’s just plot both of them for the sake of clarity, the internet is after all free.Here is the first plot:Here is the second one, with coefficients from the compact form:Notice that the magnitude plots are the same, except that the second one has different axis limits. Here’s a another little gem. If you take the magnitude spectrum of the exponential form and “fold and add” it around the vertical axis, by which I mean add the negative components to its corresponding positive components, you will get the magnitude spectrum of the compact form. You can check that this is true using the graphs we plotted above.We didn’t sat anything about phase or phase spectrum.
We will, but not right now, I don’t want this post to get too long.In conclusion: pretty cool, right?Gordan Šegon. For the square wave example you can use the code for the exponential form of the series (1st code in the post).
If you just want to compute the magnitude of a single harmonic you can use this snippet”k=5;Ck = ((1)/(pi.1i.k)).(1-exp(-pi.1i.k));% computes the k-th Fourier coefficient of the exponential form”where k is the harmonic. If you want more then one harmonics then you can loop over k with the for loop and save the results in an array(or vector) in MATLAB.Notice that Ck will now be a complex number with magnitude AND phase, so if you just want the magnitude you can use abs(Ck) to obtain just the magnitude.
I have created this code in order to calculate and sketch the Fourier Coefficients A0,An,and Bn without using Fourier Series command but,unfortunately the plots results did not look like a Fourier Series.