You are on page 1of 8

FFT,

total energy, and energy spectral density computations in MATLAB


Aaron Scher

Everything presented here is specifically focused on non-periodic signals with finite energy (also
called “energy signals”).


Part 1. THEORY


Instantaneous power of continuous-time signals:
Let 𝑥 𝑡 be a real (i.e. no imaginary part) signal. If 𝑥 𝑡 represents voltage across a 1 W resistor,
then the instantaneous power dissipated by Ohmic losses in the resistor is:

% & '
𝑝 𝑡 = = 𝑥 𝑡 ) . (Instantaneous power in [W]=[J/s]) (1)
(

Total energy of continuous-time signals:
The total energy dissipated across the resistor is the time-integral of the instantaneous power:

. .
𝐸, = /. 𝑝 𝑡 𝑑𝑡 = /. 𝑥 𝑡 ) 𝑑𝑡. (Total signal energy in [J]) (2)

A signal with a finite energy 𝐸, (i.e. 𝐸, < ∞) is called an “energy signal”. We are focusing solely
on energy signals here.

Continuous time Fourier transform (CTFT) pair:
Recall the CTFT:

.
𝑋 𝑓 = /. 𝑥 𝑡 𝑒 /5)67& 𝑑𝑡 (3)

Energy of continuous-time signals computed in frequency domain
It can be shown using Parseval’s theorem that the total energy can also be computed in the
frequency domain:

.
𝐸, = /. 𝑋 𝑓 ) 𝑑𝑓. (Total signal energy in [J] computed in frequency domain) (4)

Compare equation (4) with (2). These are two ways equations to compute the total energy E.

Energy spectral density
Looking at Equation (5), we see that we can define an energy spectral density (ESD) given by:

Ψ, 𝑓 = |𝑋 𝑓 |) . (Energy spectral density in [J/Hz]) (5)

1
Part 2. MATLAB




Our goal in this section is to use MATLAB to plot the amplitude spectrum, energy spectral
density, and numerically estimate the total energy Eg.

First, let’s sample!
How do we sample a signal 𝑥 𝑡 in MATLAB? For ease, let’s work specifically on an example
(you can easily generalize what is presented here to other signals). Suppose our signal is a small
“piece” of sinusoid with frequency 𝑓; described by the function:

0. 𝑡 < −1
𝑥 𝑡 = sin 2𝜋𝑓; 𝑡 , − 1 ≤ 𝑡 ≤ 1 (6)
0, 𝑡 > 1

where the units for time are in seconds. Given 𝑓; we need to choose a sampling frequency 𝑓G
that is sufficiently high (should be higher than Nyquist rate to avoid aliasing ). For this example,
let us choose 𝑓; = 2 𝐻𝑧 and a sampling frequency 𝑓G = 20 𝐻𝑧. The MATLAB code looks like
this:

1 - f0=2; %center frequency [Hz]
2 - fs=20; %sample rate [Hz]
3 - Ts=1/fs; %sample period [s]
4 - Tbegin=-1; %Our signal is nonzero over the time interval [Tbegin Tend].
5 - Tend=1;
6 - t=[Tbegin:Ts:Tend]; %define array with sampling times
7 - x=sin(2*pi*f0*t); %define discrete-time function. This is x[n].

Mathematically, our discrete-time function x[n] (defined in line 7 of code above) is equal to the
continuous-function 𝑥 𝑡 (ignoring quantization error) at discrete points in time. Hence, we
may write:

𝑥[𝑛] = 𝑥 𝑡M (7)

where 𝑡M are the sampling times defined in line 6 of code above:

𝑡M = 𝑇OPQRS + 𝑛 − 1 𝑇G , 𝑛 = 1 … 𝑁; (8)

where 𝑇G = 1 𝑓G is the sampling period, 𝑁; is the final index of x[n], i.e. 𝑁; =length(x), and
𝑇OPQRS is the initial time where the finite-energy signal x(t) is nonzero (e.g. for the signal given by
equation (6), 𝑇OPQRS = −1 seconds). In MATLAB, the indices n always start at 1 and are positive.
This can cause confusion, since in other programming languages indices commonly start at 0.
So, in MATLAB, x[0] would give an error. You must start with an index of 1; x[1] would be fine.

2
If we are interested in evaluating 𝑥 𝑡M at some specific time, say at t = 0, we can figure out the
index n that corresponds to t = 0, and plug it in to our expression for x. However, the easier way
is to simply plot our function x and look at the value of this function at t = 0, as done below.

8 - figure(1)
9 - plot(t,x)
10 - grid on
11 - xlabel('Time (seconds)','fontsize',14)
12 - ylabel('Amplitude','fontsize',14)

1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1


Time (seconds)

