{"id":246,"date":"2019-10-22T22:01:06","date_gmt":"2019-10-22T22:01:06","guid":{"rendered":"https:\/\/synthnotes.ucsd.edu\/wp4\/?p=246"},"modified":"2019-10-31T19:25:36","modified_gmt":"2019-10-31T19:25:36","slug":"amplitude-detection","status":"publish","type":"post","link":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/2019\/10\/22\/amplitude-detection\/","title":{"rendered":"Amplitude Detection"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Envelope Follower <\/h2>\n\n\n\n<p>An envelope follower is used to detect signal level. This signal level can then be used in dynamics processing: gating, compression, expansion, automatic gain control, etc. Typically a combination of rectification and filtering is used to create an envelope from an audio signal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mean Detection <\/h3>\n\n\n\n<p>One can use the mean of a signal to follow the envelope. Essentially, a buffer is allocated and filled with samples from the signal after rectification. Then the average of the buffer is taken to be the envelope. Naturally, the larger the buffer the smoother the output but more delayed (in time) the result. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int envPosition;\nint envArraySize = 64;\nfloat envArrayTotal;\nfloat envArray[envArraySize];\n\nfloat EFGetMean(float sample) \n{   \n  \/\/ wrap the index pointer   \n  if(envPosition &gt;= envArraySize)     \n    envPosition = 0;  \n  if(envPosition &lt; 0)\n    envPosition = 0;   \n  \/\/ FIRST: rectify the input  \n  if(sample &lt; 0.0)  \n    sample = -1.0 * sample;   \n  \/\/ SECOND: add to array to calculate mean   \n  envArrayTotal = envArrayTotal - envArray[envPosition] + sample;      \n  envArray[envPosition] = sample;   \n  envPosition++;   \n  \/\/ THIRD: mean is total\/arraysize    \n  return(envArrayTotal\/(float)envArraySize); \n}<\/pre>\n\n\n\n<p>All: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_full-1-1024x415.png\" alt=\"\" class=\"wp-image-253\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_full-1-1024x415.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_full-1-300x122.png 300w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_full-1-768x311.png 768w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_full-1-624x253.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Zoomed: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_zoom-1-1024x415.png\" alt=\"\" class=\"wp-image-254\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_zoom-1-1024x415.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_zoom-1-300x122.png 300w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_zoom-1-768x311.png 768w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/mean_zoom-1-624x253.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">RMS Detection<\/h3>\n\n\n\n<p>One can also calculate the RMS amplitude in an envelope follower. Similar to using the mean, the RMS method also uses a buffer, the size of which determines the smoothness and responsiveness of the envelope follower. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int envPosition;\nint envArraySize = 64;\nfloat envArrayTotal;\nfloat envArray[envArraySize];\n\nfloat EFGetRMS(float sample) \n{ \n  float square, mean;  \n  \/\/ wrap the index pointer   \n  if(envPosition &gt;= envArraySize)     \n    envPosition = 0;  \n  if(envPosition &lt; 0)\n    envPosition = 0;   \n  \/\/ FIRST: square the new sample   \n  \/\/ square range 0.0 to 1.0   \n  square = sample * sample;      \n  \/\/ SECOND: add to array to calculate mean   \n  envArrayTotal = envArrayTotal - envArray[envPosition] + square;      \n  envArray[envPosition] = square;   \n  envPosition++;   \n  \/\/ THIRD: mean is total\/arraysize    \n  mean = envArrayTotal\/(float)envArraySize; \n  \/\/ FOURTH: RMS is square root of mean    \n  return(sqrt(mean)); \n} <\/pre>\n\n\n\n<p>All: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_full-1024x415.png\" alt=\"\" class=\"wp-image-255\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_full-1024x415.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_full-300x122.png 300w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_full-768x311.png 768w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_full-624x253.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Zoomed: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_zoom-1024x415.png\" alt=\"\" class=\"wp-image-256\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_zoom-1024x415.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_zoom-300x122.png 300w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_zoom-768x311.png 768w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/rms_zoom-624x253.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Attack-Release Envelope Follower<\/h3>\n\n\n\n<p>The attack-release method does not use a buffer but instead takes a moving weighted average of the peak amplitude and the sample. Here, though, one must pass the samplerate which, in part, governs the responsiveness of the follower. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">float EFGetPeakAttackRelase(float attackF, float releaseF, float sample) \n{  \n  float attackMultiplier;  \n  float releaseMultiplier;   \n  \/\/ rectify  if(sample &lt; 0.0)  \n    sample = -sample;   \n  \/\/ filter   \n  attackMultiplier = exp((-6.283185 * attackF)\/samplerate);     \n  releaseMultiplier = exp((-6.283185 * releaseF)\/samplerate);    \n  if(sample &gt; peak)  \n    peak = attackMultiplier * (peak - sample) + sample;  \n  else  \n    peak = releaseMultiplier * (peak - sample) + sample;    \n  return(peak); \n}<\/pre>\n\n\n\n<p>All: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_full-1024x415.png\" alt=\"\" class=\"wp-image-258\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_full-1024x415.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_full-300x122.png 300w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_full-768x311.png 768w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_full-624x253.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Zoomed: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_zoom-1024x415.png\" alt=\"\" class=\"wp-image-257\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_zoom-1024x415.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_zoom-300x122.png 300w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_zoom-768x311.png 768w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_zoom-624x253.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Envelope Followers Compared Aurally <\/h3>\n\n\n\n<p>Plotted below are three envelope followers: mean with a window of 16, RMS with a window of 16, and attack-release with time at 1ms (attack) and 200ms (release). To test the envelope followers, we can pass noise through our resulting envelope. Here are the three enveloped plotted below. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/compared_zoom-1024x415.png\" alt=\"\" class=\"wp-image-260\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/compared_zoom-1024x415.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/compared_zoom-300x122.png 300w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/compared_zoom-768x311.png 768w, https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/compared_zoom-624x253.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/78888__oasyntax__170-funky-drummer.wav\"><\/audio><figcaption>Funky Drummer Original<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/synthnotes.ucsd.edu\/wp4\/wp-content\/uploads\/2019\/10\/attackRelease_1000-5.wav\"><\/audio><figcaption>Funky Drummer extracted Attack-Release Envelope modulating noise<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Envelope Follower An envelope follower is used to detect signal level. This signal level can then be used in dynamics processing: gating, compression, expansion, automatic gain control, etc. Typically a combination of rectification and filtering is used to create an envelope from an audio signal. Mean Detection One can use the mean of a signal [&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-246","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/posts\/246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/comments?post=246"}],"version-history":[{"count":8,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/posts\/246\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}