{"id":187,"date":"2019-10-14T21:44:37","date_gmt":"2019-10-14T21:44:37","guid":{"rendered":"https:\/\/synthnotes.ucsd.edu\/wp4\/?p=187"},"modified":"2019-11-10T19:43:06","modified_gmt":"2019-11-10T19:43:06","slug":"first-order-allpass-filter","status":"publish","type":"post","link":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/2019\/10\/14\/first-order-allpass-filter\/","title":{"rendered":"First-Order Allpass Filter"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction <\/h3>\n\n\n\n<p>An allpass filter passes a signal with the same gain response for all frequencies. Without affecting the gain response, an allpass filter changes the frequency phase. This response can be graphed from -\u03c0 to \u03c0 over the audio frequency range.<\/p>\n\n\n\n<p>The allpass filter can be used as a building block in filter construction, using phase cancellation to form standard lowpass, highpass, bandpass, and notch filters; as well as typical effects such as chorusing, flanging, and phasing. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">First Order<\/h4>\n\n\n\n<p>A first order allpass is called this because it uses a one sample delay in its implementation. <\/p>\n\n\n\n<span class=\"wp-katex-eq\" data-display=\"false\"> y[n] = c(x[n]) + x[n-1] - c(y[n-1]) <\/span>\n\n\n\n<p><\/p>\n\n\n\n<p>The coefficient <strong>c<\/strong> is  calculated from the tangent of the frequency, <strong>tf<\/strong>, <strong>fc<\/strong> is the cutoff frequency, and <strong>sr<\/strong> is the sampling rate: <\/p>\n\n\n\n<span class=\"wp-katex-eq\" data-display=\"false\">  \\begin{aligned}&amp; tf = tan\\bigg(\\pi \\frac{f_c}{sr} \\bigg) \\\\ &amp; c = \\frac{tf-1}{tf+1} \\end{aligned} <\/span>\n\n\n\n<p>You might notice that a frequency of 0 will result in a coefficient of -1 and a frequency at half the sample rate (the Nyquist frequency) will result in an error, as tan(\u03c0\/2) is \u221e. Because of this, the frequency should be limited to less than half the sample rate. The coefficient approaches 1 as the frequency approaches the Nyquist frequency.<\/p>\n\n\n\n<p>The first order allpass filter has a phase shift of 0 at 0 Hz, \u03c0 at the Nyquist frequency and \u03c0\/2 at the cutoff frequency. The following C code can implement this filter. Note that double floating point variables are used for feedback terms. This is because mathematical error can build up in a feedback network, and using doubles will minimize this error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">double in1 = 0; \/\/ delayed sample \ndouble out = 0; \/\/ keep track of the last output \n\nfoap(float *input, float *output, long samples, float cutoff) \n{   \n    double tf = tan(PI * (cutoff\/SAMPLERATE)); \/\/ tangent frequency   \n    double c = (tf - 1.0)\/(tf + 1.0); \/\/ coefficient     \n    for(int i = 0; i &lt; samples; i++) \n    {  \n        float sample = *(input+i);   \n        out = (c*sample) + in1 - (c * out); \n        in1 = sample; \/\/ remember input     \n        *(output+i) = out; \/\/ output   \n    }\n}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction An allpass filter passes a signal with the same gain response for all frequencies. Without affecting the gain response, an allpass filter changes the frequency phase. This response can be graphed from -\u03c0 to \u03c0 over the audio frequency range. The allpass filter can be used as a building block in filter construction, using [&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-187","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\/187","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=187"}],"version-history":[{"count":10,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":482,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions\/482"}],"wp:attachment":[{"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/synthnotes.ucsd.edu\/wp4\/index.php\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}