{"id":44,"date":"2009-01-30T12:27:04","date_gmt":"2009-01-30T12:27:04","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/01\/30\/mathworks-conversations-and-the-fft\/"},"modified":"2009-01-30T16:47:47","modified_gmt":"2009-01-30T16:47:47","slug":"mathworks-conversations-and-the-fft","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/01\/30\/mathworks-conversations-and-the-fft\/","title":{"rendered":"MathWorks Conversations and the FFT"},"content":{"rendered":"<p>If you are like me, you read the doc, a lot.\u00a0 I am often\r\nclicking on the help just to verify my understanding of a function\u2019s syntax, or\r\nthe behavior of a block.\u00a0 That is why I wasn\u2019t surprised to find myself\r\ninvolved in a detailed discussion about the documentation for FFT.\u00a0 <\/p>\r\n\r\n<p>You go it\u2026 just another day at the MathWorks.\u00a0 For some\r\ncontext, the doc example generates a signal corrupted with noise, and then uses\r\nthe FFT to extract the frequency components.<\/p>\r\n\r\n<p style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal;text-autospace:none'><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>Fs = 1000;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style='font-size:12.0pt;\r\nfont-family:\"Courier New\";color:forestgreen'>% Sampling frequency<\/span><br>\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>T = 1\/Fs;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style='font-size:12.0pt;\r\nfont-family:\"Courier New\";color:forestgreen'>% Sample time<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>L = 1000;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style='font-size:12.0pt;\r\nfont-family:\"Courier New\";color:forestgreen'>% Length of signal<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>t = (0:L-1)*T;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style='font-size:12.0pt;\r\nfont-family:\"Courier New\";color:forestgreen'>% Time vector<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:forestgreen'>% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); <\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>y = x + 2*randn(size(t));\u00a0\u00a0\u00a0\u00a0 <\/span><span style='font-size:12.0pt;\r\nfont-family:\"Courier New\";color:forestgreen'>% Sinusoids plus noise<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>plot(Fs*t(1:50),y(1:50))<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>title(<\/span><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:#A020F0'>'Signal Corrupted with Zero-Mean Random Noise'<\/span><span\r\nstyle='font-size:12.0pt;font-family:\"Courier New\";color:black'>)<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>xlabel(<\/span><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:#A020F0'>'time (milliseconds)'<\/span><span style='font-size:12.0pt;\r\nfont-family:\"Courier New\";color:black'>)<\/span><br>\r\n\r\n<span style='font-size:10.0pt;font-family:\"Courier New\"'>&nbsp;<\/span><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/fft_signal_plot.png\" alt=\"Noise corrupted signal plot\"><\/p>\r\n\r\n<p style='margin-bottom:0in;margin-bottom:.0001pt;line-height:\r\nnormal;text-autospace:none'><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>NFFT = 2^nextpow2(L); <\/span><span style='font-size:12.0pt;\r\nfont-family:\"Courier New\";color:forestgreen'>% Next power of 2 from length of y<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>Y = fft(y,NFFT)\/L;<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>f = Fs\/2*linspace(0,1,NFFT\/2+1);<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>\u00a0<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:forestgreen'>% Plot single-sided amplitude spectrum.<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>plot(f,2*abs(Y(1:NFFT\/2+1))) <\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>title(<\/span><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:#A020F0'>'Single-Sided Amplitude Spectrum of y(t)'<\/span><span\r\nstyle='font-size:12.0pt;font-family:\"Courier New\";color:black'>)<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>xlabel(<\/span><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:#A020F0'>'Frequency (Hz)'<\/span><span style='font-size:12.0pt;font-family:\r\n\"Courier New\";color:black'>)<\/span><br>\r\n\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>ylabel(<\/span><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:#A020F0'>'|Y(f)|'<\/span><span style='font-size:12.0pt;font-family:\"Courier New\";\r\ncolor:black'>)<\/span><br>\r\n\r\n<span style='font-size:12.0pt;font-family:\"Courier New\"'>&nbsp;<\/span><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/fft_spectrum_plot.png\" alt=\"FFT spectrum plot\"><\/p>\r\n\r\n<p>There were two questions that started the discussion.\u00a0 The\r\nfirst one,<\/p>\r\n\r\n<p><em>Why does the FFT example in the doc have so much code?<\/em><\/p>\r\n\r\n<p>We can ignore that for now and focus on the second question,\r\nand the real meat of the discussion:<\/p>\r\n\r\n<p><em>Why does the FFT example result in an amplitude of 1?<\/em><\/p>\r\n\r\n<p>My colleague Jeoffrey surprised me with his intimate\r\nfamiliarity with the discrete time Fourier transform and his ability to produce\r\nthe complicated equations out of thin air.\u00a0 I told him, \u201cI want that in a blog\r\npost!\u201d\u00a0 So here it is.\u00a0 Today\u2019s featured guest blogger is Jeoffrey Young:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/jeoffry_young.png\" alt=\"Jeoffrey Young, FT and DTFT Expert\" ><\/p>\r\n\r\n<p><strong>Why does the FFT example result in an amplitude of 1?<\/strong><br>By Jeoffrey Young<\/p>\r\n\r\n<p>In this post I will talk about one way of looking at where\r\nthe scaling <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:8.5pt'><img width=21 height=29\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image003.gif\"><\/span>\u00a0comes from in\r\nthe following example from Matlab 7.7 (R2008b)\u2019s help documentation:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/fft_code_circled.png\" alt=\"MATLAB code for the FFT circled\"><\/p>\r\n\r\n<p>To start with, let\u2019s remember that FFT is simply the sampled\r\nDiscrete Time Fourier Transform of a signal. We know from DTFT that (I\u2019ll use\r\nthe cosine function here for simplicity):<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=298 height=20 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image005.gif\"><\/span><\/p>\r\n\r\n<p>where <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:5.5pt'><img width=53 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image006.gif\"><\/span>\u00a0 and <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=77 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image007.gif\"><\/span>\u00a0(Ever wonder\r\nwhy this looks exactly the same as the Fourier Transform of the continuous time\r\ncosine signal? More on this later.) However, it is not enough to sample a\r\ncontinuous time signal that is not time-limited, such as a cosine function.\r\nTruncating <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:5.5pt'><img width=58 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image008.gif\"><\/span>\u00a0so that we\r\ncan represent the signal on a computer is equivalent to multiplying the time-domain\r\nsignal by the following rectangular window:<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=193 height=34 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image009.gif\"><\/span><\/p>\r\n\r\n<p>The DTFT of this signal is the Digital Sinc Function:<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=210 height=62 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image010.gif\"><\/span><\/p>\r\n\r\n<p>Note that <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:5.5pt'><img width=137 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image011.gif\"><\/span>\u00a0by\r\nL\u2019Hopital\u2019s Rule. <\/p>\r\n\r\n<p>Thus, from the time-domain multiplication property of\r\nFourier Transform:<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=499 height=62 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image012.gif\"><\/span><\/p>\r\n\r\n<p>Here we see that the DTFT of an L-point truncated cosine function\r\nhas a maximum amplitude of <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:8.5pt'><img width=6 height=27\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image013.gif\"><\/span>\u00a0(if we ignore\r\nthe effects of aliasing from the overlap and the fact that DTFT is <span style='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";position:relative;\r\ntop:4.5pt'><img width=17 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image014.gif\"><\/span>\u00a0periodic).<\/p>\r\n\r\n<p>Question about the scale factor used with FFT are also sent\r\nto technical support.  This happens often enough that tech support posted a\r\nsolution titled \u201c<a\r\nhref=\"https:\/\/www.mathworks.com\/support\/solutions\/data\/1-3O4RH1.html?solution=1-3O4RH1\">Why\r\nis the example of using an FFT to get a frequency spectrum scaled the way it\r\nis?<\/a>\u201d  We even have a technical note on the general topic of spectral\r\nanalysis with FFT: <a\r\nhref=\"https:\/\/www.mathworks.com\/support\/tech-notes\/1700\/1702.html\">Tech Note:\r\n1702 - Using FFT to Obtain Simple Spectral Analysis Plots<\/a>.  As we see it used in this post, the section below describes how the scale factor may also depend on the signal of interest.<\/p>\r\n\r\n<p><strong>Why does <\/strong><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:5.5pt'><img width=114 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image015.gif\"><\/span><strong>\u00a0looks\r\nexactly like <\/strong><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:5.5pt'><img width=91 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image016.gif\"><\/span><strong>?<\/strong><\/p>\r\n\r\n<p>Remember the following relationship between the DTFT of the\r\nsampled analog signal and the FT of the original signal:<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=214 height=60 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image017.gif\"><\/span><\/p>\r\n\r\n<p>In other words, the Discrete Time Fourier transform is\r\nscaled by <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:10.0pt'><img width=10 height=30\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image018.gif\"><\/span>. However, the\r\nDTFT of a sinusoid is the Delta Function, which is defined more by its\r\nproperties than its value. Thus, its amplitude has to be scaled when the axis\r\nis modified <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:10.0pt'><img width=54 height=31\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image019.gif\"><\/span>:<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=161 height=38 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image020.gif\"><\/span><\/p>\r\n\r\n<p>So that:<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=421 height=69 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/image021.gif\"><\/span><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>If you have any\r\nthoughts on how scaling the FFT results may be useful, please post your <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/?p=44&amp;#comment\">comments here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>If you are like me, you read the doc, a lot.\u00a0 I am often\r\nclicking on the help just to verify my understanding of a function\u2019s syntax, or\r\nthe behavior of a block.\u00a0 That is why I wasn\u2019t surprised to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/01\/30\/mathworks-conversations-and-the-fft\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[67,65,73],"tags":[75,74],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}