implementing a single pole hi pass filter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

implementing a single pole hi pass filter

David Akbari
Hi List,

I've been trying to make a single pole high pass filter as a UDO. For
some reason I can't get it to sound correct. Here's my code:

        opcode tonehp, a, ak
setksmps 1
#define PI #4.*taninv(1)#

ain,khp xin

kc = 2.0 + cos((2*$PI.)/sr) * khp
kb1 = kc - sqrt((kc*kc) - 1.0)
ka0 = 1.0 - kb1

alout delay1 ain
aout = (ka0 * ain) - (kb1 * alout)
        xout aout
                endop

Any help appreciated!


-David
Reply | Threaded
Open this post in threaded view
|

Re: implementing a single pole hi pass filter

Istvan Varga
Here is a fixed and somewhat optimized version:

giPI    =  4 * taninv(1)
gitpdsr =  2 * giPI / sr

         opcode tonehp, a, ak
         setksmps 1
kprvhp  init -1
ain, khp xin
         if (khp == kprvhp) kgoto nochange
kprvhp  =  khp
kc      =  2.0 - cos(gitpdsr * khp)
kb1     =  kc - sqrt((kc * kc) - 1.0)
nochange:
alout   init 0
aindiff diff ain
alout   =  kb1 * (aindiff + alout)
         xout alout
         endop
--
Send bugs reports to this list.
To unsubscribe, send email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: implementing a single pole hi pass filter

David Akbari
Thanks a lot, Istvan!

Your attentiveness to this list is nothing less than extraordinary.


> Here is a fixed and somewhat optimized version:
>
> giPI    =  4 * taninv(1)
> gitpdsr =  2 * giPI / sr

How are the global initialization variables different from macro
definitions? In what ways are they more efficient?


-David

--
Send bugs reports to this list.
To unsubscribe, send email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: implementing a single pole hi pass filter

Istvan Varga
David Akbari wrote:

> How are the global initialization variables different from macro
> definitions? In what ways are they more efficient?

There is only a small improvement in efficiency as compared to macros
(with a macro, the expression would be recalculated every time it is
used, but only at i-time so the difference is not significant); however,
I think it is more elegant to not use macros when not necessary.
The real optimizations are skipping the calculation of filter coefficients
when the cutoff frequency does not change (for example, with a global
ksmps value of 20, khp would be the same for at least every 20 control
periods in the UDO), and the use of fewer opcodes/operations at performance
time in the feedback loop.
--
Send bugs reports to this list.
To unsubscribe, send email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: implementing a single pole hi pass filter

Iain Duncan

>> How are the global initialization variables different from macro
>> definitions? In what ways are they more efficient?
>
>
> There is only a small improvement in efficiency as compared to macros
> (with a macro, the expression would be recalculated every time it is
> used, but only at i-time so the difference is not significant); however,
> I think it is more elegant to not use macros when not necessary.
> The real optimizations are skipping the calculation of filter coefficients
> when the cutoff frequency does not change (for example, with a global
> ksmps value of 20, khp would be the same for at least every 20 control
> periods in the UDO), and the use of fewer opcodes/operations at performance
> time in the feedback loop.

Macro errors in csound are also a whole bunch more difficult to debug!

Iain
--
Send bugs reports to this list.
To unsubscribe, send email to [hidden email]