/** \brief Generic Wavelet-Denoising. \ingroup grpwavelet compute DWT of signal, call the thresholding function 'threshfct' for each resolution level and IDWT the signal */ int wavelet_denoise ( double *data, int n, WaveletParameters P ){ gsl_wavelet *w; gsl_wavelet_workspace *work; int j, J, k, offset; double lambda; /* population sd, threshold */ dprintf("Db: generic_denoising\n"); w = gsl_wavelet_alloc( P.wavelet, P.vanishing_moments ); work = gsl_wavelet_workspace_alloc( n ); gsl_wavelet_transform_forward( w, data, 1, n, work ); /* -- thresholding here -- */ J = (int)round(glog((double)n, 2)); for(j=P.first_thresholding_level; j<J; j++){ /* loop through levels */ offset = (int)pow(2, j); lambda = (*(P.threshselfct))(&(data[offset]), offset); for(k=offset; k<2*offset; k++){ /* loop through coefficients */ /* soft or hard thresholding */ data[k]=(*(P.threshfct))(data[k], lambda); } } /* -- thresholding end -- */ gsl_wavelet_transform_inverse(w, data, 1, n, work); gsl_wavelet_free(w); gsl_wavelet_workspace_free(work); return 0; }
void wlet_int(t_wlet *x, long n) { float f = (float)n; int i = (int)n; if(proxy_getinlet((t_object *)x) == 1){ wlet_setupWavelet(x, gensym((char *)gsl_wavelet_name(x->w_wavelet)), (size_t)n); }else if(proxy_getinlet((t_object *)x) == 2){ if(n > sys_getblksize()){ error("wavelet: size must be smaller than the signal vector %ld", sys_getblksize()); return; } while(i > 1){ f = f / 2.0; if(f - round(f)){ error("wavelet: size must be a multiple of 2"); return; } i = (int)f; } x->w_waveletLength = (int)n; gsl_wavelet_workspace *oldWspace = x->w_workspace; x->w_workspace = gsl_wavelet_workspace_alloc((size_t)n); gsl_wavelet_workspace_free(oldWspace); double *oldTmp = x->w_tmp; x->w_tmp = (double *)calloc((int)n, sizeof(double)); free(oldTmp); }//else if(proxy_getinlet((t_object *)x) == 3){ // x->w_stride = (size_t)n; //} }
void dwt2d_forward_haar(double *data,int n,int ns) { gsl_wavelet_workspace *work; gsl_wavelet *w; gsl_matrix *m1; gsl_matrix_view m; int i,j; long dims[3]={2,n,n}; double *res = ypush_d(dims); int k=0; for (i=0;i<n;i++) { for (j=0;j<n;j++) { k = j+i*n; res[k] = data[k]; } } w = gsl_wavelet_alloc(gsl_wavelet_haar, 2); work = gsl_wavelet_workspace_alloc(n*n); if (ns == 1) gsl_wavelet2d_nstransform_forward(w, res, n, n, n,work); else gsl_wavelet2d_transform_forward(w, res, n, n, n,work); gsl_wavelet_workspace_free (work); gsl_wavelet_free (w); }
static VALUE rb_gsl_wavelet_workspace_new(VALUE klass, VALUE nn) { gsl_wavelet_workspace *wspace = NULL; CHECK_FIXNUM(nn); wspace = gsl_wavelet_workspace_alloc(FIX2INT(nn)); if (wspace == NULL) rb_raise(rb_eNoMemError, "gsl_wavelet_workspace_alloc failed"); return Data_Wrap_Struct(klass, 0, gsl_wavelet_workspace_free, wspace); }
CAMLprim value ml_gsl_wavelet_workspace_alloc (value n) { value r; gsl_wavelet_workspace *ws; ws = gsl_wavelet_workspace_alloc (Long_val (n)); Abstract_ptr (r, ws); return r; }
int main (int argc, char **argv) { (void)(argc); /* avoid unused parameter warning */ int i, n = 256, nc = 20; double *data = malloc (n * sizeof (double)); double *abscoeff = malloc (n * sizeof (double)); size_t *p = malloc (n * sizeof (size_t)); FILE * f; gsl_wavelet *w; gsl_wavelet_workspace *work; w = gsl_wavelet_alloc (gsl_wavelet_daubechies, 4); work = gsl_wavelet_workspace_alloc (n); f = fopen (argv[1], "r"); for (i = 0; i < n; i++) { fscanf (f, "%lg", &data[i]); } fclose (f); gsl_wavelet_transform_forward (w, data, 1, n, work); for (i = 0; i < n; i++) { abscoeff[i] = fabs (data[i]); } gsl_sort_index (p, abscoeff, 1, n); for (i = 0; (i + nc) < n; i++) data[p[i]] = 0; gsl_wavelet_transform_inverse (w, data, 1, n, work); for (i = 0; i < n; i++) { printf ("%g\n", data[i]); } gsl_wavelet_free (w); gsl_wavelet_workspace_free (work); free (data); free (abscoeff); free (p); return 0; }
void *wlet_new(t_symbol *dir, t_symbol *wlet, long k, long n)//, long stride) { t_wlet *x; x = (t_wlet *)newobject(wlet_class); // create a new instance of this object dsp_setup((t_pxobject *)x, 1); //x->w_proxy[2] = proxy_new(x, 3, &x->w_inletNumber); x->w_proxy[1] = proxy_new(x, 2, &x->w_inletNumber); x->w_proxy[0] = proxy_new(x, 1, &x->w_inletNumber); outlet_new((t_pxobject *)x, "signal"); outlet_new((t_pxobject *)x, "signal"); if(!strcmp(dir->s_name, "forward")) x->w_direction = forward; else if(!strcmp(dir->s_name, "backward")) x->w_direction = backward; else{ error("wavelet: direction must be forward or backward--bailing out"); return 0; } if(n){ if(sys_getblksize() < n) x->w_waveletLength = sys_getblksize(); else x->w_waveletLength = n; }else{ if(sys_getblksize() < 512) x->w_waveletLength = sys_getblksize(); else x->w_waveletLength = 512; } //if(stride) x->w_stride = stride; //else x->w_stride = 1; x->w_tmp = (double *)calloc(x->w_waveletLength, sizeof(double)); //x->w_wavelet = gsl_wavelet_alloc(gsl_wavelet_daubechies, (k) ? k : 4); x->w_workspace = gsl_wavelet_workspace_alloc(x->w_waveletLength); if(k) x->w_k = (size_t)k; else x->w_k = 4; wlet_setupWavelet(x, (!strcmp(wlet->s_name, " ")) ? wlet : gensym("daubechies"), x->w_k); return(x); }
static PyObject* PyGSL_transform_space_init(PyObject *self, PyObject *args, const enum pygsl_transform_space_type type) { PyGSL_transform_space *o=NULL; size_t n; FUNC_MESS_BEGIN(); o = (PyGSL_transform_space *) PyObject_NEW(PyGSL_transform_space, &PyGSL_transform_space_pytype); if(o == NULL){ return NULL; } if (0==PyArg_ParseTuple(args,"l", &n)) return NULL; if (n<=0) { pygsl_error("dimension must be >0", filename, __LINE__, GSL_EINVAL); return NULL; } o->type = type; switch(type){ case COMPLEX_WORKSPACE: o->space.cws = gsl_fft_complex_workspace_alloc(n); break; case COMPLEX_WAVETABLE: o->space.cwt = gsl_fft_complex_wavetable_alloc(n); break; case REAL_WORKSPACE: o->space.rws = gsl_fft_real_workspace_alloc(n); break; case REAL_WAVETABLE: o->space.rwt = gsl_fft_real_wavetable_alloc(n); break; case HALFCOMPLEX_WAVETABLE: o->space.hcwt = gsl_fft_halfcomplex_wavetable_alloc(n); break; case COMPLEX_WORKSPACE_FLOAT: o->space.cwsf = gsl_fft_complex_workspace_float_alloc(n); break; case COMPLEX_WAVETABLE_FLOAT: o->space.cwtf = gsl_fft_complex_wavetable_float_alloc(n); break; case REAL_WORKSPACE_FLOAT: o->space.rwsf = gsl_fft_real_workspace_float_alloc(n); break; case REAL_WAVETABLE_FLOAT: o->space.rwtf = gsl_fft_real_wavetable_float_alloc(n); break; case HALFCOMPLEX_WAVETABLE_FLOAT: o->space.hcwtf = gsl_fft_halfcomplex_wavetable_float_alloc(n); break; #ifdef _PYGSL_GSL_HAS_WAVELET case WAVELET_WORKSPACE : o->space.wws = gsl_wavelet_workspace_alloc(n); break; #endif default: pygsl_error("Got unknown switch", filename, __LINE__, GSL_ESANITY); return NULL; break; } assert(o->space.v); FUNC_MESS_END(); return (PyObject *) o; }
void dwt2d_filt_daub(double *data, long *mask, int order,int n,int ns) { gsl_wavelet_workspace *work; gsl_wavelet *w; gsl_matrix *m1; gsl_matrix_view m; int i,j; long dims[3]={2,n,n}; double *res = ypush_d(dims); int k=0; for (i=0;i<n;i++) { for (j=0;j<n;j++) { k = j+i*n; res[k] = data[k]; } } w = gsl_wavelet_alloc(gsl_wavelet_daubechies, order); work = gsl_wavelet_workspace_alloc(n*n); if (ns == 1) gsl_wavelet2d_nstransform_forward(w, res, n, n, n,work); else gsl_wavelet2d_transform_forward(w, res, n, n, n,work); k=0; for (i=0;i<n;i++) { for (j=0;j<n;j++) { k = j+i*n; res[k] *= (double)mask[k]; } } if (ns == 1) gsl_wavelet2d_nstransform_inverse(w, res, n, n, n,work); else gsl_wavelet2d_transform_inverse(w, res, n, n, n,work); gsl_wavelet_workspace_free (work); gsl_wavelet_free (w); }
static VALUE rb_gsl_wavelet_transform0(int argc, VALUE *argv, VALUE obj, int sss) { gsl_wavelet *w = NULL; gsl_vector *v = NULL, *vnew; gsl_wavelet_direction dir = forward; gsl_wavelet_workspace *work = NULL; int itmp, flag = 0; // local variable "status" declared and set, but never used //int status; double *ptr1, *ptr2; size_t n, stride; int naflag = 0; VALUE ary, ret; #ifdef HAVE_NARRAY_H struct NARRAY *na1 = NULL; #endif switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: if (argc < 2) rb_raise(rb_eArgError, "too few arguments"); CHECK_WAVELET(argv[0]); if (MATRIX_P(argv[1])) { return rb_gsl_wavelet2d(argc, argv, obj, gsl_wavelet2d_transform_matrix, sss); } if (VECTOR_P(argv[1])) { Data_Get_Struct(argv[0], gsl_wavelet, w); Data_Get_Struct(argv[1], gsl_vector, v); ret = argv[1]; ptr1 = v->data; n = v->size; stride = v->stride; #ifdef HAVE_NARRAY_H } else if (NA_IsNArray(argv[1])) { GetNArray(argv[1], na1); ret = argv[1]; ptr1 = (double*) na1->ptr; n = na1->total; naflag = 1; stride = 1; #endif } else { rb_raise(rb_eTypeError, "wrong argument type (Vector expected)"); } itmp = 2; break; default: if (argc < 1) rb_raise(rb_eArgError, "too few arguments"); if (MATRIX_P(argv[0])) { return rb_gsl_wavelet2d(argc, argv, obj, gsl_wavelet2d_transform_matrix, sss); } if (VECTOR_P(obj)) { CHECK_WAVELET(argv[0]); Data_Get_Struct(argv[0], gsl_wavelet, w); Data_Get_Struct(obj, gsl_vector, v); ret = obj; ptr1 = v->data; n = v->size; stride = v->stride; } else if (VECTOR_P(argv[0])) { CHECK_WAVELET(obj); Data_Get_Struct(obj, gsl_wavelet, w); Data_Get_Struct(argv[0], gsl_vector, v); ret = argv[0]; ptr1 = v->data; n = v->size; stride = v->stride; #ifdef HAVE_NARRAY_H } else if (NA_IsNArray(obj)) { CHECK_WAVELET(argv[0]); Data_Get_Struct(argv[0], gsl_wavelet, w); GetNArray(obj, na1); ret = obj; ptr1 = (double*) na1->ptr; n = na1->total; naflag = 1; stride = 1; } else if (NA_IsNArray(argv[0])) { CHECK_WAVELET(obj); Data_Get_Struct(obj, gsl_wavelet, w); GetNArray(argv[0], na1); ret = argv[0]; ptr1 = (double*) na1->ptr; n = na1->total; naflag = 1; stride = 1; #endif } else { rb_raise(rb_eTypeError, "wrong argument type"); } itmp = 1; break; } switch (argc - itmp) { case 2: CHECK_FIXNUM(argv[itmp]); CHECK_WORKSPACE(argv[itmp+1]); dir = FIX2INT(argv[itmp]); Data_Get_Struct(argv[itmp+1], gsl_wavelet_workspace, work); break; case 1: if (TYPE(argv[itmp]) == T_FIXNUM) { dir = FIX2INT(argv[itmp]); work = gsl_wavelet_workspace_alloc(v->size); flag = 1; } else if (rb_obj_is_kind_of(argv[itmp], cgsl_wavelet_workspace)) { Data_Get_Struct(argv[itmp], gsl_wavelet_workspace, work); } else { rb_raise(rb_eTypeError, "wrong argument type"); } break; case 0: work = gsl_wavelet_workspace_alloc(v->size); flag = 1; break; default: rb_raise(rb_eArgError, "too many arguments"); break; } if (naflag == 0) { if (sss == RB_GSL_DWT_COPY) { vnew = gsl_vector_alloc(v->size); gsl_vector_memcpy(vnew, v); ary = Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew); ptr2 = vnew->data; } else { ary = ret; ptr2 = ptr1; } } else { #ifdef HAVE_NARRAY_H if (sss == RB_GSL_DWT_COPY) { ary = na_make_object(NA_DFLOAT, na1->rank, na1->shape, cNArray); ptr2 = NA_PTR_TYPE(ary, double*); memcpy(ptr2, ptr1, sizeof(double)*n); } else {
/* this function handles sink events */ static gboolean gst_dwt_filter_sink_event (GstPad * pad, GstObject * parent, GstEvent * event) { gboolean ret; GstDwtFilter *filter; GstStructure *structure; gchar *format; int i; filter = GST_DWTFILTER (parent); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_CAPS: { GstCaps *caps; gst_event_parse_caps (event, &caps); /* do something with the caps */ for (i = 0; i < gst_caps_get_size (caps); i++) { structure = gst_caps_get_structure (caps, i); if(structure) { gst_structure_get_int(structure, "width", &filter->width); gst_structure_get_int(structure, "height", &filter->height); format = gst_structure_get_string(structure, "format"); g_print("width = %d height = %d format = %s\n", filter->width, filter->height, format); //pAccum = malloc(4 * width * height * sizeof(long int)); //memset(pAccum, 0, 4 * width * height * sizeof(long int)); filter->pDWTBuffer = (double*) malloc(filter->width * filter->height * sizeof(double)); filter->pTmpBuffer = (double*) malloc(filter->width * filter->height * sizeof(double)); filter->pTmpBuffer2 = (double*) malloc(filter->width * filter->height * sizeof(double)); memset(filter->pDWTBuffer, 0, filter->width * filter->height * sizeof(double)); filter->work = gsl_wavelet_workspace_alloc (filter->width); } else { // g_print("structure = 0\n"); } } /* and forward */ ret = gst_pad_event_default (pad, parent, event); break; } case GST_EVENT_QOS: g_print("GST_EVENT_QOS\n"); GstQOSType type; gdouble proportion; GstClockTimeDiff diff; GstClockTime timestamp; gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp); ret = gst_pad_event_default (pad, parent, event); break; default: ret = gst_pad_event_default (pad, parent, event); break; } return ret; }