Better estimation of phase difference between two signals with variable frequency!

Hello LabView Gurus, 
Being a power engineer and having just a little knowledge of signal processing and labview, I have been pulling my hair out for the last couple of days to get a better estimation of phase difference between two signals.
We have two analog voltage signals; 1. sine wave (50Hz ± 1Hz) and 2. a square wave with exactly half of sine wave frequency at any time.
At the starting point of operation (and simulation/acquisition) both signals will have no phase difference. However, the square wave's frequency changes unpredictably for a just a few millisecond but then it gets synchronized with sine wave's frequency again. This means that the square wave will be phased out from its original position. The task of the labview is to find the phase difference between the two signals continuously.
My approach to determine the phase difference is to measure the time when sine wave crosses zero amplitude and the time when the very next square wave changes amplitude from zero volts to +ve voltage (I have a 0.5volts threshold just to avoid any dramas from small line noise). The difference between these times is then divided by the time period and multiplied by 360 to get this phase difference in angles. 
As this part is just a small block of a big project, I can only allow 5000Hz sampling rate each for both signals. I read 500 samples (which means I read data from 5 cycles of sine wave and 2.5 cycles of square wave).
Now the problem is, as long as the frequency of sine wave stays constant at exactly 50Hz, I get a good estimation of the phase difference but when the frequency changes even a little (and it will happen in the real scenario i.e 50Hz ± 1Hz  and the square wave's frequency is dependent of sine wave's frequency), the estimation error increases.
I have attached my labview program. From front panel, you can set the phase of square wave (between -180 and 0) and you should see the labview's calculated phase in the indicator box named 'Phase'. Then you can press 'Real Frequency' switch that would cause the frequency to change like it would in real operation.
You can observe that the estimation error increases after you push the button. 
All I need to do is to reduce this estimation error and make it as close to the actual phase difference as possible. Any help would be greatly appreciated.
I am using LabView 2009 for this task.
The application is for electric machines and the stability/performance of machines under different faults.
Thank you for reading this far!
Regards,
Awais
Attachments:
v603.png ‏320 KB
v603.vi ‏186 KB

Jeff Bohrer wrote:
Basic math gives me a bit of pause on this approach.  You are sampling at 50 times the frequency of interest so you get 50 samples per cycle.  your phase resolution is 1/50th cycle or 7.2 degrees +/- noise.  You will need to samlpe faster to reduce phase resolution or average multiple readings (at a time cost that is signifigant)
Jeff- (Hardly Working)
I am sampling at 100 times the sine wave's frequency and 200 times the square wave's frequency.  Increasing the sampling rate completely solves my problem. But since I am acquiring several other inputs, I cannot afford a sampling rate higher than 5kHz.
F. Schubert wrote:
I'm not a signal processing expert, but here my basic understanding.
If you simulate sampling with 5kHz and a frequency of 50 Hz (and both are 'sync' by design), you always get an exact 5 periods. Any variation of your signals frequency gives you a propability to get 4 or 6 'trigger' events. That's an up or down of 20%!
The one measure to reduce such problems is using 'window functions'. They don't fit your current approach (counting instead of a DSP algorithm), so this needs to be reworked as well.
My approach would be to use the concept of a Locki-In amplifier. You need to phaseshift your ref-signal by 90°. Then multiply your measurement signal with the ref signal and the phase shifted ref signal. The obtained values for x/y coordinates of a complex number. Calculate the theta of the complex number (with the LV prim). Feed this in a low pass filter.
The trick on this is, that the square wave has harmonics in it, in this you are interested in the second harmonic which is the sine wave.
To get rid of the effect that the sync between sampling rate and ref signal frequency gives an error, you then can use the window I mentioned above (place it before the lock-in).
For a design that really plays well, use a producer-consumer design pattern to get the calculations done in parallel with the DAQ.
I suggest you to check on wikipedia for some of the keywords I mentioned. Go also for the external links which lead to great tutorials and AppNotes on the signal processing basics.
Sorry, it's not a simple solution I offer and we will have quite some conversation on this forum if you follow this path. Maybe someone else knows a simpler way.
Felix
www.aescusoft.de
My latest community nugget on producer/consumer design
My current blog: A journey through uml
An interesting view. the sine wave can indeed be looked as a second harmonic of the square wave. I will implement your idea and get back to you as soon as I get some results. But since I have very limited knowledge of signal processing, it might take me a while to get my hear around the solution you mentioned.

