<p>Does anyone know of anywhere I can find actual code examples of Software Phase Locked Loops (SPLLs) ? </p>

<p>I need an SPLL that can track a PSK modulated signal that is somewhere between 1.1 KHz and 1.3 KHz. A Google search brings up plenty of academic papers and patents but nothing usable. Even a trip to the University library that contains a shelf full of books on hardware PLL's there was only a single chapter in one book on SPLLs and that was more theoretical than practical.</p>

<p>Thanks for your time.</p>

<p>Ian</p>

[{'answer_id': 39597, 'author': 'Adam Hollidge', 'author_id': 4069, 'author_profile': 'https://Stackoverflow.com/users/4069', 'pm_score': 1, 'selected': False, 'text': '<p>Have Matlab with Simulink? There are PLL demo files available at Matlab Central <a href="http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14868&amp;objectType=FILE#" rel="nofollow noreferrer">here</a>. Matlab\'s code generation capabilities might get you from there to a PLL written in C.</p>\n'}, {'answer_id': 41887, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 3, 'selected': True, 'text': '<blockquote>\n  <p>This is an interactive design package\n  for designing digital (i.e. software)\n  phase locked loops (PLLs). Fill in the\n  form and press the ``Submit\'\' button,\n  and a PLL will be designed for you.</p>\n  \n  <p><a href="http://www-users.cs.york.ac.uk/~fisher/mkpll/" rel="nofollow noreferrer">Interactive Digital Phase Locked Loop Design</a></p>\n</blockquote>\n\n<p>This will get you started, but you <em>really</em> need to understand the fundamentals of PLL design well enough to build it yourself in order to troubleshoot it later - This is the realm of digital signal processing, and while not black magic it will certainly give you a run for your money during debugging.</p>\n\n<p>-Adam</p>\n'}, {'answer_id': 9118612, 'author': 'Kragen Javier Sitaker', 'author_id': 176009, 'author_profile': 'https://Stackoverflow.com/users/176009', 'pm_score': 4, 'selected': False, 'text': '<p>I suppose this is probably too late to help you (what did you end up doing?) but it may help the next guy.</p>\n\n<p>Here\'s a golfed example of a software phase-locked loop I just wrote in one line of C, which will sing along with you:</p>\n\n<pre><code>main(a,b){for(;;)a+=((b+=16+a/1024)&amp;256?1:-1)*getchar()-a/512,putchar(b);}\n</code></pre>\n\n<p>I present this tiny golfed version first in order to convince you that software phase-locked loops are actually fairly simple, as software goes, although they can be tricky.</p>\n\n<p>If you feed it 8-bit linear samples on stdin, it will produce 8-bit samples of a sawtooth wave attempting to track one octave higher on stdout.  At 8000 samples per second, it tracks frequencies in the neighborhood of 250Hz, just above B below middle C.  On Linux you can do this by typing <code>arecord | ./pll | aplay</code>.  The low 9 bits of <code>b</code> are the oscillator (what might be a VCO in a hardware implementation), which generates a square wave (the 1 or -1) which gets multiplied by the input waveform (<code>getchar()</code>) to produce the output of the phase detector. That output is then low-pass filtered into <code>a</code> to produce the smoothed phase error signal which is used to adjust the oscillation frequency of <code>b</code> to push <code>a</code> toward 0.  The natural frequency of the square wave, when <code>a == 0</code>, is for <code>b</code> to increment by 16 every sample, which increments it by 512 (a full cycle) every 32 samples.  32 samples at 8000 samples per second are 1/250 of a second, which is why the natural frequency is 250Hz.</p>\n\n<p>Then <code>putchar()</code> takes the low 8 bits of <code>b</code>, which make up a sawtooth wave at 500Hz or so, and spews them out as the output audio stream.</p>\n\n<p>There are several things missing from this simple example:</p>\n\n<ol>\n<li><p>It has no good <strong>way to detect lock</strong>.  If you have silence, noise, or a strong pure 250Hz input tone, a will be roughly zero and b will be oscillating at its default frequency.  Depending on your application, you might want to know whether you\'ve found a signal or not!  Camenzind\'s suggestion in chapter 12 of <a href="http://designinganalogchips.com/">Designing Analog Chips</a> is to feed a second "phase detector" 90° out of phase from the real phase detector; its smoothed output gives you the amplitude of the signal you\'ve theoretically locked onto.</p></li>\n<li><p>The natural frequency of the oscillator is fixed and <strong>does not sweep</strong>.  The <em>capture range</em> of a PLL, the interval of frequencies within which it will notice an oscillation if it\'s not currently locked onto one, is pretty narrow; its <em>lock range</em>, over which it will will range in order to follow the signal once it\'s locked on, is much larger.  Because of this, it\'s common to sweep the PLL\'s frequency all over the range where you expect to find a signal until you get a lock, and then stop sweeping.</p></li>\n</ol>\n\n<p>The golfed version above is reduced from a <a href="http://lists.canonical.org/pipermail/kragen-hacks/2012-February/000534.html">much more readable example of a software phase-locked loop in C</a> that I wrote today, which does do lock detection but does not sweep.  It needs about 100 CPU cycles per input sample per PLL on the Atom CPU in my netbook.</p>\n\n<p>I think that if I were in your situation, I would do the following (aside from obvious things like looking for someone who knows more about signal processing than I do, and generating test data).  I probably wouldn\'t filter and downconvert the signal in a front end, since it\'s at such a low frequency already.  Downconverting to a 200Hz-400Hz band hardly seems necessary.  I suspect that PSK will bring up some new problems, since if the signal suddenly shifts phase by 90° or more, you lose the phase lock; but I suspect those problems will be easy to resolve, and it\'s hardly untrodden territory.</p>\n'}]
['https://Stackoverflow.com/questions/39485', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3875/']