The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Correlation
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

geoff.brow1

UK
2 Posts

Posted - 09/09/2004 :  12:21:06 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin):
Operating System:WinXP
V7.5SR4 7.5853(B853)

Problem with correlation.

If I generate two data columns of sin(x) and cos(x) and correlate I do not get a phase shift of 90 deg, rather 81deg. What have I done wrong?

Geoff

easwar

USA
1965 Posts

Posted - 09/10/2004 :  1:27:48 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Geoff,

The menu command, and associated internal code, uses the FFT object to compute the correlation. I suspect the "wrong" peak is because the FFT object requires the input data size to be a power of 2, and if not it pads the input with zeroes - this may be leading to the input signals to the correlation code not being pure sin and cos and so the peak is shifted...

Since you have version 7.5, you could instead use the NAG FFT to perform the correlation. The NAG FFT does not have the restriction of input size to be a power of 2.

In fact, a function for performing correlation using NAG FFT alredy exists in Origin 7.5. It accepts and returns vectors. I have pasted below a small Origin C function which calls this internal function, and gives access to this capability to you in the script window.

In order to use this, you need to set this function up by compiling etc. one time, as follows. Once compiled and ready, can use any time from script window:

1> Go to Code Builder (Origin menu command View->Code Builder)
2> In CB, use the menu command File->New to create a new Origin C file - give it a name such as FFT Correlation.c
This will add the new file to the User branch of the CB workspace tree.
3> Cut and paste the code from below into the edit window for that file - note that the include statement for Origin.h would be already there, and you just need to paste the code from here, starting with the include statement for fft_utils.h
4> Right-click on the System branch of the CB workspace tree, then select "Add Files" from the context menu
5> Navigate to the OriginC->OriginLab subfolder under the Origin EXE installation path and select the file fft_utils.c and click Open in the file dialog to add this file to system branch
6> Compile your workspace. This will compile the function fft_correlate in your new C file
7> Drag the C file from the User branch of the CB workspace tree and drop it on the System branch.
This step is optional, but if you do this, the function fft_correlate will be available in every Origin session from now on - otherwise may have to add back to workspace if your workspace changes etc.

Once the above is done, you can any time go to the script window and use the function.

8> For example, set up Data1 worksheet with four columns A, B, C, D
9> Fill A with (i-1) from 1 to 360 - so will have numbers 0 to 359
10> Fill B with cos(col(1))+ran()/3
ran() just adds some noise to the cos curve
11> Fill C with sin(col(1))+ran()/3
12> Now go to the script window and type
fft_correlate data1_b data1_c data1_d
The correlation result will appear in col(D) - in this case it will be a sin curve with peak at 89 (not 90, because we started with 0 in col A) such as in image pasted below


Easwar
OriginLab


#include <fft_utils.h>

////////////////////////////////////////////////////////////////////////////////////

// Perform correlation of two signals.
// Input:
// strSignal1: Name of signal 1 dataset
// strSignal2: Name of signal 2 dataset
// strResult: Name of dataset to hold correlation result
// iNorm: Optional - normalize correlation result to +/- 1
//
////////////////////////////////////////////////////////////////////////////////////
void fft_correlate(string strSignal1, string strSignal2, string strResult, int iNorm = 1)
{
// Declare datasets and check validity
Dataset dsSignal1( strSignal1 );
Dataset dsSignal2( strSignal2 );
Dataset dsResult( strResult );
// If any dataset is not valid, report error and return
if( !dsSignal1 || !dsSignal2 || !dsResult )
{
printf("Invalid Dataset(s)!\n");
return;
}

// Set normalization flag
bool bNorm = true;
if( 1 != iNorm ) bNorm = false;

// Copy datasets to vectors and call the internal function to compute correlation
vector vecSignal1, vecSignal2, vecCorrelation;
vecSignal1 = dsSignal1;
vecSignal2 = dsSignal2;
int iRet = fft_correlate( vecSignal1, vecSignal2, vecCorrelation, bNorm );

// If success, copy result vector to result dataset
if( 0 == iRet ) dsResult = vecCorrelation;
// If failure, report error code
else
printf("Failed to compute correlation. Error code: %d\n", iRet);
}
////////////////////////////////////////////////////////////////////////////////////








Go to Top of Page

geoff.brow1

UK
2 Posts

Posted - 09/10/2004 :  2:04:11 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks for that. I shall try it now. In the meantime, I did infact use a set of data points that was a power of 2, 512!!
Geoff


Edited by - geoff.brow1 on 09/10/2004 2:05:37 PM

Edited by - geoff.brow1 on 09/10/2004 2:06:21 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000