Var* ff_fft(vfuncptr func, Var* arg) { Var *real = NULL, *img = NULL; double* data; int i, j, n, x, y, z; COMPLEX *in, *out; Alist alist[4]; alist[0] = make_alist("real", ID_VAL, NULL, &real); alist[1] = make_alist("img", ID_VAL, NULL, &img); alist[2].name = NULL; if (parse_args(func, arg, alist) == 0) return (NULL); if (real == NULL && img == NULL) { parse_error("%s: No real or imaginary objects specified\n", func->name); return (NULL); } x = GetSamples(V_SIZE(real), V_ORG(real)); y = GetLines(V_SIZE(real), V_ORG(real)); z = GetBands(V_SIZE(real), V_ORG(real)); if (img == NULL && x == 2) { n = y * z; in = (COMPLEX*)calloc(n, sizeof(COMPLEX)); out = (COMPLEX*)calloc(n, sizeof(COMPLEX)); for (i = 0; i < y; i++) { for (j = 0; j < z; j++) { in[i].re = extract_double(real, cpos(0, i, j, real)); in[i].im = extract_double(real, cpos(1, i, j, real)); } } } else { n = V_DSIZE(real); in = (COMPLEX*)calloc(n, sizeof(COMPLEX)); out = (COMPLEX*)calloc(n, sizeof(COMPLEX)); for (i = 0; i < n; i++) { in[i].re = extract_double(real, i); in[i].im = (img == NULL ? 0.0 : extract_double(img, i)); } } if (func->fdata == (void*)1) { fft(in, n, out); } else { rft(in, n, out); } data = (double*)calloc(n * 2, sizeof(double)); for (i = 0; i < n; i++) { data[i * 2] = out[i].re; data[i * 2 + 1] = out[i].im; } return (newVal(BSQ, 2, n, 1, DV_DOUBLE, data)); }
Var* ff_realfft(vfuncptr func, Var* arg) { Var* obj = NULL; int i, n; double *in, *out; Alist alist[3]; alist[0] = make_alist("obj", ID_VAL, NULL, &obj); alist[1].name = NULL; if (parse_args(func, arg, alist) == 0) return (NULL); if (obj == NULL) { parse_error("%s: No object specified\n", func->name); return (NULL); } n = V_DSIZE(obj); in = (double*)calloc(n, sizeof(double)); out = (double*)calloc(n, sizeof(double)); for (i = 0; i < n; i++) { in[i] = extract_double(obj, i); } if (func->fdata == (void*)1) { realfft(in, n, out); } else { realrft(in, n, out); } return (newVal(BSQ, 1, n, 1, DV_DOUBLE, out)); }
static double *extract_dblarr(const struct cfg_field *self, size_t *size, enum cfg_status *status) { double *out=NULL, tmp=0; struct cfg_strvec *vec=NULL; size_t i=0; *size=0; *status = CFG_SUCCESS; if (!self) { // not OK to have null field! *status = CFG_EMPTY; goto _extract_dblarr_bail; } vec = CFG_FIELD_GET_VEC(self); if (!vec || vec->size==0) { // ok to have empty array, just exit goto _extract_dblarr_bail; } out = calloc(vec->size, sizeof(double)); if (!out){ fprintf(stderr,"Failed to allocate double array copy for field '%s'\n", CFG_FIELD_NAME(self)); exit(1); } for (i=0; i<vec->size; i++) { tmp = extract_double(vec->data[i], status); if (*status) { goto _extract_dblarr_bail; } out[i] = tmp; } *size = vec->size; _extract_dblarr_bail: if (*status) { free(out); out=NULL; } return out; }
/* * Public * * Extract a scalar as a double */ double cfg_get_double(const struct cfg *self, const char *name, enum cfg_status *status) { double val=0; const struct cfg_field *field=NULL; char *str=NULL; field = cfg_find(self, name, status); if (*status==CFG_SUCCESS) { if (CFG_TYPE_SCALAR != CFG_FIELD_TYPE(field)) { *status = CFG_TYPE_ERROR; } else { str = CFG_FIELD_GET_DATA(field, 0); val = extract_double(str, status); } } return val; }
Var* ff_realfft3(vfuncptr func, Var* arg) { Var* obj = NULL; size_t i, n; double* in; Alist alist[3]; alist[0] = make_alist("obj", ID_VAL, NULL, &obj); alist[1].name = NULL; if (parse_args(func, arg, alist) == 0) return (NULL); if (obj == NULL) { parse_error("%s: No object specified\n", func->name); return (NULL); } n = V_DSIZE(obj); if (n > INT_MAX) { parse_error("%s: fft function does not handle objects greater than %ld bytes.\n", func->name, INT_MAX); return NULL; } in = (double*)calloc(n, sizeof(double)); if (in == NULL) { parse_error("%s: Unable to alloc %ld bytes.\n", func->name, n * sizeof(double)); return NULL; } for (i = 0; i < n; i++) { in[i] = extract_double(obj, i); } if (func->fdata == (void*)1) { mayer_realfft(n, in); } else { mayer_realifft(n, in); } return (newVal(BSQ, 1, n, 1, DV_DOUBLE, in)); }
Var* ff_realfft2(vfuncptr func, Var* arg) { Var* obj = NULL; int i, j, n, x; double* in; Alist alist[3]; alist[0] = make_alist("obj", ID_VAL, NULL, &obj); alist[1].name = NULL; if (parse_args(func, arg, alist) == 0) return (NULL); if (obj == NULL) { parse_error("%s: No object specified\n", func->name); return (NULL); } n = V_DSIZE(obj); for (x = n; (x & 1) == 0; x >>= 1) ; if (x != 1) { parse_error("dimension not a power of 2. Use version 0.\n"); return (NULL); } in = (double*)calloc(n, sizeof(double)); for (i = 0; i < n; i++) { in[i] = extract_double(obj, i); } if (func->fdata == (void*)1) { rdft(n, cos(M_PI / n), sin(M_PI / n), in); } else { rdft(n, cos(M_PI / n), -sin(M_PI / n), in); for (j = 0; j <= n - 1; j++) { in[j] *= 2.0 / n; } } return (newVal(BSQ, 1, n, 1, DV_DOUBLE, in)); }
/* ** compute_windowed_mean() - computes the mean and count of the pixels ** within a wxh window, using a running-sum table. ** Returns the mean and count for each pixel. */ void init_sums(Var* data, int w, int h, int d, Var** rn, Var** rs, Var** rcount, Var** rmean, Var** rsigma, double ignore) { int x, y, z; int i, j, k; size_t p1, p2, p3, p4, p5, p6, p7, p8; size_t nelements; int east, south, north, west, front, back; double *s, *s2; int* n; float* mean; float* sigma; int* c; double value; double sum, sum2; int count; x = GetX(data); y = GetY(data); z = GetZ(data); nelements = V_DSIZE(data); s = calloc(nelements, sizeof(double)); /* running sum */ s2 = calloc(nelements, sizeof(double)); /* running sum */ n = calloc(nelements, sizeof(int)); /* running count */ mean = calloc(nelements, sizeof(float)); sigma = calloc(nelements, sizeof(float)); c = calloc(nelements, sizeof(int)); /* ** compute the running sum and count of V ** ** s(i,j) = f(i,j) +s(i-1,j)+s(i,j-1)-s(i-1,j-1) */ for (k = 0; k < z; k++) { for (j = 0; j < y; j++) { for (i = 0; i < x; i++) { p1 = cpos(i, j, k, data); value = extract_double(data, p1); if (value != ignore) { s[p1] = value; s2[p1] = value * value; n[p1] = 1; } if (i) { p2 = cpos(i - 1, j, k, data); s[p1] += s[p2]; s2[p1] += s2[p2]; n[p1] += n[p2]; } if (j) { p3 = cpos(i, j - 1, k, data); s[p1] += s[p3]; s2[p1] += s2[p3]; n[p1] += n[p3]; } if (i && j) { p4 = cpos(i - 1, j - 1, k, data); s[p1] -= s[p4]; s2[p1] -= s2[p4]; n[p1] -= n[p4]; } if (k) { p5 = cpos(i, j, k - 1, data); s[p1] += s[p5]; s2[p1] += s2[p5]; n[p1] += n[p5]; if (i) { p6 = cpos(i - 1, j, k - 1, data); s[p1] -= s[p6]; s2[p1] -= s2[p6]; n[p1] -= n[p6]; } if (j) { p7 = cpos(i, j - 1, k - 1, data); s[p1] -= s[p7]; s2[p1] -= s2[p7]; n[p1] -= n[p7]; } if (i && j) { p8 = cpos(i - 1, j - 1, k - 1, data); s[p1] += s[p8]; s2[p1] += s2[p8]; n[p1] += n[p8]; } } } } } /* Formula to compute windowed sum from running sum is: ** s(u,v) = s(u+M-1,v+N-1) - s(u-1,v+N-1) - s(u+M-1,v-1) + s(u-1,v-1) ** This is: sum(pt) = s(se) - s(sw) - s(ne) + s(nw). ** Given a 4x3 mask, it's this: ** ** nw -- -- -- ne ** -- pt -- -- -- ** -- -- -- -- -- ** sw -- -- -- se **/ for (k = 0; k < z; k++) { for (j = 0; j < y; j++) { for (i = 0; i < x; i++) { east = min(i + (w / 2), x - 1); south = min(j + (h / 2), y - 1); front = min(k + (d / 2), z - 1); west = i - (w / 2) - 1; north = j - (h / 2) - 1; back = k - (d / 2) - 1; p1 = cpos(east, south, front, data); count = n[p1]; sum = s[p1]; sum2 = s2[p1]; if (west >= 0) { p2 = cpos(west, south, front, data); count -= n[p2]; sum -= s[p2]; sum2 -= s2[p2]; } if (north >= 0) { p3 = cpos(east, north, front, data); count -= n[p3]; sum -= s[p3]; sum2 -= s2[p3]; } if (north >= 0 && west >= 0) { p4 = cpos(west, north, front, data); count += n[p4]; sum += s[p4]; sum2 += s2[p4]; } if (back >= 0) { p5 = cpos(east, south, back, data); count -= n[p5]; sum -= s[p5]; sum2 -= s2[p5]; if (west >= 0) { p6 = cpos(west, south, back, data); count += n[p6]; sum += s[p6]; sum2 += s2[p6]; } if (north >= 0) { p7 = cpos(east, north, back, data); count += n[p7]; sum += s[p7]; sum2 += s2[p7]; } if (north >= 0 && west >= 0) { p8 = cpos(west, north, back, data); count -= n[p8]; sum -= s[p8]; sum2 -= s2[p8]; } } /* sum = s[cpos(min(i+w-1, x-1), min(j+h-1, y-1), k, data)]; sum -= (i ? s[cpos(i-1, min(j+h-1, y-1), k, data)] : 0.0); sum -= (j ? s[cpos(min(i+w-1, x-1), j-1, k, data)] : 0.0); sum += (i == 0 || j == 0 ? 0.0 : s[cpos(i-1,j-1,k,data)]); */ if (count) { p1 = cpos(i, j, k, data); mean[p1] = sum / count; if (count > 1) { sigma[p1] = sqrt((sum2 - (sum * sum / count)) / (count - 1)); } else { sigma[p1] = ignore; } c[p1] = count; } else { mean[cpos(i, j, k, data)] = ignore; sigma[cpos(i, j, k, data)] = ignore; c[p1] = ignore; } } } } free(s2); *rn = newVal(V_ORG(data), V_SIZE(data)[0], V_SIZE(data)[1], V_SIZE(data)[2], DV_INT32, n); *rs = newVal(V_ORG(data), V_SIZE(data)[0], V_SIZE(data)[1], V_SIZE(data)[2], DV_DOUBLE, s); *rcount = newVal(V_ORG(data), V_SIZE(data)[0], V_SIZE(data)[1], V_SIZE(data)[2], DV_INT32, c); *rmean = newVal(V_ORG(data), V_SIZE(data)[0], V_SIZE(data)[1], V_SIZE(data)[2], DV_FLOAT, mean); *rsigma = newVal(V_ORG(data), V_SIZE(data)[0], V_SIZE(data)[1], V_SIZE(data)[2], DV_FLOAT, sigma); }
bool UNPACK_VALUE(serial_context *ser_cont, as_val **value) { int32_t type = READ_CHAR(ser_cont->fd, ser_cont->line_no, ser_cont->col_no, ser_cont->bytes); if (type == EOF) { err("Error while reading value type"); return false; } switch (type) { case 0xc0: // nil return unpack_nil(ser_cont, value); case 0xc3: // boolean true return unpack_boolean(ser_cont, true, value); case 0xc2: // boolean false return unpack_boolean(ser_cont, false, value); case 0xca: { // float float tmp; return extract_float(ser_cont, &tmp) && unpack_double(ser_cont, tmp, value); } case 0xcb: { // double double tmp; return extract_double(ser_cont, &tmp) && unpack_double(ser_cont, tmp, value); } case 0xd0: { // signed 8 bit integer int8_t tmp; return extract_uint8(ser_cont, (uint8_t *)&tmp) && unpack_integer(ser_cont, tmp, value); } case 0xcc: { // unsigned 8 bit integer uint8_t tmp; return extract_uint8(ser_cont, &tmp) && unpack_integer(ser_cont, tmp, value); } case 0xd1: { // signed 16 bit integer int16_t tmp; return extract_uint16(ser_cont, (uint16_t *)&tmp) && unpack_integer(ser_cont, tmp, value); } case 0xcd: { // unsigned 16 bit integer uint16_t tmp; return extract_uint16(ser_cont, &tmp) && unpack_integer(ser_cont, tmp, value); } case 0xd2: { // signed 32 bit integer int32_t tmp; return extract_uint32(ser_cont, (uint32_t *)&tmp) && unpack_integer(ser_cont, tmp, value); } case 0xce: { // unsigned 32 bit integer uint32_t tmp; return extract_uint32(ser_cont, &tmp) && unpack_integer(ser_cont, tmp, value); } case 0xd3: { // signed 64 bit integer int64_t tmp; return extract_uint64(ser_cont, (uint64_t *)&tmp) && unpack_integer(ser_cont, tmp, value); } case 0xcf: { // unsigned 64 bit integer uint64_t tmp; return extract_uint64(ser_cont, &tmp) && unpack_integer(ser_cont, (int64_t)tmp, value); } case 0xc4: case 0xd9: { // raw bytes with 8 bit header uint8_t size; return extract_uint8(ser_cont, &size) && unpack_blob(ser_cont, size, value); } case 0xc5: case 0xda: { // raw bytes with 16 bit header uint16_t size; return extract_uint16(ser_cont, &size) && unpack_blob(ser_cont, size, value); } case 0xc6: case 0xdb: { // raw bytes with 32 bit header uint32_t size; return extract_uint32(ser_cont, &size) && unpack_blob(ser_cont, size, value); } case 0xdc: { // list with 16 bit header uint16_t size; return extract_uint16(ser_cont, &size) && unpack_list(ser_cont, size, value); } case 0xdd: { // list with 32 bit header uint32_t size; return extract_uint32(ser_cont, &size) && unpack_list(ser_cont, size, value); } case 0xde: { // map with 16 bit header uint16_t size; return extract_uint16(ser_cont, &size) && unpack_map(ser_cont, size, value); } case 0xdf: { // map with 32 bit header uint32_t size; return extract_uint32(ser_cont, &size) && unpack_map(ser_cont, size, value); } default: if ((type & 0xe0) == 0xa0) { // raw bytes with 8 bit combined header return unpack_blob(ser_cont, type & 0x1f, value); } if ((type & 0xf0) == 0x80) { // map with 8 bit combined header return unpack_map(ser_cont, type & 0x0f, value); } if ((type & 0xf0) == 0x90) { // list with 8 bit combined header return unpack_list(ser_cont, type & 0x0f, value); } if (type < 0x80) { // 8 bit combined unsigned integer return unpack_integer(ser_cont, type, value); } if (type >= 0xe0) { // 8 bit combined signed integer return unpack_integer(ser_cont, type - 0xe0 - 32, value); } return false; } }
double controller::get_double(string var_name, double default_value){ return extract_double(file_data, var_name, default_value); }