Simulating and Analyzing The Fourier Series and Fourier Transform Using Matlab Objectives
Simulating and Analyzing The Fourier Series and Fourier Transform Using Matlab Objectives
A more compact representation of the Fourier Series uses complex exponentials. In this
case we end up with the following synthesis and analysis equations:
The derivation is similar to that for the Fourier cosine series given above. Note that this
form is quite a bit more compact than that of the trigonometric series; that is one of its
primary appeals. Other advantages include: a single analysis equation (versus three
equations for the trigonometric form), notation is similar to that of the Fourier Transform it
is often easier to mathematically manipulate exponentials rather sines and cosines. A
principle advantage of the trigonometric form is that it is easier to visualize sines and
cosines (in part because the cn are complex number,, and the series can be easily used if the
original xT is either purely even or odd.
Lab task 1
Matalb Code for exponential fourier series
clc
clear all;
close all;
syms t;
x=heaviside(t)-2*heaviside(t-1)
figure
ezplot(x,[0 2])
n=-30:30;
t0=0;
T=2;
w=2*pi/T;
a=(1/T)*int(x*exp(-j*n*w*t),t,t0,t0+T);
xx=sum(a.*exp(j*n*w*t));
figure
ezplot(xx,[0 10])
output
Part 2
TRIGONOMETRIC FOURIER SERIES
A periodic signal(t) with a period T0 can be expressed as a sum of a sinusoid of frequency and its
entire harmonics, as shown in equation below:
In class we study the analytic approach for determining the Fourier transform of a continuous
time signal. In this tutorial numerical methods are used for finding the Fourier transform of
continuous time signals with MATLAB are presented.
t
-2 2
has a Fourier transform sinc function
This can be found using the Table of Fourier Transforms. We can use MATLAB to plot this
transform. MATLAB has a built-in sinc function. However, the definition of the MATLAB sinc
function is slightly different than the one used in class and on the Fourier transform table. In
MATLAB:
Thus, in MATLAB we write the transform, X, using sinc(4f), since the π factor is built in to the
function. The following MATLAB commands will plot this Fourier Transform:
>> f=-5:.01:5;
>> X=4*sinc(4*f);
>> plot(f,X)
In this case, the Fourier transform is a purely real function. Thus, we can plot it as shown above.
In general, Fourier transforms are complex functions and we need to plot the amplitude and
phase spectrum separately. This can be done using the following commands:
>> plot(f,abs(X))
>> plot(f,angle(X))
Note that the angle is either zero or π. This reflects the positive and negative values of the
transform function.
For the pulse presented above, the Fourier transform can be found easily using the table.
However, for some functions, an integration will need to be performed to find the transform
using
This integration can be performed using the trapz command in MATLAB. This command has
the form: trapz(x,y) where the integral of the function y is found with respect to the variable of
integration, x. Or, using MATLAB:
>> clear
>> f=0;
>> t=-2:.01:2;
>> trapz(t,exp(-j*2*pi*f*t)) ans =
4.0000
This is consistent with our earlier results. The value of the transform at f = 0 was found to be 4
using the transform from the table. In order to find the complete transform over a range of
frequencies we can use a for loop as shown below.
>> clear
>> t=-2:.01:2;
>> k=0;
>> for f=-5:.01:5
k=k+1;
X(k)=trapz(t,exp(-j*2*pi*f*t)); end
>> f=-5:.01:5;
>> plot(f,X)
This should match the transform plotted above. This technique can also be used to approximate
the Fourier transform of an infinite duration signal. Say we want to find the amplitude spectrum
of the two-frequency signal:
We begin by creating a vector, x, with sampled values of the continuous time function. If we
want to sample the signal every 0.0002 seconds and create a sequence of length 250, this will
cover a time interval of length 250*0.0002 = 0.05 seconds. A plot of this signal is generated
using the following MATLAB code:
>> clear
>> N=250;
>> ts=.0002;
>> t=[0:N-1]*ts;
>> x=cos(2*pi*100*t)+cos(2*pi*500*t);
>> plot(t,x)
We can find approximate the Fourier transform integral for 0 ≤ f ≤ 800 Hz using:
>> k=0;
>> for f=0:1:800
k=k+1;
X(k)=trapz(t,x.*exp(-j*2*pi*f*t)); end
>> f=0:800;
>> plot(f, abs(X))
As expected the peaks in the spectrum are at 100 and 500 Hz the two frequencies contained in
the signal. Theoretically, we expect to see impulse functions at these two frequencies and zero at
every other frequency. This is not what we observe. This is because the MATLAB code only
approximates the transform. There are three elements that make the results approximate. The
sequence used to compute the transform is a sampled version of a continuous signal. The value
of the sampling interval (Ts) and number of samples taken (N) affect the approximation.
Additionally, the trapz command is using a summation to approximate the integral. We can
observe the effects of the sampling interval and number of samples by changing their values.
For example, if we increase the sampling interval by a factor of 10 (to .002) and decrease the
number of samples to 25 (so that we still cover the same range total time interval), and repeat the
MATLAB code above we get:
In this case the approximation is less accurate. If we return the sampling interval to the original
value of .0002 and increase the number of samples to 500, the approximation to the transform is
found to be:
Note that the transform is more accurate than the original. This is expected because we are
included more cycles of the waveform in the approximation (increasing the limits of integration).
In general, if a continuous time function, x(t), is sampled every T s seconds until N samples are
collected, the DFT/FFT of this sequence of length N is also of length N. The components of the
resulting transform correspond to frequencies spaced every 1/(N*T s) Hz. For example, using the
same two-frequency signal x(t) used above we can produce a sequence of samples of length N =
250 spaced every Ts = .0002 seconds as shown previously.
However, this time we will find the amplitude spectrum of this signal using the fft command.
The resulting transform will contain N = 250 values. Since the frequency components are spaced
every 1/(N*Ts) Hz these correspond to frequency values from 0 to (N-1)/(N*T s) Hz as shown
below.
Lab task 3
Matlab code for fast fourier transform
>> clear
>> N=250;
>> ts=.0002;
>> deltaf=1/(N*ts);
>> t=[0:N-1]*ts;
>> x=cos(2*pi*100*t)+cos(2*pi*500*t);
>> Xf=fft(x);
>> f=[0:N-1]*deltaf;
>> plot(f,abs(Xf))
Note that the spectrum shows four components. Two are at the expected frequencies of 100 and
500 Hz. The other two are at 4500 and 4900 Hz, frequencies that do not appear in the signal.
This is due to the periodic nature of the DFT. Only frequencies up to 0.5/T s correspond to the
actual frequencies in the Fourier transform. We can produce the spectrum plot that only shows
these frequencies and shows the negative frequency components by applying the fftshift function
as shown.
>> Xf_shift=fftshift(Xf);
>> plot([-N/2:N/2-1]*deltaf, abs(Xf_shift))
The values of the sequence length, N, and the time sampling interval, Ts, will have an effect on
the accuracy of the spectrum that is calculated. First, we will increase the spacing between
samples by a factor of two, or Ts = 0.0004. From the sampling theorem we know that the slower
we sample, the lower the frequency that we can accurately represent. Repeating the FFT:
Lab task 4
>> clear
>> N=250;
>> ts=.0004;
>> deltaf=1/(N*ts);
>> t=[0:N-1]*ts;
>> x=cos(2*pi*100*t)+cos(2*pi*500*t);
>> Xf=fft(x);
>> Xf_shift=fftshift(Xf);
>> plot([-N/2:N/2-1]*deltaf, abs(Xf_shift))
We observe that the maximum frequency is now 1250 Hz, instead of 2500 Hz as in the original
computation and the frequency components of 100 and 500 Hz are still represented correctly. In
general, the maximum frequency represented is given by 0.5/T s. Say we increase the sampling
interval to Ts = 0.00125 as shown below:
Lab task 5
>> clear
>> N=250;
>> ts=.00125;
>> deltaf=1/(N*ts);
>> t=[0:N-1]*ts;
>> x=cos(2*pi*100*t)+cos(2*pi*500*t);
>> Xf=fft(x);
>> Xf_shift=fftshift(Xf);
>> plot([-N/2:N/2-1]*deltaf, abs(Xf_shift))
It appears as if the signal, x, has frequency content at 100 Hz (correct) and 300 Hz (incorrect).
This incorrect component is due to the aliasing effect and the fact that the signal has been
sampled at too low of a frequency. The sampling frequency (1/Ts) always needs to be at least two
times the highest frequency component in the signal being transformed, or in our example at
least 2*500 Hz = 1000Hz or Ts < 1/1000 = .001.
Next we will observe the effect of N, the number of samples taken. With T s = .0004 seconds we
repeat the computation of the FFT with N = 100, 200, and 400 samples. The results are shown
below.
Note that the effect of larger N is to increase the resolution of graph. This is due to the fact
that the frequency spacing is given by 1/NTs, or in these three cases 25 Hz, 12.5 Hz, and 6.25
Hz, respectively. For a given sampling interval, T s, as N is increased, the length of time that
the continuous time signal is sampled increased (NT s). Thus, we would expect that the
resulting spectrum would have greater accuracy as shown here.
Q.1 Parseval’s relation states that the energy of a signal can be computed by integrating the
squared magnitude of the signal either over the time domain or frequency domain. If x(t) and
X(f) are a Fourier transform pair, then
Q2.
The trigonometric Fourier series expression for periodic signal x (t) shown above is:
Write code in MATLAB to synthesize the above signal, and run it for different values of n.
Q3
The trigonometric Fourier series expression for periodic signal f(t) shown above is:
Write code in MATLAB to synthesize the above signal, and run it for different values of n.