{"id":91,"date":"2019-10-01T00:31:57","date_gmt":"2019-10-01T00:31:57","guid":{"rendered":"https:\/\/synthnotes.ucsd.edu\/wp7\/?p=91"},"modified":"2020-08-18T21:49:32","modified_gmt":"2020-08-18T21:49:32","slug":"the-phasor","status":"publish","type":"post","link":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/2019\/10\/01\/the-phasor\/","title":{"rendered":"The Phasor"},"content":{"rendered":"<h1>The Phasor<\/h1>\n<p>The phasor is perhaps the most simple and fundamental of oscillators. It takes the shape of a sawtooth wave but has two important differences:<\/p>\n<p>1. It ascends from 0 to 1, as opposed from -1 to +1.<br \/>\n2. It is _not_ bandlimited (in discrete time this matters).<\/p>\n<span class=\"wp-katex-eq\" data-display=\"false\"> x{phasor}(t) = \\frac{t}{p} \\text{mod } 1<\/span>\n<p>where <em>p<\/em> is the period in Hertz and <em>t<\/em> is time.<\/p>\n<pre>sampleRate = 128;    \/\/ the samplerate in Hz\nfreq = 1;            \/\/ the frequency in Hz\nphasePerSecond = freq;  \/\/ the change in phase per unit of time; \n                        \/\/ i.e. the frequency when time is seconds\nphasePerSample = freq\/sampleRate;   \/\/ the amount of change in phase per \n                                    \/\/ sample for a given frequency at a \n                                    \/\/ given samplerate\nOsc::Osc()\n{\n    phase = 0.0;\n    phasePerSample = 0.0;\n}\n\nvoid Osc::Phasor(float frequency, float *output, long samplesPerBlock)\n{\n    long sample;\n    \/\/ calculate for each sample in a block\n    for(sample = 0; sample&lt;samplesPerBlock; sample++)\n    {\n        phasePerSample = frequency\/sampleRate; \/\/ get the phase increment for this sample\n        *(output+sample) = phase; \/\/ calculate the output for this sample\n        phase = phase + phasePerSample; \/\/ increment the phase\n    }\n}<\/pre>\n<p>Important here is the relationship between `phasePerSample` and `freq`: they are, in fact, representations of the same thing. With frequency, we give the total number of rotations (oscillations) in a second while phase per sample is the rate of change at the sampling period. If we want to give an oscillator a frequency, we will need to calculate the change in phase per sample; i.e. `phasePerSample`.<\/p>\n<p>Using a samplerate of 128Hz, the first 512 samples of a 1Hz phasor are plotted below:<\/p>\n<p><a href=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/phasor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-92\" src=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/phasor.png\" alt=\"phasor\" width=\"2542\" height=\"570\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/phasor.png 2542w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/phasor-300x67.png 300w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/phasor-1024x230.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/phasor-624x140.png 624w\" sizes=\"auto, (max-width: 2542px) 100vw, 2542px\" \/><\/a><\/p>\n<p>Notice how the waveform ascends from 0 to 1 as noted above. This makes it extraordinarily useful in waveshaping, indexing into tables, etc. As a simplified block diagram, the phasor looks like this:<\/p>\n<p><a href=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/process-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-147\" src=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/process-1.png\" alt=\"\" width=\"417\" height=\"212\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/process-1.png 417w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/10\/process-1-300x153.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/a><\/p>\n<p>`phaseInc(1)` is the phase increment for a 1Hz signal at a 128Hz samplerate. 1\/128 = 0.0078125.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Phasor The phasor is perhaps the most simple and fundamental of oscillators. It takes the shape of a sawtooth wave but has two important differences: 1. It ascends from 0 to 1, as opposed from -1 to +1. 2. It is _not_ bandlimited (in discrete time this matters). where p is the period in [&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-91","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\/91","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=91"}],"version-history":[{"count":8,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}