Phase detection

This is the second part of my DCF77 experiments. It is also the first step in the direction of a significantly better DCF77 decoder. Because the improved decoder is signifcantly more complex I will proceed in smaller steps. The first step introduces a so called phase lock. Before I go into the details lets have a look at how the exponential filter of the first experiment would perform with 90% noise.


In the diagram you can see some very undesirable effects. The filter will now pick up wrong signal levels. Additionaly even the proper signals have undesirable phase jitter. Especially the length of the decoded pulses starts to vary. The combined impact is that the successive signal decoder can not decode anything reasonable at all.

This does not only look bad, it is bad. Fortunately due to the scientific work of Claude Elwood Shannon it is possible to precisely quantify the impact of noise on a communication channel.

The diagram below shows the impact of noise on a binary symmetric channel. Given the bit error rate p the relative channel capacity C can be computed by

C = 1-H(p) = 1+p\log_2 (p) + (1-p)\log_2 (1-p)

Take notice that 100% noise corresponds to an bit error rate of 50% and 90% noise to a bit error rate of 45%. As you can see 90% noise will reduce the channel capacity to below 5%.


If you read the details on binary symmetric channels you might conclude that this does not apply to our case. After all DCF77 does not use a binary symmetric channel. And of course you are right. However this does not make things better. Asymmetric channels are harder to model but if you go through the math you will find that the DCF77 channel capacity is even lower. Even worse: the channel encoding for DCF77 is far from optimal. Thus in the presence of noise we will end up significantly below the theoretical limit.

The PTB experts were neither lazy nor stupid. At the time they invented their encoding scheme computational resources where expensive. Thus the channel was instead optimized for easy decoding. If you read through their documentation you will find out that they are actually very clever and already have a good solution. Unfortunately I did not find any cheap receiver for the improved transmission scheme. Hence I will stick to the old school signal.

Now what does this all mean? Well, it means that right now the standard decoding mechanism + filter tries to extract to much information from the signal. Each second the data (short pulse, long pulse, no pulse at all) is decoded. In addition phase information is retrieved. That is the start of the pulses is retrieved. If we assume that standard decoders try to capture the phase to about a tenth of a second we will find that they actually try to extract something around 4 bits per second. In the presence of noise this is definitely to much.

In order to deal with this we will now split the whole deconding process into pieces. The first step will be to capture the phase and nothing else. That is we want to find the starts of the 1s ticks and nothing else. So how much information is required to find those? The answer is “it depends” 😉 Suppose we would have a perfect precise clock. Then we could generate 1s ticks on our own. The only information we would need would the phase difference to the DCF77 signal. Once the signals would be in phase we would not need any additional information at all. So if we find a way to use the information contained in a longer sequence then we could decrease the average information required as much as we want.

Unfortunately we do not have a perfect local clock. Otherwise we would not need an external clock in the first place. However we have a pretty good local clock – the crystal of the Arduino.

Attention: there are Arduinos with only a resonator. Especially the most up to date model the Uno does not have a crystal. What I am going to implement needs a reasonable accurate local clock. If you want to repeat my experiment ensure that you get an Arduino with a crystal. Of course the Blinkenlighty has a crystal instead of a resonator.

The idea is to synchronize the local clock of the Arduino with the DCF77 signal. The point is that the phase is then synthesized by the Arduino. The only information that needs to be extracted from the signal is the phase difference. As it turns out this can be done by evaluating the signal during longer timer periods than just 1s intervalls. Thus we will extract significantly less information on average. The underlying mechanism is known as a phase detector. An implementation that uses a phase detector to sync a clock is also known as a phase locked loop.

Enough theory – here is the code.

//  Copyright 2012 Udo Klein
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  GNU General Public License for more details.
//  You should have received a copy of the GNU General Public License
//  along with this program. If not, see

const uint8_t dcf77_sample_pin = 19; // A5
const uint8_t dcf77_analog_sample_pin = 5;
const uint8_t dcf77_monitor_pin = 18;
const uint8_t dcf77_synthetic_clock_pin = 17;

