{"id":116,"date":"2019-10-01T17:13:43","date_gmt":"2019-10-01T17:13:43","guid":{"rendered":"https:\/\/synthnotes.ucsd.edu\/wp7\/?p=116"},"modified":"2019-10-01T23:41:45","modified_gmt":"2019-10-01T23:41:45","slug":"complex-sine-calculation","status":"publish","type":"post","link":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/2019\/10\/01\/complex-sine-calculation\/","title":{"rendered":"Complex Sine Calculation"},"content":{"rendered":"<h1>Complex Sinusoids<\/h1>\n<p>Complex sinusoids are two-dimensional signals whose value at any instant can be specified by a complex number; that is, a number with <em>real<\/em>\u00a0part and an <em>imaginary<\/em>\u00a0part. These two parts are in what is known as <em>phase quadrature<\/em>\u00a0with one another; i.e. the imaginary part is pi\/2 radians out of phase with the real part (1\/4 of a cycle). That is why the components of the complex signal are sometimes known as <em>in-phase<\/em>\u00a0and <em>quadrature phase<\/em>.<\/p>\n<p>A complex sinusoid is defined by the following mathematical representations:<\/p>\n<p>Cartesian Form:<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> x(t) \\triangleq Acos(\\omega t + \\phi) + jAsin(\\omega t + \\phi)<\/span><\/p>\n<p>Polar Form:<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> (t) = Ae^{j(\\omega t + \\phi)} <\/span><\/p>\n<p>where <em>A<\/em>\u00a0is amplitude, <em>\u03c9<\/em>\u00a0is the angular frequency in radians per second (the frequency in Hertz scaled by 2\u03c0; <em>2\u03c0f<\/em>), <em>\u03d5<\/em>\u00a0is the initial phase offset in radians, and <em>t<\/em>\u00a0is time in seconds. The cosine portion of the sinusoid is the real component and the sine portion is the imaginary (phase quadrature) component. (In engineering, the variable <em>j<\/em>\u00a0is typically denoted to mean the square root of negative 1, whereas pure math often uses the variable <em>i<\/em>\u00a0to mean the same thing).<\/p>\n<h2>Projection<\/h2>\n<p>Recall the Pythagorean Identity:<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> cos^2(\\phi) + sin^2(\\phi) = 1 <\/span><\/p>\n<p>Reconfiguring the Cartesian form of a complex sinusoid with this in mind, we get:<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> |x(t)| \\triangleq \\sqrt{re^2\\{x(t)\\} + im\\{x(t)\\}} \\equiv A <\/span><\/p>\n<p>In other words, a complex sinusoid has a constant complex magnitude (<em>A<\/em>). Since this is true, the projection of a complex signal (in complex form) onto the complex plane must lie on a circle; i.e. the radius (magnitude) remains the same while the angle changes). Recall the animation in the beginning of this section:<\/p>\n<p><a href=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/09\/Unfasor.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87\" src=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/09\/Unfasor.gif\" alt=\"\" width=\"219\" height=\"432\" \/><\/a><\/p>\n<p>This animation shows the counter-clockwise motion of the real (cosine) component of the complex sinusoid projected onto the complex plane. We can also project the complex phasor onto orthogonal axes:<\/p>\n<p><a href=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/circle_cos_sin.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-170\" src=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/circle_cos_sin.gif\" alt=\"\" width=\"600\" height=\"360\" \/><\/a><\/p>\n<p>The green circle in the bottom right corner is the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unit_circle\">unit circle<\/a>\u00a0in the complex plane. We can rewrite the trigonometric form of the equation into polar form:<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> cos\\theta + jsin\\theta = re^{j\\theta} <\/span><\/p>\n<p>where <em>j<\/em>\u00a0is the square root of negative 1, <em>r<\/em> is the length (or magnitude), <em>\u03b8<\/em>\u00a0is the angle in radians, and <em>e<\/em>\u00a0is Euler&#8217;s number.<\/p>\n<p>Relabeling a sine wave:<\/p>\n<p><a href=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/sine_radians.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-171\" src=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/sine_radians.png\" alt=\"\" width=\"1220\" height=\"520\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/sine_radians.png 1220w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/sine_radians-300x128.png 300w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/sine_radians-768x327.png 768w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/sine_radians-1024x436.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/sine_radians-624x266.png 624w\" sizes=\"auto, (max-width: 1220px) 100vw, 1220px\" \/><\/a><\/p>\n<h2>Advantages<\/h2>\n<p>Representing a sinewave in complex form has several distinct advantages:<\/p>\n<p>Instantaneous Frequency:<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> f = \\omega + \\frac{d}{dt}\\phi(t) <\/span><\/p>\n<p>Instantaneous Phase:<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> \\angle x(t) = \\omega t + \\phi <\/span><\/p>\n<p>Instantaneous Magnitude (Amplitude):<br \/>\n<span class=\"wp-katex-eq\" data-display=\"false\"> A = \\sqrt{re^2\\{x(t)\\} + im\\{x(t)\\}} <\/span><\/p>\n<h3>Using complex sinusoid generation in an oscillator<\/h3>\n<p>This method of calculation can be used for a fairly efficient sine and cosine wave generator. The only CPU intensive calculation is the calculation of the complex phase factor. If using a limited set of frequencies, the complex phase could be pre-calculated in a table.<\/p>\n<div class=\"entry-content\">\n<pre>double cosZ, sinZ;\n\nSinComplex(float freq, float *output, long samplesPerBlock)\n{\n\tlong sample;\n\tfloat freqAngle, angleReal, angleImag;\n\t\n\tfreqAngle = twoPi * freq;\n        \/\/ calculate the phose angle for given frequency\n\tangleReal = cos(freqAngle);\n\tangleImag = sin(freqAngle);\n\tfor(sample = 0; sample&lt;samplesPerBlock; sample++) \t\n        { \t\t\n            \/\/ complex multiply by phase angle for sin output\n            *(output+sample) = angleReal * sinZ - angleImag * cosZ;\n            \/\/ complex multiply by phase angle for cos output\n            cosZ = angleReal * cosZ + angleImag * sinZ; \n            sinZ = *(output+sample); \n            \/\/ correction for accumulated computation inaccuracies\n            if(sinZ &lt; -1.0) sinZ = -1.0;\n            if(sinZ &gt; 1.0f) sinZ = 1.0f; \n        } \n}<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Complex Sinusoids Complex sinusoids are two-dimensional signals whose value at any instant can be specified by a complex number; that is, a number with real\u00a0part and an imaginary\u00a0part. These two parts are in what is known as phase quadrature\u00a0with one another; i.e. the imaginary part is pi\/2 radians out of phase with the real part [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":5,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":479,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions\/479"}],"wp:attachment":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}