void exit_prog(t_env *env) { if (E_EN != NULL) free2d(E_EN); if (I_HIS != NULL) free2d(I_HIS); FREE_L1; FREE_L2; if (env->l) free(env->l); if (I_CUR) free(I_CUR); ft_bzero(env, sizeof(t_env)); exit(1); }
int minimumTotal(int** triangle, int triangleRowSize, int *triangleColSizes) { int maxCols = maximum(triangleColSizes, triangleRowSize); struct Pair last[2]; int x, y; int k, n, t; int** cache = alloc2d(triangleRowSize, maxCols); int i, j; for (i = 0; i < triangleColSizes[0]; i++) { cache[0][i] = triangle[0][i]; } for (i = 1; i < triangleRowSize; i++) { for (j = 0; j < triangleColSizes[i]; j++) { n = prev(triangle, triangleRowSize, triangleColSizes, i, j, last); t = maxBound; for (k = 0; k < n; k++) { x = last[k].x; y = last[k].y; if (cache[x][y] < t) { t = cache[x][y]; } } cache[i][j] = triangle[i][j] + t; } } t = minimum(cache[triangleRowSize-1], triangleColSizes[triangleRowSize-1]); free2d(cache, triangleRowSize, maxCols); return t; }
void DLL_PREFIX free_net(ANN *net) { free (net->sz); free (net->w); free2d ((void **) (net->act)); free (net); }
int maximalSquare(char** matrix, int rows, int cols) { int i, j, r = 0; int** res; if (rows == 0 || cols == 0) return 0; res = alloc2d(rows, cols); for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { if (matrix[i][j] == '1') {res[i][j] = 1; } } } for (i = 1; i < rows; i++) { for (j = 1; j < cols; j++) { if (matrix[i][j] == '1') { int t; t = min(res[i-1][j-1], res[i-1][j]); res[i][j] = 1 + min(t, res[i][j-1]); } else { res[i][j] = 0; } } } for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { if (res[i][j] > r) r = res[i][j]; } } free2d(res, rows, cols); return (r*r); }
DLL_PREFIX ANN *init_net(char *weightfile) { float ferr; int il, szmax, itemp, nconn; int nlayers, nl1, nhl; FILE *wp; ANN *net; net = (ANN *) malloc(sizeof(ANN)); /* Initialize weights from file*/ if ((wp = fopen(weightfile, "r")) == (FILE *) NULL) { free(net); return (ANN *) NULL; } fread (&itemp, sizeof (int), 1, wp); fread (&nlayers, sizeof (int), 1, wp); net->nlayers = nlayers; net->sz = (int *) malloc(nlayers * sizeof(int)); nl1 = nlayers - 1; nhl = nl1 - 1; fread (net->sz, sizeof (int), nlayers, wp); szmax = 0; nconn = 0; for (il = 0; il < nl1; il++) { if ((il > 0) && (net->sz[il] > szmax)) szmax = net->sz[il]; nconn += net->sz[il] * net->sz[il+1]; } /* * act = matrix of neural activities * w = weight matrix */ if ((net->act = (float **) alloc2d (nhl, szmax, sizeof(float))) == (float **) NULL) { free(net->sz); free(net); return (ANN *) NULL; } if ((net->w = (float *) malloc (nconn * sizeof(float))) == (float *) NULL) { free2d((void **) (net->act)); free(net->sz); free(net); return (ANN *) NULL; } fread (net->w, sizeof(float), nconn, wp); fread (&ferr, sizeof(float), 1, wp); fclose (wp); return(net); }
void grid_destroy(grid* g) { free(g->name); if (g->htype == GRIDHTYPE_LATLON_REGULAR || g->htype == GRIDHTYPE_LATLON_IRREGULAR) gnxy_simple_destroy(g->gridnodes_xy); #if !defined(NO_GRIDUTILS) else if (g->htype == GRIDHTYPE_CURVILINEAR) gnxy_curv_destroy(g->gridnodes_xy); #endif else enkf_quit("programming_error"); if (g->gridnodes_z != NULL) gnz_destroy(g->gridnodes_z); if (g->numlevels != NULL) free2d(g->numlevels); if (g->depth != NULL) free2d(g->depth); free(g); }
void map2d ( struct vtx_data **graph, /* data structure with vertex weights */ double **xvecs, /* vectors to partition */ int nvtxs, /* number of vertices */ int *sets, /* set each vertex gets assigned to */ double *goal, /* desired set sizes */ int vwgt_max /* largest vertex weight */ ) { extern int DEBUG_BPMATCH; /* turn on debuging for bipartite matching */ extern int N_VTX_MOVES; /* total number of vertex moves */ extern int N_VTX_CHECKS; /* total number of moves contemplated */ double *vals[4][MAXSETS]; /* values in sorted lists */ double dist[4]; /* trial separation point */ double size[4]; /* sizes of each set being modified */ int *indices[4][MAXSETS]; /* indices sorting lists */ int startvtx[4][MAXSETS]; /* indices defining separation */ int nsection = 2; /* number of xvectors */ int nsets = 4; /* number of sets being divided into */ void genvals2d(), sorts2d(), inits2d(), checkbp(), movevtxs(); N_VTX_CHECKS = N_VTX_MOVES = 0; /* Generate all the lists of values that need to be sorted. */ genvals2d(xvecs, vals, nvtxs); /* Sort the lists of values. */ sorts2d(vals, indices, nvtxs); /* Now initialize dists and assign to sets. */ inits2d(graph, xvecs, vals, indices, nvtxs, dist, startvtx, size, sets); /* Determine the largest and smallest allowed set sizes. */ /* (For now, assume all sets must be same size, but can easily change.) */ if (DEBUG_BPMATCH > 1) { printf(" Calling check before movevtxs\n"); checkbp(graph, xvecs, sets, dist, nvtxs, nsection); } movevtxs(graph, nvtxs, nsets, dist, indices, vals, startvtx, sets, size, goal, vwgt_max); if (DEBUG_BPMATCH > 0) { printf(" N_VTX_CHECKS = %d, N_VTX_MOVES = %d\n", N_VTX_CHECKS, N_VTX_MOVES); checkbp(graph, xvecs, sets, dist, nvtxs, nsection); } free2d(vals, indices); }
void command(t_env *env, char *s) { char **sa; if (s == NULL) return ; sa = NULL; rm_tabs(&s); sa = ft_strsplit(s, ' '); if (is_own(s)) own_command(env, sa, s); else link_files(env, s); free2d(sa); }
void llsm_delete(llsm* model) { if(model == NULL) return; int nfrm = model -> conf.nfrm; free2d(model -> noise, nfrm); free2d(model -> sinu -> freq, nfrm); free2d(model -> sinu -> ampl, nfrm); free2d(model -> sinu -> phse, nfrm); free2d(model -> eenv -> freq, nfrm); free2d(model -> eenv -> ampl, nfrm); free2d(model -> eenv -> phse, nfrm); free(model -> sinu); free(model -> eenv); if(model -> emin != NULL) free(model -> emin); free(model -> f0); free(model); }
void pruneSpurs(unsigned char **_edge, int s0, int e0, int ss, int es, int dotimes) { // 0 0 0 0 // 0 0 0 0 // 0 0 1 0 // 0 0 0 0 #define checkTemp1(p) !p[1][1] && !p[1][2] && !p[1][3] && !p[2][1] && !p[2][3] && !p[3][1] int S = s0 + ss - 2; int E = e0 + es - 2; unsigned char ** rotatArea = getRotatArea<unsigned char>(1, 1); // unsigned char ** tt = rotatn<unsigned char>((unsigned char**)image, 0, 0, 5, 5, 45); do{ for(int s = s0 + 2; s < S; s++) { for(int e = e0 + 2; e < E; e++) { if(_edge[s][e] > 0) { for(int i=0; i<8; i++) { //順時針for 3×3 template // rotat45n<unsigned char>(_edge, s, e, 1, 1, i, rotatArea); if(checkTemp1(rotatArea)) { _edge[s][e] = 0; break; } } } } } }while(--dotimes > 0); free2d(rotatArea); }
static void model_freemodeldata(model* m) { int i; for (i = 0; i < m->ndata; ++i) { modeldata* data = &m->data[i]; if (data->alloctype == ALLOCTYPE_1D) free(data->data); else if (data->alloctype == ALLOCTYPE_2D) free2d(data->data); else if (data->alloctype == ALLOCTYPE_3D) free3d(data->data); else enkf_quit("programming error"); free(data->tag); } free(m->data); m->ndata = 0; }
int can_equally_split(int arr[], int n) { int sum = 0; int i, s, ret = 0; int **dp; for (i = 0; i < n; ++i) sum += arr[i]; if (sum % 2 != 0) return FALSE; /* initialize dp array */ dp = alloc2d(sum / 2 + 1, n + 1); for (i = 0; i < n + 1; ++i) dp[0][i] = TRUE; for (i = 1; i < sum / 2 + 1; ++i) dp[i][0] = FALSE; /* build dp array * dp[s][i] is true if set Ai: { arr[0], ..., arr[i-1] } * have subset with sum = s. There are two cases: * a) arr[i-1] is part of that subset, so exclude it * from from Ai and take into account sum = s - arr[i-1] * b) arr[i-1] is not part of that subset, so exclude it but still sum = s; */ for (s = 1; s <= sum / 2; s++) { for (i = 1; i <= n; i++) { dp[s][i] = dp[s][i - 1]; if (s >= arr[i - 1]) dp[s][i] = dp[s][i] || dp[s - arr[i - 1]][i - 1]; } } ret = dp[sum / 2][n]; free2d(dp); return ret; }
/** Updates ensemble observations by applying X5 */ static void update_HE(dasystem* das) { model* m = das->m; int ngrid = model_getngrid(m); int gid; observations* obs = das->obs; int e, o; float* HEi_f; float* HEi_a; char do_T = 'T'; float alpha = 1.0f; float beta = 0.0f; int inc = 1; enkf_printf(" updating HE:\n"); assert(das->s_mode == S_MODE_HE_f); HEi_f = malloc(das->nmem * sizeof(ENSOBSTYPE)); HEi_a = malloc(das->nmem * sizeof(ENSOBSTYPE)); /* * the following code for interpolation of X5 essentially coincides with * that in das_updatefields() */ for (gid = 0, o = 0; gid < ngrid && o < obs->nobs; ++gid) { void* grid = model_getgridbyid(m, gid); int periodic_i = grid_isperiodic_x(grid); int periodic_j = grid_isperiodic_y(grid); char fname_X5[MAXSTRLEN]; int ncid; int varid; int dimids[3]; size_t dimlens[3]; size_t start[3], count[3]; float** X5j = NULL; float** X5jj = NULL; float** X5jj1 = NULL; float** X5jj2 = NULL; int mni, mnj; int* iiter; int* jiter; int i, j, ni, nj; int jj, stepj, ii, stepi; assert(obs->obstypes[obs->data[o].type].gridid == gid); das_getfname_X5(das, grid, fname_X5); ncw_open(fname_X5, NC_NOWRITE, &ncid); ncw_inq_varid(fname_X5, ncid, "X5", &varid); ncw_inq_vardimid(fname_X5, ncid, varid, dimids); for (i = 0; i < 3; ++i) ncw_inq_dimlen(fname_X5, ncid, dimids[i], &dimlens[i]); ni = dimlens[1]; nj = dimlens[0]; assert((int) dimlens[2] == das->nmem * das->nmem); jiter = malloc((nj + 1) * sizeof(int)); /* "+ 1" to handle periodic * grids */ iiter = malloc((ni + 1) * sizeof(int)); for (j = 0, i = 0; j < nj; ++j, i += das->stride) jiter[j] = i; if (periodic_j) jiter[nj] = jiter[nj - 1] + das->stride; for (i = 0, j = 0; i < ni; ++i, j += das->stride) iiter[i] = j; if (periodic_i) iiter[ni] = iiter[ni - 1] + das->stride; grid_getdims(grid, &mni, &mnj, NULL); start[0] = 0; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = ni; count[2] = das->nmem * das->nmem; X5j = alloc2d(mni, das->nmem * das->nmem, sizeof(float)); if (das->stride > 1) { X5jj = alloc2d(ni, das->nmem * das->nmem, sizeof(float)); X5jj1 = alloc2d(ni, das->nmem * das->nmem, sizeof(float)); X5jj2 = alloc2d(ni, das->nmem * das->nmem, sizeof(float)); ncw_get_vara_float(fname_X5, ncid, varid, start, count, X5jj2[0]); } /* * jj, ii are the indices of the subsampled grid; i, j are the indices * of the model grid */ for (jj = 0, j = 0; jj < nj; ++jj) { for (stepj = 0; stepj < das->stride && j < mnj; ++stepj, ++j) { if (das->stride == 1) { /* * no interpolation necessary; simply read the ETMs for the * j-th row from disk */ start[0] = j; ncw_get_vara_float(fname_X5, ncid, varid, start, count, X5j[0]); } else { /* * the following code interpolates the ETM back to the * original grid, first by j, and then by i */ if (stepj == 0) { memcpy(X5jj[0], X5jj2[0], ni * das->nmem * das->nmem * sizeof(float)); memcpy(X5jj1[0], X5jj2[0], ni * das->nmem * das->nmem * sizeof(float)); if (jj < nj - 1 || periodic_j) { start[0] = (jj + 1) % nj; ncw_get_vara_float(fname_X5, ncid, varid, start, count, X5jj2[0]); } } else { float weight2 = (float) stepj / das->stride; float weight1 = (float) 1.0 - weight2; for (ii = 0; ii < ni; ++ii) { float* X5jjii = X5jj[ii]; float* X5jj1ii = X5jj1[ii]; float* X5jj2ii = X5jj2[ii]; for (e = 0; e < das->nmem * das->nmem; ++e) X5jjii[e] = X5jj1ii[e] * weight1 + X5jj2ii[e] * weight2; } } for (ii = 0, i = 0; ii < ni; ++ii) { for (stepi = 0; stepi < das->stride && i < mni; ++stepi, ++i) { if (stepi == 0) memcpy(X5j[i], X5jj[ii], das->nmem * das->nmem * sizeof(float)); else { float weight2 = (float) stepi / das->stride; float weight1 = (float) 1.0 - weight2; float* X5jjii1 = X5jj[ii]; float* X5ji = X5j[i]; float* X5jjii2; if (ii < ni - 1) X5jjii2 = X5jj[ii + 1]; else X5jjii2 = X5jj[(periodic_i) ? (ii + 1) % ni : ii]; for (e = 0; e < das->nmem * das->nmem; ++e) X5ji[e] = X5jjii1[e] * weight1 + X5jjii2[e] * weight2; } } } } /* stride != 1 */ /* * (at this stage X5j should contain the array of X5 matrices * for the j-th row of the grid) */ if (o >= obs->nobs) break; if ((int) (obs->data[o].fj) > j) continue; for (; o < obs->nobs && (int) (obs->data[o].fj) == j; ++o) { float inflation = model_getvarinflation(m, obs->obstypes[obs->data[o].type].vid); /* * HE(i, :) = HE(i, :) * X5 */ i = (int) (obs->data[o].fi); for (e = 0; e < das->nmem; ++e) HEi_f[e] = das->S[e][o]; sgemv_(&do_T, &das->nmem, &das->nmem, &alpha, X5j[i], &das->nmem, HEi_f, &inc, &beta, HEi_a, &inc); /* * applying inflation: */ if (fabsf(inflation - 1.0f) > EPSF) { float v_av = 0.0f; for (e = 0; e < das->nmem; ++e) v_av += HEi_a[e]; v_av /= (float) das->nmem; for (e = 0; e < das->nmem; ++e) HEi_a[e] = (HEi_a[e] - v_av) * inflation + v_av; } for (e = 0; e < das->nmem; ++e) das->S[e][o] = HEi_a[e]; } } /* for stepj */ } /* for jj */ ncw_close(fname_X5, ncid); free(iiter); free(jiter); free2d(X5j); if (das->stride > 1) { free2d(X5jj); free2d(X5jj1); free2d(X5jj2); } } /* for gid */ free(HEi_a); free(HEi_f); das->s_mode = S_MODE_HE_a; } /* update_HE() */
void das_getHE(dasystem* das) { observations* obs = das->obs; model* m = das->m; ENSOBSTYPE* Hx = NULL; int i, e; das->s_mode = S_MODE_HE_f; if (obs->nobs == 0) return; if (das->nmem <= 0) das_getnmem(das); enkf_printf(" ensemble size = %d\n", das->nmem); assert(das->nmem > 0); distribute_iterations(0, das->nmem - 1, nprocesses, rank, " "); /* * ensemble observation array to be filled */ assert(das->S == NULL); das->S = alloc2d(das->nmem, obs->nobs, sizeof(ENSOBSTYPE)); if (das->mode == MODE_ENOI) Hx = calloc(obs->nobs, sizeof(ENSOBSTYPE)); for (i = 0; i < obs->nobstypes; ++i) { obstype* ot = &obs->obstypes[i]; float*** vvv = NULL; float** vv = NULL; H_fn H = NULL; int mvid; int ni, nj, nk; int nobs; int* obsids; char fname[MAXSTRLEN]; enkf_printf(" %s ", ot->name); fflush(stdout); mvid = model_getvarid(m, obs->obstypes[i].varname); if (mvid < 0) enkf_quit("variable \"%s\" required for observation type \"%s\" is not defined", obs->obstypes[i].varname, ot->name); if (ot->issurface) { model_getvardims(m, mvid, &ni, &nj, NULL); vv = alloc2d(nj, ni, sizeof(float)); } else { model_getvardims(m, mvid, &ni, &nj, &nk); vvv = alloc3d(nk, nj, ni, sizeof(float)); } /* * set H */ H = getH(ot->name, ot->hfunction); if (ot->isasync) { int t1 = get_tshift(ot->date_min, ot->async_tstep); int t2 = get_tshift(ot->date_max, ot->async_tstep); int t; for (t = t1; t <= t2; ++t) { enkf_printf("|"); obs_find_bytypeandtime(obs, i, t, &nobs, &obsids); if (nobs == 0) continue; if (das->mode == MODE_ENKF || !enkf_fstatsonly) { for (e = my_first_iteration; e <= my_last_iteration; ++e) { int success = model_getmemberfname_async(m, das->ensdir, ot->varname, ot->name, e + 1, t, fname); H(das, nobs, obsids, fname, e + 1, t, ot->varname, ot->varname2, (ot->issurface) ? (void*) vv : (void*) vvv, das->S[e]); enkf_printf((success) ? "a" : "s"); fflush(stdout); } } if (das->mode == MODE_ENOI) { if (enkf_obstype == OBSTYPE_VALUE) { int success = model_getbgfname_async(m, das->bgdir, ot->varname, ot->name, t, fname); H(das, nobs, obsids, fname, -1, t, ot->varname, ot->varname2, (ot->issurface) ? (void*) vv : (void*) vvv, Hx); enkf_printf((success) ? "A" : "S"); fflush(stdout); } else if (enkf_obstype == OBSTYPE_INNOVATION) { Hx[0] = 0; enkf_printf("-"); fflush(stdout); } } free(obsids); } } else { obs_find_bytype(obs, i, &nobs, &obsids); if (nobs == 0) goto next; if (das->mode == MODE_ENKF || !enkf_fstatsonly) { for (e = my_first_iteration; e <= my_last_iteration; ++e) { model_getmemberfname(m, das->ensdir, ot->varname, e + 1, fname); H(das, nobs, obsids, fname, e + 1, MAXINT, ot->varname, ot->varname2, (ot->issurface) ? (void*) vv : (void*) vvv, das->S[e]); enkf_printf("."); fflush(stdout); } } if (das->mode == MODE_ENOI) { if (enkf_obstype == OBSTYPE_VALUE) { model_getbgfname(m, das->bgdir, ot->varname, fname); H(das, nobs, obsids, fname, -1, MAXINT, ot->varname, ot->varname2, (ot->issurface) ? (void*) vv : (void*) vvv, Hx); enkf_printf("+"); fflush(stdout); } else if (enkf_obstype == OBSTYPE_INNOVATION) { Hx[0] = 0; enkf_printf("-"); fflush(stdout); } } free(obsids); } next: if (ot->issurface) free2d(vv); else free3d(vvv); enkf_printf("\n"); } /* for i (over obstypes) */ #if defined(MPI) if (das->mode == MODE_ENKF || !enkf_fstatsonly) { #if !defined(HE_VIAFILE) /* * communicate HE via MPI */ int ierror, count; /* * Blocking communications can create a bottleneck in instances with * large number of observations (e.g., 2e6 obs., 144 members, 48 * processes), but asynchronous send/receive seem to work well */ if (rank > 0) { MPI_Request request; /* * send ensemble observations to master */ count = (my_last_iteration - my_first_iteration + 1) * obs->nobs; ierror = MPI_Isend(das->S[my_first_iteration], count, MPIENSOBSTYPE, 0, rank, MPI_COMM_WORLD, &request); assert(ierror == MPI_SUCCESS); } else { int r; MPI_Request* requests = malloc((nprocesses - 1) * sizeof(MPI_Request)); /* * collect ensemble observations from slaves */ for (r = 1; r < nprocesses; ++r) { count = (last_iteration[r] - first_iteration[r] + 1) * obs->nobs; ierror = MPI_Irecv(das->S[first_iteration[r]], count, MPIENSOBSTYPE, r, r, MPI_COMM_WORLD, &requests[r - 1]); assert(ierror == MPI_SUCCESS); } ierror = MPI_Waitall(nprocesses - 1, requests, MPI_STATUS_IGNORE); assert(ierror == MPI_SUCCESS); free(requests); } /* * now send the full set of ensemble observations to slaves */ count = das->nmem * obs->nobs; ierror = MPI_Bcast(das->S[0], count, MPIENSOBSTYPE, 0, MPI_COMM_WORLD); assert(ierror == MPI_SUCCESS); #else /* * communicate HE via file */ { int ncid; int varid; size_t start[2], count[2]; if (rank == 0) { int dimids[2]; ncw_create(FNAME_HE, NC_CLOBBER | NC_64BIT_OFFSET, &ncid); ncw_def_dim(FNAME_HE, ncid, "m", das->nmem, &dimids[0]); ncw_def_dim(FNAME_HE, ncid, "p", obs->nobs, &dimids[1]); ncw_def_var(FNAME_HE, ncid, "HE", NC_FLOAT, 2, dimids, &varid); ncw_close(FNAME_HE, ncid); } MPI_Barrier(MPI_COMM_WORLD); ncw_open(FNAME_HE, NC_WRITE, &ncid); ncw_inq_varid(FNAME_HE, ncid, "HE", &varid); start[0] = my_first_iteration; start[1] = 0; count[0] = my_last_iteration - my_first_iteration + 1; count[1] = obs->nobs; ncw_put_vara_float(FNAME_HE, ncid, varid, start, count, das->S[my_first_iteration]); ncw_close(FNAME_HE, ncid); MPI_Barrier(MPI_COMM_WORLD); ncw_open(FNAME_HE, NC_NOWRITE, &ncid); ncw_inq_varid(FNAME_HE, ncid, "HE", &varid); ncw_get_var_float(FNAME_HE, ncid, varid, das->S[0]); ncw_close(FNAME_HE, ncid); } #endif } #endif if (das->mode == MODE_ENOI) { /* * subtract ensemble mean; add background */ if (!enkf_fstatsonly) { double* ensmean = calloc(obs->nobs, sizeof(double)); for (e = 0; e < das->nmem; ++e) { ENSOBSTYPE* Se = das->S[e]; for (i = 0; i < obs->nobs; ++i) ensmean[i] += Se[i]; } for (i = 0; i < obs->nobs; ++i) ensmean[i] /= (double) das->nmem; for (e = 0; e < das->nmem; ++e) { ENSOBSTYPE* Se = das->S[e]; for (i = 0; i < obs->nobs; ++i) Se[i] += Hx[i] - ensmean[i]; } free(ensmean); } else { for (e = 0; e < das->nmem; ++e) { ENSOBSTYPE* Se = das->S[e]; for (i = 0; i < obs->nobs; ++i) Se[i] = Hx[i]; } } } if (das->mode == MODE_ENOI) free(Hx); }
static void update_Hx(dasystem* das) { model* m = das->m; int ngrid = model_getngrid(m); int gid; observations* obs = das->obs; int e, o; enkf_printf(" updating Hx:\n"); assert(das->s_mode == S_MODE_HE_f); /* * the following code for interpolation of X5 essentially coincides with * that in das_updatefields() */ for (gid = 0, o = 0; gid < ngrid && o < obs->nobs; ++gid) { void* grid = model_getgridbyid(m, gid); int periodic_i = grid_isperiodic_x(grid); int periodic_j = grid_isperiodic_y(grid); char fname_w[MAXSTRLEN]; int ncid; int varid; int dimids[3]; size_t dimlens[3]; size_t start[3], count[3]; float** wj = NULL; float** wjj = NULL; float** wjj1 = NULL; float** wjj2 = NULL; int mni, mnj; int* iiter; int* jiter; int i, j, ni, nj; int jj, stepj, ii, stepi; assert(obs->obstypes[obs->data[o].type].gridid == gid); das_getfname_w(das, grid, fname_w); ncw_open(fname_w, NC_NOWRITE, &ncid); ncw_inq_varid(fname_w, ncid, "w", &varid); ncw_inq_vardimid(fname_w, ncid, varid, dimids); for (i = 0; i < 3; ++i) ncw_inq_dimlen(fname_w, ncid, dimids[i], &dimlens[i]); ni = dimlens[1]; nj = dimlens[0]; assert((int) dimlens[2] == das->nmem); jiter = malloc((nj + 1) * sizeof(int)); /* "+ 1" to handle periodic * grids */ iiter = malloc((ni + 1) * sizeof(int)); for (j = 0, i = 0; j < nj; ++j, i += das->stride) jiter[j] = i; if (periodic_j) jiter[nj] = jiter[nj - 1] + das->stride; for (i = 0, j = 0; i < ni; ++i, j += das->stride) iiter[i] = j; if (periodic_i) iiter[ni] = iiter[ni - 1] + das->stride; grid_getdims(grid, &mni, &mnj, NULL); start[0] = 0; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = ni; count[2] = das->nmem; wj = alloc2d(mni, das->nmem, sizeof(float)); if (das->stride > 1) { wjj = alloc2d(ni, das->nmem, sizeof(float)); wjj1 = alloc2d(ni, das->nmem, sizeof(float)); wjj2 = alloc2d(ni, das->nmem, sizeof(float)); ncw_get_vara_float(fname_w, ncid, varid, start, count, wjj2[0]); } /* * jj, ii are the indices of the subsampled grid; i, j are the indices * of the model grid */ for (jj = 0, j = 0; jj < nj; ++jj) { for (stepj = 0; stepj < das->stride && j < mnj; ++stepj, ++j) { if (das->stride == 1) { /* * no interpolation necessary; simply read the ETMs for the * j-th row from disk */ start[0] = j; ncw_get_vara_float(fname_w, ncid, varid, start, count, wj[0]); } else { /* * the following code interpolates the ETM back to the * original grid, first by j, and then by i */ if (stepj == 0) { memcpy(wjj[0], wjj2[0], ni * das->nmem * sizeof(float)); memcpy(wjj1[0], wjj2[0], ni * das->nmem * sizeof(float)); if (jj < nj - 1 || periodic_j) { start[0] = (jj + 1) % nj; ncw_get_vara_float(fname_w, ncid, varid, start, count, wjj2[0]); } } else { float weight2 = (float) stepj / das->stride; float weight1 = (float) 1.0 - weight2; for (ii = 0; ii < ni; ++ii) { float* wjjii = wjj[ii]; float* wjj1ii = wjj1[ii]; float* wjj2ii = wjj2[ii]; for (e = 0; e < das->nmem; ++e) wjjii[e] = wjj1ii[e] * weight1 + wjj2ii[e] * weight2; } } for (ii = 0, i = 0; ii < ni; ++ii) { for (stepi = 0; stepi < das->stride && i < mni; ++stepi, ++i) { if (stepi == 0) memcpy(wj[i], wjj[ii], das->nmem * sizeof(float)); else { float weight2 = (float) stepi / das->stride; float weight1 = (float) 1.0 - weight2; float* wjjii1 = wjj[ii]; float* wji = wj[i]; float* wjjii2; if (ii < ni - 1) wjjii2 = wjj[ii + 1]; else wjjii2 = wjj[(periodic_i) ? (ii + 1) % ni : ii]; for (e = 0; e < das->nmem; ++e) wji[e] = wjjii1[e] * weight1 + wjjii2[e] * weight2; } } } } /* stride != 1 */ /* * (at this stage wj should contain the array of b vectors for * the j-th row of the grid) */ if (o >= obs->nobs) break; if ((int) (obs->data[o].fj) > j) continue; for (; o < obs->nobs && (int) (obs->data[o].fj) == j; ++o) { double dHx = 0.0; double Hx = 0.0; for (e = 0; e < das->nmem; ++e) Hx += das->S[e][o]; Hx /= (double) das->nmem; i = (int) (obs->data[o].fi); /* * HE(i, :) += HA(i, :) * b * 1' */ for (e = 0; e < das->nmem; ++e) dHx += (das->S[e][o] - Hx) * wj[i][e]; for (e = 0; e < das->nmem; ++e) das->S[e][o] += dHx; } } /* for stepj */ } /* for jj */ ncw_close(fname_w, ncid); free(iiter); free(jiter); free2d(wj); if (das->stride > 1) { free2d(wjj); free2d(wjj1); free2d(wjj2); } } /* for gid */ das->s_mode = S_MODE_HE_a; } /* update_Hx() */
/* pääfunktio ****************************/ void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) { doubleMatrix trainingData, A, means, variances, weights; double noiseLevel; int numberOfIterations, updateDistributions; int n, k, i, j, l; /* parametrit */ trainingData = createDoubleMatrixMx(TRAININGDATA_IN); A = createDoubleMatrixMx(A_IN); means = createDoubleMatrixMx(MEANS_IN); variances = createDoubleMatrixMx(VARIANCES_IN); weights = createDoubleMatrixMx(WEIGHTS_IN); noiseLevel = mxGetScalar(NOISELEVEL_IN); numberOfIterations = (int)mxGetScalar(MAX_ITERATIONS_IN); updateDistributions = (int)mxGetScalar(UPDATE_DISTRIBUTIONS); /* globaaleiden muuttujien alustus */ numberOfGaussians = mxGetM(MEANS_IN); dimensions = mxGetN(MEANS_IN); numberOfObserved = mxGetM(TRAININGDATA_IN); numberOfDataRows = mxGetN(TRAININGDATA_IN); numberOfLatent = dimensions - numberOfObserved; states = statelist(); numberOfStates = pow(numberOfGaussians,dimensions); /* skaalaus */ doubleMatrix dummyA = createDoubleMatrix(numberOfObserved,dimensions); rescaleModel(dummyA,means,variances,weights); freeDoubleMatrix(dummyA); /* virhefunktion arvot */ doubleMatrix objMatrix = createDoubleMatrix(2,numberOfIterations / RANGE); l = 0; n = dimensions*dimensions; /* alustetaan globaalit matriisit */ matrixInitialize(); /* EM-algoritmin pääsilmukka */ for (k = 0;k < numberOfIterations;k++) { /* painojen päivitys */ if (updateDistributions) meanMarginalpstategivenx(weights, trainingData, A, means, variances, weights, noiseLevel); /* A:n päivitys */ meanssgivenx(mgssx, mgsx, trainingData, A, means, variances, weights, noiseLevel); multiplyABt(trainingData, mgsx, XmgsxTran); multiplyConst(XmgsxTran,1.0 / numberOfDataRows, XmgsxTranDiv); for (i = 0;i < n;i++) IND(meanMgssx,i,0) = 0; for (i = 0;i < n;i++) for (j = 0;j < numberOfDataRows;j++) IND(meanMgssx,i,0) += IND(mgssx,i,j) / numberOfDataRows; for (i = 0;i < dimensions;i++) for (j = 0;j < dimensions;j++) IND(meanMgssx2,j,i) = IND(meanMgssx,i*dimensions+j,0); inverse(meanMgssx2, invMeanMgssx); multiply(XmgsxTranDiv, invMeanMgssx, A); /* varianssien ja keskiarvojen päivitys */ if (updateDistributions) { meanMarginalpstategivenx(mmpgx, trainingData, A, means, variances, weights, noiseLevel); a11meanx2(ax2, ax, trainingData, A, means, variances, weights, noiseLevel); for (i = 0;i < numberOfGaussians;i++) for (j = 0;j < dimensions;j++) IND(means,i,j) = IND(ax,i,j) / IND(mmpgx,i,j); for (i = 0;i < numberOfGaussians;i++) for (j = 0;j < dimensions;j++) IND(variances,i,j) = IND(ax2,i,j) / IND(mmpgx,i,j) - pow(IND(means,i,j),2); } /* skaalataan IF-malli */ rescaleModel(A,means,variances,weights); if (k % RANGE == 0) { /* tarkistetaan A:n validisuus (ei sisällä pelkästään nollia) */ /* if (!isValid(variances)) { */ if (IND(A,0,0) - NAN < EPSILON) { /* jos ei enää validi, niin laitetaan tästä eteenpäin virhefunktiolle vain Inf -arvoa */ while (l < numberOfIterations / RANGE) { IND(objMatrix,0,l) = FLT_MAX; IND(objMatrix,1,l++) = k; } /* tulostetaan varianssit ja häivytään silmukasta */ printDoubleMatrix(variances); break; } else { IND(objMatrix,0,l) = evaluateObj(trainingData, A, means, variances, weights, noiseLevel); IND(objMatrix,1,l++) = k; } } } /* vapautetaan globaaleiden matriisien varaama muisti*/ matrixFinalize(); /* ulostulot */ A_OUT = createMxArray(A); MEANS_OUT = createMxArray(means); VARIANCES_OUT = createMxArray(variances); WEIGHTS_OUT = createMxArray(weights); OBJ_OUT = createMxArray(objMatrix); free2d(states,dimensions); freeDoubleMatrix(trainingData); freeDoubleMatrix(A); freeDoubleMatrix(means); freeDoubleMatrix(variances); freeDoubleMatrix(weights); freeDoubleMatrix(objMatrix); return; }
void reader_mmt_standard(char* fname, int fid, obsmeta* meta, model* m, observations* obs) { int ncid; int dimid_nprof, dimid_nz; size_t nprof, nz; int varid_lon, varid_lat, varid_z, varid_type; int varid_v = -1; double* lon; double* lat; double** z; double** v; double missval; double validmin = DBL_MAX; double validmax = -DBL_MAX; char* type; char buf[MAXSTRLEN]; int len; int year, month, day; double tunits_multiple, tunits_offset; int mvid; int p, i; for (i = 0; i < meta->npars; ++i) enkf_quit("unknown PARAMETER \"%s\"\n", meta->pars[i].name); if (meta->nstds == 0) enkf_quit("ERROR_STD is necessary but not specified for product \"%s\"", meta->product); ncw_open(fname, NC_NOWRITE, &ncid); ncw_inq_dimid(fname, ncid, "N_PROF", &dimid_nprof); ncw_inq_dimlen(fname, ncid, dimid_nprof, &nprof); ncw_inq_dimid(fname, ncid, "N_LEVELS", &dimid_nz); ncw_inq_dimlen(fname, ncid, dimid_nz, &nz); enkf_printf(" # profiles = %u\n", (unsigned int) nprof); if (nprof == 0) { ncw_close(fname, ncid); return; } enkf_printf(" # z levels = %u\n", (unsigned int) nz); ncw_inq_varid(fname, ncid, "LONGITUDE", &varid_lon); lon = malloc(nprof * sizeof(double)); ncw_get_var_double(fname, ncid, varid_lon, lon); ncw_inq_varid(fname, ncid, "LATITUDE", &varid_lat); lat = malloc(nprof * sizeof(double)); ncw_get_var_double(fname, ncid, varid_lat, lat); ncw_inq_varid(fname, ncid, "PRES_BLUELINK", &varid_z); z = alloc2d(nprof, nz, sizeof(double)); ncw_get_var_double(fname, ncid, varid_z, z[0]); if (strncmp(meta->type, "TEM", 3) == 0) { validmin = -2.0; validmax = 40.0; ncw_inq_varid(fname, ncid, "TEMP_BLUELINK", &varid_v); } else if (strncmp(meta->type, "SAL", 3) == 0) { validmin = 0; validmax = 50.0; ncw_inq_varid(fname, ncid, "PSAL_BLUELINK", &varid_v); } else enkf_quit("observation type \"%s\" not handled for MMT product", meta->type); v = alloc2d(nprof, nz, sizeof(double)); ncw_get_var_double(fname, ncid, varid_v, v[0]); ncw_get_att_double(fname, ncid, varid_v, "_FillValue", &missval); ncw_inq_varid(fname, ncid, "WMO_INST_TYPE", &varid_type); type = malloc(nprof * WMO_INSTSIZE); ncw_get_var_text(fname, ncid, varid_type, type); ncw_close(fname, ncid); strcpy(buf, fname); len = strlen(buf); buf[len - 10] = 0; /* _mmt_qc.nc */ if (!str2int(&buf[len - 12], &day)) enkf_quit("MMT reader: could not convert file name \"%s\" to date", fname); buf[len - 12] = 0; if (!str2int(&buf[len - 14], &month)) enkf_quit("MMT reader: could not convert file name \"%s\" to date", fname); buf[len - 14] = 0; if (!str2int(&buf[len - 18], &year)) enkf_quit("MMT reader: could not convert file name \"%s\" to date", fname); snprintf(buf, MAXSTRLEN, "days since %4d-%02d-%02d", year, month, day); tunits_convert(buf, &tunits_multiple, &tunits_offset); mvid = model_getvarid(m, obs->obstypes[obstype_getid(obs->nobstypes, obs->obstypes, meta->type)].varname, 1); for (p = 0; p < (int) nprof; ++p) { char inststr[MAXSTRLEN]; snprintf(inststr, MAXSTRLEN, "WMO%04u", type[p * WMO_INSTSIZE]); for (i = 0; i < (int) nz; ++i) { observation* o; obstype* ot; if (fabs(v[p][i] - missval) < EPS || v[p][i] < validmin || v[p][i] > validmax) continue; if (z[p][i] < 0.0) continue; obs_checkalloc(obs); o = &obs->data[obs->nobs]; o->product = st_findindexbystring(obs->products, meta->product); assert(o->product >= 0); o->type = obstype_getid(obs->nobstypes, obs->obstypes, meta->type); assert(o->type >= 0); ot = &obs->obstypes[o->type]; o->instrument = st_add_ifabscent(obs->instruments, inststr, -1); o->id = obs->nobs; o->fid = fid; o->batch = p; o->value = v[p][i]; o->std = 0.0; o->lon = lon[p]; o->lat = lat[p]; o->depth = z[p][i]; o->status = model_xy2fij(m, mvid, o->lon, o->lat, &o->fi, &o->fj); if (!obs->allobs && o->status == STATUS_OUTSIDEGRID) break; if (o->status == STATUS_OK) o->status = model_z2fk(m, mvid, o->fi, o->fj, o->depth, &o->fk); else o->fk = NaN; if ((o->status == STATUS_OK) && (o->lon <= ot->xmin || o->lon >= ot->xmax || o->lat <= ot->ymin || o->lat >= ot->ymax || o->depth <= ot->zmin || o->depth >= ot->zmax)) o->status = STATUS_OUTSIDEOBSDOMAIN; o->date = tunits_offset + 0.5; o->aux = -1; obs->nobs++; } } free(lon); free(lat); free2d(v); free2d(z); free(type); }
int find_max_partition(int arr[], int n) { int sum = 0; int i, s, ret = 0; int **dp; int *a; int cnt, p; for (i = 0; i < n; ++i) sum += arr[i]; if (sum % 2 != 0) sum -= 1; /* initialize dp array */ dp = alloc2d(sum + 1, n + 1); for (i = 0; i < n + 1; ++i) dp[0][i] = TRUE; for (i = 1; i < sum + 1; ++i) dp[i][0] = FALSE; /* build dp array * dp[s][i] is true if set Ai: { arr[0], ..., arr[i-1] } * have subset with sum = s. There are two cases: * a) arr[i-1] is part of that subset, so exclude it * from from Ai and take into account sum = s - arr[i-1] * b) arr[i-1] is not part of that subset, so exclude it but still sum = s; */ for (s = 1; s <= sum; s++) { for (i = 1; i <= n; i++) { dp[s][i] = dp[s][i - 1]; if (s >= arr[i - 1]) dp[s][i] = dp[s][i] || dp[s - arr[i - 1]][i - 1]; } } a = (int*) malloc(n * sizeof(int)); /* If set Arr: { arr[0], ..., arr[n-1] } has two * disjoint subsets with the same sum, Arr has to have * subset A1 with sum = x and A2 with sum = 2*x, and * moreover A1 has to be subset of A2 * be careful, checking if dp[s][n] and dp[s/2][n] is TRUE is not enough (A1 and A2 could be disjoint) * moreover, from dp one cannot conclude that A1 \subset A2, bcos there can be two subsets A1a and A1v * with sum x, and unluckily dp algorithm could take wrong one into account, so * A2 has to be checked if can be splited into two equal parts */ for (s = sum; s >= 2; s -= 2) { if (dp[s][n] == FALSE && dp[s / 2][n] == FALSE) continue; cnt = 0; p = s; while (p != 0) for (i = n; i >= 1; --i) if (dp[p][i] == TRUE && dp[p][i - 1] == FALSE) { a[cnt++] = arr[i - 1]; p -= arr[i - 1]; break; } if (can_equally_split(a, cnt) == TRUE) { ret = s / 2; break; } } free(a); free2d(dp); return ret; }
llsm* llsm_analyze(llsm_parameters param, FP_TYPE* x, int nx, int fs, FP_TYPE* f0, int nf0) { llsm* model = malloc(sizeof(llsm)); model -> sinu = malloc(sizeof(llsm_sinparam)); model -> eenv = malloc(sizeof(llsm_sinparam)); int nfft = pow(2, ceil(log2(fs * param.a_tfft))); FP_TYPE fftbuff[65536]; // C2 FP_TYPE** spectrogram = (FP_TYPE**)malloc2d(nf0, nfft / 2, sizeof(FP_TYPE)); FP_TYPE** phasegram = (FP_TYPE**)malloc2d(nf0, nfft / 2, sizeof(FP_TYPE)); FP_TYPE** phasegram_d = (FP_TYPE**)malloc2d(nf0, nfft / 2, sizeof(FP_TYPE)); spectrogram_analyze(param, x, nx, fs, f0, nf0, nfft, fftbuff, "blackman_harris", spectrogram, phasegram, phasegram_d, NULL); // C3 model -> f0 = refine_f0(param, nfft, fs, f0, nf0, spectrogram, phasegram, phasegram_d); FP_TYPE* rf0 = f0; // C4 model -> sinu -> freq = (FP_TYPE**)malloc2d(nf0, param.a_nhar, sizeof(FP_TYPE)); model -> sinu -> ampl = (FP_TYPE**)malloc2d(nf0, param.a_nhar, sizeof(FP_TYPE)); model -> sinu -> phse = (FP_TYPE**)malloc2d(nf0, param.a_nhar, sizeof(FP_TYPE)); FP_TYPE* tmpfreq = calloc(nf0, sizeof(FP_TYPE)); FP_TYPE* tmpampl = calloc(nf0, sizeof(FP_TYPE)); FP_TYPE* tmpphse = calloc(nf0, sizeof(FP_TYPE)); for(int i = 0; i < param.a_nhar; i ++) { find_harmonic_trajectory(param, spectrogram, phasegram, nfft, fs, rf0, nf0, i + 1, tmpfreq, tmpampl, tmpphse); for(int j = 0; j < nf0; j ++) { model -> sinu -> freq[j][i] = tmpfreq[j]; model -> sinu -> ampl[j][i] = tmpampl[j]; model -> sinu -> phse[j][i] = tmpphse[j]; } if(i == 0) for(int j = 0; j < nf0; j ++) if(fabs(rf0[j] - tmpfreq[j]) > 1) rf0[j] = tmpfreq[j]; } free2d(spectrogram, nf0); free2d(phasegram, nf0); free2d(phasegram_d, nf0); // C6 FP_TYPE* resynth = calloc(nx + param.a_nhop, sizeof(FP_TYPE)); int nresynth = 2 * param.a_nhop; FP_TYPE* resynth_window = hanning(nresynth); for(int i = 0; i < nf0; i ++) { int tn = i * param.a_nhop; FP_TYPE* resynth_frame = synth_sinusoid_frame( model -> sinu -> freq[i], model -> sinu -> ampl[i], model -> sinu -> phse[i], param.a_nhar, fs, nresynth); for(int j = 0; j < nresynth; j ++) if(tn + j - nresynth / 2 > 0) resynth[tn + j - nresynth / 2] += resynth_frame[j] * resynth_window[j]; free(resynth_frame); } free(resynth_window); // C7 -> CB7 for(int i = 0; i < nx; i ++) resynth[i] = x[i] - resynth[i]; FP_TYPE** noise_spectrogram = (FP_TYPE**)malloc2d(nf0, nfft / 2, sizeof(FP_TYPE)); model -> noise = (FP_TYPE**)calloc(nf0, sizeof(FP_TYPE*)); spectrogram_analyze(param, resynth, nx, fs, f0, nf0, nfft, fftbuff, "hanning", noise_spectrogram, NULL, NULL, NULL); free(resynth); FP_TYPE* freqwrap = llsm_wrap_freq(0, fs / 2, param.a_nnos, param.a_noswrap); for(int t = 0; t < nf0; t ++) { /* // cut out the spectrum content around harmonics FP_TYPE resf = f0[t]; int winlen = min(nfft, floor(fs / resf * 2.5) * 2); int wmlobe = ceil(1.3 * nfft / winlen); for(int i = 0; i < param.a_nhar; i ++) { FP_TYPE centerf = model -> sinu -> freq[t][i]; if(centerf > fs / nfft) { // make sure the frequency is valid int lbin = max(0 , round(centerf / fs * nfft - wmlobe)); int hbin = min(nfft / 2 - 1, round(centerf / fs * nfft + wmlobe)); for(int j = lbin; j <= hbin; j ++) noise_spectrogram[t][j] = -30; } }*/ model -> noise[t] = llsm_geometric_envelope(noise_spectrogram[t], nfft / 2, fs, freqwrap, param.a_nnos); } free(freqwrap); free2d(noise_spectrogram, nf0); // CB2 const int filtord = 60; FP_TYPE* h = fir1(filtord, param.a_mvf / fs * 2.0, "highpass", "hanning"); FP_TYPE* xh = conv(x, h, nx, filtord); free(h); // CB3 for(int i = 0; i < nx + filtord - 1; i ++) xh[i] = xh[i] * xh[i]; int mavgord = round(fs / param.a_mvf * 5); FP_TYPE* xhe = moving_avg(xh + filtord / 2, nx, mavgord); free(xh); // CB5 FP_TYPE** env_spectrogram = (FP_TYPE**)malloc2d(nf0, nfft / 2, sizeof(FP_TYPE)); FP_TYPE** env_phasegram = (FP_TYPE**)malloc2d(nf0, nfft / 2, sizeof(FP_TYPE)); model -> emin = calloc(nf0, sizeof(FP_TYPE)); spectrogram_analyze(param, xhe + mavgord / 2, nx, fs, f0, nf0, nfft, fftbuff, "blackman_harris", env_spectrogram, env_phasegram, NULL, model -> emin); free(xhe); // CB6 model -> eenv -> freq = (FP_TYPE**)malloc2d(nf0, param.a_nhar, sizeof(FP_TYPE)); model -> eenv -> ampl = (FP_TYPE**)malloc2d(nf0, param.a_nhar, sizeof(FP_TYPE)); model -> eenv -> phse = (FP_TYPE**)malloc2d(nf0, param.a_nhar, sizeof(FP_TYPE)); for(int i = 0; i < param.a_nhare; i ++) { find_harmonic_trajectory(param, env_spectrogram, env_phasegram, nfft, fs, rf0, nf0, i + 1, tmpfreq, tmpampl, tmpphse); for(int j = 0; j < nf0; j ++) { model -> eenv -> freq[j][i] = tmpfreq[j]; model -> eenv -> ampl[j][i] = tmpampl[j]; model -> eenv -> phse[j][i] = tmpphse[j]; } } free(tmpfreq); free(tmpampl); free(tmpphse); free2d(env_spectrogram, nf0); free2d(env_phasegram, nf0); // CB8 for(int i = 0; i < nf0; i ++) { FP_TYPE base = model -> sinu -> phse[i][0]; if(rf0[i] <= 0.0) continue; for(int j = 0; j < param.a_nhar; j ++) { model -> sinu -> phse[i][j] -= base * model -> sinu -> freq[i][j] / rf0[i]; model -> sinu -> phse[i][j] = fmod(model -> sinu -> phse[i][j], M_PI * 2.0); } for(int j = 0; j < param.a_nhare; j ++) { model -> eenv -> phse[i][j] -= base * model -> eenv -> freq[i][j] / rf0[i]; model -> eenv -> phse[i][j] = fmod(model -> eenv -> phse[i][j], M_PI * 2.0); } } model -> sinu -> nfrm = nf0; model -> eenv -> nfrm = nf0; model -> sinu -> nhar = param.a_nhar; model -> eenv -> nhar = param.a_nhare; model -> conf.nfrm = nf0; model -> conf.nhop = param.a_nhop; model -> conf.nhar = param.a_nhar; model -> conf.nhare = param.a_nhare; model -> conf.nnos = param.a_nnos; model -> conf.nosf = fs / 2.0; model -> conf.mvf = param.a_mvf; model -> conf.noswrap = param.a_noswrap; return model; }
int main(int argc, char** argv) { int i; double t_opt = 0.; param_ param, param_0; gsl_complex **K, *W, *CW, *dW, *complex_rot; arr_info_ arr_info; double *argW, *absW; double *rec, *prec; pr_max_ *pr_max; double AREA, AREA_0=1000.; lattice_point_ **nghb; if(argc!=3){ printf("Usage: recall/precision threshold, random seed\n"); exit(1); } /* * Param initialization */ param.idum = (long *)malloc(sizeof(long)); // pointer to long integer for random number generator *param.idum = atoi(argv[2]); // initialization to random number param.thresh = HeaviSide; // function pointer for threshold param.T = 2.0; param.T_opt = 1.0; param.dt = 0.01; param.L = 100; param.L_sub = 5; param.int_range_rat = 3; param.sigma = 0.008; param.mu = 15; param.Kinh = 0.012; param.Kloc = 1.; param.d = 0.25; param.test_d = 4; param.excl_d = 8; param.M = 1.; param.delta = 5.; param.A = 5.; param.N_max = 2; param.kappa_upp = 0.6; param.kappa_low = 0.4; param.ecc_upp = 0.6; param.ecc_low = 0.4; param.kappa_xy = 0.1; param.mask_min = 2; param.mask_max = 4; param.kappa_mask = 0.25; param.pr_range = atoi(argv[1]); param.clutter_thresh = 0.15; param.excl_thresh = 1/2; param.temp = 0.005; param.mut = 0.01; param.gnuplot = 1; // 0 for screen and 1 for print to file /**** PIPE TO GNUPLOT ***/ FILE *pipe=popen("gnuplot -persist","w"); fprintf(pipe,"set size square\n"); fprintf(pipe,"unset key\n"); if(param.gnuplot == 1){ fprintf(pipe,"set term post enhanced color \"Helvetica\" 20\n"); } fflush(pipe); /* * Specify the precision/recall range * and initiate pr_max. */ rec =(double *)malloc(param.pr_range*sizeof(double)); prec =(double *)malloc(param.pr_range*sizeof(double)); param.pr_thresh =(double *)malloc(param.pr_range*sizeof(double)); for(i=0;i<param.pr_range;i++) param.pr_thresh[i] = (double) i/param.pr_range; pr_max = (pr_max_ *)malloc(param.pr_range*sizeof(pr_max_)); /* * Need to define param_0. */ param_0 = param; /* * Kernel Initialization */ nghb = NghbInit(param, &arr_info, param.int_range_rat/sqrt(2*param.sigma)); K = KernelInit(param); /* * Initializing the rW, pW, eW size * variable. */ W=(gsl_complex *)malloc(sizeof(gsl_complex)*param.L*param.L); CW=(gsl_complex *)malloc(sizeof(gsl_complex)*param.L*param.L); dW=(gsl_complex *)malloc(sizeof(gsl_complex)*param.L*param.L); complex_rot=(gsl_complex *)malloc(sizeof(gsl_complex)*param.L*param.L); argW=(double *)malloc(sizeof(double)*param.L*param.L); absW=(double *)malloc(sizeof(double)*param.L*param.L); /* * Optimization loop */ while(t_opt < param.T_opt){ t_opt += OneSim(param, pipe, arr_info, nghb, K, W, CW, dW, complex_rot, argW, absW, rec, prec, pr_max); /* * Do we keep or reject the current choice * of parameters? */ AREA = CalcArea(param, pr_max); if( 1./(1.+exp((AREA - AREA_0)/param.temp)) > ran1(param.idum) ){ param_0 = param; AREA_0 = AREA; } param = NewParam(param_0); /* * Here we print out the current choice of * parameters */ printf("%e %e %e %e %e %e %e\n", t_opt, param_0.sigma, param_0.mu, param_0.Kinh, param_0.delta, param_0.A, AREA_0); fflush(stdout); } //End optimization loop; free(param.idum); free(W); free(CW); free(dW); free(absW); free(argW); free(rec); free(prec); free(param.pr_thresh); free(pr_max); free2d((void **) nghb, param.L*param.L); free2d((void **) K, 4*param.L+1); pclose(pipe); return (EXIT_SUCCESS); }
FP_TYPE* llsm_synthesize(llsm_parameters param, llsm* model, int* ny) { int nfrm = model -> conf.nfrm; int nhop = model -> conf.nhop; int fs = param.s_fs; int nfft = nhop * 4; *ny = nfrm * nhop + nfft; FP_TYPE* y_sin = calloc(*ny, sizeof(FP_TYPE)); FP_TYPE* y_env = calloc(*ny, sizeof(FP_TYPE)); FP_TYPE* y_env_mix = calloc(*ny, sizeof(FP_TYPE)); // D1 FP_TYPE** sin_phse = (FP_TYPE**)copy2d(model -> sinu -> phse, nfrm, model -> conf.nhar , sizeof(FP_TYPE)); FP_TYPE** env_phse = (FP_TYPE**)copy2d(model -> eenv -> phse, nfrm, model -> conf.nhare, sizeof(FP_TYPE)); FP_TYPE phse0 = 0; for(int i = 1; i < nfrm; i ++) { FP_TYPE f0 = model -> sinu -> freq[i][0]; phse0 += f0 * nhop / fs * 2.0 * M_PI; sin_phse[i][0] = fmod(phse0, 2.0 * M_PI) - M_PI; for(int j = 1; j < model -> conf.nhar; j ++) sin_phse[i][j] = sin_phse[i][0] / f0 * model -> sinu -> freq[i][j] + model -> sinu -> phse[i][j]; for(int j = 0; j < model -> conf.nhare; j ++) env_phse[i][j] = sin_phse[i][0] / f0 * model -> eenv -> freq[i][j] + model -> eenv -> phse[i][j]; } // D2, D3 FP_TYPE* ola_window = hanning(nhop * 2); for(int i = 0; i < nfrm; i ++) { int tn = i * nhop; if(model -> f0[i] <= 0.0) continue; FP_TYPE* sin_frame = synth_sinusoid_frame( model -> sinu -> freq[i], model -> sinu -> ampl[i], sin_phse[i], model -> conf.nhar, fs, nhop * 2); FP_TYPE* env_frame = synth_sinusoid_frame( model -> eenv -> freq[i], model -> eenv -> ampl[i], env_phse[i], model -> conf.nhare, fs, nhop * 2); for(int j = 0; j < nhop * 2; j ++) if(tn + j - nhop > 0) { y_sin[tn + j - nhop] += sin_frame[j] * ola_window[j]; y_env[tn + j - nhop] += env_frame[j] * ola_window[j]; } free(sin_frame); free(env_frame); } free2d(sin_phse, nfrm); free2d(env_phse, nfrm); // DA1 FP_TYPE* y_nos = synth_noise(param, model -> conf, model -> noise, 1); // DA2 const int filtord = 60; FP_TYPE* h_high = fir1(filtord, model -> conf.mvf / fs * 2.0, "highpass", "hanning"); FP_TYPE* h_low = fir1(filtord, model -> conf.mvf / fs * 2.0, "lowpass" , "hanning"); FP_TYPE* y_high = conv(y_nos, h_high, *ny, filtord); FP_TYPE* y_low = conv(y_nos, h_low , *ny, filtord); free(h_high); free(h_low); // DA3, D4 subtract_minimum_envelope(y_env, *ny, model -> f0, nhop, nfrm, fs); FP_TYPE* y_high_normalized = calloc(*ny, sizeof(FP_TYPE)); for(int i = 0; i < nfrm; i ++) { FP_TYPE* hfrm = fetch_frame(y_high + filtord / 2, *ny, i * nhop, nhop * 2); FP_TYPE* efrm = fetch_frame(y_env, *ny, i * nhop, nhop * 2); FP_TYPE havg = 0; for(int j = 0; j < nhop * 2; j ++) havg += hfrm[j] * hfrm[j]; havg /= nhop * 2; for(int j = 0; j < nhop * 2; j ++) hfrm[j] *= sqrt(1.0 / (havg + EPS)); if(model -> f0[i] <= 0.0) for(int j = 0; j < nhop * 2; j ++) efrm[j] = havg; else for(int j = 0; j < nhop * 2; j ++) efrm[j] += model -> emin[i]; for(int j = 0; j < nhop * 2; j ++) if(i * nhop + j - nhop > 0) { y_high_normalized[i * nhop + j - nhop] += hfrm[j] * ola_window[j]; y_env_mix [i * nhop + j - nhop] += efrm[j] * ola_window[j]; } free(hfrm); free(efrm); } free(ola_window); free(y_high); // DB1, DB2 for(int i = 0; i < *ny - nfft; i ++) y_sin[i] += y_low[i + filtord / 2] + y_high_normalized[i] * sqrt(fabs(y_env_mix[i])); free(y_env_mix); free(y_high_normalized); free(y_nos); free(y_low); free(y_env); return y_sin; }