Figure 1. Plot of 𝑥 𝑛 = 𝑥 𝑡M . Discrete points are connected by straight lines and the x-axis is
labeled as time in seconds. This gives the illusion that this is a plot of a true continuous time
signal 𝑥 𝑡 . It’s actually a plot of a discrete time signal.

From the plot above, it is clear that the function x(t) = 0 at t = 0. If we weren’t in the mood to
plot, we could also find the index value that corresponds to t = 0, and plug that directly into x to
find the value of x at t = 0, as shown in the code below:

13 - index = find(t==0,1) %find index n corresponding to t=0.This returns 21
14 - x(index) %This returns 0, as expected.


Total energy approximation
We can numerically estimate the total energy by approximating equation (2) with a Riemann
sum:

^
Eg ≈ 𝑇G n=_ 1 𝑥[𝑛] ) (Estimate total signal energy in [J]) (9)





3
MATLAB’s FFT function
Matlab’s fft function is an efficient algorithm for computing the discrete Fourier transform
(DFT) of a function. To find the double-sided spectrum you need to use the fftshift
function.

Equation (3) shows how to manually compute the continuous time Fourier transform (CTFT)
𝑋 𝑓 of a continuous time function 𝑥 𝑡 . Instead of using an integral, we can use MATLAB to
numerically compute the CFT at discrete frequency points 𝑓` as follows:

a
𝑋 𝑓` = fftshift(fft(x,N)) (10)
7b

where N is the number of frequency points in the FFT, and 𝑓` are discrete frequency points:

7 7
𝑓` = − b + 𝑛 − 1 b , 𝑛 = 1 … 𝑁 (11)
) ^/a

Recall that 𝑁; = length(x) is the number of discrete time points of the original signal 𝑥[𝑛].
In general 𝑁; ≠ 𝑁, and you are free to choose N as large as you want (so long as your
computer can handle it). In fact, you will generally set N>>N0. A good value for N is N=216.
The code below demonstrates how to calculate and plot the FFT.

15 - N=2^16; %good general value for FFT (this is the number of discrete
16 - points in the FFT.)
17 - y=fft(x,N); %compute FFT! There is a lot going on "behind the scenes"
18 - with this one line of code.
19 - z=fftshift(y); %Move the zero-frequency component to the center of the
20 - %array. This is used for finding the double-sided spectrum (as opposed
21 - to the single-sided spectrum).
22 - f_vec=[0:1:N-1]*fs/N-fs/2; %Create frequency vector (this is an array
23 - of numbers. Each number corresponds to a discrete point in frequency
24 - in which we shall evaluate and plot the function.)
25 - amplitude_spectrum=abs(z)/fs %Extract the amplitude of the
26 - spectrum. Here we also apply a scaling factor of 1/fs so that
27 - the amplitude of the FFT at a frequency component equals that of the
28 - CFT and to preserve Parseval’s theorem.
29 - figure(2)
30 - plot(f_vec,amplitude_spectrum);
31 - set(gca,'FontSize',18) %set font size of axis tick labels to 18
32 - xlabel('Frequency [Hz]','fontsize',18)
33 - ylabel('Amplitude','fontsize',18)
34 - title('Amplitude spectra','fontsize',18)
35 - grid on
36 - set(gcf,'color','w');%set background color from grey(default) to white
37 - axis tight

4
Amplitude spectra
1

0.8

0.6

Amplitude
0.4

0.2

0
-10 -5 0 5

Frequency [Hz]

Figure 2

Once you have computed 𝑋 𝑓` using equation (10), you can then numerically compute and
plot the energy spectral density (ESD):

Ψ, [𝑓` ]=|𝑋 𝑓` |) . (Energy spectral density in [J/Hz]) (12)

The total energy can then be found by approximating equation (4) with a Riemann sum:

7 ^
Eg ≈ b n=1 𝑋 𝑓` ) . (Total signal energy in [J] computed in frequency domain) (13)
^

The code below demonstrates how to calculate and plot the energy spectral density.

38 - figure(3)
39 - plot(f_vec,abs(amplitude_spectrum).^2);
40 - xlabel('Frequency [Hz]','fontsize',18)
41 - ylabel('Energy/Hz','fontsize',18)
42 - title('Energy spectral density','fontsize',18)
43 - grid on
44 - set(gcf,'color','w'); %set background color to white
45 - axis tight

Energy spectral density
1

0.9

0.8

0.7
Energy/Hz

0.6

0.5

0.4

0.3

0.2

0.1

0
-10 -8 -6 -4 -2 0 2 4 6 8

Frequency [Hz]

Figure 3

5
We can now numerically calculate the total energy using either equation (9) or (13):

%calculate total energy
46 - Eg=sum(x.^2)*Ts %using discrete time function x[n]
47 - Eg=sum(amplitude_spectrum.^2)*fs/N %using ESD

MATLAB returns Eg = 1.000 for both methods. The units of energy are Joules [J].

