{"id":316,"date":"2010-03-03T20:36:35","date_gmt":"2010-03-04T01:36:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/03\/aliasing-and-a-sampled-cosine-signal\/"},"modified":"2019-10-29T13:22:28","modified_gmt":"2019-10-29T17:22:28","slug":"aliasing-and-a-sampled-cosine-signal","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/03\/aliasing-and-a-sampled-cosine-signal\/","title":{"rendered":"Aliasing and a sampled cosine signal"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>One challenge of teaching Fourier transform concepts is that each concept can be (and is) interpreted and explained in many\r\n      different ways. It isn't always obvious how the different explanations for the same concepts are connected.\r\n   <\/p>\r\n   <p>For example, in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/02\/22\/aliasing-and-the-discrete-time-fourier-transform\/\">last Fourier transform post<\/a> I talked about <i>aliasing<\/i>.  I said that if you sample a continuous-time cosine at a sampling frequency <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_eq72778.png\"> , then you can't distinguish between a cosine with frequency <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_eq87919.png\">  and a cosine with frequency <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_eq72870.png\"> .\r\n   <\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/02\/22\/aliasing-and-the-discrete-time-fourier-transform\/#comment-22765\">In response<\/a>, Dave S. wanted to know how this related to what he learned about aliasing: that aliasing is a \"problem that occurs when\r\n      the sampling rate of your discrete signal is not at least twice the [...] highest existing frequency [in the continuous-time\r\n      signal].\"\r\n   <\/p>\r\n   <p>I know I promised to introduce the discrete Fourier transform next, but I'd like to change my mind and try to answer Dave's\r\n      question instead.  There are two key pieces of the question to address: What is the nature of the \"problem,\" and what is the\r\n      significance of \"twice the highest frequency\"?\r\n   <\/p>\r\n   <p>I thought about drawing some new frequency-domain diagrams showing overlapping triangles like you'd see in <a href=\"http:\/\/www.amazon.com\/Discrete-Time-Signal-Processing-Prentice-Hall\/dp\/0131988425\/ref=sr_1_2?ie=UTF8&amp;s=books&amp;qid=1267639933&amp;sr=8-2\">Oppenheim and Schafer<\/a>, but then I thought it might be better to just continue the <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/02\/22\/aliasing-and-the-discrete-time-fourier-transform\/\">sampled cosine example from last time<\/a>.\r\n   <\/p>\r\n   <p>Let's start with a continuous-time cosine signal at 60 Hz.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">f = 60;  <span style=\"color: #228B22\">% Hz<\/span>\r\ntmin = -0.05;\r\ntmax = 0.05;\r\nt = linspace(tmin, tmax, 400);\r\nx_c = cos(2*pi*f * t);\r\nplot(t,x_c)\r\nxlabel(<span style=\"color: #A020F0\">'t (seconds)'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_01.png\"> <p>Let's sample <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_eq04002.png\">  with a sampling frequency of 800 Hz.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">T = 1\/800;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(n*T,x1,<span style=\"color: #A020F0\">'.'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_02.png\"> <p>The sampling frequency of 800 Hz is well above 120 Hz, which is twice the frequency of the cosine. And you can see that the\r\n      samples are clearly capturing the oscillation of the continuous-time cosine.\r\n   <\/p>\r\n   <p>Let's try a lower sampling frequency.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">T = 1\/400;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(n*T, x1, <span style=\"color: #A020F0\">'.'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_03.png\"> <p>The samples above are still adequately capturing the shape of the cosine.  Now let's drop the sampling frequency down to exactly\r\n      120 Hz, twice the frequency of the 60 Hz cosine. (And I'll switch to using circle markers to make the samples easier to see.)\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">T = 1\/120;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(n*T, x1, <span style=\"color: #A020F0\">'o'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_04.png\"> <p>See how the samples jump back and forth between 1 and -1? And how they capture only the extremes of each period of the cosine\r\n      oscillation?  This is the significance of \"twice the highest frequency of the signal\" value for sampling frequency.  If you'll\r\n      allow a \"hand-wavy\" explanation here, I'll say that this sampling frequency of 120 Hz is just enough to capture the cosine\r\n      oscillation.\r\n   <\/p>\r\n   <p>But aliasing is worse that \"just\" losing information.  When we drop the sampling frequency too low, the samples start to look\r\n      increasingly like they came from a <b>different, lower-frequency signal<\/b>.\r\n   <\/p>\r\n   <p>Let's try 70 Hz.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">T = 1\/70;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(n*T, x1, <span style=\"color: #A020F0\">'o'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_05.png\"> <p>The samples above look like they actually could have come from a <b>10 Hz cosine signal<\/b>, instead of a 60 Hz cosine signal.  Take a look:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">T = 1\/70;\r\nx_c = cos(2*pi*10 * t);\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(n*T, x1, <span style=\"color: #A020F0\">'o'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/nyquist_frequency_06.png\"> <p>That's the heart of the \"problem\" of aliasing. Because the sampling frequency was too low, a high-frequency cosine looked\r\n      like a low-frequency cosine after we sampled it.\r\n   <\/p>\r\n   <p>Later on in <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/fourier-transforms\/\">this series<\/a> I plan to come back again to the concept of aliasing and show some examples of how it looks in an image.\r\n   <\/p>\r\n   <p>OK, now I'll start working on the upcoming discrete Fourier transform (DFT) post.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_fd156d59ed514acb9b2a81d2b1669f34() {\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='fd156d59ed514acb9b2a81d2b1669f34 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fd156d59ed514acb9b2a81d2b1669f34';\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        author = '';\r\n        copyright = 'Copyright 2010 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 author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\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      \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_fd156d59ed514acb9b2a81d2b1669f34()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.9<br><\/p>\r\n<\/div>\r\n<!--\r\nfd156d59ed514acb9b2a81d2b1669f34 ##### SOURCE BEGIN #####\r\n%%\r\n% One challenge of teaching Fourier transform concepts is that each concept can be\r\n% (and is) interpreted and explained in many different ways. It isn't always\r\n% obvious how the different explanations for the same concepts are connected.\r\n%\r\n% For example, in my <https:\/\/blogs.mathworks.com\/steve\/2010\/02\/22\/aliasing-and-the-discrete-time-fourier-transform\/ \r\n% last Fourier transform post> I talked about _aliasing_.  I\r\n% said that if you sample a continuous-time cosine at a sampling frequency\r\n% $F_s$, then you can't distinguish between a cosine with frequency $\\alpha$ and\r\n% a cosine with frequency $F_s k \\pm \\alpha$.\r\n%\r\n% <https:\/\/blogs.mathworks.com\/steve\/2010\/02\/22\/aliasing-and-the-discrete-time-fourier-transform\/#comment-22765 \r\n% In response>, Dave S. wanted to know how this related to what he learned about\r\n% aliasing: that aliasing is a \"problem that occurs when the sampling rate of\r\n% your discrete signal is not at least twice the [...] highest existing\r\n% frequency [in the continuous-time signal].\"\r\n%\r\n% I know I promised to introduce the discrete Fourier transform next, but I'd\r\n% like to change my mind and try to answer Dave's question instead.  There are\r\n% two key pieces of the question to address: What is the nature of the\r\n% \"problem,\" and what is the significance of \"twice the highest frequency\"?\r\n%\r\n% I thought about drawing some new frequency-domain diagrams showing overlapping\r\n% triangles like you'd see in\r\n% <http:\/\/www.amazon.com\/Discrete-Time-Signal-Processing-Prentice-Hall\/dp\/0131988425\/ref=sr_1_2?ie=UTF8&s=books&qid=1267639933&sr=8-2 Oppenheim and Schafer>, but then I thought it\r\n% might be better to just continue the <https:\/\/blogs.mathworks.com\/steve\/2010\/02\/22\/aliasing-and-the-discrete-time-fourier-transform\/ \r\n% sampled cosine example from last time>.\r\n%\r\n% Let's start with a continuous-time cosine signal at 60 Hz.\r\n\r\nf = 60;  % Hz\r\ntmin = -0.05;\r\ntmax = 0.05;\r\nt = linspace(tmin, tmax, 400);\r\nx_c = cos(2*pi*f * t);\r\nplot(t,x_c)\r\nxlabel('t (seconds)')\r\n\r\n%%\r\n% Let's sample $x_c$ with a sampling frequency of 800 Hz.\r\nT = 1\/800;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nhold on\r\nplot(n*T,x1,'.')\r\nhold off\r\n\r\n%%\r\n% The sampling frequency of 800 Hz is well above 120 Hz, which is twice the\r\n% frequency of the cosine. And you can see that the samples are clearly\r\n% capturing the oscillation of the continuous-time cosine.\r\n%\r\n% Let's try a lower sampling frequency.\r\n\r\nT = 1\/400;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold on\r\nplot(n*T, x1, '.')\r\nhold off\r\n\r\n%%\r\n% The samples above are still adequately capturing the shape of the cosine.  Now\r\n% let's drop the sampling frequency down to exactly 120 Hz, twice the frequency\r\n% of the 60 Hz cosine. (And I'll switch to using circle markers to make the\r\n% samples easier to see.)\r\n\r\nT = 1\/120;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold on\r\nplot(n*T, x1, 'o')\r\nhold off\r\n\r\n%%\r\n% See how the samples jump back and forth between 1 and -1? And how they capture\r\n% only the extremes of each period of the cosine oscillation?  This is the\r\n% significance of \"twice the highest frequency of the signal\" value for sampling\r\n% frequency.  If you'll allow a \"hand-wavy\" explanation here, I'll say that this\r\n% sampling frequency of 120 Hz is just enough to capture the cosine oscillation.\r\n%\r\n% But aliasing is worse that \"just\" losing information.  When we drop the\r\n% sampling frequency too low, the samples start to look increasingly like they\r\n% came from a *different, lower-frequency signal*.\r\n%\r\n% Let's try 70 Hz.\r\n\r\nT = 1\/70;\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold on\r\nplot(n*T, x1, 'o')\r\nhold off\r\n\r\n%%\r\n% The samples above look like they actually could have come from a *10 Hz cosine\r\n% signal*, instead of a 60 Hz cosine signal.  Take a look:\r\n\r\nT = 1\/70;\r\nx_c = cos(2*pi*10 * t);\r\nnmin = ceil(tmin \/ T);\r\nnmax = floor(tmax \/ T);\r\nn = nmin:nmax;\r\nx1 = cos(2*pi*f * n*T);\r\nplot(t, x_c)\r\nhold on\r\nplot(n*T, x1, 'o')\r\nhold off\r\n\r\n%%\r\n% That's the heart of the \"problem\" of aliasing. Because the sampling frequency\r\n% was too low, a high-frequency cosine looked like a low-frequency cosine after\r\n% we sampled it.\r\n%\r\n% Later on in <https:\/\/blogs.mathworks.com\/steve\/category\/fourier-transforms\/ \r\n% this series> I plan to come back again to the concept of aliasing\r\n% and show some examples of how it looks in an image.\r\n%\r\n% OK, now I'll start working on the upcoming discrete Fourier transform (DFT) post.\r\n\r\n##### SOURCE END ##### fd156d59ed514acb9b2a81d2b1669f34\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   One challenge of teaching Fourier transform concepts is that each concept can be (and is) interpreted and explained in many\r\n      different ways. It isn't always obvious how the different... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/03\/aliasing-and-a-sampled-cosine-signal\/\">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":[539,74,252,90,32,532,68,94],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/316"}],"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=316"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":3667,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/316\/revisions\/3667"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}