/user/chenyk/cemdutil1.c

https://github.com/ahay/src · C · 750 lines · 551 code · 86 blank · 113 comment · 87 complexity · 7616fca921a30011d8843f031acdf576 MD5 · raw file

  1. #include <rsf.h>
  2. #include "cemdutil1.h"
  3. #define DEFAULT_THRESHOLD 0.05
  4. #define DEFAULT_TOLERANCE 0.05
  5. #define DEFAULT_NBPHASES 4
  6. #define MAX_ITERATIONS 1000
  7. #define LIM_GMP 30000
  8. #define NBSYM 2
  9. #define SQUARE(A) (A*A)
  10. #define CUBE(A) (A*A*A)
  11. #define GREATER(A,B) ((A) >= (B) ? (A) : (B))
  12. #define SMALLER(A,B) ((A) < (B) ? (A) : (B))
  13. #ifdef C99_OK
  14. #include <complex.h>
  15. #define COMPLEX_T double complex
  16. #define CREAL creal
  17. #define CIMAG cimag
  18. #define CABS cabs
  19. #else
  20. #define COMPLEX_T complex_data_t
  21. #define CREAL emd_creal
  22. #define CIMAG emd_cimag
  23. #define CABS emd_cabs
  24. #endif
  25. /*^*/
  26. #ifndef _cemdutil1_h
  27. //emd_complex.h
  28. #ifndef EMD_COMPLEX_H
  29. #define EMD_COMPLEX_H
  30. typedef struct {
  31. double r;
  32. double i;
  33. } complex_data_t;
  34. /*^*/
  35. #endif
  36. //cextr.h
  37. #ifndef CEXTR_H
  38. #define CEXTR_H
  39. /* structure used to store the local extrema of the signal */
  40. typedef struct {
  41. int n_min;
  42. int n_max;
  43. int *ind_min;
  44. int *ind_max;
  45. double *x_min;
  46. double *ry_min;
  47. double *iy_min;
  48. double *x_max;
  49. double *ry_max;
  50. double *iy_max;
  51. } extrema_t;
  52. /*^*/
  53. #endif
  54. //interpolation.h
  55. #ifndef INTERPOLATION_H
  56. #define INTERPOLATION_H
  57. #endif
  58. //cio.h
  59. #ifndef EMD_IO_H
  60. #define EMD_IO_H
  61. typedef struct {
  62. float threshold;
  63. float tolerance;
  64. } stop_t;
  65. /*^*/
  66. /* structure used to store an IMF and the associated number of iterations */
  67. typedef struct i {
  68. int nb_iterations;
  69. COMPLEX_T *pointer;
  70. struct i *next;
  71. } imf_t;
  72. /*^*/
  73. /* structure of the IMF list */
  74. typedef struct {
  75. imf_t *first;
  76. imf_t *last;
  77. int m;
  78. int n;
  79. } imf_list_t;
  80. /*^*/
  81. #endif
  82. //clocal_mean.h
  83. #ifndef CLOCAL_MEAN_H
  84. #define CLOCAL_MEAN_H
  85. /* structure used to store envelopes and temporary data */
  86. typedef struct {
  87. int n;
  88. double *re_min;
  89. double *ie_min;
  90. double *re_max;
  91. double *ie_max;
  92. double *tmp1;
  93. double *tmp2;
  94. } envelope_t;
  95. /*^*/
  96. #endif
  97. #endif
  98. //cextr.c
  99. /************************************************************************/
  100. /* */
  101. /* INITIALIZATION OF EXTREMA STRUCTURE */
  102. /* */
  103. /************************************************************************/
  104. extrema_t init_extr(int n)
  105. /*<initialization for extremas extraction>*/
  106. {
  107. extrema_t ex;
  108. ex.ind_min=(int *)malloc(n*sizeof(int));
  109. ex.ind_max=(int *)malloc(n*sizeof(int));
  110. ex.x_min=(double *)malloc(n*sizeof(double));
  111. ex.x_max=(double *)malloc(n*sizeof(double));
  112. ex.ry_min=(double *)malloc(n*sizeof(double));
  113. ex.ry_max=(double *)malloc(n*sizeof(double));
  114. ex.iy_min=(double *)malloc(n*sizeof(double));
  115. ex.iy_max=(double *)malloc(n*sizeof(double));
  116. return ex;
  117. }
  118. /************************************************************************/
  119. /* */
  120. /* DETECTION OF LOCAL EXTREMA */
  121. /* */
  122. /************************************************************************/
  123. void extr(double x[], COMPLEX_T z[], double phi,int n,extrema_t *ex)
  124. /*< extract extremas >*/
  125. {
  126. int cour;
  127. double val,valp,valn;
  128. #ifdef C99_OK
  129. COMPLEX_T eiphi;
  130. #endif
  131. ex->n_min=0;
  132. ex->n_max=0;
  133. #ifdef C99_OK
  134. eiphi = cexp(I*phi);
  135. #endif
  136. #ifdef C99_OK
  137. val = CREAL(eiphi*z[0]);
  138. #else
  139. val = crealeiphi(phi,z[0]);
  140. #endif
  141. #ifdef C99_OK
  142. valn = CREAL(eiphi*z[1]);
  143. #else
  144. valn = crealeiphi(phi,z[1]);
  145. #endif
  146. /* search for extrema in direction phi*/
  147. for(cour=1;cour<(n-1);cour++) {
  148. valp = val;
  149. val = valn;
  150. #ifdef C99_OK
  151. valn = CREAL(eiphi*z[cour+1]);
  152. #else
  153. valn = crealeiphi(phi,z[cour+1]);
  154. #endif
  155. if (val<valp && val<valn) /* local minimum */ {
  156. ex->x_min[ex->n_min+NBSYM]=x[cour];
  157. ex->ry_min[ex->n_min+NBSYM]=CREAL(z[cour]);
  158. ex->iy_min[ex->n_min+NBSYM]=CIMAG(z[cour]);
  159. ex->ind_min[ex->n_min+NBSYM]=cour;
  160. ex->n_min++;
  161. }
  162. if (val>valp && val>valn) /* local maximum */ {
  163. ex->x_max[ex->n_max+NBSYM]=x[cour];
  164. ex->ry_max[ex->n_max+NBSYM]=CREAL(z[cour]);
  165. ex->iy_max[ex->n_max+NBSYM]=CIMAG(z[cour]);
  166. ex->ind_max[ex->n_max+NBSYM]=cour;
  167. ex->n_max++;
  168. }
  169. }
  170. }
  171. /************************************************************************/
  172. /* */
  173. /* EXTRAPOLATION OF EXTREMA TO LIMIT BORDER EFFECTS */
  174. /* */
  175. /************************************************************************/
  176. void boundary_conditions(double x[],COMPLEX_T z[],double phi,int n,extrema_t *ex)
  177. /*< setting boundary conditions >*/
  178. {
  179. int cour,nbsym;
  180. #ifdef C99_OK
  181. COMPLEX_T eiphi;
  182. #endif
  183. #ifdef C99_OK
  184. eiphi = cexp(I*phi);
  185. #endif
  186. nbsym = NBSYM;
  187. /* reduce the number of symmetrized points if there is not enough extrema */
  188. while(ex->n_min < nbsym+1 && ex->n_max < nbsym+1) nbsym--;
  189. if (nbsym < NBSYM) {
  190. for(cour=0;cour<ex->n_max;cour++) {
  191. ex->ind_max[nbsym+cour] = ex->ind_max[NBSYM+cour];
  192. ex->x_max[nbsym+cour] = ex->x_max[NBSYM+cour];
  193. ex->ry_max[nbsym+cour] = ex->ry_max[NBSYM+cour];
  194. ex->iy_max[nbsym+cour] = ex->iy_max[NBSYM+cour];
  195. }
  196. for(cour=0;cour<ex->n_min;cour++) {
  197. ex->ind_min[nbsym+cour] = ex->ind_min[NBSYM+cour];
  198. ex->x_min[nbsym+cour] = ex->x_min[NBSYM+cour];
  199. ex->ry_min[nbsym+cour] = ex->ry_min[NBSYM+cour];
  200. ex->iy_min[nbsym+cour] = ex->iy_min[NBSYM+cour];
  201. }
  202. }
  203. /* select the symmetrized points and the axis of symmetry at the beginning of the signal*/
  204. if (ex->x_max[nbsym] < ex->x_min[nbsym]) { /* first = max */
  205. #ifdef C99_OK
  206. if (CREAL(eiphi*z[0]) > CREAL(eiphi*z[ex->ind_min[nbsym]])) { /* the edge is not a min */
  207. #else
  208. if (crealeiphi(phi,z[0]) > crealeiphi(phi,z[ex->ind_min[nbsym]])) { /* the edge is not a min */
  209. #endif
  210. if (2*ex->x_max[nbsym]-ex->x_min[2*nbsym-1] > x[0]) { /* symmetrized parts are too short */
  211. for(cour=0;cour<nbsym;cour++) {
  212. ex->x_max[cour] = 2*x[0]-ex->x_max[2*nbsym-1-cour];
  213. ex->ry_max[cour] = ex->ry_max[2*nbsym-1-cour];
  214. ex->iy_max[cour] = ex->iy_max[2*nbsym-1-cour];
  215. ex->x_min[cour] = 2*x[0]-ex->x_min[2*nbsym-1-cour];
  216. ex->ry_min[cour] = ex->ry_min[2*nbsym-1-cour];
  217. ex->iy_min[cour] = ex->iy_min[2*nbsym-1-cour];
  218. }
  219. } else { /* symmetrized parts are long enough */
  220. for(cour=0;cour<nbsym;cour++) {
  221. ex->x_max[cour] = 2*ex->x_max[nbsym]-ex->x_max[2*nbsym-cour];
  222. ex->ry_max[cour] = ex->ry_max[2*nbsym-cour];
  223. ex->iy_max[cour] = ex->iy_max[2*nbsym-cour];
  224. ex->x_min[cour] = 2*ex->x_max[nbsym]-ex->x_min[2*nbsym-1-cour];
  225. ex->ry_min[cour] = ex->ry_min[2*nbsym-1-cour];
  226. ex->iy_min[cour] = ex->iy_min[2*nbsym-1-cour];
  227. }
  228. }
  229. } else { /* edge is a min -> sym with respect to the edge*/
  230. for(cour=0;cour<nbsym;cour++) {
  231. ex->x_max[cour] = 2*x[0]-ex->x_max[2*nbsym-1-cour];
  232. ex->ry_max[cour] = ex->ry_max[2*nbsym-1-cour];
  233. ex->iy_max[cour] = ex->iy_max[2*nbsym-1-cour];
  234. }
  235. for(cour=0;cour<nbsym-1;cour++) {
  236. ex->x_min[cour] = 2*x[0]-ex->x_min[2*nbsym-2-cour];
  237. ex->ry_min[cour] = ex->ry_min[2*nbsym-2-cour];
  238. ex->iy_min[cour] = ex->iy_min[2*nbsym-2-cour];
  239. }
  240. ex->x_min[nbsym-1] = x[0];
  241. ex->ry_min[nbsym-1] = CREAL(z[0]);
  242. ex->iy_min[nbsym-1] = CIMAG(z[0]);
  243. }
  244. } else { /* first = min */
  245. #ifdef C99_OK
  246. if (CREAL(eiphi*z[0]) < CREAL(eiphi*z[ex->ind_max[nbsym]])) { /* the edge is not a max */
  247. #else
  248. if (crealeiphi(phi,z[0]) < crealeiphi(phi,z[ex->ind_max[nbsym]])) { /* the edge is not a max */
  249. #endif
  250. if (2*ex->x_min[nbsym]-ex->x_max[2*nbsym-1] > x[0]) { /* symmetrized parts are too short */
  251. for(cour=0;cour<nbsym;cour++) {
  252. ex->x_max[cour] = 2*x[0]-ex->x_max[2*nbsym-1-cour];
  253. ex->ry_max[cour] = ex->ry_max[2*nbsym-1-cour];
  254. ex->iy_max[cour] = ex->iy_max[2*nbsym-1-cour];
  255. ex->x_min[cour] = 2*x[0]-ex->x_min[2*nbsym-1-cour];
  256. ex->ry_min[cour] = ex->ry_min[2*nbsym-1-cour];
  257. ex->iy_min[cour] = ex->iy_min[2*nbsym-1-cour];
  258. }
  259. } else { /* symmetrized parts are long enough */
  260. for(cour=0;cour<nbsym;cour++) {
  261. ex->x_max[cour] = 2*ex->x_min[nbsym]-ex->x_max[2*nbsym-1-cour];
  262. ex->ry_max[cour] = ex->ry_max[2*nbsym-1-cour];
  263. ex->iy_max[cour] = ex->iy_max[2*nbsym-1-cour];
  264. ex->x_min[cour] = 2*ex->x_min[nbsym]-ex->x_min[2*nbsym-cour];
  265. ex->ry_min[cour] = ex->ry_min[2*nbsym-cour];
  266. ex->iy_min[cour] = ex->iy_min[2*nbsym-cour];
  267. }
  268. }
  269. } else { /* edge is a max -> sym with respect to the edge*/
  270. for(cour=0;cour<nbsym;cour++) {
  271. ex->x_min[cour] = 2*x[0]-ex->x_min[2*nbsym-1-cour];
  272. ex->ry_min[cour] = ex->ry_min[2*nbsym-1-cour];
  273. ex->iy_min[cour] = ex->iy_min[2*nbsym-1-cour];
  274. }
  275. for(cour=0;cour<nbsym-1;cour++) {
  276. ex->x_max[cour] = 2*x[0]-ex->x_max[2*nbsym-2-cour];
  277. ex->ry_max[cour] = ex->ry_max[2*nbsym-2-cour];
  278. ex->iy_max[cour] = ex->iy_max[2*nbsym-2-cour];
  279. }
  280. ex->x_max[nbsym-1] = x[0];
  281. ex->ry_max[nbsym-1] = CREAL(z[0]);
  282. ex->iy_max[nbsym-1] = CIMAG(z[0]);
  283. }
  284. }
  285. (ex->n_min) += nbsym-1;
  286. (ex->n_max) += nbsym-1;
  287. /* select the symmetrized points and the axis of symmetry at the end of the signal*/
  288. if (ex->x_max[ex->n_max] < ex->x_min[ex->n_min]) { /* last is a min */
  289. #ifdef C99_OK
  290. if (CREAL(eiphi*z[n-1]) < CREAL(eiphi*z[ex->ind_max[ex->n_max]])) { /* the edge is not a max */
  291. #else
  292. if (crealeiphi(phi,z[n-1]) < crealeiphi(phi,z[ex->ind_max[ex->n_max]])) { /* the edge is not a max */
  293. #endif
  294. if (2*ex->x_min[ex->n_min]-ex->x_max[ex->n_max-nbsym+1] < x[n-1]) { /* symmetrized parts are too short */
  295. for(cour=0;cour<nbsym;cour++) {
  296. ex->x_max[ex->n_max+1+cour] = 2*x[n-1]-ex->x_max[ex->n_max-cour];
  297. ex->ry_max[ex->n_max+1+cour] = ex->ry_max[ex->n_max-cour];
  298. ex->iy_max[ex->n_max+1+cour] = ex->iy_max[ex->n_max-cour];
  299. ex->x_min[ex->n_min+1+cour] = 2*x[n-1]-ex->x_min[ex->n_min-cour];
  300. ex->ry_min[ex->n_min+1+cour] = ex->ry_min[ex->n_min-cour];
  301. ex->iy_min[ex->n_min+1+cour] = ex->iy_min[ex->n_min-cour];
  302. }
  303. } else { /* symmetrized parts are long enough */
  304. for(cour=0;cour<nbsym;cour++) {
  305. ex->x_max[ex->n_max+1+cour] = 2*ex->x_min[ex->n_min]-ex->x_max[ex->n_max-cour];
  306. ex->ry_max[ex->n_max+1+cour] = ex->ry_max[ex->n_max-cour];
  307. ex->iy_max[ex->n_max+1+cour] = ex->iy_max[ex->n_max-cour];
  308. ex->x_min[ex->n_min+1+cour] = 2*ex->x_min[ex->n_min]-ex->x_min[ex->n_min-1-cour];
  309. ex->ry_min[ex->n_min+1+cour] = ex->ry_min[ex->n_min-1-cour];
  310. ex->iy_min[ex->n_min+1+cour] = ex->iy_min[ex->n_min-1-cour];
  311. }
  312. }
  313. } else { /* edge is a max -> sym with respect to the edge*/
  314. for(cour=0;cour<nbsym;cour++) {
  315. ex->x_min[ex->n_min+1+cour] = 2*x[n-1]-ex->x_min[ex->n_min-cour];
  316. ex->ry_min[ex->n_min+1+cour] = ex->ry_min[ex->n_min-cour];
  317. ex->iy_min[ex->n_min+1+cour] = ex->iy_min[ex->n_min-cour];
  318. }
  319. for(cour=0;cour<nbsym-1;cour++) {
  320. ex->x_max[ex->n_max+2+cour] = 2*x[n-1]-ex->x_max[ex->n_max-cour];
  321. ex->ry_max[ex->n_max+2+cour] = ex->ry_max[ex->n_max-cour];
  322. ex->iy_max[ex->n_max+2+cour] = ex->iy_max[ex->n_max-cour];
  323. }
  324. ex->x_max[ex->n_max+1] = x[n-1];
  325. ex->ry_max[ex->n_max+1] = CREAL(z[n-1]);
  326. ex->iy_max[ex->n_max+1] = CIMAG(z[n-1]);
  327. }
  328. } else { /* last is a max */
  329. #ifdef C99_OK
  330. if (CREAL(eiphi*z[n-1]) > CREAL(eiphi*z[ex->ind_min[ex->n_min]])) { /* the edge is not a min */
  331. #else
  332. if (crealeiphi(phi,z[n-1]) > crealeiphi(phi,z[ex->ind_min[ex->n_min]])) { /* the edge is not a min */
  333. #endif
  334. if (2*ex->x_max[ex->n_max]-ex->x_min[ex->n_min-nbsym+1] < x[n-1]) { /* symmetrized parts are too short */
  335. for(cour=0;cour<nbsym;cour++) {
  336. ex->x_max[ex->n_max+1+cour] = 2*x[n-1]-ex->x_max[ex->n_max-cour];
  337. ex->ry_max[ex->n_max+1+cour] = ex->ry_max[ex->n_max-cour];
  338. ex->iy_max[ex->n_max+1+cour] = ex->iy_max[ex->n_max-cour];
  339. ex->x_min[ex->n_min+1+cour] = 2*x[n-1]-ex->x_min[ex->n_min-cour];
  340. ex->ry_min[ex->n_min+1+cour] = ex->ry_min[ex->n_min-cour];
  341. ex->iy_min[ex->n_min+1+cour] = ex->iy_min[ex->n_min-cour];
  342. }
  343. } else { /* symmetrized parts are long enough */
  344. for(cour=0;cour<nbsym;cour++) {
  345. ex->x_max[ex->n_max+1+cour] = 2*ex->x_max[ex->n_max]-ex->x_max[ex->n_max-1-cour];
  346. ex->ry_max[ex->n_max+1+cour] = ex->ry_max[ex->n_max-1-cour];
  347. ex->iy_max[ex->n_max+1+cour] = ex->iy_max[ex->n_max-1-cour];
  348. ex->x_min[ex->n_min+1+cour] = 2*ex->x_max[ex->n_max]-ex->x_min[ex->n_min-cour];
  349. ex->ry_min[ex->n_min+1+cour] = ex->ry_min[ex->n_min-cour];
  350. ex->iy_min[ex->n_min+1+cour] = ex->iy_min[ex->n_min-cour];
  351. }
  352. }
  353. } else { /* edge is a min -> sym with respect to the edge*/
  354. for(cour=0;cour<nbsym;cour++) {
  355. ex->x_max[ex->n_max+1+cour] = 2*x[n-1]-ex->x_max[ex->n_max-cour];
  356. ex->ry_max[ex->n_max+1+cour] = ex->ry_max[ex->n_max-cour];
  357. ex->iy_max[ex->n_max+1+cour] = ex->iy_max[ex->n_max-cour];
  358. }
  359. for(cour=0;cour<nbsym-1;cour++) {
  360. ex->x_min[ex->n_min+2+cour] = 2*x[n-1]-ex->x_min[ex->n_min-cour];
  361. ex->ry_min[ex->n_min+2+cour] = ex->ry_min[ex->n_min-cour];
  362. ex->iy_min[ex->n_min+2+cour] = ex->iy_min[ex->n_min-cour];
  363. }
  364. ex->x_min[ex->n_min+1] = x[n-1];
  365. ex->ry_min[ex->n_min+1] = CREAL(z[n-1]);
  366. ex->iy_min[ex->n_min+1] = CIMAG(z[n-1]);
  367. }
  368. }
  369. (ex->n_min) = ex->n_min + nbsym + 1;
  370. (ex->n_max) = ex->n_max + nbsym + 1;
  371. }
  372. /************************************************************************/
  373. /* */
  374. /* FREE ALLOCATED MEMORY */
  375. /* */
  376. /************************************************************************/
  377. void free_extr(extrema_t ex)
  378. /*<free allocated extrema struct>*/
  379. {
  380. free(ex.x_max);
  381. free(ex.x_min);
  382. free(ex.ind_max);
  383. free(ex.ind_min);
  384. free(ex.ry_max);
  385. free(ex.ry_min);
  386. free(ex.iy_max);
  387. free(ex.iy_min);
  388. }
  389. //interpolation.c
  390. /*************************************************************************/
  391. /* */
  392. /* INTERPOLATION */
  393. /* */
  394. /* interpolates the sequence (xs,ys) at instants in x using cubic spline */
  395. /* */
  396. /*************************************************************************/
  397. void interpolation(double y[],double xs[],double ys[],int n,double x[], int nx,double *ys2, double *temp)
  398. /*< interpolates the sequence (xs,ys) at instants in x using cubic spline >*/
  399. {
  400. int i,j,jfin,cur,prev;
  401. double p,sig,a,b,c,d,e,f,g,a0,a1,a2,a3,xc;
  402. /* Compute second derivatives at the knots */
  403. ys2[0]=temp[0]=0.0;
  404. for (i=1;i<n-1;i++) {
  405. sig=(xs[i]-xs[i-1])/(xs[i+1]-xs[i-1]);
  406. p=sig*ys2[i-1]+2.0;
  407. ys2[i]=(sig-1.0)/p;
  408. temp[i]=(ys[i+1]-ys[i])/(xs[i+1]-xs[i])-(ys[i]-ys[i-1])/(xs[i]-xs[i-1]);
  409. temp[i]=(6.0*temp[i]/(xs[i+1]-xs[i-1])-sig*temp[i-1])/p;
  410. }
  411. ys2[n-1]=0.0;
  412. for (j=n-2;j>=0;j--) ys2[j]=ys2[j]*ys2[j+1]+temp[j];
  413. /* Compute the spline coefficients */
  414. cur=0;
  415. j=0;
  416. jfin=n-1;
  417. while (xs[j+2]<x[0]) j++;
  418. while (xs[jfin]>x[nx-1]) jfin--;
  419. for (;j<=jfin;j++) {
  420. /* Compute the coefficients of the polynomial between two knots */
  421. a=xs[j];
  422. b=xs[j+1];
  423. c=b-a;
  424. d=ys[j];
  425. e=ys[j+1];
  426. f=ys2[j];
  427. g=ys2[j+1];
  428. a0=(b*d-a*e+CUBE(b)*f/6-CUBE(a)*g/6)/c+c*(a*g-b*f)/6;
  429. a1=(e-d-SQUARE(b)*f/2+SQUARE(a)*g/2)/c+c*(f-g)/6;
  430. a2=(b*f-a*g)/(2*c);
  431. a3=(g-f)/(6*c);
  432. prev=cur;
  433. while ((cur<nx) && ((j==jfin) || (x[cur]<xs[j+1]))) cur++;
  434. /* Compute the value of the spline at the sampling times x[i] */
  435. for (i=prev;i<cur;i++) {
  436. xc=x[i];
  437. y[i]=a0+a1*xc+a2*SQUARE(xc)+a3*CUBE(xc);
  438. }
  439. }
  440. }
  441. //cio.c
  442. /************************************************************************/
  443. /* */
  444. /* INITIALIZATION OF THE LIST */
  445. /* */
  446. /************************************************************************/
  447. imf_list_t init_imf_list(int n)
  448. /*< define imf list struct >*/
  449. {
  450. imf_list_t list;
  451. list.first=NULL;
  452. list.last=NULL;
  453. list.n=n;
  454. list.m=0;
  455. return list;
  456. }
  457. /************************************************************************/
  458. /* */
  459. /* ADD AN IMF TO THE LIST */
  460. /* */
  461. /************************************************************************/
  462. void add_imf(imf_list_t *list,COMPLEX_T *p,int nb_it)
  463. /*< adding imf to imf list >*/
  464. {
  465. COMPLEX_T *v=(COMPLEX_T *)malloc(list->n*sizeof(COMPLEX_T));
  466. int i;
  467. imf_t *mode=(imf_t *)malloc(sizeof(imf_t));
  468. for (i=0;i<list->n;i++) v[i]=p[i];
  469. mode->pointer=v;
  470. mode->nb_iterations=nb_it;
  471. mode->next=NULL;
  472. if (!list->first) {
  473. list->first=mode;
  474. } else {
  475. (list->last)->next=mode;
  476. }
  477. list->last=mode;
  478. list->m++;
  479. }
  480. /************************************************************************/
  481. /* */
  482. /* FREE MEMORY ALLOCATED FOR THE LIST */
  483. /* */
  484. /************************************************************************/
  485. void free_imf_list(imf_list_t list)
  486. /*<free allocated imf list struct >*/
  487. {
  488. imf_t *current=list.first, *previous;
  489. while (current) {
  490. previous=current;
  491. current=current->next;
  492. free(previous->pointer);
  493. free(previous);
  494. }
  495. }
  496. //clocal_mean.c
  497. /********************************************************/
  498. /* ALLOCATE MEMORY FOR THE ENVELOPES AND TEMPORARY DATA */
  499. /********************************************************/
  500. envelope_t init_local_mean(int n)
  501. /*< initialization for local mean of bivariate emd >*/
  502. {
  503. envelope_t env;
  504. env.re_min = (double*)malloc(n*sizeof(double));
  505. env.ie_min = (double*)malloc(n*sizeof(double));
  506. env.re_max = (double*)malloc(n*sizeof(double));
  507. env.ie_max = (double*)malloc(n*sizeof(double));
  508. env.tmp1 = (double*)malloc(n*sizeof(double));
  509. env.tmp2 = (double*)malloc(n*sizeof(double));
  510. return env;
  511. }
  512. /*************************/
  513. /* FREE ALLOCATED MEMORY */
  514. /*************************/
  515. void free_local_mean(envelope_t env)
  516. /*<free allocated local mean struct >*/
  517. {
  518. free(env.re_min);
  519. free(env.ie_min);
  520. free(env.re_max);
  521. free(env.ie_max);
  522. free(env.tmp1);
  523. free(env.tmp2);
  524. }
  525. /***************************************************************************/
  526. /* COMPUTES THE MEAN OF THE ENVELOPES AND THE AMPLITUDE OF THE CURRENT IMF */
  527. /***************************************************************************/
  528. int mean_and_amplitude(double *x,COMPLEX_T *z,COMPLEX_T *m,double *a,int n,int nbphases,extrema_t *ex,envelope_t *env)
  529. /*< compute the mean of the envelopes and the amplitude of the current imf >*/
  530. {
  531. int i,k;
  532. double phi;
  533. #ifdef C99_OK
  534. for (i=0;i<n;i++) m[i]=0;
  535. #else
  536. for (i=0;i<n;i++) {
  537. m[i].r = 0;
  538. m[i].i = 0;
  539. }
  540. #endif
  541. for (i=0;i<n;i++) a[i]=0;
  542. for(k=0;k<nbphases;k++) {
  543. phi = k*M_PI/nbphases;
  544. /* detect maxima and minima in direction phi=k*M_PI/nbphases*/
  545. extr(x,z,phi,n,ex);
  546. if (ex->n_max+ex->n_min <3){ /* not enough extrema in a direction -> stop */
  547. return 1;
  548. }
  549. /* add extra points at the edges */
  550. boundary_conditions(x,z,phi,n,ex);
  551. /* interpolation - upper envelope */
  552. interpolation(env->re_max,ex->x_max,ex->ry_max,ex->n_max,x,n,env->tmp1,env->tmp2);
  553. interpolation(env->ie_max,ex->x_max,ex->iy_max,ex->n_max,x,n,env->tmp1,env->tmp2);
  554. /* interpolation - lower envelope */
  555. interpolation(env->re_min,ex->x_min,ex->ry_min,ex->n_min,x,n,env->tmp1,env->tmp2);
  556. interpolation(env->ie_min,ex->x_min,ex->iy_min,ex->n_min,x,n,env->tmp1,env->tmp2);
  557. if ((ex->n_min > LIM_GMP)||(ex->n_min > LIM_GMP)) {
  558. sf_warning("Too many extrema, interpolation may be unreliable\n");
  559. }
  560. /* compute the mean and amplitude*/
  561. #ifdef C99_OK
  562. for (i=0;i<n;i++) m[i]+=(env->re_max[i]+env->re_min[i]+I*(env->ie_max[i]+env->ie_min[i]))/(2*nbphases);
  563. for (i=0;i<n;i++) a[i]+=CABS(env->re_max[i]-env->re_min[i]+I*(env->ie_max[i]-env->ie_min[i]))/(2*nbphases);
  564. #else
  565. for (i=0;i<n;i++) {
  566. m[i].r+=(env->re_max[i]+env->re_min[i])/(2*nbphases);
  567. m[i].i+=(env->ie_max[i]+env->ie_min[i])/(2*nbphases);
  568. }
  569. for (i=0;i<n;i++) a[i]+=sqrt((env->re_max[i]-env->re_min[i])*(env->re_max[i]-env->re_min[i])+(env->ie_max[i]-env->ie_min[i])*(env->ie_max[i]-env->ie_min[i]))/(2*nbphases);
  570. #endif
  571. }
  572. return 0;
  573. }
  574. /*********************************************************/
  575. /* COMPUTES THE MEAN OF THE ENVELOPES OF THE CURRENT IMF */
  576. /*********************************************************/
  577. int mean(double *x,COMPLEX_T *z,COMPLEX_T *m,int n,int nbphases,extrema_t *ex,envelope_t *env)
  578. /*< compute the mean of the envelopes of the current imf >*/
  579. {
  580. int i,k;
  581. double phi;
  582. #ifdef C99_OK
  583. for (i=0;i<n;i++) m[i]=0;
  584. #else
  585. for (i=0;i<n;i++) {
  586. m[i].r = 0;
  587. m[i].i = 0;
  588. }
  589. #endif
  590. for(k=0;k<nbphases;k++) {
  591. phi = k*M_PI/nbphases;
  592. /* detect maxima and minima in direction phi=k*M_PI/nbphases*/
  593. extr(x,z,phi,n,ex);
  594. if (ex->n_max+ex->n_min <3){ /* not enough extrema in a direction -> stop */
  595. return 1;
  596. }
  597. boundary_conditions(x,z,phi,n,ex);
  598. /* interpolation - upper envelope */
  599. if (ex->n_max < LIM_GMP) {
  600. interpolation(env->re_max,ex->x_max,ex->ry_max,ex->n_max,x,n,env->tmp1,env->tmp2);
  601. interpolation(env->ie_max,ex->x_max,ex->iy_max,ex->n_max,x,n,env->tmp1,env->tmp2);
  602. }
  603. else {
  604. sf_warning("Too many extrema, interpolation may be unreliable\n");
  605. }
  606. /* interpolation - lower envelope */
  607. if (ex->n_min < LIM_GMP) {
  608. interpolation(env->re_min,ex->x_min,ex->ry_min,ex->n_min,x,n,env->tmp1,env->tmp2);
  609. interpolation(env->ie_min,ex->x_min,ex->iy_min,ex->n_min,x,n,env->tmp1,env->tmp2);
  610. }
  611. else {
  612. sf_warning("Too many extrema, interpolation may be unreliable\n");
  613. }
  614. /* compute the mean*/
  615. #ifdef C99_OK
  616. for (i=0;i<n;i++) m[i]+=(env->re_max[i]+env->re_min[i]+I*(env->ie_max[i]+env->ie_min[i]))/(2*nbphases);
  617. #else
  618. for (i=0;i<n;i++) {
  619. m[i].r+=(env->re_max[i]+env->re_min[i])/(2*nbphases);
  620. m[i].i+=(env->ie_max[i]+env->ie_min[i])/(2*nbphases);
  621. }
  622. #endif
  623. }
  624. return 0;
  625. }
  626. //emd_complex.c
  627. double CREAL(complex_data_t c)
  628. /*< creal >*/
  629. {
  630. return c.r;
  631. }
  632. double CIMAG(complex_data_t c)
  633. /*< cimag >*/
  634. {
  635. return c.i;
  636. }
  637. double CABS(complex_data_t c)
  638. /*< cabs >*/
  639. {
  640. return sqrt((c.r)*(c.r)+(c.i)*(c.i));
  641. }
  642. double crealeiphi(double phi,complex_data_t c)
  643. /*< crealeiphi >*/
  644. {
  645. return cos(phi)*c.r-sin(phi)*c.i;
  646. }
  647. //cemdc.c
  648. /************************************************************************/
  649. /* STOP TEST FOR THE SIFTING LOOP */
  650. /************************************************************************/
  651. int stop_sifting(COMPLEX_T *m, double *a,extrema_t *ex,stop_t *sp,int n, int counter, int max_iterations)
  652. /*< decide if stop sifting >*/
  653. {
  654. int i,count;
  655. double tol,eps;
  656. tol = sp->tolerance*n;
  657. eps = sp->threshold;
  658. count = 0;
  659. if (counter >= MAX_ITERATIONS) return 1;
  660. for (i=0;i<n;i++) {
  661. if (CABS(m[i]) > eps*a[i]) if (++count>tol) return 0;
  662. }
  663. return 1;
  664. }