The full MATLAB code for the above example is pasted below. Simply save this code in an m-
file and run the m-file.

clc;
clear all;
f0=2; %center frequency [Hz]
fs=20; %sample rate [Hz]
Ts=1/fs; %sample period [s]
Tbegin=-1; %Our signal is nonzero over the time interval [Tbegin Tend]
Tend=+1;
t=[Tbegin:Ts:Tend]; %define array with discrete sampling times.
x=sin(2*pi*f0*t); %define discrete-time function. This is x[n].
figure(1)
plot(t,x)
grid on
xlabel('Time (seconds)','fontsize',14)
ylabel('Amplitude','fontsize',14)
N=2^16; %good general value for FFT (this is the number of discrete points in the
FFT.)
y=fft(x,N); %compute FFT! There is a lot going on "behind the scenes" with
%this one line of code.
z=fftshift(y); %Move the zero-frequency component to the center of the
%array. This is used for finding the double-sided spectrum (as opposed to
%the single-sided spectrum).
f_vec=[0:1:N-1]*fs/N-fs/2; %Create frequency vector (this is an array of
%numbers. Each number corresponds to a discrete point in frequency in which we
%shall evaluate and plot the function.)
amplitude_spectrum=abs(z)/fs; %Extract the amplitude of the spectrum
%Here we also apply a scaling factor of 1/fs, so that the amplitude
%of the FFT at a frequency component equals that of the the CFT and to
%preserve Parseval's theorem.
amplitude_spectrum=abs(z)*Ts;
figure(2)
plot(f_vec,amplitude_spectrum);
set(gca,'FontSize',18) %set font size of axis tick labels to 18
xlabel('Frequency [Hz]','fontsize',18)
ylabel('Amplitude','fontsize',18)
title('Amplitude spectra','fontsize',18)
grid on
set(gcf,'color','w'); %set background color from grey (default) to white
axis tight
figure(3)
plot(f_vec,abs(amplitude_spectrum).^2);
xlabel('Frequency [Hz]','fontsize',18)
ylabel('Energy/Hz','fontsize',18)
title('Energy spectral density','fontsize',18)
grid on
set(gcf,'color','w'); %set background color from grey (default) to white
axis tight
%calculate total energy
Eg=sum(x.^2)*Ts %using discrete time function x[n]
Eg=sum(amplitude_spectrum.^2)*fs/N %Using ESD

6



Part 3. One last thing – Autocorrelation Method

Believe it or not, there is a second way to calculate the energy spectral density (ESD), Ψ, 𝑡 .
Equation (5) presents one method for calculating Ψ, . But folks, in life we have choices. Let’s
examine the second method.

First, the time autocorrelation function is defined as:


.
𝜓, 𝜏 = /. 𝑥 𝑡 𝑥 𝑡 + 𝜏 𝑑𝑡. (Autocorrelation function) (14)

It can be proved that the Fourier transform of the autocorrelation function is equal to the ESD,
Ψ, 𝑡 , i.e.


ℱ 𝜓, 𝜏 = Ψ, 𝑓 = |𝑋 𝑓 |) (FT of autocorrelation function is ESD) (15)


In MATLAB, we can numerically compute the autocorrelation function presented in equation
(14) using the xcorr function :

a
𝜓, 𝜏M = xcorr(x,x) (16)
7b

We can then find the ESD by taking the FFT of 𝜓, 𝜏M . The MATAB code below shows how to
compute ESD using the autocorrelation method. Figure 4 shows a spectral plot of the ESD.
Compare Figure 4 with Figure 3 – they are identical – which demonstrates the two different
methods for yield identical results for the ESD.


%This code continues from the previous code (i.e., x, Ts, fs, f_vec and
%N are already defined)
%Below I demonstrate an alternative method to plot EST. This method
%uses autocorrelation
cor=Ts*xcorr(x,x); %numerically compute autocorrelation.
%cor=Ts*conv(flipud(x),x); %this is an alternative way to compute
%autocorrelation
y=fft(cor,N)/fs; %take FFT of autocorrrelation function and scale.

7
%This yields the ESD.
z=fftshift(y); %we want to plot double-sided spectrum
figure(4) %open new figure
plot(f_vec,abs(z)) %plot ESD
xlabel('Frequency [Hz]','fontsize',18)
ylabel('Energy/Hz','fontsize',18)
title('Energy spectral density - Via FFT of Autocorrelation','fontsize',18)
grid on
set(gcf,'color','w'); %set background color from grey (default) to white
axis tight
%calculate total energy with autocorrelation function
Eg=sum(abs(z))*fs/N %Using ESD

Energy spectral density - Via FFT of Autocorrelation


1

0.9

0.8

0.7
Energy/Hz

0.6

0.5

0.4

0.3

0.2

0.1

-10 -8 -6 -4 -2 0 2 4 6 8

Frequency [Hz]

Figure 4


You might also like