{"id":393,"date":"2019-11-09T23:26:25","date_gmt":"2019-11-09T23:26:25","guid":{"rendered":"https:\/\/synthnotes.ucsd.edu\/wp7\/?p=393"},"modified":"2019-11-17T21:25:16","modified_gmt":"2019-11-17T21:25:16","slug":"second-order-allpass-filter","status":"publish","type":"post","link":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/2019\/11\/09\/second-order-allpass-filter\/","title":{"rendered":"Second-Order Allpass Filter"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the section on phase shifters, 2 first order allpass filters are cascaded to obtain a phase shift of -\u03c0 in the middle of the frequency range. The -\u03c0 phase shift makes it possible to implement a band reject filter by adding the input to the phase shifted allpass output or a band pass by subtracting the allpass output from the input. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are many ways to formulate a second order allpass, but in this section we will be using a filter developed by professor fred harris at SDSU for an efficient parametric equalizer used in an early <a href=\"http:\/\/www.eb100s.com\">hybrid guitar amp<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"wp-katex-eq\" data-display=\"false\"> y[n] = -c(x[n]) + (d - dc)x[n-1] + x[n-2] -  (d - dc)(y[n-1]) + c(y[n-2]) <\/span><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Above is the difference equation for this allpass. The coefficients are <em>c<\/em> and <em>d<\/em>. The input parameters used are frequency and bandwidth. Frequency is the -\u03c0 phase shift point and bandwidth is the frequency distance between the -\u03c0\/2 and -3\u03c0\/2 points.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The coefficients are calculated from frequency (<em>f<\/em>) and bandwidth (<em>bw<\/em>) by:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"wp-katex-eq\" data-display=\"false\">  \\begin{aligned}&amp; d = -cos\\bigg(2\\pi \\frac{f}{sr} \\bigg) \\\\ &amp; tf = tan\\bigg(\\pi \\frac{bw}{sr} \\bigg) \\\\ &amp; c = \\frac{tf-1}{tf+1} \\end{aligned} <\/span><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Notice the <em>d<\/em> is completely dependent on <em>f<\/em> and <em>c<\/em> is completely dependent on <em>bw<\/em>. The decoupling of coefficients will allow easy optimization of this filter. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"402\" src=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-09-at-3.25.58-PM-1024x402.png\" alt=\"\" class=\"wp-image-405\" srcset=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-09-at-3.25.58-PM-1024x402.png 1024w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-09-at-3.25.58-PM-300x118.png 300w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-09-at-3.25.58-PM-768x301.png 768w, https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-09-at-3.25.58-PM-624x245.png 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>phase response of fred harris 2nd order allpass, f=2500, bw=1000<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The plot above shows that this filter reaches -\u03c0\/2 at 2000Hz, -\u03c0 at 2500Hz and -3\u03c0\/2 at 3000Hz. The C implementation is straightforward and can be derived directly from the difference equation above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#define SAMPLERATE 44100.0\n#define PI 3.141592653589793\ndouble x0 = 0.0; \/\/ input\ndouble x1 = 0.0; \/\/ delayed input\ndouble x2 = 0.0; \/\/ delayed input\ndouble y1 = 0.0; \/\/ delayed output\ndouble y2 = 0.0; \/\/ delayed output\n\nsoap(float *input, float *output, long samples, float cutoff, float bw) \n{   \n    double d = -cos(2.0 * PI * (cutoff\/SAMPLERATE));\n    double tf = tan(PI * (bw\/SAMPLERATE)); \/\/ tangent bandwidth   \n    double c = (tf - 1.0)\/(tf + 1.0); \/\/ coefficient     \n    for(int i = 0; i &lt; samples; i++) \n    {  \n        x0 = *(input+i);   \n        *(output+i) = -c*x0 + (d - d*c)*x1 + x2 - (d - d*c)y1 + c*y2;\n        \/\/ move samples in delay for next sample\n        x2 = x1;\n        x1 = x0;\n        y2 = y1;\n        y1 = *(output+i);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/synthnotes.ucsd.edu\/wp7\/wp-content\/uploads\/2019\/11\/bpf.wav\"><\/audio><figcaption>swept band pass filter using above code. narrow band width: 1\/10th the frequency<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In the section on phase shifters, 2 first order allpass filters are cascaded to obtain a phase shift of -\u03c0 in the middle of the frequency range. The -\u03c0 phase shift makes it possible to implement a band reject filter by adding the input to the phase shifted allpass output or a band pass [&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-393","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\/393","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=393"}],"version-history":[{"count":17,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/posts\/393\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp7\/index.php\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}