Seth on Simulink

January 30th, 2009

MathWorks Conversations and the FFT

If you are like me, you read the doc, a lot.  I am often clicking on the help just to verify my understanding of a function’s syntax, or the behavior of a block.  That is why I wasn’t surprised to find myself involved in a detailed discussion about the documentation for FFT. 

You go it… just another day at the MathWorks.  For some context, the doc example generates a signal corrupted with noise, and then uses the FFT to extract the frequency components.

Fs = 1000;                    % Sampling frequency
T = 1/Fs;                     % Sample time
L = 1000;                     % Length of signal
t = (0:L-1)*T;                % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t));     % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
 

Noise corrupted signal plot

NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
 
% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
 

FFT spectrum plot

There were two questions that started the discussion.  The first one,

Why does the FFT example in the doc have so much code?

We can ignore that for now and focus on the second question, and the real meat of the discussion:

Why does the FFT example result in an amplitude of 1?

My colleague Jeoffrey surprised me with his intimate familiarity with the discrete time Fourier transform and his ability to produce the complicated equations out of thin air.  I told him, “I want that in a blog post!”  So here it is.  Today’s featured guest blogger is Jeoffrey Young:

Jeoffrey Young, FT and DTFT Expert

Why does the FFT example result in an amplitude of 1?
By Jeoffrey Young

In this post I will talk about one way of looking at where the scaling  comes from in the following example from Matlab 7.7 (R2008b)’s help documentation:

MATLAB code for the FFT circled

To start with, let’s remember that FFT is simply the sampled Discrete Time Fourier Transform of a signal. We know from DTFT that (I’ll use the cosine function here for simplicity):

where   and  (Ever wonder why this looks exactly the same as the Fourier Transform of the continuous time cosine signal? More on this later.) However, it is not enough to sample a continuous time signal that is not time-limited, such as a cosine function. Truncating  so that we can represent the signal on a computer is equivalent to multiplying the time-domain signal by the following rectangular window:

The DTFT of this signal is the Digital Sinc Function:

Note that  by L’Hopital’s Rule.

Thus, from the time-domain multiplication property of Fourier Transform:

Here we see that the DTFT of an L-point truncated cosine function has a maximum amplitude of  (if we ignore the effects of aliasing from the overlap and the fact that DTFT is  periodic).

Question about the scale factor used with FFT are also sent to technical support. This happens often enough that tech support posted a solution titled “Why is the example of using an FFT to get a frequency spectrum scaled the way it is?” We even have a technical note on the general topic of spectral analysis with FFT: Tech Note: 1702 - Using FFT to Obtain Simple Spectral Analysis Plots. As we see it used in this post, the section below describes how the scale factor may also depend on the signal of interest.

Why does  looks exactly like ?

Remember the following relationship between the DTFT of the sampled analog signal and the FT of the original signal:

In other words, the Discrete Time Fourier transform is scaled by . However, the DTFT of a sinusoid is the Delta Function, which is defined more by its properties than its value. Thus, its amplitude has to be scaled when the axis is modified :

So that:

Now it's your turn

If you have any thoughts on how scaling the FFT results may be useful, please post your comments here.

2 Responses to “MathWorks Conversations and the FFT”

  1. Saurabh replied on :

    Sir,
    I have tried a lot but i am unable to find fft in simulunk. Can you tell me which block should i use to find fft and which scope block to see it?
    Thankyou.

  2. Seth replied on :

    @Saurabh - The blocks I think you are looking for are part of the Signal Processing Blockset. You can find an FFT block in the Power Spectrum Estimation library. There is also a Spectrum Scope in the Signal Processing sinks. If you don’t have the Signal Processing Blockset, output your signal data to the MATLAB work space and compute and FFT from there. I hope that helps.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Seth Popinchalk is an Application Engineer for The MathWorks. He writes here about Simulink and other MathWorks tools used in Model-Based Design.
  • wei: @Ashish, I agree with your observation on compiler optimization but fail to see why Han’s code would be...
  • Ashish Sadanandan: Han, Sorry for butting in with a reply, but the modification to the for-loop you’ve...
  • arun kumar: dear seth i have been involved in developing and simulating asynchronous systems for my VLSI lab. in...
  • Han Geerligs: Hi Aarti, thanks for providing the example! I was just wondering why the lines model_YDim = model_XDim;...
  • Seth: @Han - Aarti’s response is in a post titled Generated Code for Variable Size Signals.
  • garla: what is advantage of exporting the bus object with the format as “cell” and “object” ....
  • J.r: @ Guy - Example sent to your account. Thank you.
  • Guy: @ J.r - The ability for atomic subsystems and Model Reference to remove the “fake algebraic loop”...
  • J.r: Sorry to hit an old thread, but this seems like the best place to solicit help on this topic. Is there supposed...
  • Bindu: please tell me how can i get the sampling time used in simulation

These postings are the author's and don't necessarily represent the opinions of The MathWorks.