# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Align Two Signals and Measure the Phase Shift

Hello,

I am doing an experiment in which I use the NI USB-6221 DAQ board. The board is capable of taking 250k samples/second. I want to measure two voltages in a circuit and find the phase shift between them at frequencies between 1 and 10000. First I ouputted a sine wave of varying frequency through the board and applied it to a test circuit. Then I used the board to measure both voltages consecutively (thus reducing the maximum sampling rate to 125k). I used the align signals VI and measured both phases and then calculated the phase shift (VI attached as Phase 1). This worked fine for the test circuit that I built in which the phase shift increased logarithmically from .20 degrees to ~84.5 degrees and then leveled off. At frequencies above 5000 Hz the phase shift should have stayed constant, but it varied by plus or minus 1 degree. When the phase shift is 84.5 degrees, this one degree variability is not especially meaningful. When I applied my program to the circuit that I actually wanted to measure, the phase shift increased from -.5 degrees up to about 1.2 degrees. At high frequencies (>3000) the variation in phase shift values was about .2 degrees. Given the small phase shift, this variation is unacceptable. I have now tried to use a sequence in order to measure each voltage individually (increasing the maximum sampling rate to 250k again) and then align the two signals and measure the phase shift of each. When I use the Align and Resample Express VI to realign the two signals, I receive the message "Error 20333 Analysis:  Cannot align two waveforms with same dt if their samples are not clocked in-phase." Is there a way to align the two signals that I am describing here? I have attached the new VI as Phase 2

Message 1 of 14
(11,108 Views)

## Re: Align Two Signals and Measure the Phase Shift

Zipmeister,

Can you provide some data? Without hardware it is difficult to really see what is going on.  Run your program(s) until you have a typical dataset showing the effects you are questioning. Then Edit >> Make Current Values Default.  Save (or Save as... with a differnt name). The post those VIs.

You can do a lot of simplifying and size of block diagram reduction.  You do not need the sequence structures or local variables. Use dataflow. The Frequency to output variable formula nodes can be reduced to a case structure occupying less than one tenth the space. And it may be easier to determine what the output is for any specified frequency.  I have attached a cleaned up version of Phase 2.vi.

One of your issues is the sampling rate. Sampling a 5000 Hz signal at 125 kHz gets one sample every 14.4 degrees. Even though the Extract Single Tone Information.vi does some interpolation, there will be a limit to how much accuracy and resolution you can get on phase measurements at the higher frequencies.

I dug into the Align and Resample VI until I found the error you are getting.  Did you know that you cna open the front panel of Express VIs? Then you can look at the block diagram to see how they do it.  It appears that the error occurs if the differences between the timestamps (t0) of the waveforms does not meet some criteria related to the dt.

Lynn

Message 2 of 14
(11,088 Views)

## Re: Align Two Signals and Measure the Phase Shift

Thanks very much, Lynn. Your version is much more aesthetically pleasing (and presumably more effecient and functional) than mine. I've only been working with LabVIEW for a couple of months, so I have only a rudimentary understanding of much of this. I'll have access to the DAQ later this afternoon and will post the VI with data. I have the idea that I will want to change the time stamp values of the second set of data so that the two sets have the same timeframe. Should I generate the same signal again before measuring the second signal? Assuming that all other factors are equal, wouldn't this insure that each signal's time stamp is equivalent relative to its respective signal generation? In other words, would that make it so that the difference in time between each sample and the beginnning of the generation of the signal is the same for each both waves?

I agree with your analysis of significance of the sampling rate. That is my motivation for trying to ultilize the full 250k/s capacity of the DAQ board.

Matthew

Message 3 of 14
(11,074 Views)

## Re: Align Two Signals and Measure the Phase Shift

Matthew,

I do not have the DAQ Assistant available so I cannot tell exactly how you have things set up. It looks like you have separate DAQ Assistants for reading the two channels.  It might be better to set up on DAQ Assistant woth two channels and read both the excitation and response at the same time.  Then both will have the same t0 and dt.  The only timing difference will be the interchannel times.

Lynn

Message 4 of 14
(11,071 Views)

## Re: Align Two Signals and Measure the Phase Shift

Here's the VI with data. The reason that I can't just add another channel is that then the box can only sample each channel at 125k/s. If I use two DAQ assistants then the board can take 250k/s.

Message 5 of 14
(11,059 Views)
Solution
Accepted by topic author Zipmeister

## Re: Align Two Signals and Measure the Phase Shift

Matthew,

I think I have an idea of at least part of the problem.

I took your program with the data and removed the DAQ stuff.  I converted the Signal Out graph to a control and then looked at what was going on with the signal analysis.

The output of the Align Waveforms.vi has two waveforms, just like the input.  However, the Y arrays in both waveforms are empty!  It does not generate an error. After some head scratching, reading help files and trying some things, this is what I think is happening: The t0 times of the two input waveforms are 1.031 seconds apart. Since the wavefoms contain 1.000 seconds of data, there is no overlap and it cannot align them.

I changed the t0 on both waveforms to be the same and it aligns.  The number of elements in the Y arrays is reduced by one. Then I increased the t0 by 0.1 second on the first element. The output had both t0 larger than the input t0 by dt and the size of the Y arrays was 224998.  Reversing the t0 of the two elements shifts the phase in the opposite direction.

What this tells me is that you cannot reliably align two waveforms that do not overlap.

I suggest that you go to the 2-channel data acquisition and accept the reduced sample rate.  You will not get the resolution you want, but you should be able to tell whether anything important is happening.

You might be able to improve the equivalent resolution by taking multiple measurements with a slight phase shift. This is similar to the way the old (analog) sampling oscilloscopes worked. Take a series of measurements with the signal you are now using.  Do enough averaging to minimize the variation due to noise. Then shift the phase of the excitation signal by an amount which is smaller than the sampling rate phase resolution and repeat the measurements.  Recall that I calulated that for a 5 kHz signal sampled at 125 kHz, you get one sample every 14.4 degrees. If you offset the phase by 1 degree (at the mathematical/simulation point), you get a different set of samples for the excitation.  They are still separated by 14.4 degrees.  Take another series of measurements. Shift the phase another degree and repeat.  As long as your sampling clocks are stable enough that the frequency does not drift significantly (and it should not with your equipment), you should be able to get the resolution close to what you need.  The tradeoff is that you need to make many more measurements and may need to keep track of the phase shifts between measurements.

Lynn

Message 6 of 14
(11,039 Views)

## Re: Align Two Signals and Measure the Phase Shift

I guess, this VI should be able to help you out....hopefully 🙂

Message 7 of 14
(11,030 Views)

## Re: Align Two Signals and Measure the Phase Shift

Thanks so much Lynn! Your considerate assistance is unmatched by anything that I have previously encountered on the famously helpful NI LabVIEW forums. I shall try your idea about changing the phase shift. I think this sounds very promising. I am excited to try it out this weekend/next week and will let you know how it goes!

Message 8 of 14
(11,026 Views)

Lynn

Message 9 of 14
(11,014 Views)

## Re: Align Two Signals and Measure the Phase Shift

Is there anyway you could post those VIs in a version for labview 2011? It sounds just like something I need for my project!

Message 10 of 14
(10,239 Views)