I'm trying to develop a flow meter using the cross correlation algorithm.

I'm using 2x IR leds and 2x phototransistors and a MSP430F5529. One pair of phototransistor-Led go in the upper level and the other in a lower level.

When the liquid passes in the first pair of phototransistor - IR Led, the microcontroller reads the voltage from the phototransistor out, and after the liquid cross the other pair and the microcontroller reads the voltage again.

The signals collected are very similar but phase shifted as expected.

I analysed the data collected in Matlab software and used the cross correlation algorithm to calculate the phase shift between them, in windows of 250 points.

The results were very good, but sometimes the algorithm results in values of zero phase shift. And analysing the plot, it can be seen, visually, that the 2 waves have a nonzero phase shift.

An example of a window is showed below. Matlab plot

For this 2 signals, the cross correlation results 0. But as can be seen, the 2 signals are phase shifted by about 13 points.

I know that the cross correlation algorithm will give me the delay of the maximum value of the multiplication of each point. But how I can calculate the phase shift between 2 signals like this example?

  • \$\begingroup\$ Can you post the matlab code? If you have the Signal Processing Toolbox, do the examples for xcor help? \$\endgroup\$
    – shuckc
    Nov 17, 2014 at 20:09
  • 1
    \$\begingroup\$ For those of us not familiar with building a flow meter like this, can you provide a link to a good introductory reference? All of the papers I found are behind pay walls. \$\endgroup\$
    – Matt B
    Nov 19, 2014 at 1:30
  • \$\begingroup\$ Dealing with the same problem for some time, especially coding the algorithm into the microcontroller, as it is very time sensitive. Did you have any progress? As far as tired codes are taking too long. \$\endgroup\$
    – caga
    Feb 19, 2015 at 20:17

1 Answer 1


I believe that the answer is here. Pay attention that xcorr() function returns value that is twice the size (minus 1) of the original vectors' length.

  • 3
    \$\begingroup\$ Konstantin - The link you posted is no longer valid. This happens frequently so perhaps you would like to include the material in your answer in addition to providing a link. \$\endgroup\$
    – JonnyBoats
    Jan 2, 2015 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.