/**
* Phase calculation
* @author maeda
* @date 2008/09/01
*/
#include "phase.h"
#define _USE_MATH_DEFINES
#include "math.h"
 
/**
* get the phase of the signal and normalize it to 0-1 range
* if we can't get the phase i.e. power=0, set -1
* @param in input data ( frequency domain )
* @param out 0-1 normalized phase ( frequency domain )
* @param rows ( width )
* @param cols ( height )
* @return true if succeed
*/
int maGetPhase2D( fftw_complex *data, double *out, int rows, int cols )
{
	int i,j;
	int idx;
 
	if( data==NULL || out==NULL ) return false;
	if( rows<0 || cols<0 )        return false;
 
	for( j=0; j<cols; j++ ){
		for( i=0; i<rows; i++ ){
			idx = j*rows + i;
			if( data[idx][0]!=0 || data[idx][1]!=0 ){
				// function atan2 returns [-M_PI, M_PI], so normalize it to 0-1
				out[idx] = 0.5 + 0.5*atan2( data[idx][1], data[idx][1] )/M_PI;
			}else{
				// if we can't decide phase, return -1
				out[idx] = -1;
			}
		}
	}
 
	return true;
}
最終更新:2008年09月24日 00:27