const uint8_t first_debug_pin = 5;

namespace DCF77_Demodulator {
    const uint8_t bin_count = 100;
    typedef struct {
        uint16_t data[bin_count];
        uint8_t tick;
        uint32_t noise_max;
        uint32_t max;
        uint8_t max_index;
    } phase_bins;
    phase_bins bins;
    const uint16_t samples_per_second = 1000;
    const uint16_t samples_per_bin = samples_per_second / bin_count;
    const uint16_t bins_per_10ms  = bin_count / 100;
    const uint16_t bins_per_50ms  =  5 * bins_per_10ms;
    const uint16_t bins_per_60ms  =  6 * bins_per_10ms;
    const uint16_t bins_per_100ms = 10 * bins_per_10ms;
    const uint16_t bins_per_200ms = 20 * bins_per_10ms;
    const uint16_t bins_per_500ms = 50 * bins_per_10ms;

    void setup() {
        for (uint8_t index = 0; index < bin_count; ++index) {
  [index] = 0;
        bins.tick = 0;
        bins.max = 0;
        bins.max_index = 255;
        bins.noise_max = 0;

    uint16_t wrap(const uint16_t value) {
        // faster modulo function which avoids division
        uint16_t result = value;
        while (result >= bin_count) {
            result-= bin_count;
        return result;
    void phase_detection() {
        // We will compute the integrals over 200ms.
        // The integrals is used to find the window of maximum signal strength.
        uint32_t integral = 0;
        for (uint16_t bin = 0; bin < bins_per_100ms; ++bin)  {
            integral += ((uint32_t)[bin])<<1;
        for (uint16_t bin = bins_per_100ms; bin < bins_per_200ms; ++bin)  {
            integral += (uint32_t)[bin];
        bins.max = 0;
        bins.max_index = 0;
        for (uint16_t bin = 0; bin < bin_count; ++bin) {
            if (integral > bins.max) {
                bins.max = integral;
                bins.max_index = bin;
            integral -= (uint32_t)[bin]<<1;
            integral += (uint32_t)([wrap(bin + bins_per_100ms)] +[wrap(bin + bins_per_200ms)]);
        // max_index indicates the position of the 200ms second signal window.
        // Now how can we estimate the noise level? This is very tricky because
        // averaging has already happened to some extend.
        // The issue is that most of the undesired noise happens around the signal,
        // especially after high->low transitions. So as an approximation of the
        // noise I test with a phase shift of 200ms.
        bins.noise_max = 0;
        const uint16_t noise_index = wrap(bins.max_index + bins_per_200ms);
        for (uint16_t bin = 0; bin < bins_per_100ms; ++bin)  {
            bins.noise_max += ((uint32_t)[wrap(noise_index + bin)])<<1;
        for (uint16_t bin = bins_per_100ms; bin < bins_per_200ms; ++bin)  {
            bins.noise_max += (uint32_t)[wrap(noise_index + bin)];

    void advance_tick() {
        if (bins.tick < bin_count - 1) {
        } else {
            bins.tick = 0;
    uint8_t phase_binning(const uint8_t input) {
        // how many seconds may be cummulated
        // this controls how slow the filter may be to follow a phase drift
        // N times the clock precision shall be smaller 1
        // clock 30 ppm => N < 300
        const uint16_t N = 300;
        if (input) {
            if ([bins.tick] < N) {
        } else {
            if ([bins.tick] > 0) {
        return bins.tick;
    void detector_stage_2(const uint8_t input) {
        const uint8_t current_bin = bins.tick;
        const uint8_t threshold = 30;
        if (bins.max-bins.noise_max < threshold ||
            wrap(bin_count + current_bin - bins.max_index) == 53) {
            // Phase detection far enough out of phase from anything that
            // might consume runtime otherwise.
    void detector(const uint8_t sampled_data) {
        static uint8_t current_sample = 0;
        static uint8_t average = 0;
        // detector stage 0: average 10 samples (per bin)
        average += sampled_data;
        if (++current_sample >= samples_per_bin) {
            // once all samples for the current bin are captured the bin gets updated
            // that is each 10ms control is passed to stage 1
            const uint8_t input = (average> samples_per_bin/2);
            average = 0;
            current_sample = 0;
    void get_quality(uint32_t &lock_max, uint32_t &noise_max) {
        lock_max = bins.max;
        noise_max = bins.noise_max;

    uint8_t get_time_value() {
        const uint8_t threshold = 2;
        return (bins.max-bins.noise_max >= threshold)? (bins.max_index + bins.tick + 1) % bin_count: -1;
    uint8_t debug() {
        Serial.print(F("Phase: "));
        const bool uses_integrals = sizeof(bins.max) == 4;
        Serial.print(get_time_value(), HEX);
        Serial.print(F(" Tick: "));
        Serial.print(F(" Quality: "));
        Serial.print(bins.max, DEC);
        Serial.print(bins.noise_max, DEC);
        Serial.print(F(" Max Index: "));
        Serial.print(bins.max_index, DEC);
        for (uint8_t index = 0; index < bin_count; ++index) {
            if (index == bins.max_index ||
                (!uses_integrals && index == (bins.max_index+1) % bin_count) ||
                (uses_integrals && (index == (bins.max_index+10) % bin_count ||
                (index == (bins.max_index+20) % bin_count)))) {
        return bins.max_index;

void process_one_sample() {
    // The Blinkenlight LEDs will cut off the input signal
    // below a logical high. This is due to the weak
    // output signal of the DCF module.
    // Hence for the Blinkenlighty
    // and the Blinkenlight shield we can not use
    // digitalRead.
    // Comment the line below if you are not using this code with a
    // Blinkenlighty or Blinkenlight shield.
    const uint8_t sampled_data = analogRead(dcf77_analog_sample_pin)>200? 1: 0;
    // Uncomment the line below if you are using this code with a standard Arduino
    //const uint8_t sampled_data = digitalRead(dcf77_sample_pin);
    digitalWrite(dcf77_monitor_pin, sampled_data);


void initTimer2() {
    // Timer 2 CTC mode, prescaler 64
    TCCR2B = (0<<WGM22) | (1<<CS22);
    TCCR2A = (1<<WGM21) | (0<<WGM20);
    // 249 + 1 == 250 == 250 000 / 1000 =  (16 000 000 / 64) / 1000
    OCR2A = 249;
    // enable Timer 2 interrupts
    TIMSK2 = (1<<OCIE2A);

void stopTimer0() {
    // ensure that the standard timer interrupts will not
    // mess with msTimer2
    TIMSK0 = 0;

void setup() {

    pinMode(dcf77_sample_pin, INPUT);
    digitalWrite(dcf77_sample_pin, HIGH);

    pinMode(dcf77_monitor_pin, OUTPUT);
    pinMode(dcf77_synthetic_clock_pin, OUTPUT);
    for (uint8_t pin = 0; pin<10; ++pin) {
        pinMode(first_debug_pin + pin, OUTPUT);



void loop() {
    using namespace DCF77_Demodulator;
    const uint8_t phase = debug();
    for (uint8_t pin = 0; pin<10; ++pin) {
        digitalWrite(first_debug_pin + pin, phase % 10 == pin);
    digitalWrite(dcf77_synthetic_clock_pin, (bins.tick - phase + bin_count) % bin_count < bin_count / 10);

If you look at the code you will see 3 important features.

  1. The code samples at 1 kHz. That is one sample per millisecond. However it does not use the msTimer2 library.
  2. The code averages every 10 samples and put the result into “bins”.
  3. The code does not search sudden changes in the values of the bins. Instead it computes some sum that I called “integral” and searches for a maximum of this sum.

Lets start with the bins. The idea is as follows. If the signal would be in sync then each second (but the last of the minute) there would be a 100ms high signal. Then 100ms either high or low and then a 800ms low signal. This repeats every second (but the last of the minute). Even if the signal is not in sync I know that both the local clock and the DCF77 clock have a 1s period. Thus the signal I see at any specific time should be the same 1s earlier or later. Except for the last second of the minute and except for the second 100ms after the start of the second (relative to DCF77). Thus I introduced 100 bins that represent the averaged signal for 10ms time slots. Whenever the code has 10ms worth of samples it averages them and puts them into the current bin. Afterwards it will proceed to the next. After the last bin it will start over again.

The number of bins as well as the sample rate were not chosen by chance. More bins imply better resolution. With 100 bins my clock can have a resolution of 10ms. However more bins consume more memory. Since the Arduino has only 2k of Ram I stick with 100 bins (which consume 200 Bytes of memory). In addition the Arduino has only a 16 Mhz clock. So if I sample at 1kHz I will have only 16000 clock cycles per sample. This is not yet an issue but later stages of my clock will consume more cycles. So increasing the number of bins and the sample rate is a bad idea given the memory and the clock constraints. Additionaly there is not to much to gain by increasing the sample rate as most DCF77 receivers have a bandwidth well below 1 kHz.

So what I end up with is a set of bins with averages of the signal. Have a look at red graph in the plot below. The red graph represents the bins for 80% noise. I normalized them to the range of the initial signal. As you can see the noise brings everything closer to the average of 0.5. Still it has no significant issue with the noise.


In the plot you can also see a green graph labeled “convolution”. This has very clean peaks at in sync with the phase. Now what is this? These are the “integrals” or sums that I compute during phase detection.

Now what is behind those integrals? Basically I want to correlate the binned signal with something that looks like the real signal. Notice that I know that the signal should be high for 100ms and low for 800ms but I do not know how it will behave between in the middle. Thus I will correlate with something that is 0 during the second 100ms.


The next step is some simple mathmatical trickery and implementation optimization. First I will use convolution instead of correlation. This is almost the same. The only thing is I have to replace the kernel by a mirrored version of itself. There is nothing magic about this. It makes no difference for the implementation. However it makes theoretical analysis simpler because convolution is easier to deal with. In addition I translate the kernel up. That is I add 1 to it. Thus I need not deal with negative numbers anymore. This will translate convolution result up as well but it has no impact on the argument (place on the time/bin axis) of the maximum.

Below you find a picture of the convolution kernel.


Quite a lot of effort. Will it pay of? Lets have a look at the simulation for higher noise levels (click to enlarge).

The simple exponential filter had no more chance at 90%. This is at a bit error rate of 45% or a theoretical channel capacity of about 0.72%. The convoluted bins still give a very clear indication of the phase.


At 95% noise things are still well under control.


Even 98% noise can still be handled by this detector. Notice that this is a bit error rate of 49%. Which in turn implies that the theoretical channel capacity is below 0.029%. About 25 times less than at 90% noise.


At 99% noise finally things start to get out of control though.


Now back to the magic number N=300. Why did I chose N=300 and not more (or less)? More would mean I average on more bins. This in turn would allow to achieve even more noise tolerance. However there is some limit imposed by the accuracy of the local clock. If the local clock deviates from DCF77 this has the same effect like an additional convolution with some rectangle pulse. The width of this pulse is the clock deviation during the sample period. So if N=300 and the clock is accurate to 30 ppm this pulse has a width of ~3000 microseconds or 9ms. This is slightly less than one bin. Below there is a plot of the impact of the clock deviation to a clean signal. That is I convoluted a clean signal with the convolution kernel and an additional rectangle pulse. The plots are for N=300. Increasing N by a factor of x is the same as increasing the clock deviation by a factor of x (and vice versa). Since the Blinkenlighty has a clock that is at least accurate to 30 ppm (actually it is a little bit better but I wanted to stay save) I settled for N=300.


Also notice that resonator based Arduinos will have clock deviations beyond 1000 ppm. So the smoothest of the curves applies. With other words even without noise the detector will fail with those. As I already said this applies especially to the Arduino Uno.

This finally brings me back to the comment on msTimer2. The msTimer2 library has some very minuscule glitch that will make it very slightly slower than 1000 samples per second (less than 0.1%). Usually this is completely irrelevant but for the current purposes the deviation must be kept as small as possible. This was the reason I implemented the timer code on my own instead of using one of my favourite libraries.

Here is a video of the code in action. Notice that it uses the LEDs to visualize the phase at a resolution of 10ms, 1/100s. If you have a close look at the monitor LEDs you can see that the signal is somewhat noisy. Not very noisy but enough such that none of my commercial clocks will get a lock. Watch and see how fast and nicely it locks to the DCF77 phase and keeps it 🙂


23 Responses to Phase detection

  1. Tobias says:


    Very nice to get the theory and working example for a DCF77 receiver which can handle such noise! I am currently trying to build a DCF77 receiver using the arduino platform and will definitely come back to some aspects and ideas you mentioned!
    What I dont get in this article is whether you still use the code from step 1 as input for your PLL or whether this new approach will completely replace the first step. I think that this is a complete replace because you cannot use the values after applying the Schmitt-Trigger here but this is only my current suggestion 😉
    Or do you currently only try different approaches (your steps toward a new DCF77 decoder) to combine them later on to “one big” solution?

    I am looking forward to the remaining work on the DCF77 decoder as this sounds very promising to me!

    Best Regards,

  2. Michał says:

    Compiled the code on arduino win 1.0.5, loaded to atmega328@16Mhz. The problem is phase_detection function takes 3.6ms to complete, but it has to finish in less than 1ms (before next interrupt). Do you have any idea why it takes so long?

    Thank you for excellent work!.
    Kind Regards,

  3. I looked it up what version of Arduino I am using: Arduino 1.0 on Ubuntu. Would you mind to compile my code with 1.0 and see if you still have the same issue? I definitely have no idea why it might take that long. Especially because with the subsequent experiments I add more and more computations for the signal processing stages and it stays below 1ms. Did you do any changes to my code besides the debug pin instrumentation?

  4. Michał says:

    My changes to your code:

    void process_one_sample() {

    const uint8_t sampled_data = !digitalRead(dcf77_sample_pin); //(I have inverted dcf77 output)


    ISR(TIMER2_COMPA_vect) {
    digitalWrite(16, HIGH);
    digitalWrite(16, LOW);

    void setup() {

    pinMode(16, OUTPUT);


    I’ve tried arduino 022, 1.0, 1.0.3, 1.0.5 for Windows, every time the same 3.6ms every 10th interrupt.

    It looks like the loop:

    for (uint16_t bin = 0; bin < bin_count; ++bin) {


    causes the problem. After commenting out the loop interrupt time dropped to 340us.

    The best way to see the problem is to reset arduino without dcf77 receiver connected, then the phase_detection is called for every bin.

    Could you please check it on your arduino using DSO?

    Could you please send me your hex code in case my Windows environment causes the problem.


  5. Michał says:

    Still don’t know why it doesn’t work on my arduino. I’ve compiled your code on NXP LPC1114 (cortex-m0 48MHz) using LPCExpresso 6. The result is phase decoder works very good (as far as I remember longest interrupt time takes about 300 us).


  6. Sorry for not answering earlier with the HEX file. I had some issues with my system. But here it is:


  7. Ian says:

    Thanks for an incredible blog and amazing DCF library. I have learnt a lot just by reading through the code and trying out the experiments. I am using a Conrad module in central London where the background noise level is very high.

    I have one question: In the initTimer2() function you select a prescaler of 64 by setting bit CS22, and CTC mode by setting both WGM21 and WGM22. However according to my copy of the AT328P datasheet, CTC mode is enabled by setting bit WGM21 only. if WGM22 is also set then the mode is “Reserved”.

    All the best, Ian

  8. John O'Hare says:

    Great blog, did the Arduino incompatibility ever resolve.

  9. nivagswerdna says:

    Hi Udo

    I am experimenting with the MSF 60kHz signal here in England under noisy conditions and was about to give up until I saw some of your experiments. I captured the MSF signal at averaged 10mS buckets and then applied your convolution algorithm and seem to be able to recover phase to within a few bins.

    The MSF signal is 100ms ON and then 100ms ON or OFF (A-bit) and 100ms ON or OFF (B-bit) and then 700ms off or 500ms ON and 500ms OFF for Minute Marker.

    Your convolution is 100mS x 2+ next 100mS x1. Would the same be appropriate for MSF?


  10. John Prentice says:

    As an alternative to buying Udo’s processor board (e.g. if you need more USB capability than the FTDI chip provides) you might find this hack useful to get a crystal controlled Arduino UNO

    The clock library runs really well on this for me. Thanks Udo.

    Feel free to ask if anything is unclear

    John Prentice

  11. kris jaxa says:

    Hi Udo,
    1) i think that your kernel model is a little wrong, It seems that you’ve forgotten than we accumulate in-signal, so 100 – 200 ms level isn’t simple 0.5 of 0 – 100 ms level. If probability of 0 and 1 bits are equal (= 1/2) then the 100 – 200 ms level is (slowly) grows to upper limit (300 in your code) . Practically we can see that this level is near 0 (it suggest than zeros are more frequent?) – any way, “0.5” seams not right.
    2) In my test, I noticed that max level area (0..100 ms) gets wider after some time – I try more aggressive bins averaging – to subtract 2 where signal is 0 – this improves correlation result

    thanks for the very good articles
    /kris jaxa

    • Hi Jaxa,

      yes, you are absolutely right. And I know this for quite a while. However I have not yet attacked this issue because it was good enough for me so far. The challenges are as follows:

      1) If the signal fades, what happens to the receiver output. Is it biased to 0 or to 1? I have seen both ways.
      2) For the slower changing parts of the signal (e.g. years), the change rate of the bits is much slower than the bandwidth of the filter. So even if it is statistically correct I will bias in the wrong direction a lot of thje time.

      To fix this more parametrization and more sophisticated model are needed.

      Hence I just use the most blunt approach and looked how far I can get.

      Another thing that could be factored in: at this time I do not use the information that might be gathered during the 800 ms with full carrier. I could use this to determine if the reception conditions are currently good or bad. Also the Kernel is only properly matched to the sender side. Usually receivers have some low pass effect. Hence an optimal Kernel would also factor in the receiver behaviour. This results in slightly different period length. Would also be interesting.

      In the end the whole library was some practice in signal processing. Thus I doubt that I will find the time to fix this any time soon.

      But nevertheless, great insight.

      Best regards, Udo

  12. Rodolfo says:

    Hi Udo.
    Thanks for your fantastic filter (sorry for my bat English… i’m Italian).
    I have a problem with your filter (works on an Arduino uno Atmega328 with 16 Mhz crystal modification):
    I look filter locks signal in few times but… the first bits in the filter synthetized output (the reserved meteo bits), have a wrong, time duration.
    For example i measure an “1” bit comes from 170ms to 260ms, a “0” bit, from 55 to 160 msecs.
    This “only” for the first 15-16 bits in an dcf77 frame, all the others are simply perfects!
    Hy this? What i wrong?
    Is there a modification on your filter to avoid this problem?

    • Hi Rodolfo,
      this is correct behaviour. The superfilter is not really a filter. Actually it is a dcf77 clock followed by a synthesizer. Because the meteo stuff is encrypted I can not synthesize it. Hence I can do just a little bit of “grooming”. All other bits are synthesized. Hence the other bits are 100% spot on. For the meteo time I could do this if I would implement the meteo chiffre. Unfortunately it is proprietary. Still I know how to do this but I do not know what the legal implications are. So unless someone would be willing to sponsor the legal clarification I will not implement this.
      Best regards,

Leave a Reply to kris jaxa Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.