Many, many mistakes later I got something that seems useful, given the low
implementation cost.

The structure is:

3 * hp/lp bandpass -> down modulation -> 1 pole lowpass (terminated)

-> phase -> unwrapped differential -> accumalator with wrapping ( to
integrate and synch )

-> differential -> log conversion -> 2nd inetegrator with hard reset -> Freq

Seems a bit odd with the unwrapping, diff and phasor afterwards just to
differentiate again,
but makes sense.

Still needs some value tweaking but can track fast and smooth enough
for music and voice input.

It's a trade off between speed / sensitivity and smooth waveform though,
and sometimes it hangs, or jumps... so its not perfect yet.

There is a "kick it"  button ment to make it recover but that doesnt work
well.

Could be a low cost gimmick for Eurorack, or a tuner.
Maybe someone finds it useful or wants to improve on it.

Have a nice weekend, all.


// frequency tracker test 17d
//
// work in progress

import("stdfaust.lib");

drywet = hslider("[1]DryWet[style:knob]", 1, 0, 1, 0.01) ;

// injects 261 Hz and restarts integrator
kick_it = button("[2]kick it");

// octave is an offset in the f to log conversion, not a selector
octave = hslider("[3]Ocatve[style:knob]", 0, -2, 2, 1);

// sense is scaling of log scale before / after integration
// higher value == more inertia and smoother output
sense = hslider("[3]Sens./Smmothn.[style:knob]", 2.5, 1, 4, 0.001);


