{"id":1140,"date":"2014-09-23T17:01:50","date_gmt":"2014-09-23T21:01:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1140"},"modified":"2019-11-01T11:20:39","modified_gmt":"2019-11-01T15:20:39","slug":"sinusoids-and-fft-frequency-bins","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2014\/09\/23\/sinusoids-and-fft-frequency-bins\/","title":{"rendered":"Sinusoids and FFT frequency bins"},"content":{"rendered":"<div class=\"content\"><h3>Contents<\/h3><div><ul><li><a href=\"#f94dfa5f-3776-484f-880a-8baa03d75da8\">Construct the signal<\/a><\/li><li><a href=\"#c46125e7-13a5-4d48-814d-9e2fe85881c4\">Exploring the \"spread\" around the peak<\/a><\/li><li><a href=\"#7d55e695-5020-4e8b-9512-1f50002f9bee\">Moving the sinusoid frequency to line up with a bin<\/a><\/li><li><a href=\"#3861228e-c853-408f-be0b-0c8e62557c30\">Using a longer signal<\/a><\/li><li><a href=\"#cfae9c6e-08f9-464f-9f50-5ba2d3ee6e46\">Summary<\/a><\/li><\/ul><\/div><p><i>Note: I really didn't expect to be writing about the FFT two weeks in a row. But ready or not, here we go!<\/i><\/p><p>My friend Joe walked into my office the other day and announced, \"Steve, I've got a question about the FFT.\"<\/p><p>(Joe was one of my MathWorks interviewers 21 years ago this month. His question for me then was, \"So what do you bring to the party?\" I don't remember how I answered.)<\/p><p>Joe is one of the biggest MATLAB experimenters I know. He's always trying to use MATLAB for some new kind of analysis or to automate something dubious (such as changing the hue of the lights in his house. Hmm.)<\/p><p>Anyway, lately he's been into automatic analysis of music. And something was bothering him. If you have a pure sinusoid signal and take the FFT of it, why does it spread out in the frequency domain? How do you interpret the output of <tt>fft<\/tt> when the frequency is in between two adjacent frequency bins of the FFT? And is that situation fundamentally different somehow that when the sinusoid's frequency is exactly one of the FFT bin frequencies?<\/p><p>Oh boy. I told Joe, \"Well, you don't really have a sinusoid. A sinusoid is an infinitely long signal. You have a finite portion of a sinusoid, which I think of as a rectangularly windowed sinusoid. Go away, let me think more about how to explain all that, and then I'll come find you tomorrow.\"<\/p><p>Because of my background in digital signal processing, I tend to think about this in terms of the relationships between three kinds of Fourier transforms:<\/p><div><ul><li>Fourier transform (continuous time - continuous frequency)<\/li><li>discrete-time Fourier transform (DTFT) (discrete time - continuous frequency<\/li><li>discrete Fourier transform (DFT) (discrete time - discrete frequency)<\/li><\/ul><\/div><p>I have written before (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/11\/23\/fourier-transforms\/\">23-Nov-2009<\/a>) about the various kinds of Fourier transforms. It is this last kind, the DFT, that is computed by the MATLAB <tt>fft<\/tt> function.<\/p><p>It's hard to tell exactly where to start in any discussion about Fourier transform properties because the use of terminology and the mathematical convensions vary so widely. Let me just dive in and walk through Joe's example. If you have questions about what's going on, please post a comment.<\/p><p><i>Note: In what follows, I am making a very big simplifying assumption. Specifically, I'm assuming that the sampling frequency is high enough for us to be able to discount the possibility of aliasing in the sampling of the sinusoid. This isn't a completely valid assumption because, unlike a real sinusoid, the truncated or windowed sinusoid doesn't have a finite bandwidth.<\/i><\/p><p>Here some sample parameters that Joe gave me.<\/p><p>Sinusoid frequency in Hz:<\/p><pre class=\"codeinput\">f0 = 307;\r\n<\/pre><p>Sampling frequency in Hz:<\/p><pre class=\"codeinput\">Fs = 44100;\r\n<\/pre><p>Sampling time:<\/p><pre class=\"codeinput\">Ts = 1\/Fs;\r\n<\/pre><p>How many samples of the sinusoid do we have? From a theoretical perspective, think of this as the length of a rectangular window that multiplies our infinitely long sinusoid sequence.<\/p><pre class=\"codeinput\">M = 1500;\r\n<\/pre><h4>Construct the signal<a name=\"f94dfa5f-3776-484f-880a-8baa03d75da8\"><\/a><\/h4><pre class=\"codeinput\">n = 0:(M-1);\r\ny = cos(2*pi*f0*Ts*n);\r\n<\/pre><p>Compute M-point discrete Fourier transform (DFT) of <tt>f<\/tt>. This is what the function <tt>fft<\/tt> computes.<\/p><pre class=\"codeinput\">Y = fft(y);\r\n<\/pre><p>Plot the DFT. The DFT itself doesn't have physical frequency units associated with it, but we can plot it on a Hz frequency scale by incorporating our knowledge of the sampling rate. (I'm plotting the DFT as disconnected points to emphasize its discrete-frequency nature. Also, I'll be plotting magnitudes of the Fourier transforms throughout.)<\/p><pre class=\"codeinput\">f = (0:(M-1))*Fs\/M;\r\nplot(f,abs(Y),<span class=\"string\">'*'<\/span>)\r\nxlim([0 Fs\/2])\r\ntitle(<span class=\"string\">'Magnitude of DFT'<\/span>)\r\nxlabel(<span class=\"string\">'Frequency (Hz)'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_01.png\" alt=\"\"> <h4>Exploring the \"spread\" around the peak<a name=\"c46125e7-13a5-4d48-814d-9e2fe85881c4\"><\/a><\/h4><p>Zoom in on the peak.<\/p><pre class=\"codeinput\">p = 2*Fs\/(M+1);\r\nf1 = f0 - 10*p;\r\nf2 = f0 + 10*p;\r\nxlim([f1 f2])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_02.png\" alt=\"\"> <p>What's with the \"spread\" around the peak? The spread is caused by the fact that we do not have a pure sinusoid. We have only a finite portion of a pure sinusoid.<\/p><p>To explore this further, let's relate the DFT to what some might call the \"real\" Fourier transform of our windowed sinusoid. If we can discount aliasing, then the discrete-time Fourier transform (DTFT) is the same as the \"real\" Fourier transform up to half the sampling frequency.  And, for a signal that is nonzero only in the interval 0 &lt;= n &lt; N, an M-point DFT exactly samples the DTFT as long as M &gt;= N. The DTFT of an N-point windowed sinuoid has the form a bunch of copies of $sin(\\omega (N+1)\/2) \/ sin(\\omega\/2)$, where the copies are spaced apart by multiples of the sampling frequency.<\/p><p>Can we get a better idea of the true shape of the DTFT? Yes, by zero-padding the input to the DFT. This has the effect of sampling the DTFT more closely. We can use this relationship to plot a pretty good approximation of the DTFT and then superimpose the DFT (the output of <tt>fft<\/tt>).<\/p><pre class=\"codeinput\">M10 = length(y)*10;\r\nY10 = fft(y,M10);\r\nf10 = (0:(M10-1))*Fs\/M10;\r\n\r\nplot(f10,abs(Y10));\r\nhold <span class=\"string\">on<\/span>\r\nplot(f,abs(Y),<span class=\"string\">'*'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n\r\nxlim([f1 f2])\r\n\r\nlegend({<span class=\"string\">'DTFT'<\/span>,<span class=\"string\">'DFT'<\/span>})\r\nxlabel(<span class=\"string\">'Frequency (Hz)'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_03.png\" alt=\"\"> <h4>Moving the sinusoid frequency to line up with a bin<a name=\"7d55e695-5020-4e8b-9512-1f50002f9bee\"><\/a><\/h4><p>The DFT samples the Fourier transform at a spacing of Fs\/M. Let's pick a frequency that lines up exactly on a bin.<\/p><pre class=\"codeinput\">f0 = 294;\r\nw0 = 2*pi*f0\/Fs;\r\nn = 0:(M-1);\r\ny = cos(w0*n);\r\nY = fft(y);\r\np = 2*Fs\/(M+1);\r\nf1 = f0 - 10*p;\r\nf2 = f0 + 10*p;\r\nplot(f,abs(Y),<span class=\"string\">'*'<\/span>)\r\nxlim([f1 f2])\r\ntitle(<span class=\"string\">'DFT'<\/span>)\r\nxlabel(<span class=\"string\">'Frequency (Hz)'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_04.png\" alt=\"\"> <p>The DFT samples (output of <tt>fft<\/tt>) <b>appears<\/b> to be perfect above. It has just one nonzero value at the sinusoid frequency.<\/p><p>But the appearance of perfection is just a kind of illusion. You can see what's really happening by superimposing the DFT samples on top of the DTFT, which we compute as before using the zero-padding trick.<\/p><pre class=\"codeinput\">Y10 = fft(y,M10);\r\nplot(f10,abs(Y10));\r\nhold <span class=\"string\">on<\/span>\r\nplot(f,abs(Y),<span class=\"string\">'*'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\nxlim([f1 f2])\r\ntitle(<span class=\"string\">'DTFT with DFT superimposed'<\/span>)\r\nxlabel(<span class=\"string\">'Frequency (Hz)'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_05.png\" alt=\"\"> <p>You can see that most of the DFT samples happen to line up precisely at the zeros of the DTFT.<\/p><h4>Using a longer signal<a name=\"3861228e-c853-408f-be0b-0c8e62557c30\"><\/a><\/h4><p>Finally, let's look at what happens when you use a longer signal. (Joe said, \"Can't I make the spread go away by using a sufficiently large number of samples of the sinusoid?\" The answer, as we'll see demonstrated, is that you can make the spread get smaller, but you can't make it go away entirely as long as you have a finite number of samples.) Let's go up a factor of 10, to 15,000 samples of the sinusoid. (And let's go back to the original frequency of 307 Hz.)<\/p><pre class=\"codeinput\">f0 = 307;\r\nM = 15000;\r\nw0 = 2*pi*f0\/Fs;\r\nn = 0:(M-1);\r\ny = cos(w0*n);\r\nY = fft(y);\r\n\r\nM10 = length(y)*10;\r\nY10 = fft(y,M10);\r\nf10 = (0:(M10-1))*Fs\/M10;\r\nf = (0:(M-1))*Fs\/M;\r\n\r\nplot(f10,abs(Y10));\r\nhold <span class=\"string\">on<\/span>\r\nplot(f,abs(Y),<span class=\"string\">'*'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n\r\nxlim([f1 f2])\r\n\r\ntitle(<span class=\"string\">'DTFT with DFT superimposed'<\/span>)\r\nxlabel(<span class=\"string\">'Frequency (Hz)'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_06.png\" alt=\"\"> <p>The plot above is shown at the same scale as the one for M = 1500 further up. You can see that the peak is much narrower, but it's hard to see the details. Let's zoom in closer.<\/p><pre class=\"codeinput\">p = 2*Fs\/(M+1);\r\nf1 = f0 - 10*p;\r\nf2 = f0 + 10*p;\r\nxlim([f1 f2])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_07.png\" alt=\"\"> <p>Now you can see that the DTFT shape and DFT samples look very much like what we got for the 1500-point windowed sinusoid. The width of the peak, as well as the spacing of the sidelobes, has been reduced by a factor of 10 (15000 \/ 1500). And the spacing (in Hz) of the DFT samples has been reduced by the same amount.<\/p><h4>Summary<a name=\"cfae9c6e-08f9-464f-9f50-5ba2d3ee6e46\"><\/a><\/h4><p>In summary, the Fourier transform of a finite portion of a sinusoid has a characteristic shape: a central peak (the \"main lobe\") with a bunch of \"side lobes\" in either direction from the peak. The DFT simply samples this shape. Depending on the exact frequency, the output of the DFT might look a little different, depending on where the frequency happens to line up with the DFT frequency bins.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a25f21e723ec4079bba4a0b0746478a6() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='a25f21e723ec4079bba4a0b0746478a6 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a25f21e723ec4079bba4a0b0746478a6';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_a25f21e723ec4079bba4a0b0746478a6()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><\/div><!--\r\na25f21e723ec4079bba4a0b0746478a6 ##### SOURCE BEGIN #####\r\n%% \r\n% _Note: I really didn't expect to be writing about the FFT two weeks in a\r\n% row. But ready or not, here we go!_\r\n%\r\n% My friend Joe walked into my office the other day and announced, \"Steve,\r\n% I've got a question about the FFT.\"\r\n%\r\n% (Joe was one of my MathWorks interviewers 21 years ago this month. His\r\n% question for me then was, \"So what do you bring to the party?\" I don't\r\n% remember how I answered.)\r\n%\r\n% Joe is one of the biggest MATLAB experimenters I know. He's always\r\n% trying to use MATLAB for some new kind of analysis or to automate\r\n% something dubious (such as changing the hue of the lights in his house.\r\n% Hmm.)\r\n%\r\n% Anyway, lately he's been into automatic analysis of music. And something\r\n% was bothering him. If you have a pure sinusoid signal and take the FFT of\r\n% it, why does it spread out in the frequency domain? How do you interpret\r\n% the output of |fft| when the frequency is in between two adjacent\r\n% frequency bins of the FFT? And is that situation fundamentally different\r\n% somehow that when the sinusoid's frequency is exactly one of the FFT bin\r\n% frequencies?\r\n%\r\n% Oh boy. I told Joe, \"Well, you don't really have a sinusoid. A sinusoid\r\n% is an infinitely long signal. You have a finite portion of a sinusoid,\r\n% which I think of as a rectangularly windows sinusoid. Go away, let me\r\n% think more about how to explain all that, and then I'll come find you\r\n% tomorrow.\"\r\n%\r\n% Because of my background in digital signal processing, I tend to think\r\n% about this in terms of the relationships between three kinds of Fourier\r\n% transforms:\r\n%\r\n% * Fourier transform (continuous time <-> continuous frequency)\r\n% * discrete-time Fourier transform (DTFT) (discrete time <-> continuous\r\n% frequency\r\n% * discrete Fourier transform (DFT) (discrete time <-> discrete frequency)\r\n%\r\n% I have written before\r\n% (<https:\/\/blogs.mathworks.com\/steve\/2009\/11\/23\/fourier-transforms\/\r\n% 23-Nov-2009>) about the various kinds of Fourier transforms. It is this\r\n% last kind, the DFT, that is computed by the MATLAB |fft| function.\r\n%\r\n% It's hard to tell exactly where to start in any discussion about Fourier\r\n% transform properties because the use of terminology and the mathematical\r\n% convensions vary so widely. Let me just dive in and walk through Joe's\r\n% example. If you have questions about what's going on, please post a\r\n% comment.\r\n%\r\n% _Note: In what follows, I am making a very big simplifying assumption.\r\n% Specifically, I'm assuming that the sampling frequency is high enough for\r\n% us to be able to discount the possibility of aliasing in the sampling of\r\n% the sinusoid. This isn't a completely valid assumption because, unlike a\r\n% real sinusoid, the truncated or windowed sinusoid doesn't have a finite\r\n% bandwidth._\r\n%\r\n% Here some sample parameters that Joe gave me.\r\n%\r\n% Sinusoid frequency in Hz:\r\n\r\nf0 = 307;\r\n\r\n%%\r\n% Sampling frequency in Hz:\r\n\r\nFs = 44100;\r\n\r\n%%\r\n% Sampling time:\r\n\r\nTs = 1\/Fs;\r\n\r\n%%\r\n% How many samples of the sinusoid do we have? From a theoretical\r\n% perspective, think of this as the length of a rectangular window that\r\n% multiplies our infinitely long sinusoid sequence.\r\n\r\nM = 1500;\r\n\r\n%% Construct the signal\r\n\r\nn = 0:(M-1);\r\ny = cos(2*pi*f0*Ts*n);\r\n\r\n%%\r\n% Compute M-point discrete Fourier transform (DFT) of |f|. This is what the\r\n% function |fft| computes.\r\n\r\nY = fft(y);\r\n\r\n%%\r\n% Plot the DFT. The DFT itself doesn't have physical frequency units\r\n% associated with it, but we can plot it on a Hz frequency scale by\r\n% incorporating our knowledge of the sampling rate. (I'm plotting the DFT\r\n% as disconnected points to emphasize its discrete-frequency nature. Also,\r\n% I'll be plotting magnitudes of the Fourier transforms throughout.)\r\n\r\nf = (0:(M-1))*Fs\/M;\r\nplot(f,abs(Y),'*')\r\nxlim([0 Fs\/2])\r\ntitle('Magnitude of DFT')\r\nxlabel('Frequency (Hz)')\r\n\r\n%% Exploring the \"spread\" around the peak\r\n%\r\n% Zoom in on the peak.\r\n\r\np = 2*Fs\/(M+1);\r\nf1 = f0 - 10*p;\r\nf2 = f0 + 10*p;\r\nxlim([f1 f2])\r\n\r\n%%\r\n% What's with the \"spread\" around the peak? The spread is caused by the\r\n% fact that we do not have a pure sinusoid. We have only a finite portion\r\n% of a pure sinusoid.\r\n%\r\n% To explore this further, let's relate the DFT to what some might call the\r\n% \"real\" Fourier transform of our windowed sinusoid. If we can discount\r\n% aliasing, then the discrete-time Fourier transform (DTFT) is the same as\r\n% the \"real\" Fourier transform up to half the sampling frequency.  And, for\r\n% a signal that is nonzero only in the interval 0 <= n < N, an M-point DFT\r\n% exactly samples the DTFT as long as M >= N. The DTFT of an N-point\r\n% windowed sinuoid has the form a bunch of copies of $sin(\\omega (N+1)\/2) \/\r\n% sin(\\omega\/2)$, where the copies are spaced apart by multiples of the\r\n% sampling frequency.\r\n%\r\n% Can we get a better idea of the true shape of the DTFT? Yes, by\r\n% zero-padding the input to the DFT. This has the effect of sampling the\r\n% DTFT more closely. We can use this relationship to plot a pretty good\r\n% approximation of the DTFT and then superimpose the DFT (the output of\r\n% |fft|).\r\n\r\nM10 = length(y)*10;\r\nY10 = fft(y,M10);\r\nf10 = (0:(M10-1))*Fs\/M10;\r\n\r\nplot(f10,abs(Y10));\r\nhold on\r\nplot(f,abs(Y),'*')\r\nhold off\r\n\r\nxlim([f1 f2])\r\n\r\nlegend({'DTFT','DFT'})\r\nxlabel('Frequency (Hz)')\r\n\r\n%% Moving the sinusoid frequency to line up with a bin\r\n% The DFT samples the Fourier transform at a spacing of Fs\/M. Let's pick a\r\n% frequency that lines up exactly on a bin.\r\n\r\nf0 = 294;\r\nw0 = 2*pi*f0\/Fs;\r\nn = 0:(M-1);\r\ny = cos(w0*n);\r\nY = fft(y);\r\np = 2*Fs\/(M+1);\r\nf1 = f0 - 10*p;\r\nf2 = f0 + 10*p;\r\nplot(f,abs(Y),'*')\r\nxlim([f1 f2])\r\ntitle('DFT')\r\nxlabel('Frequency (Hz)')\r\n\r\n%%\r\n% The DFT samples (output of |fft|) *appears* to be perfect above. It has\r\n% just one nonzero value at the sinusoid frequency.\r\n%\r\n% But the appearance of perfection is just a kind of illusion. You can see\r\n% what's really happening by superimposing the DFT samples on top of the\r\n% DTFT, which we compute as before using the zero-padding trick.\r\n\r\nY10 = fft(y,M10);\r\nplot(f10,abs(Y10));\r\nhold on\r\nplot(f,abs(Y),'*')\r\nhold off\r\nxlim([f1 f2])\r\ntitle('DTFT with DFT superimposed')\r\nxlabel('Frequency (Hz)')\r\n\r\n%%\r\n% You can see that most of the DFT samples happen to line up precisely at\r\n% the zeros of the DTFT.\r\n%\r\n%% Using a longer signal\r\n% Finally, let's look at what happens when you use a longer signal. (Joe\r\n% said, \"Can't I make the spread go away by using a sufficiently large\r\n% number of samples of the sinusoid?\" The answer, as we'll see\r\n% demonstrated, is that you can make the spread get smaller, but you can't\r\n% make it go away entirely as long as you have a finite number of samples.)\r\n% Let's go up a factor of 10, to 15,000 samples of the sinusoid. (And let's\r\n% go back to the original frequency of 307 Hz.)\r\n\r\nf0 = 307;\r\nM = 15000;\r\nw0 = 2*pi*f0\/Fs;\r\nn = 0:(M-1);\r\ny = cos(w0*n);\r\nY = fft(y);\r\n\r\nM10 = length(y)*10;\r\nY10 = fft(y,M10);\r\nf10 = (0:(M10-1))*Fs\/M10;\r\nf = (0:(M-1))*Fs\/M;\r\n\r\nplot(f10,abs(Y10));\r\nhold on\r\nplot(f,abs(Y),'*')\r\nhold off\r\n\r\nxlim([f1 f2])\r\n\r\ntitle('DTFT with DFT superimposed')\r\nxlabel('Frequency (Hz)')\r\n\r\n%%\r\n% The plot above is shown at the same scale as the one for M = 1500 further\r\n% up. You can see that the peak is much narrower, but it's hard to see the\r\n% details. Let's zoom in closer.\r\n\r\np = 2*Fs\/(M+1);\r\nf1 = f0 - 10*p;\r\nf2 = f0 + 10*p;\r\nxlim([f1 f2])\r\n\r\n%%\r\n% Now you can see that the DTFT shape and DFT samples look very much like\r\n% what we got for the 1500-point windowed sinusoid. The width of the peak,\r\n% as well as the spacing of the sidelobes, has been reduced by a factor of\r\n% 10 (15000 \/ 1500). And the spacing (in Hz) of the DFT samples has been\r\n% reduced by the same amount.\r\n%\r\n%% Summary\r\n% In summary, the Fourier transform of a finite portion of a sinusoid has a\r\n% characteristic shape: a central peak (the \"main lobe\") with a bunch of\r\n% \"side lobes\" in either direction from the peak. The DFT simply samples\r\n% this shape. Depending on the exact frequency, the output of the DFT might\r\n% look a little different, depending on where the frequency happens to line\r\n% up with the DFT frequency bins.\r\n##### SOURCE END ##### a25f21e723ec4079bba4a0b0746478a6\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/frequency_bins_for_sinusoid_v2_07.png\" onError=\"this.style.display ='none';\" \/><\/div><p>ContentsConstruct the signalExploring the \"spread\" around the peakMoving the sinusoid frequency to line up with a binUsing a longer signalSummaryNote: I really didn't expect to be writing about the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/09\/23\/sinusoids-and-fft-frequency-bins\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[208,74,426,90,92,705,68,52,94,360],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1140"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=1140"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":2310,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1140\/revisions\/2310"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}