Similar Messages

  • How to find the phase difference between two signals using Hilbert transform

    hi, 
        I am new to LabView.... I am trying to find phase difference between two signals. I sucessfuly found out the phase difference between two predefined waves using single tone measurement. .... But I really want to know how can I measure phase difference between two signals( not predefined... ie we don't know the initial conditions) using hilbert transform or any transformation techniques (without using zero cross detection).. I tried by using hilbert transform based on algorithm... bt I am getting error.... plz help me
    Attachments:
    phase_differece.vi ‏66 KB

    you could try something similar to this, for each table pair that you want to compare:
    SELECT 'TABLE_A has these columns that are not in TABLE_B', DIFF.*
      FROM (
            SELECT  COLUMN_NAME, DATA_TYPE, DATA_LENGTH
              FROM all_tab_columns
             WHERE table_name = 'TABLE_A'
             MINUS
            SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
              FROM all_tab_columns
             WHERE table_name = 'TABLE_B'
          ) DIFF
    UNION
    SELECT 'TABLE_B has these columns that are not in TABLE_A', DIFF.*
      FROM (
            SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
              FROM all_tab_columns
             WHERE table_name = 'TABLE_B'
             MINUS
            SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
              FROM all_tab_columns
             WHERE table_name = 'TABLE_A'
          ) DIFF;that's assuming, column_name, data_type and data_length are all you want to compare on.

  • How to calculate the phase difference between two square wave (acquired from two channel in one DAQ)

    Hello everyone ,
     I need a quickly help that as below :
    I am trying use PCI-6220 to acquired six signals from one rotation encoder (channel A, channel B,channel Z ,and their non-signals) .The encoder out signals will be square wave and 4000pulses per revolution. I set it rotation at 300rpm speed. I need show every square wave of six out channels in waveform and measure the A-B phase difference to check if the value is correct (designed value should be 90deg) .
    I have no idea how to measure or calculate the phase difference of two square wave base on synchronizate to acquire these two square wave from two channel on the PCI-6620....
    Anyone can give a idea how to calculate the phase difference with two square wave ?
    Thank a lot and Thanks again...
    Tim

    Tim,
    Here is a simple rising edge detector for one channel.
    Lynn
    Attachments:
    Rising edge.vi ‏15 KB

  • Measure phase difference between 2 signals

    I am very new to labview. i m trying to use labview to measure phase between 2 signals from encoder. I also want to measure the state of 2 quadrature square signals. anyone has any idea how to do it?

    Hi "rvn10",
    Quadrature encoders do by nature have a phase difference of 90 degrees between channel A and B. This phase difference is required to know in which direction the shaft is rotating when a transition from one of the channels is received. Please see these two links for some useful information:
    Encoders
    Linear and Rotary Encoders
    Quadrature Encoder / Position Measurement
    If your goal is to measure the angular position using the two quadrature square signals, then your task is quite simple if you use one of our Counter/Timer cards. Check for example the shipping example called "Measure Angular Position.vi".
    If you want to read more about our most affordable Counter/Timer product, then check this link:
    NI PCI-6601
    Please let us know if you are using different hardware to acquire the signals.
    - Philip Courtois, Thinkbot Solutions

  • Can counter measure pulse width difference between two signals

    I am interested in determining the high pulse width time that two signals have when they are in various phases between eachother.

    Why don't you try one of the pulse width examples. I don't know what ADE you are using, but you can search for examples (http://www.ni.com/devzone/dev_exchange/ex_search.htm) with the key words "+pulse +width" and find several examples in different languages. This will measure the pulse width of one signal, but you can program most of the counters to do multiple tasks at the same time. Just copy the code twice and change the counter number to program counter 2 as well. If you want the ORed pulse width time, I would recommend adding a OR gate to your circuit and then feeding that into a counter.
    I hope this helps.
    Joshua

  • Calculating difference between two dates in Variable Exits

    Hi,
          I need to calculate the difference between the two dates in months in Variable exits. I want the result in months so that I need to find out the Average Usage.
            How can I find the difference between them in months.
    Thanks
    Dave.

    Hi Christophe, Senthil
                                       Sorry for late response, I checked this  MONTHS_BETWEEN_TWO_DATES_NEW  in SE37/SE38 and about the date they are going to enter at query run time it should be like 03/03/2006 to 08/03/2006 and I should get in 5 months and or if the date range 03/15/2006 to 08/03/2006 and I should get like 4.5 months. Please could you tell me how to get the values in months or please give me a functional module so that I can use.
    Thanks
    Dave.

  • Difference between two dates, using Variables in reporting

    how to calculate the days difference between Delivery date and Billing date in reporting, using variables (SAP BW 3.5v)?

    Hi,
    If you want to do the arethmatioc operations with the date then you will have to declare the date as key figure in the cube and add it there and then update it with the date from R/3.
    You can use formula variable for this subtraction.
    Create a formula in the bex analyzer and then in the down you will get option for the formula varibale.Just make a replacement path varibale on this time characteristic for which yuou want subtraction.
    During creation use "key" in the display and "number" as the data type.
    If number is not working them you can use "date" type.
    Nothing alse needs to be done during the variable creation.
    Once you have made the formula inlcude it in the columns of the report.
    Thanks

  • Calculated columns to get difference between two dates with half day

    Hi,
    So there is the problem I'm using a calculated column to track the number of day when an employee make a vacation request. I'm already taking weekends of the case but now I would need to be able to ask for only a half day off.
    How could I perform that without taking out my first requirement?
    Just in case there is my current formula:
    =IF(AND((WEEKDAY([EndDate],2))<(WEEKDAY([StartDate],2)),((WEEKDAY([StartDate],2))-(WEEKDAY([EndDate],2)))>1),(((DATEDIF([StartDate],[EndDate],"D")+1))-(FLOOR((DATEDIF([StartDate],[EndDate],"D")+1)/7,1)*2)-2),
    Thanks for your answers :)  

    Hi amber,
    you can simple calculate.
    try this:
    DATA: date1 like sy-datum,
          date2 like sy-datum.
    data  I type i.
    date1 = '20070301'.
    date2 = '20070228'.
    i = date1 - date2.
    write: / date1, date2, i.
    date1 = sy-datum.
    date2 = sy-datum - 30.
    i = date1 - date2.
    write: / date1, date2, i.
    Regards, Dieter

  • Phase difference from 2 signals(HELP!)

     Hi! I'm having trouble finding accurate phase difference between two signals.. one signal is from the input serial communication arduino and the second signal is a simulated signal. I want to find the phase difference between these two. I've googled a lot and found many similar threads of phase difference measurements, but it won't work.
    The phase angle from the simulated signal is correct but the reading of the phase angle from the input signal keeps changing and I could not find an exact phase angle or phase difference.
    What do you think I should do?

    Doing phase difference measurements using two completely separate sets of hardware is in general a bad idea. You need to think carefully about how to synchronize the acquisition clocks of the computer vs. the Arduino. Better would be to measure your signal and generate your reference using one system, i.e. every time through the Arduino's outer loop, have it measure the voltage and also generate a reference level based on the internal clock, and transfer both to the computer.
    That said, unless you know the signal frequency very precisely a priori, the phase difference between a synthetic reference and a measured signal will inevitably drift with time, at a rate proportional to the difference between the nominal and actual frequencies of the sample signal. 

  • How can I measure the phase difference of two input signal?

    Hello,
    I'm trying to measure the phase difference of two input sine waves with the
    same frequency.
    Is there anybody how I can do this with labview?
    Thanks,
    Dae-han

    This is my case! I have TDS3034 which can measure phase. However when I open Read Waveform Measurement vi that comes with tkds30xx.llb, I have a number of options under "measurement function" but not the phase measurement. May be I need another vi to do this?

  • Difference between two files

    Hi, I need to find difference between two files.
    This is an example:
    oldfile.txt:
    This is the old file.
    I need some help with
    this task!.
    newfile.txt:
    This is the new file.
    I need help with this task please!.
    Differences:
    * old -> new
    * need -> need
    * "some" was deleted
    * please was added
    Notice that I don't care spaces, new lines or tabs.
    The code should looks like:
    enum DifferenceType { Added, Deleted, Replaced };
    class DiffText {
      public String Text;
      public DifferenceType Type;
      public String ReplacedText;
      Diff d = new Diff("oldfile.txt","newfile.txt");
      while(!d.end()) {
        DiffText dtext = d.getNext();
       String s = "";
        switch(s.Type) {
          case Added:       System.out.println(s.Text + " ~ added");
          case Deleted:    System.out.println(s.Text + " ~ deleted");
          case Replaced: System.out.println(s.Text + " replaced by " + s.ReplacedText);
      }Thanks for your help in advance.

    Suppose you have two arrays filled with integers.
    What you want to do is map the integers from one
    array to the other.
    In your example, your arrays would look like:
    this is the old file i need some help with this task
    [0 1 2 3 4 5 6 7 8 9 0 10]
    this is the new file i need help with this task please
    [0 1 2 11 4 5 6 8 9 0 10 12]
    Here, I've assumed you've removed all punctuation and
    new line characters, and changed to lowercase.
    The easiest way is to walk through the arrays greedily.
    However, you may be able to research algorithms that
    do a better job of matching.

  • Difference between Temp table and Variable table and which one is better performance wise?

    Hello,
    Anyone could you explain What is difference between Temp Table (#, ##) and Variable table (DECLARE @V TABLE (EMP_ID INT)) ?
    Which one is recommended to use for better performance?
    also Is it possible to create CLUSTER and NONCLUSTER Index on Variable table?
    In my case: 1-2 days transactional data are more than 3-4 Millions. I tried using both # and table variable and found table variable is faster.
    Is that Table variable using Memory or Disk space?
    Thanks Shiven:) If Answer is Helpful, Please Vote

    Check following link to see differences b/w TempTable & TableVariable: http://sqlwithmanoj.com/2010/05/15/temporary-tables-vs-table-variables/
    TempTables & TableVariables both use memory & tempDB in similar manner, check this blog post: http://sqlwithmanoj.com/2010/07/20/table-variables-are-not-stored-in-memory-but-in-tempdb/
    Performance wise if you are dealing with millions of records then TempTable is ideal, as you can create explicit indexes on top of them. But if there are less records then TableVariables are good suited.
    On Tables Variable explicit index are not allowed, if you define a PK column, then a Clustered Index will be created automatically.
    But it also depends upon specific scenarios you are dealing with , can you share it?
    ~manoj | email: http://scr.im/m22g
    http://sqlwithmanoj.wordpress.com
    MCCA 2011 | My FB Page

  • SSRS expression for difference between two currency values

    Hi All;
    I need to get the difference between two currency values
    Below is my report
    I need to get differene between Actual Income and Profile Income and this value should be in "£" In this example it should be £0.00
    Any help much appreciated
    Thanks
    Pradnya07

    =sum(Fields!ProfileIncome.Value - Fields!ActualIncome.Value)
    This could have either field first, but depends on which field is expected to be larger. You would then format the textbox to show in pounds.
    HTH

  • As to the data type of the data type of the difference between two date type of datas

    Hi,
    I have a question about the data type of the difference between two date type of datas.
    There are two date type of datas as:
    SSHIPMENTS.RECEIVEDATETIME
    SSHIPMENTS.PROMISEDATETIME
    I try to use the following SQL Script in Oracle SQL*Plus as:
    SELECT CASE
    WHEN (SSHIPMENTS.RECEIVEDATETIME - SSHIPMENTS.PROMISEDATETIME) < '000 01:00:00.000' THEN 'OnTime'
    WHEN (SSHIPMENTS.RECEIVEDATETIME - SSHIPMENTS.PROMISEDATETIME) < '000 01:30:00.000' THEN '60-89 Minutes'
    ELSE '3+ Hours'
    END
    FROM SSHIPMENTS;
    The error message of "Invalid Number" for the '000 01:30:00.000' happens.
    I don't know if the data type of the interval is wrong.
    Many Thanks,
    Cathy

    SELECT CASE
    WHEN (to_char(SSHIPMENTS.RECEIVEDATETIME,'hhmiss') - to_char(SSHIPMENTS.PROMISEDATETIME,'hh24miss')) < '010000' THEN 'OnTime'
    WHEN (to_char(SSHIPMENTS.RECEIVEDATETIME,'hhmiss') - to_char(SSHIPMENTS.PROMISEDATETIME,'hh24miss'))< '000 01:30:00.000' THEN '60-89 Minutes'
    ELSE '3+ Hours'
    END
    FROM SSHIPMENTS;
    just try it out..

  • Need to find the Difference between two table

    Hello ,
    I have stucked in program as below scenario:-
    I have two tables of huge data of same structure in a same schema.I need to find the difference exact values in tables.
    By using MINUS we can find the difference between two table ,i need to find the what exact difference in the there values with colunm and value at that column.
    Example TableA
    Col1 col2 col3 col4 col5.... col50
    10 ABC 2001 EE 444 TT
    40 XYZ 3002 RR 445 TT3
    80 DEF 6005 YY 446 YY8
    TableB
    Col1 col2 col3 col4 col5.... col50
    10 ABC 2001 EE 444 TT
    40 XYZ 3002 RR 445 TT3
    81 DEF 6005 Yu 447 YY8
    I need to the out put like this :-
    The Diffence between two table is
    TableA.COL1=80 TableB.Col1=81, Different
    TableA.Col4=YY TableB.col4=Yu,Different
    TableA.Col5=446TableB.col5=447,Different
    Please suggest me to write the pl/sql program for the same
    thanx in advance
    KK

    Thanx friends for all your efforts
    I have a sample code for the same,this will compare the two tables for single row in each table .
    what r the modification needed for the multiple rows of values in the two tables??
    Please suggest!!
    CREATE OR REPLACE PROCEDURE test_compare
    IS
    TYPE t_col
    IS
    TABLE OF VARCHAR2 (30)
    INDEX BY PLS_INTEGER;
    l_col t_col;
    j NUMBER := 0;
    l_sql VARCHAR2 (2000);
    col1 VARCHAR2 (30);
    col2 VARCHAR2 (30);
    val1 NUMBER;
    val2 NUMBER;
    status VARCHAR2 (30);
    CURSOR c1
    IS
    SELECT column_id, column_name
    FROM all_tab_columns
    WHERE table_name = 'TEST1';
    BEGIN
    FOR i IN c1
    LOOP
    j := j + 1;
    l_col (j) := i.column_name;
    END LOOP;
    FOR k IN 1 .. j
    LOOP
    l_sql :=
    'SELECT '
    || ''''
    || l_col (k)
    || ''''
    || ', '
    || 'TEST2.'
    || l_col (k)
    || ', '
    || ''''
    || l_col (k)
    || ''''
    || ', '
    || 'TEST1.'
    || l_col (k )
    || ', '
    || 'DECODE(TEST2.'
    || l_col (k)
    || ' -TEST1.'
    || l_col (k)
    || ', 0, ''NO CHANGE'', ''CHANGED'') FROM TEST2, TEST1';
    EXECUTE IMMEDIATE l_sql INTO col1, val1,col2, val2, status;
    IF status = 'CHANGED'
    THEN
    DBMS_OUTPUT.put_line( 'TEST2.'
    || col1
    || '='
    || val1
    || ', TEST1.'
    || col2
    || '='
    || val2
    || ', '
    || status);
    END IF;
    END LOOP;
    EXCEPTION
    WHEN OTHERS
    THEN
    DBMS_OUTPUT.put_line ('Error:- ' || SQLERRM);
    END;
    /

Maybe you are looking for