<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_ym19_1_1476886770750_4201">When implemented in C, which approach takes the least amount of time <br></div><div dir="ltr">to read, reason about, and fully comprehend?<br></div><div><br></div><div>-Jonathan<br></div><div id="yui_3_16_0_ym19_1_1476886770750_4096"><span></span><br></div><div id="yui_3_16_0_ym19_1_1476886770750_4224"><br></div><div class="qtdSeparateBR"><br><br></div><div style="display: block;" id="yui_3_16_0_ym19_1_1476886770750_4104" class="yahoo_quoted"> <div id="yui_3_16_0_ym19_1_1476886770750_4103" style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div id="yui_3_16_0_ym19_1_1476886770750_4102" style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div id="yui_3_16_0_ym19_1_1476886770750_4101" dir="ltr"> <font id="yui_3_16_0_ym19_1_1476886770750_4106" face="Arial" size="2"> <hr id="yui_3_16_0_ym19_1_1476886770750_4140" size="1"> <b><span style="font-weight:bold;">From:</span></b> katja <katjavetter@gmail.com><br> <b><span style="font-weight: bold;">To:</span></b> "pd-list@lists.iem.at" <pd-list@lists.iem.at> <br> <b><span style="font-weight: bold;">Sent:</span></b> Wednesday, October 19, 2016 9:06 AM<br> <b><span style="font-weight: bold;">Subject:</span></b> [PD] efficient approximation of trig functions for hi pass formula (was: could vanilla borrow iemlib's hi pass filter recipe?)<br> </font> </div> <div id="yui_3_16_0_ym19_1_1476886770750_4225" class="y_msg_container"><br>Changing the thread title to reflect the new approach. Extract of the<br>original thread;<br><br>- I suggested using iemlib's hi pass filter recipe to improve<br>frequency response of [hip~]<br>- Christof Ressi pointed to formula in<br><a href="http://www.arpchord.com/pdf/coeffs_first_order_filters_0p1.pdf" target="_blank">http://www.arpchord.com/pdf/coeffs_first_order_filters_0p1.pdf</a><br>- this formula calculates feedback coefficient k = (1 - sin(a)) /<br>cos(a) where a = 2 * pi * fc / SR<br>- the filter implementation is y[n] = (x[n] - x[n-1]) * (1 + k) / 2<br>+ k * y[n-1]<br>- following convention in d_filter.c (and pd tilde classes in<br>general), trig functions should best be approximated<br>- Cyrille provided libre office linear regression result for (1-sin(x))/cos(x)<br><br>Thanks for the useful infos and discussion. My 'math coach' suggested<br>using odd powers of -(x-pi/2) in an approximation polynomial for<br>(1-sin(x))/cos(x). The best accuracy/performance balance I could get<br>is with this 5th degree polynomial:<br><br>(-(x-pi/2))*0.4908 - (x-pi/2)^3*0.04575 - (x-pi/2)^5*0.00541<br><br>Using this approximation in the filter formula, response at cutoff<br>frequency is -3 dB with +/-0.06 dB accuracy in the required range 0 <<br>x < pi. It can be efficiently implemented in C, analogous to an<br>approximation Miller uses in [bp~]. So that is what I'll try next.<br><br>Attached patch hip~-models.pd illustrates and compares filter recipes<br>using vanilla objects:<br><br>- current implementation, most efficient, accuracy +/- 3 dB<br>- implementation with trig functions, least efficient, accuracy +/- 0.01 dB<br>- implementation with approximation for trig functions, efficient,<br>accuracy +/- 0.06 dB<br><br>A note on efficiency: coefficients in [hip~] are only recalculated<br>when cutoff frequency is changed. How important is performance for a<br>function rarely called? I'm much aware of the motto 'never optimize<br>early', yet I spent much time on finding a fast approximation, for<br>several reasons: it's a nice math challenge, instructive for cases<br>where performance matters more, and I want to respect Miller's code<br>efficiency when proposing a change. Today pd is even deployed on<br>embedded devices so the frugal coding approach is still relevant.<br>After 20 years.<br><br>Katja<br><br><br>On Tue, Oct 18, 2016 at 10:28 AM, cyrille henry <<a ymailto="mailto:ch@chnry.net" href="mailto:ch@chnry.net">ch@chnry.net</a>> wrote:<br>><br>><br>> Le 18/10/2016 à 00:47, katja a écrit :<br>>><br>>> The filter recipe that Christof pointed to was easy to plug into the C<br>>> code of [hip~] and works perfectly. But when looking further in<br>>> d_filter.c I came across an approximation function 'sigbp_qcos()' used<br>>> in the bandpass filter. It made me realize once more how passionate<br>>> Miller is about efficiency. I'm not going to make a fool of myself by<br>>> submitting a 'fix' using two trig functions to calculate a filter<br>>> coefficient when a simple approximation could do the job. So that is<br>>> what I'm now looking into, with help of a math friend: an efficient<br>>> polynomial approximation for (1-sin(x))/cos(x).<br>><br>> according to libre office linear regression, for x between 0 and Pi,<br>> (1-sin(x))/cos(x) is about :<br>> -0.057255x³ + 0.27018x² - 0.9157x + 0.99344<br>><br>> the calc is in attachment, if you want to tune the input source or<br>> precision.<br>> cheers<br>> c<br>_______________________________________________<br><a ymailto="mailto:Pd-list@lists.iem.at" href="mailto:Pd-list@lists.iem.at">Pd-list@lists.iem.at</a> mailing list<br>UNSUBSCRIBE and account-management -> <a href="https://lists.puredata.info/listinfo/pd-list" target="_blank">https://lists.puredata.info/listinfo/pd-list</a><br><br><br></div> </div> </div> </div></div></body></html>