LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Find Phase Shift Using FFT

Hi All,

I want to find the phase shift between two signals that I am reading in from Channels 0 & 1 from a E6024 Multidaq card.  I have a reference signal and an input signal which will be DC sinusoidal which I want to compute the phase difference for.  On the NI website I was looking at the reference: The Fundamentals of FFT-Based Signal Analysis and Measurement in LabVIEW and LabWindows/CVI.  It mentions that you can calculate the phase difference between signals using the FFT func, but doesn't go into detail as to how.  I was wondering if anyone out there has done something similar or knows how to do this and would be willing to make some sugggestions.  Example code would be nice as well.  Thanks.

Cheers,
Tyler
0 Kudos
Message 1 of 8
(6,979 Views)

Hi Tyler,

I don't know exactly wich txype of signal do you have (sinusoidal + DC offset?).

Anyway for a similar problem (phase difference between two sinusoidal waveforms), I used the "CrossPowerSpectrum" function to find the amplitude and phase of each harmonic component, then the "PeakDetector" function to find the higher harmonic value.

The phase difference (phDiff) is expressed in degrees; NUM_VALUES is the buffer length of my measurements (wave1, wave2).

The code is like this:

static double mag[NUM_VALUES/2],ph[NUM_VALUES/2],dt;

static double *pkloc = NULL,*pkamp = NULL,*pksec = NULL;

err = CrossPowerSpectrum(wave1, wave2, NUM_VALUES, 1.0, &mag[0], &ph[0], &dt);

if(err == 0){

   err = PeakDetector(mag, NUM_VALUES/2, LEVEL, 3, 0, 1, 1, &count, &pkloc, &pkamp, &pksec);

   if(err == 0){

      i = RoundRealToNearestInteger(pkloc[0]);

      if(i >= 0)

         phDiff = RadToDeg(ph[i]); // = ph[i]*180.0/Pi();

   }

}

The LEVEL constant on PeakDetector is used to reject peaks too small. 

You can use the same code, or modify it according your specific input data.

0 Kudos
Message 2 of 8
(6,970 Views)

Sorry, just an additional remark.

Your board is not simultaneous sampling, so you will always find a phase error due the lag between 2 consecutive measurements.

0 Kudos
Message 3 of 8
(6,970 Views)
Hi baloss,

Thanks for that information, I will play around with using the cross spectrum function and the code you provided.   I was just curious however, I am working right now on reading two signals at the same time and plotting them.  Do you have any suggestions for doing this, to minimize the simultaneous sampling error?  And is there a way to calculate that error?  If this error is on the order of an oscilliscope card then I don't think I'll need to worry.  If not, then this board might not work for me and I will need one that does sample simultaneous.  Can you suggest any cards that do?  Thanks.

Cheers,
ScKaSx
0 Kudos
Message 4 of 8
(6,951 Views)

Hi ScKaSx,

the delay between two measurements is given by

1) the DAQ card choosen (6024E is a 200kS/s, so at least 5 microseconds between 2 measurements)

2) the number of channels used (i.e if you acquire channel 0,1 and 2, the delay between 0 and 2 will be larger (the double) than between 0 and 1

I have no experience with the 6024 card, so I suggest you to apply the same signal on two inputs, and determine the phase difference with the code posted. Convert the error from degrees to time, to identify the delay of your board.

After that, it is up to you to determine if the 6024 board is suitable for your application. If you continue to use the 6024, you can modify your code to substact the error.

Till now, in my applications, I never used simultaneous sampling boards. In one application I used a 6025 to acquire 16 analog inputs (some DC, some 50Hz+harmonics), but I didn't need phase difference on these signals.

regards

baloss

 

 

0 Kudos
Message 5 of 8
(6,938 Views)
Baloss,

The actual delay between channels will depend several factors.  If you are using DAQmx 7.4 or later or the NI-DAQ Traditional driver the delay between samples is defined as follows:

The period of the max sampling rate of the card (1/200kS/s= 5us for the 6024E) + 10 us or 15us by default for your card.

However, if all the channels are sampled faster than would allow for such a delay the delay between each sample will be the longest possible for the sampling rate.  For example if you are sampling at 80kS/s on 2 channels the entire period for each scan should be 12.5 us much less than the 30us with the default delay.  The delay between channels in this case will be 12.5/2 = 6.25 us.

Hopefully this sheds some light on the actual delay between channels.  If you have more questions about this let me know.

Regards,

Neil S.
Applications Engineer
National Instruments

Message Edited by Neil S. on 07-19-2006 09:31 AM

0 Kudos
Message 6 of 8
(6,926 Views)
Thanks baloss and neil,

I will try your suggestions and hopefully, I can get something to work.  Here's another question you might help me with, otherwise I'll repost it as a new topic in the forum.  One of my signals (generated by a detector) has a large DC offset (>5V).   This means that  I have to set my gain to the lowest setting (+10-10V), which means my resolution is only 5mV.  This is not sufficent since my signal is on that order of magnitude.  My QUESTION is, do multidaq cards support the ability to redefine the ground or offset the signal so I can center my signal.  And I can't AC couple the signal.  The problem with AC coupling is that I am at such low frequencies that the signal is attenuated.  Thanks.

Cheers,
ScKaSx
0 Kudos
Message 7 of 8
(6,917 Views)

Hello ScKaSx,

about your signal with DC offset, I think the best solution is to acquire the signal in differential mode.

Maybe your detector (transducer?) has an output with the reference voltage used (5V). In this case you can simply use this output as an input for differential amplifier.

I see 2 other methods:

1) build a dedicated HW: filter your signal to determine DC component, then substract (op-amp) the signal with the filtered signal.

2) use an analog output to generate a suitable DC offset (5V), and use this output for differential analog input (input +, your signal; input -, analog output)

The first method require external HW and a large filter.

The second one maybe don't have the necessary accuracy, and require a dedicated algorithm if your DC offset vary into time...

Anyway, it is only an idea: I never tried it... but I am curious to know if you find the solution to fix this issue.

Regards

baloss

 

0 Kudos
Message 8 of 8
(6,895 Views)