freqtrack( x) = (_,x:prefilt:track)~_:_,(x:an.amp_follower_ar(1/7,
1/7):max(0))
        with {

                //these and the filters still need tweaking :
                fwmin = 27.5 / ma.SR;  // min. frequency
                fwmax = 3520 / ma.SR;  // max. frequency

                bwsemi = 2.5;   // half bandwitdh semitones
                //octave = 0;     // octave offset in log conversion
                //sense = 3;      // scaling of octave/pitch conversion
before integration
                //-----------------------------------------

                prefilt( fw, x) = x:bp36(fw):hetfilt(fw)
                    with{
                        bwff = 2^( bwsemi /12);
                        bp36(fw, x) = x <:_,lpw( (1/bwff)*fw):-:lpw(
bwff*fw )
                                        <:_,lpw( (1/bwff)*fw):-:lpw(
bwff*fw )
                                        <:_,lpw( (1/bwff)*fw):-:lpw(
bwff*fw );

                        hetfilt(fw, x) = x<:_*qsin(ph),_*qsin( ph -
0.25):lp1t, lp1t
                        with {
                                ph = mphasor(fw);
                                bw = bwff * fw  - fw;

                                del = fw:max(fwmin, min(fwmax, _)):1/_:
int;
                                lp1t = _<:_,_@del:_,_*tresp:-:lpw(bw);
                                a = ma.E^(- 2*ma.PI * bw);
                                tresp = a <:_,_:(1-_),_^del:*;
                                };
                        };

                track( x, y) = (x,y,_:wtrack)~_
                with{
                wtrack(x, y, fw) =
                        dx1uwr: mphasor <:_,_,_,_:
                        (_<:_,_':-:uwr1),_,_,_:
                        (w2o:_*sense),_,_,_:
                        acc(reset),ba.countup(4096, 1-reset),_:_/(_+1),_:
                        _',_:
                        ba.sAndH( 1- reset):
                        _/sense: o2w:
                        max(fwmin, min(fwmax,_)):
                        _, (262.63/ma.SR): select2( (kick_it<:_,_':-))


                        with  {
                                ph = y,x:norm:qatan2;
                                norm( x, y) =  x*x, y*y:+:sqrt:max(
1e-7):0.125/_<:_*y,_*x;
                                dx1 = ph - ph';
                                dx1uwr = dx1: uwr1;

                                reset = _<:_,_':-: abs: ba.if( _> 0.5, 0,
1):_+(kick_it<:_,_':-);

                                reff = fwmin/ 2 ;
                                refo = ma.SR / reff :ma.log2 ;
                                w2o( w) =
w/(reff):max(1e-16):ma.log2:_+refo :_+ octave + (kick_it<:_,_':-):_*1;

                                o2w( o) = o-refo:max( -1):2^_:_*(reff):
min( 0.25);
                                do2ff( do) = 2^do;

                                acc( rst) = (+ (_*rst) )~_ ;

                        };
                };
                // approx. atan2 , normalized output -0.5...0.5
                qatan2(y, x) = ba.if(x > 0, qatn(y, x), (ba.if(y > 0,
qatn(y, x)+0.5, qatn(y, x)-0.5))):
                                max(-0.5, min( 0.5, _))
                        with{
                                qatn(y, x) = y,(ba.if(x>0, x +  1e-14 ,
x-1e-14) ):
                                 _/ _: _<:_,_,_:_,(_*_<:_,_):_*(1-_ *(
(1/3-0.2*_)));
                            };

                // wraps between -0.5..0.5
                wrap(x) = x-( floor( x +0.5)) ;
                // unwraps delta phase
                uwr1(dw) =  ba.if( dw < -0.5, dw + dw' , dw)  : ba.if( dw >
0.5 , dw + dw'  , _);
                // phase difference
                diph(ph) = ph <:_,_':-:uwr1;
                // approx sine -0.5...0.5
                qsin(x) = x+(0.5): wrap <:
(abs:-(0.25):qsincurve),_:ma.copysign
                    with { qsincurve(x) = 1 - ( (x*x)<:
*(1.2253517*16),(_<:*:* (-3.60562732*16)):>_ );};
                // phasor / accumulator -0.5...0.5
                mphasor(fw) = (+ (fw : wrap) )~_;
                lpw( fw, x)= x *(1-lpp): fi.pole(lpp)
                    with {  lpp = ma.E^(- 2*ma.PI * fw); };


                // fw to pitch
                w2p( w) = w/(440/ma.SR):max(1e-14):ma.log2:_*12:_+69;
                // pitch to w
                p2w( p) =
p-69:_*(1/12):max(-69):2^_:_*(440/ma.SR);//:min(0.25);

};

testsaw(fw, amp) = os.saw2( fw * ma.SR ) : fi.svf.lp(700, 0.7)*amp;

process = +: _<:_*(1-drywet), (freqtrack<:testsaw) * drywet :>_ <:_,_ ;

On Tue, Sep 1, 2026 at 11:15 PM ga <[email protected]> wrote:

>
> Thanks. I am always totally surprised by such small FAUST code.
>
> Corellation would be precise, but you need to find the right candidate for
> the period,
> which seems tricky for some inputs. And the filters would not cost much
> processing, if it worked.
>
>
> I improved it somewhat by combining down modulation and lowpass with
> slight bandpass at the found frequency,
> and reduced filter orders in both parts, and adaptive bandwith.
>
> The idea was to have a shorter response.
>
> I also try to cancel the response of the lowpass filters after one period.
>
> So its:
>
> bandpass filter ( 1 pole hp / bp) -> down modulation, canceled lowpass
> filter ( 2 * 1 pole), -> frequency.
>
>
> Since the bandwitdh is modulated constantly, cancelling should not work at
> all, but it seemed better.
>
> It still has a tendency to stay too low, and for some reason now settles
> at ca. 94 Hz when there is no input,
> and drifts a too much with still too much lag.
>
> I would like to know why its 94 Hz to avoid or shift that up,
> but its probably just an arbitrary effect of the combined loop that cant
> be changed.
>
> It now borders on something that seems usable for some applications, but
> still isnt.
>
> I think I'll leave it at that for now until I have some more ideas,
> or more insight that it cant get better by principle.
>
> Although one thing to try might be to quantize the filters to note pitches,
> to reduce drifting by self modulation.
>
> I am attaching this version in case someone wants to try it
>
> // frequency tracker test 15
> //
>
> import("stdfaust.lib");
>
> drywet = (hslider("[1]DryWet[style:knob]", 0.71, 0, 1, 0.01)) ;
>
> bwsemi = hslider("[3]1/2 BW semitones[style:knob]", 1.5, 0.5, 6, 0.5) ;
>
> freqtrack( x) = (_,x:prefilt:track)~_:_,(x:an.amp_follower_ar(1/8,
> 1/8):max(0))
>         with {
>                 fwmin =  27.5 / ma.SR;  // min. frequency
>                 fwmax = 3520 / ma.SR;   // max. frequency
>
>                 prefilt( fw, x) = x:bp2(fw):hetfilt(fw)
>                     with{
>                         bwff = 2^( bwsemi /12);
>
>                         bp4(fw, x) = x:fi.svf.hp( (1/bwff)*fw * ma.SR,
> 0.5):fi.svf.lp( bwff*fw * ma.SR, 0.5);
>                         bp2(fw, x) = x <:_,lpw( (1/bwff)*fw):-:lpw(
> bwff*fw );
>                         bp1_2(fw, x) = x:fi.svf.hp( (1/bwff)*fw * ma.SR,
> 0.5):lpw( bwff*fw );
>
>                         hetfilt(fw, x) = x<:_*qsin(ph),_*qsin( ph -
> 0.25):lp2t, lp2t
>                         with {
>                                 ph = mphasor(fw);
>
>                                 bw = bwff * fw  - fw;
>                                 del = fw:max(fwmin, min(fwmax, _)):1/_:
> int;
>                                 lp2t =
> lpw(bw)<:_,_@del:_,_*tresp:-:lpw(bw)<:_,_@del:_,_*tresp:-;
>                                 tresp = ma.E^(- 2*ma.PI * fw)
> <:_,_:(1-_),_^del:*;
>                                 };
>                         };
>
>                 track( x, y) = (x,y,_:wtrack)~_
>                     with{
>                          wtrack(x, y, fw) = (y,x:norm:qatan2:diph),
> (fw:phacc:diph):
>                                     w2p,(w2p
> <:_,_):_-_,_:(integ(fw):integ(fw)),_:+:p2w:
>                                     max(fwmin, min(fwmax, _));
>
>                         norm( x, y) =  x * x, y * y: +: sqrt:max(
> 1e-7):0.125/_<: _ *x,_*y;
>                         // short averaging at 1/12 wavelength
>                         integ(fw, x) = x<:_,_@del:_,_*tresp:-:lpw(avat)
>                                 with{
>                                     del = fw:max(fwmin, min(fwmax,
> _)):(1/12)/_:int;
>                                     avat = 3 / ma.SR;
>                                     tresp = ma.E^(- 2*ma.PI * avat)
> <:_,_:1-_,_^del: * ;
>                                 };
>                         reff = 440;
>                         // fw to pitch  ref. 440 hz
>                         w2p( w) = w*(ma.SR/reff):max(1e-12):ma.log2:_+12;
>                         // pitch to w ref 440 hz
>                         p2w( po) =
> po*(1/12):2^_:_*(reff/ma.SR):min(ma.SR/4);
>                         // freq ratio a / b
>                     };
>
>                 // phase accumulation
>                 phacc( dph) = dph: mphasor;
>                 // approx. atan2 , normalized output -0.5...0.5
>                 qatan2(y, x) = ba.if(x > 0, qatn(y, x), (ba.if(y > 0 ,
> qatn(y, x) + 0.5, qatn(y, x) - 0.5))):
>                                 max(-0.5, min( 0.5, _))
>                         with{
>                                 qatn(y, x) = y,(ba.if(x>0, x +  1e-14 ,
> x-1e-14) ):
>                                  _/ _: _<:_,_,_:_,(_*_<:_,_):_*(1-_ *(
> (1/3-0.2*_)));
>                             };
>
>                 // wraps between -0.5..0.5
>                 wrap(x) = x-( floor( x +0.5)) ;
>                 // unwraps delta phase
>                 uwr1(dw) = dw:ba.if( dw< -0.25, dw- (dw'+dw) , _):  ba.if(
> dw> 0.25, dw + (dw'-dw), _);
>                 // phase difference
>                 diph(ph) = ph <:_,_':-:uwr1;
>                 // approx sine -0.5...0.5
>                 qsin(x) = x+(0.5): wrap <:
> (abs:-(0.25):qsincurve),_:ma.copysign
>                     with { qsincurve(x) = 1 - ( (x*x)<:
> *(1.2253517*16),(_<:*:* (-3.60562732*16)):>_ );};
>                 // phasor / accumulator -0.5...0.5
>                 mphasor(fw) = (+(fw) :(wrap))~_;
>                 lpw( fw, x)= x : fi.pole(lpp)*(1-lpp)
>                     with {  lpp = ma.E^(- 2*ma.PI * fw); };
>             };
>
> testsaw(fw, amp) = os.saw2( fw * ma.SR ) : fi.svf.lp(700, 0.707)*amp;
> //readout
> process = +: _<:_*(1-drywet), (freqtrack<:testsaw) * drywet :>_ <:_,_ ;
>
>
>
>
>
>
>
>
> On Tue, Sep 1, 2026 at 11:06 AM Julius Smith <[email protected]>
> wrote:
>
>> Many years ago I experimented with F0-following using autocorrelation
>> (like SuperCollider's Pitch function).  I got it to work in simple cases
>> but did not push it very far.  See attached.  I also attach one by Nicolas
>> Gravillon that I don't remember any details about.
>>
>> Cheers,
>> Julius
>>
>> On Mon, Aug 31, 2026 at 1:39 PM ga <[email protected]> wrote:
>>
>>> I didnt get it work with hilbert and pospass ( cause I dont understand
>>> either ),
>>> so I used allpass chains with coefficients by Oli Niemital ( I think).
>>>
>>> I also made a version with heterodyne filtering, but the response gets
>>> too long,
>>> it seems you need a cutoff < 3 hz (?)
>>>
>>> I got something that works, but its quite sluggish, and prefers lower
>>> notes.
>>>  BUT it works with polyphonic input, which is amazing to me
>>>
>>> Mabye someone has ideas how to improve it, the best place to start may be
>>> the integrations which are home baked.
>>>
>>> It works like this
>>> adaptive bandpass -> hilbertt transform > phase difference
>>> this goes to a refernce oscillator, whos pitch is compared and updated
>>> similar to phase locked loop but in pitch
>>>
>>>
>>>
>>> and sorry, I dont know how to send unformatted plain text from gmail nor
>>> how to respond so
>>> that the list topics sty intact 8 but I try)
>>>
>>>
>>> // frequency tracker test 9f
>>> //
>>>
>>> import("stdfaust.lib");
>>>
>>> drywet = (hslider("[1]DryWet[style:knob]", 0.71, 0, 1, 0.01)) ;
>>>
>>> filterS = (hslider("[2]filer samples[style:knob]", 20, 2, 48, 1)) ;
>>> filterF = (hslider("[3]filter F[style:knob]", 3, 0.5, 20, 0.5)) ;
>>>
>>> //readout = hbargraph("[5]f [style:numerical]", 0, 1);
>>>
>>> freqtrack(x) = 
>>> (_,x:prefilt:track)~_:_,(x@(filterS*2)<:_,_:*:an.amp_follower_ar(1/8,
>>> 1/8):max(0):sqrt)
>>>         with {
>>>                 fwmin =  27.5 / ma.SR;     // min. frequency
>>>                 fwmax = 3520 / ma.SR;   // max. frequency
>>>
>>>                 octave = 1;
>>>
>>>                 prefilt(fw, x) = x:fi.svf.hp( fwmin * ma.SR,
>>> 0.707):fi.svf.lp( fwmax * ma.SR, 0.707):
>>>                                 bfilt4(fw/octave):hil;
>>>
>>>                 bfilt4( fw, x) = x: fi.svf.lp(fw*ma.SR*0.7, 0.541):
>>> fi.svf.lp(fw*ma.SR*0.7, 1.307):
>>>                                 fi.svf.hp(fw*ma.SR*1.19, 0.541):
>>> fi.svf.hp(fw*ma.SR *1.19, 1.307);
>>>                 bfilt6( fw, x) = x<:
>>>                 fi.svf.hp(fw*ma.SR*1.19, 0.518): fi.svf.hp(fw*ma.SR
>>> *1.19, 0.707): fi.svf.hp(fw*ma.SR *1.19, 1.932):
>>>                 fi.svf.lp(fw*ma.SR*0.7, 0.518): fi.svf.lp(fw*ma.SR*0.7,
>>> 0.707): fi.svf.lp(fw*ma.SR*0.7, 1.932);
>>>
>>>                 hil(x) = x<:
>>>                         fi.allpassn1m(4, ( 0.6923878, 0.9360654323,
>>>  0.9882295227, 0.9987488453)),
>>>                         fi.allpassn1m(4, ( 0.4021921162, 0.8561710882,
>>>  0.9722909546, 0.9952884791));
>>>
>>>                 track( x, y) = (x,y,_:wtrack)~_
>>>                     with{
>>>                          wtrack(x, y, fw) = (y,x:norm:qatan2:diph),
>>> (fw:phacc:diph):
>>>                                     w2p,(w2p
>>> <:_,_):_-_,_:(integ(filterS):integ(filterS)),_:+:p2w:
>>>                                     max(fwmin, min(fwmax, _)):
>>>
>>>                                     integ(filterS):
>>>                                     _;
>>>
>>>                         norm( x, y) =  x * x, y * y: +: sqrt:max(
>>> 1e-7):0.5/_<: _ *x,_*y;
>>>                         integ(del, x) = x<:_,_@del:_,_*tresp(del):-:
>>> lpw(avat)
>>>                                 with{
>>>                                         avat = filterF/ ma.SR;
>>>                                         tresp( smp) = ma.E^(- 2*ma.PI *
>>> avat) <:_,_:1-_,_^del: * ;
>>>                                 };
>>>                         reff = 440;
>>>                         // fw to pitch  ref. 440 hz
>>>                         w2p( w) = w*(ma.SR/reff):max(1e-12):ma.
>>> log2:_+12;
>>>                         // pitch to w ref 440 hz
>>>                         p2w( po) = po*(1/12):2^_:_*(reff/ma.SR):
>>> min(ma.SR/4);
>>>                         // freq ratio a / b
>>>                         ffab( fw1, fw2) = (fw1:max( 0.0004)) / (fw2:max(
>>> 0.0004)) ;
>>>                         // delta pitch octave to freq. ratio
>>>                         dpo2ff( dpo) = dpo:min(12):2^_;
>>>                     };
>>>
>>>                 // phase accumulation
>>>                 phacc( dph) = dph: mphasor;
>>>                 // approx. atan2 , normalized output -0.5...0.5
>>>                 qatan2(y, x) = ba.if(x > 0, qatn(y, x), (ba.if(y > 0 ,
>>> qatn(y, x) + 0.5, qatn(y, x) - 0.5))):
>>>                                 max(-0.5, min( 0.5, _))
>>>                         with{
>>>                                 qatn(y, x) = y,(ba.if(x>0, x +  1e-14 ,
>>> x-1e-14) ):
>>>                                  _/ _: _<:_,_,_:_,(_*_<:_,_):_*(1-_ *(
>>> (1/3-0.2*_)));
>>>                             };
>>>                 atan2n(y, x) = y, x: ba.if( x != 0, atan2,
>>> 0):_/(2*ma.PI);
>>>
>>>                 // wraps between -0.5..0.5
>>>                 wrap(x) = x-( floor( x +0.5)) ;
>>>                 // unwraps delta phase
>>>                 uwr1(dw) = dw:ba.if( dw< -0.25, dw- (dw'+dw) , _):
>>>  ba.if( dw> 0.25, dw + (dw'-dw), _);
>>>                 // phase difference
>>>                 diph(ph) = ph <:_,_':-:uwr1;
>>>                 // approx sine -0.5...0.5
>>>                 qsin(x) = x+(0.5): wrap <: 
>>> (abs:-(0.25):qsincurve),_:ma.copysign
>>>
>>>                     with { qsincurve(x) = 1 - ( (x*x)<:
>>> *(1.2253517*16),(_<:*:* (-3.60562732*16)):>_ );};
>>>                 // phasor / accumulator -0.5...0.5
>>>                 mphasor(fw) = (+(fw) :(wrap))~_;
>>>                 lpw( fw, x)= x : fi.pole(lpp)*(1-lpp)
>>>                     with {  lpp = ma.E^(- 2*ma.PI * fw); };
>>>
>>>             };
>>>
>>> testsaw(fw, amp) = os.saw2( fw * ma.SR ) : fi.svf.lp(700, 0.707)*amp;
>>> //readout
>>> process = +: _<:_*(1-drywet)@(filterS*2), (freqtrack<:testsaw) * drywet
>>> :>_ <:_,_ ;
>>> _______________________________________________
>>> Faudiostream-users mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/faudiostream-users
>>>
>>
>>
>> --
>> AI has taken my job, but only I know what it is.
>>
>
_______________________________________________
Faudiostream-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/faudiostream-users

Reply via email to