Example Code

Calculate Phase Difference of Waveforms Acquired via FPGA

Code and Documents

Attachment

Download All

This VI shows how to generate and/or acquire two waveforms, pass them via DMA FIFO to the host, and calculate the phase difference. An alternate method, not shown here, is to acquire on the FPGA, process on the Real-time Controller, and analyze on the host via FIFO and Network Streaming.

FPGABD.JPGBD.JPGFP.JPG

The Project was created in LabVIEW 2011 and is located in the zip file.

Applications Engineer
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
blockmarc
Member
Member
on

This project is exactly what I am looking for except I have LabVIEW 2010.  I can not open this.  Can you send me a version that I can open in LV2010.  Or how does one open this project with lV2010

Thanks

blockmarc@hotmail.com

Chris.N
Member
Member
on

I have uploaded a version for 2009 SP1 in the attachments so you should be able to open that in 2010

blockmarc
Member
Member
on

Thanks a million for the 2009 version.

It is at 50K it is very stable.  I have tried to change the the signal generation to 2K but I no longer get a stable phase difference. 

Do I have to change the number of elementis on the WaveformFifo.Read in calculatePhase(host)?

For each Configure Sine wave generator should I leave the look up table size to 1024?

Also should I change the clock rate?

Thanks a million for your efforts  It would really help out if I can get your code working for 2k signals.

--marc

blockmarc
Member
Member
on

I just noticed that the number of elements in your waveformfifo is 1024.  I am still confused why the number of elements in waveformfifo.read is set at 4000?  how did you determine the 40000?

I still need a hand on determine what to change to get your program working at for two 2k simulated inputs

Thanks in advance for your help

Chris.N
Member
Member
on

Hi Marc,

I will start by explaining why the numbers are the way they are for the example at 50KHz, and then give you the settings for 2KHz.  The VI that calculates the phase difference should not receive an integer number of periods of the signal.  If you look at the signals in the example at 50 KHz, you will notice the Phase Difference VI receives 2.5 periods of data.  Here is the math involved to ensure there are 2.5 periods of data:  The sample rate is 40 MHz which is 1 sample every 25ns.  The amount of time required for 1 period of the 50KHz signal to complete is 20uS.  Therefore, if you take 20uS/25nS, you will find that there are 800 samples for 1 period of the sine wave.  To make sure we send 2.5 periods to the Phase Difference VI, we multiply 800 points by 2.5 which is 2000 points.   Because we are sending two waveforms in the FIFO, the total number of elements we need to read out of the FIFO each loop iteration is 4000 elements.  You will notice, that if you send 3.5 periods (800*3.5=2800*2=5600 elements out of FIFO) to the Phase Difference VI, that it will also be stable.

Now to change this to work with a 2KHz sine wave.  A 2KHz sine wave has a period of 500 uS and we want 1 period of the sine wave to contain 800 points.  Therefore, to solve the correct FPGA clock rate, we say 800points/500uS = 1.6 M points/Sec or a sample rate of 1.6MHZ to result in 1 period containing 800 points. Then, reading 4000 points from the FIFO is still valid so the only modifications to the example are changing the Frequency to 2 KHz and the FPGA clock rate to 1.6 MHz.  Note to change the FPGA clock rate you must uncheck Use top-level clock. Hope this clears things up.

Chris

blockmarc
Member
Member
on

Chris

Many, Many Thanks!  Yes this code is stable at 2K.  Your solution is fantastic! It works!

Thanks for all of your help and explantion.  It all makes sense now.

Naturally I am going to make some changes in your program to make it work with live data.  I will add an IRQ.  Also I want to try some alternative algrothim.  I can not tell you how much having this code working means to me.  I am doing this project as a volunteer and I know by using this I can get this project up and running.  In the future I can modify it but it gives me an excellent base to start off with.

What an incredible xmas gift this has been.

Thanks Marc

Chris.N
Member
Member
on

You're welcome, I'm glad I was able to help.  Good luck with the rest of your implementation!

mathijsen
Member
Member
on

Hi! Thank you for you vi. It is very useful. But I found one problem. For example - in real situation if firstly should appears signal 2 (wave 2) - Phase Differerence should be negative. But this vi does not even show red wave 2 on chart if I put manually phase offset negative. Expected Phase difference - works properly and shows negative value, but Calculated Phase Difference in this case - 0, what is wrong. How to obtain red wave 2 on graph and negative calculated phase difference?

Contributors