// Author: Ce Liu (c) Dec, 2009; celiu@mit.edu // Modified By: Deepak Pathak (c) 2016; pathak@berkeley.edu #ifndef STOCHASTIC_H #define STOCHASTIC_H #include "math.h" #include "stdlib.h" #include "project.hpp" #include "memory.h" #define _Release_2DArray(X,i,length) for(i=0;i=0)?x:-x #endif #ifndef PI #define PI 3.1415927 #endif enum SortType{SortAscending,SortDescending}; class CStochastic { public: CStochastic(void); ~CStochastic(void); static void ConvertInt2String(int x,char* string,int BitNumber=3); static double UniformSampling(); static int UniformSampling(int R); static double GaussianSampling(); template static void GetMeanVar(T* signal,int length,double* mean,double* var); static int Sampling(double* Density,int NumSamples); static double GetMean(double *signal,int length); static void Generate1DGaussian(double* pGaussian,int size,double sigma=0); static void Generate2DGaussian(double* pGaussian,int size,double sigma=0); static double entropy(double* pDensity,int n); template static T sum(int NumData,T* pData); template static void Normalize(int NumData,T* pData); template static T mean(int NumData, T* pData); template static void sort(int number, T* pData,int *pIndex,SortType m_SortType=SortDescending); template static T Min(int NumData, T* pData); template static T Min(int NumData, T* pData1,T* pData2); template static T Max(int NumData ,T* pData); template static int FindMax(int NumData,T* pData); template static void ComputeVectorMean(int Dim,int NumData,T1* pData,T2* pMean,double* pWeight=NULL); template static void ComputeMeanCovariance(int Dim,int NumData,T1* pData,T2* pMean,T2* pCovarance,double* pWeight=NULL); template static double VectorSquareDistance(int Dim,T1* pVector1,T2* pVector2); template static void KMeanClustering(int Dim,int NumData,int NumClusters,T1* pData,int *pPartition,double** pClusterMean=NULL,int MaxIterationNum=10,int MinClusterSampleNumber=2); template static double norm(T* X,int Dim); template static int FindClosestPoint(T1* pPointSet,int NumPoints,int nDim,T2* QueryPoint); template static void GaussianFiltering(T1* pSrcArray,T2* pDstArray,int NumPoints,int nChannels,int size,double sigma); }; template void CStochastic::GetMeanVar(T* signal,int length,double* mean,double* var) { double m_mean=0,m_var=0; int i; for (i=0;i T CStochastic::sum(int NumData, T* pData) { T sum=0; int i; for(i=0;i void CStochastic::Normalize(int NumData,T* pData) { int i; T Sum; Sum=sum(NumData,pData); for(i=0;i T CStochastic::mean(int NumData,T* pData) { return sum(NumData,pData)/NumData; } //////////////////////////////////////////////////////////// // sort data in descending order template void CStochastic::sort(int Number,T* pData,int *pIndex,SortType m_SortType) { int i,j,offset_extreme,*flag; double extreme; flag=new int[Number]; memset(flag,0,sizeof(int)*Number); for(i=0;ipData[j])) { extreme=pData[j]; offset_extreme=j; } } pIndex[i]=offset_extreme; flag[offset_extreme]=1; } delete flag; } template T CStochastic::Min(int NumData,T* pData) { int i; T result=pData[0]; for(i=1;i T CStochastic::Min(int NumData,T* pData1,T* pData2) { int i; T result=pData1[0]+pData2[0]; for(i=1;i T CStochastic::Max(int NumData,T* pData) { int i; T result=pData[0]; for(i=1;i int CStochastic::FindMax(int NumData,T* pData) { int i,index; T result=pData[0]; index=0; for(i=1;iresult) { index=i; result=pData[i]; } return index; } template void CStochastic::ComputeMeanCovariance(int Dim,int NumData,T1* pData,T2* pMean,T2* pCovariance,double* pWeight) { int i,j,k; memset(pMean,0,sizeof(T2)*Dim); memset(pCovariance,0,sizeof(T2)*Dim*Dim); bool IsWeightLoaded=false; double Sum; if(pWeight!=NULL) IsWeightLoaded=true; // compute mean first Sum=0; if(IsWeightLoaded) for(i=0;i void CStochastic::ComputeVectorMean(int Dim,int NumData,T1* pData,T2* pMean,double* pWeight) { int i,j; memset(pMean,0,sizeof(T2)*Dim); bool IsWeightLoaded; double Sum; if(pWeight==NULL) IsWeightLoaded=false; else IsWeightLoaded=true; Sum=0; if(IsWeightLoaded) for(i=0;i double CStochastic::VectorSquareDistance(int Dim,T1* pVector1,T2* pVector2) { double result=0,temp; int i; for(i=0;i void CStochastic::KMeanClustering(int Dim,int NumData,int NumClusters,T1* pData,int *pPartition,double** pClusterMean,int MaxIterationNum, int MinClusterSampleNumber) { int i,j,k,l,Index,ClusterSampleNumber; double MinDistance,Distance; double** pCenters; pCenters=new double*[NumClusters]; for(i=0;i double CStochastic::norm(T* X,int Dim) { double result=0; int i; for(i=0;i int CStochastic::FindClosestPoint(T1* pPointSet,int NumPoints,int nDim,T2* QueryPoint) { int i,j,Index=0,offset; T1 MinDistance,Distance,x; MinDistance=0; for(j=0;j void CStochastic::GaussianFiltering(T1* pSrcArray,T2* pDstArray,int NumPoints,int nChannels,int size,double sigma) { int i,j,u,l; double *pGaussian,temp; pGaussian=new double[2*size+1]; Generate1DGaussian(pGaussian,size,sigma); for(i=0;i