gsl_wavelet__wavelet.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* wavelet/wavelet.c
  2. *
  3. * Copyright (C) 2004 Ivo Alxneit
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_errno.h"
  22. #include "gsl_wavelet.h"
  23. gsl_wavelet *
  24. gsl_wavelet_alloc (const gsl_wavelet_type * T, size_t k)
  25. {
  26. int status;
  27. gsl_wavelet *w = (gsl_wavelet *) malloc (sizeof (gsl_wavelet));
  28. if (w == NULL)
  29. {
  30. GSL_ERROR_VAL ("failed to allocate space for wavelet struct",
  31. GSL_ENOMEM, 0);
  32. };
  33. w->type = T;
  34. status = (T->init) (&(w->h1), &(w->g1), &(w->h2), &(w->g2),
  35. &(w->nc), &(w->offset), k);
  36. if (status)
  37. {
  38. free (w);
  39. GSL_ERROR_VAL ("invalid wavelet member", GSL_EINVAL, 0);
  40. }
  41. return w;
  42. }
  43. void
  44. gsl_wavelet_free (gsl_wavelet * w)
  45. {
  46. free (w);
  47. }
  48. const char *
  49. gsl_wavelet_name (const gsl_wavelet * w)
  50. {
  51. return w->type->name;
  52. }
  53. /* Let's not export this for now (BJG) */
  54. #if 0
  55. void
  56. gsl_wavelet_print (const gsl_wavelet * w)
  57. {
  58. size_t n = w->nc;
  59. size_t i;
  60. printf ("Wavelet type: %s\n", w->type->name);
  61. printf
  62. (" h1(%d):%12.8f g1(%d):%12.8f h2(%d):%12.8f g2(%d):%12.8f\n",
  63. 0, w->h1[0], 0, w->g1[0], 0, w->h2[0], 0, w->g2[0]);
  64. for (i = 1; i < (n < 10 ? n : 10); i++)
  65. {
  66. printf
  67. (" h1(%d):%12.8f g1(%d):%12.8f h2(%d):%12.8f g2(%d):%12.8f\n",
  68. i, w->h1[i], i, w->g1[i], i, w->h2[i], i, w->g2[i]);
  69. }
  70. for (; i < n; i++)
  71. {
  72. printf
  73. ("h1(%d):%12.8f g1(%d):%12.8f h2(%d):%12.8f g2(%d):%12.8f\n",
  74. i, w->h1[i], i, w->g1[i], i, w->h2[i], i, w->g2[i]);
  75. }
  76. }
  77. #endif
  78. gsl_wavelet_workspace *
  79. gsl_wavelet_workspace_alloc (size_t n)
  80. {
  81. gsl_wavelet_workspace *work;
  82. if (n == 0)
  83. {
  84. GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0);
  85. }
  86. work = (gsl_wavelet_workspace *) malloc (sizeof (gsl_wavelet_workspace));
  87. if (work == NULL)
  88. {
  89. GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0);
  90. }
  91. work->n = n;
  92. work->scratch = (double *) malloc (n * sizeof (double));
  93. if (work->scratch == NULL)
  94. {
  95. /* error in constructor, prevent memory leak */
  96. free (work);
  97. GSL_ERROR_VAL ("failed to allocate scratch space", GSL_ENOMEM, 0);
  98. }
  99. return work;
  100. }
  101. void
  102. gsl_wavelet_workspace_free (gsl_wavelet_workspace * work)
  103. {
  104. /* release scratch space */
  105. free (work->scratch);
  106. work->scratch = NULL;
  107. free (work);
  108. }