1. IMPORTANT:
    We launched a new online community and this space is now closed. This community will be available as a read-only resources until further notice.
    JOIN US HERE

Costas loop AMA aka mod-demod redux

Discussion in 'REAKTOR' started by ANDREW221231, May 12, 2021.

  1. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    as everyone already must have heard, i recently uploaded an implementation of the mysterious "costas loop" phased lock loop, making me instantly world famous. i have been advised fielding questions about my achievement on this forum would be advantageous for my career, so ask em if you got em, especially you are Catman Dude, because then your questions will be given absolute priority
     
    • Like Like x 2
    • Funny Funny x 1
  2. Catman Dude

    Catman Dude NI Product Owner

    Messages:
    761
    I understand in principle Phase Locked Loop. An input signal, a variable frequency oscillator whose job is to keep in phase with the input signal, by way of a phase detector that resets the oscillator more or less till the 2 signals get as close as possible to lock-step. Or something like that.
    And I also sort of understand the Costas version of PLL uses a VCO that outputs both sin and cosin, each of which is multiplied by the input signal as phase detection, and that each of these products is low-pass filtered. And then fed back into the VCO. Sounds kind of awesome.

    Now I want to apply this to your code.
    The heart of your code, as per above, is 'glorious pll'. I can see Laureano's oscillator, and the sin and cosin outputs, and what better oscillator could you have asked for?
    These get multiplied, as above, by the input and are sent into 4-Pole Butterworth filters, each of which appears to be a cascaded pair of 2-pole SVFs.
    I think I'm following that. Why Butterworths? What happened on using the unused SVFs at the bottom of the macro?
    Of course this setup had 2 fewer poles. How does the difference affect the input into the freq detect macro? More important, the output?
    I see that the LP output of freq detect is the delayed sample input into the Phase Driver's frequency input.
    So that's the phase detection part, low passing and delaying by a sample the frequency detection output?
    I was wondering how the phase detection was arrived at.
    Of course next we would need to examine the way freq detect now operates and what it produces.
    It looks somewhat familiar... but it has 3 sample delays to calculate the differences between the samples on each Positive Crossing, and then to do a division of the last of these. Oh, and then another 1-sample delay.

    How did you come up with that part of the algorithm? And is it somewhere in the Costas Loop paradigm?
    And the constants in the various macros, where do they come from?
    That's enough questions for me for now I think.
    Thanks!
     
    • Like Like x 1
  3. Catman Dude

    Catman Dude NI Product Owner

    Messages:
    761
    BTW, I tried your Costas PLL on an audio file, produced with a pair of modified Auralis instruments, somewhat tied to each other, and I thought it was doing (to my untutored eyes) a credible job. A lot of the input was noisy, sometimes very short percussive pitches, sometimes phrases of pitches, and the curves CPLL produced looked like they could be used effectively as CV for amplitude modulation, or filter cutoff modulation, though there would need to be some smoothing of the signal first. It looks very cool!
     
    • Like Like x 1
  4. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    pretty much nailed both of em exactly right

    i wasn't running in that wolf pack whenever that was going on, so i never got in the discussion, but i've heard they are quality and i can attest to their, use i would have died to get my hands on them a few years ago.

    many legendary DSP techniques begin in much the same way, i've found. (including some in the pipeline)


    this is potentially a million dollar question, but in this case, with these filters, it was more of an "oh, ugna, thorg want more filter, thorg want locking more strong!! thorg no can read!!!!" - a caveman setting up a PLL impression if you didn't catch that. but setting aside the svf's was as much just kind of the traditional leaving extra crap around for people to mess with, and also kind of being left unsure about best choice for filter, but butterworth was beefier. cpu cycles for extra beef is often good

    i think "phase detection" is one of those abstract dsp things where you call what you're doing one thing and what that means in practice is pretty different. while a phase locked loop certainly does measure phase, i've never dealt with a method that actually calculated or even looked at phase as we usually measure it, its always amounted to counting how fast something happens. its a bit like some guy who never stops screaming in public about his "transfer function" but when you finally get a look at whatever he's got in his hand, its just a simple impulse response? (there's an equivalence between frequency and phase that doesn't get talked about enough imo [also a transfer function and impulse response of a system are two different ways of describing the same thing)

    anyway, the delaying by a sample has nothing to do with interesting parts of what's happening. this is a concession demanded of us by Father Causality, and if we cut it down to a fraction of its length without oversampling a ridiculous amount of times we would not hesitate to snap our fingers and make it happen


    well apart from it being born from the husk of your macro the same way a xenomorph alien broods its young, we have one other elements aside from instantaneous frequency detection:

    better frequency detection by applying a little patience as well as salamander anagrams' smarts. we can get a better reading if we wait long enough to do a linear interpolation across zero crossings. this gives us some integer number of samples as well as two fractional values of samples to measure time across. we also need to hang on to the fractional value from the previous zero crossing, but we only have to wait a couple extra samples. im actually not sure if all mine were necessary, but i ran out of actual patience to figure that out (if only everything could be figured out in a couple sample tics am i right??!?!)


    have you heard of the "lifting scheme" used in calculating wavelets? no? well my "lifting scheme" was lifting salamander's frequency hack haha. i already explained that part, just couldn't resist another joke

    actually this part is pretty interesting, what i had to go on was just the basic block diagram shoot from the hip.png
    which was a thing i'd stared longingly at every six months or so for a few years now. only this time did it sink it that "duh, obviously this can be built" i think i saw too many uncertainties in what stuff actually meant. i guess all that's changed since then is i've had more luck since
    trying the most obvious thing and it usually working. there was some other stuff on the wiki page that i'm sure could have held some physical meaning to me if i could have deciphered it. writing that part in some form of polynesian or possibly even japanese dialect seems like an odd choice for an encyclopedia o_O:rolleyes::confused:

    the only other clues i could manage was from some exchange on stack overflow, where i got a diagram of a filter that was plain enough but also had too much that was gibberish to derive any physical meaning, also a short exchange about the phase detection filter and how it was quite a requirement for it to be some specific type of fir filter i definitely don't have, so with that it was , "alright, core factory 1p and svf/butterworth it is then" :cool: so to answer your question about where the constants came from, about a half hour of "ok got a hammer, what looks like a nail? probably its a developing sense of what i can afford to flout. had i heard it needs to be a kind of fir filter, as well as a compelling reason i might have listened. , but... "i heard it really should be a certain type of fir filter" sounds more like a fun challenge


    so, i believe the most probing thing you were trying to get at was some sense of how these sine and cosine are helping with phase measurement and where this could be considered to be happening. well, i can do better than leaving you empty handed by offering my rough ideas on the issue.

    first off, sine and cosine are THE bees knees for measuring all kinds of phase of anything anywhere. once you know the period of the phase you need, if you get two measurements with that offset, your measurement cannot be considered more locked in that that... that's like the whole reason fourier transforms can do what they do

    a more pressing question maybe is "just what exactly is the meaning of all this multiplying, filtering, multiplying.... filtering and expecting anything intelligent to happen as a result?" its basically this:

    you have the sin/cosine bros following a signal, presumably at some frequency present in the signal itself

    multiplying them gives new frequencies at output: at double the input freq, and at DC

    you really don't want the one that's double hz left over hanging around, so filtering it off is the most important step and why the first filters are critical.

    the version at dc, along with sine/cosine, are all friends together, and can pretty much perfectly track and represent whatever it is happening down there. im pretty unclear on how that final multiplication and also steep filtering right after is supposed to shift everything back up to original band for 1:1 frequency tracking, (usually that not quite how that works) but we all seen it happen. so some aspect of the signal at least is doing exactly that. quadrature/complex analysis/ complex analysis is actually kinda just magic, it really is
     
    Last edited: May 13, 2021
    • Like Like x 1
  5. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    apparently the costas loop flat out outperforms a conventional PLL and has double precision again noisy signals and doppler shift. also seems able to find a happy place even in a messy signal, where pll's freak out a bit if anything isn't monophonic, unless...

    actually this is a good place to cut this off. aside from whatever you reply here, you can dm me if you'd like the full update on where this has gone since first uploaded
     
  6. Catman Dude

    Catman Dude NI Product Owner

    Messages:
    761
    love this stuff. not that I totally understand it... that comes never, but the haze begins to lift, slowly
     
  7. Paule

    Paule NI Product Owner

    Messages:
    7,555
    • Like Like x 1
  8. colB

    colB NI Product Owner

    Messages:
    3,969
    OK, I have a question :)

    PLLs are something I've played with a few times and never got the results I'd hoped for.

    Having a look at your implementation and the documentation I can find on the 'costas loop', I can see that yours partly depends on a frequency detector that counts samples between positive zero crossings. None of the documentation on the costas loop mentions any kind of frequency detection module as part of the system.

    So is that explicit frequency detection necessary, and if so, why is it not mentioned in documentation on the costas PLL?

    Are you counting between crossings to get the frequency right, and the loop just aligns and tracks the phase?
     
  9. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    that's a good question, because it's something i used to get hung up on when looking at the diagrams, i didn't really consider it at all when putting this together, as it was made with a few simple changes to the direct port of "doepher A-196" to core i'd just completed

    as i understand it, for a more conventional PLL that works with flip flops, using frequency detection to model how it keeps phase in sync is appropriate because the VCO is always falling out of sync and requires constant corrections in the opposing direction, and that frequency detection of the phase difference between the two signals will produce "spurs" in the opposing direction, which, tempered by the filter, provide that corrective action,

    whatever is going on here, while it seems to work, doesn't appear to be quite the same mechanism in play. from what i can tell its closer to fairly straightforward frequency detection of a single sideband of the input modulated by the loop VCO's 'analytic' output.

    i've been doing a lot of different stuff with analytic signals or 'complex envelopes', shifting their frequencies to DC and back. so the idea that frequency detection is an appropriate way to measure whatever is going on in the mixed signals leading up to it mostly makes sense

    pulling up the relevant literature, the only thing that's really jumping out at me is that my placing of the second filter makes this a 'modified costas loop', if it indeed is one. it could very well be some kind of regular PLL with costas loop characteristics, or some bastard new thing that works completely by accident? :eek: i haven't tried demodulating anything in the fairly specific way that seems to denote their primary function, which i hope is just a fancy way of saying "fm demodulator" or else i'll be totally lost in the woods here haha

    i would say yes, in a way not totally dissimilar to what was described above, though it seems like the frequency detection being updated at zero crossings certainly doesn't hurt its chances of staying in phase either, not really positive either way if that would be preserved as 'information' in the loop. think i must listen to the signal that's presented at the frequency detector, which will need a boost of about 200x and some robust saturation as PPE for me and my equipment.

    it seems like the mechanism for exactly how the VCO is controlled—in the context of costas loops as well as convention PLLs—is never explicitly mentioned, as if it should be obvious to all reading, so they didn't bother to include it. i mean that's a fair question, and id have thought it'd be easier to find a mention if it: how does one turn whatever method is used for phase detection into a control voltage for an oscillator? i've wondered in the past if frequency counting was not correct, and that was the reason the original "doepher style" offering to the UL seemed not to work so well. i see the above example has this part labeled g(t), but maybe you have an idea of what in tarnation ᵘLF(t) is supposed to mean.. is it anything more than a descriptive way of saying "this is the output of the filter"?

    uLF(t).png


    in the past i've messed around with hard sync instead as a control method, but that requires an input frequency for the oscillator to be set in the first place, and seems to give a pretty limited range for it to work in. could be that's a feature not a bug, but i wouldn't exactly call it 'free running' either
     
    Last edited: May 17, 2021
  10. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    this is interesting, under the types section, it almost seems to describe a method for deriving a voltage for a VCO that sounds closer to your method using a slew limiter

    i love how deep this topic goes: "In 1976 it was shown..." lol

    https://en.wikipedia.org/wiki/Phase_detector
     
  11. colB

    colB NI Product Owner

    Messages:
    3,969
    Yeah, I think part of the problem is that these devices were never intended to be used directly for making weird music noises, so the descriptions assume things that don't make sense directly in this context.
    e.g. maybe something like tuning a radio - in this case you have some audio, but it is already modulated on some high frequency carrier. The PLL is used so that when you are almost tuned in, the PLL does the last little bit and tracks any small fluctuations to maintain tuning. In this type of use case it is never required to search for or detect a frequency, just to lock on when its already close. IIRC, some PLLs also have a feature where they have a gate that is high when they are locked and low otherwise - also great for radio, because it can shut off the sound or reduce the volume unless you are tuned in... And I suppose radio is used for lots of things that are not audio - like wifi communications etc.
     
    • Like Like x 1
  12. Catman Dude

    Catman Dude NI Product Owner

    Messages:
    761
    sounds spot on. remembering what it was like at night trying to tune in a faraway station that could only be listened to really late. You'd get fuzzy sound and suddenly, with a slight twist of the dial, the thing would lock in and clear out the close-to-home (on the dial and geographically) stations. Until it would fade away again and you'd lose your favorite song in the middle somewhere. :(
     
    • Like Like x 2
  13. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    i doubt there is sufficient bandwidth to demodulate a properly modulated audio rate fm signal, but it occurs to me that's just the thing to try next, maybe just try to recover a sine wave
     
  14. colB

    colB NI Product Owner

    Messages:
    3,969
    Here's a related vid that you might find interesting! worth checking out the other Makenoise vids as well, particularly the Walker Farrell ones.
     
    • Like Like x 1
  15. KoaN

    KoaN NI Product Owner

    Messages:
    266
    Interesting sonic textures with this technique!
    I didn't quite get how to demodulate though? You have your carrier signal being modulated by whatever signal and then you use the same carrier frequency+phase on another signal and...he said "mult" for multiply? I get confused at that part.
     
  16. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    oh yeah, this is absolutely relevent
     
  17. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    i guess he would be talking about amplitude or ring modulation... im not familiar with the specific demodulation scenario that would would allow for this, but yeah, he was talking about multiplying it with a signal with the same frequency and phase as the carrier (so.. basically the carrier? again not entirely sure)

    i know for AM, you can demod with an envelope detector and and a DC filter, and for FM one way is to use a phase locked loop that tracks the frequency (havent tried this yet, but im fixing to here with this)
     
  18. colB

    colB NI Product Owner

    Messages:
    3,969
    "mult" in this case is short for multiple. A multiple is a hardware module that gives you multiple copies of a signal - totally unnecessary in Reaktor where we can take as many wired form the same output as we want. 'Mult'ing is the act of making a copy of a signal using a 'multiple' module.

    What he's doing initially is pretty simple. He's taking two copies of a sine wave, multiplying (as in ring mod) one copy with the original signal, and then multiplying the result with the other copy. He then uses a different sine wave to 'demodulate' and gets a bunch of interesting tones as a result.

    To get something similar in Reaktor you're better using some sort of ring mod emulation rather than a simple multiplier. That way you get some of the same kinds of artefacts in the result.

    Walker does mention in the vid that in a real demodulator, you might use a pll to lock the phase. So I'm guessing we might get some interesting results using a PLL in combination with a couple of ring modulators. vary the frequency of the demodulation sinewave and see what kinds of glitches we get as the pll tries, and fails, to maintain a phase lock. It might be crap, but it also might be cool :)

    I guess the patch might be:

    input signal and carrier into ring mod = modulated signal
    modulated signal into pll = phase locked(sometimes) oscillator
    phase locked osc and modulated signal into second ring mod = cool (or crappy) result ?
     
  19. ANDREW221231

    ANDREW221231 NI Product Owner

    Messages:
    875
    welp here's an apparent demod of a 30hz sine wave from a carrier approx 250hz. thought better about having the output be audible, but the recovered wave seems a bit.. hilariously nonlinear, which seems to bode well for the whole "might be crap, might be cool" idea

    it already gets chaotic trying to mess difference mod carrier frequencies, and trying to maintain a lock with the loop
     

    Attached Files:

    Last edited: May 20, 2021
    • Like Like x 1
  20. KoaN

    KoaN NI Product Owner

    Messages:
    266
    Hmm interesting.
    Sounds pretty cool on a voice for sure! Might be more atonal with musical stuff but maybe worth exploring.

    Also reminds me of that effect from Native,beautiful effect and very flexible for Am/ring mod/crazy radio sounds.
    Called Freak,last one on the page.

    https://www.native-instruments.com/en/products/komplete/effects/effects-series-crush-pack/