From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Cross Correlation?

Hello,

 

Here is what I am trying to accomplish but stuggling to make progress.  The idea is see if a machine is running accoustically similar before and after downtime.  The idea I have is to use two .wav files recoreded and compare them to eachother.  I am trying to get a single % value of similarity so anyone could walk up, do this, and understand it.  I was thinking of using the cross correlation vi but I have not had any luck get to the final % number.  As a current test I am using the Simulate Signal VI and feeding the same signal into X/Y of the Cross Correlation VI.  I have the Cross Correlation VI set to unbiased/frequency domain.  After this is what I am stuck though, the output of Rxy is displaying in an array with numbers varying widely.

 

Just as another test, I used the X/Y graph VI to make sure the signals were aligned correctly and it plots a perfect diagonal line so I am confinident I set up the singals to mach properly.  The reason I am trying to do Cross Correlation is I thought it would account for any delay between the two signals and could give me the similarity in a single % form that is easy for anyone to read.  If there is a better method or idea, I am open to ideas.

 

Thank you!

0 Kudos
Message 1 of 13
(5,548 Views)

My solution doesn't have a mathematical basis for being correct, but I think it will give you something like what you want.

 

Given signal A and signal B

Max_Corr = Max( xcorr(A, B) )

Max_Corr_A = Max( xcorr(A, A) )

Max_Corr_B = Max( xcorr(B, B) )

Max_Corr_Possible = Max( Max_Corr_A, Max_Corr_B )

Percentage = Max_Corr / Max_Corr_Possible

 

Note: This is for xcorr with normalization set to 'none'

0 Kudos
Message 2 of 13
(5,517 Views)

That is the idea but also doing normalization.  I was just trying to find a way using the Cross Correlation VI instead of building an array for each singal and going through all the mathematical steps.

0 Kudos
Message 3 of 13
(5,473 Views)

@WallyX wrote:

I was just trying to find a way using the Cross Correlation VI instead of building an array for each singal and going through all the mathematical steps.


What do you mean by that? The cross correlation always works on arrays.

 

Of course there is also a ptbypt version. try to use that instead.

 

Maybe all you need is the absolute max amplitude of the normalized cross correlation.

0 Kudos
Message 4 of 13
(5,450 Views)

Sorry for the confusion.  I'll try to explain my problem better this time.  I have attached a very simple way of how I am trying to do this as a test.  I have signal A and B, and I want to see how silimar B is to A.  I went down the path of cross correlation with normalization because to my understanding I can obtain similar signal B is to A reguardless of a time shift which could be possible in the .wav files later, as the signals may not align pro 

 

So to test used the simulate signal and feed in the same signal to X/Y on the cross correlation vi.  I then put it to unbiased for normalization and direct(from reading the help, direct should be ok with this application but frequency shouldn't be bad either) for algorithm.  This is when I get stuck, I expect an output of 1 as the signals are exactly the same but the Rxy output is in the form of an array has values that very widely.  This could be a miss understanding of how to do what I am looking for or how the VI works. (Array useage/manipulation is not a strong area for me)

 

As a side note, I have tried several of the different signal genertors and all have done the same thing. 

Cross.JPG

0 Kudos
Message 5 of 13
(5,407 Views)
Have you tried graphing the results. Could it be most values are very close to zero, but the exponent is truncated because your indicators are not wide enough?

(Sorry, posting by phone, cannot test).
0 Kudos
Message 6 of 13
(5,390 Views)

The simulated signal is set to f=50, Amp=2, Samples per sec=1000, and number of samples=100.  Here is the input and output graphed.Graphs.JPG

0 Kudos
Message 7 of 13
(5,361 Views)

I went ahead and just built out the cross correlation formula instead of using the VI.  I also am using two .wav files and I got my expected result of 1 when using the same file.  However when I tried to use two different .wav files, I get an unexpected problem when doing the dot product of the two signals (ie A dot B).  I know this is a Not a Number result from doing a calulation with part of the array not being initialised but I am unsure how to correct it. 

Xcor.PNG

0 Kudos
Message 8 of 13
(5,287 Views)

I don't know what's going wrong with your vi, perhaps the arrays are of different lengths? (check the error output of the dot product vi, always check errors, especially if you are getting erroneous results)

 

Anyways, cross correlation is NOT just a dot product. It is a dot product at every possible offset of the two signals (pretty much), which is why it has an array as an output (one output for each offset). (I guess it would be more appropriate to say that cross correlation is a function having a parameter of offset and returning the dot product with the signals offset. However most cross-correlation functions you are bound to come accross give the cross-correlation for each offset)

 

Here, take a look at the implementation of the suggestion I gave in my previous post.

cross correlation similarity.png

Message 9 of 13
(5,263 Views)

Hi there,

 

I guess i have a similar problem in understanding normalized Xcorr. I always thought, that if a signal A is equal a signal B (A=B), you get a peak of 1 at the position where the signal A fits best to the signal B. Is that correct?

 

I dont get a coeff 1 in any kind of normalisation. see images tried with some generated pulse pattern. same signal (neg), same array length (sampling)

 

GybrushThreepwood_1-1583936620845.png

 

 

I want to use that feature for a kind of "pattern recognition". So I thought, if I just use a smaller part of signal A and corellate it with A itself, I should get a position with the highest correlation peak where this small part best fits to the rest of A? Is that correct too, or am I missing something?

 

cause when i try it it doesn't seem to work at all. I even tried  kuind of zeropadding to make the signals to same length. im confused..

 

Here the test images: left up: base signal (A) the lines mark the part of the part i use as pattern (up right / part of A) to detect.. but i cant see any peak in the correlation (down left).. I just see the same base signal... smoothed. (image left down) I then tried to split the base signal to loop the correlation with the pattern signal at same array length (down right). but the result seems similar. 

 

GybrushThreepwood_0-1583936201406.png

I got stuck with this. 😐 maybe someone can help?

 

Thanks in advance!!

 

Best Gybrush

 

0 Kudos
Message 10 of 13
(3,206 Views)