Exemplo n.º 1
0
/** Return a new vector that is the moving average of the input vector.
 \param v The input vector, unsmoothed
 \param bandwidth The number of elements to be smoothed.
 */
gsl_vector *apop_vector_moving_average(gsl_vector *v, size_t bandwidth) {
    apop_assert_c(v,  NULL, 0, "You asked me to smooth a NULL vector; returning NULL.\n");
    apop_assert_s(bandwidth, "Bandwidth must be >=1.\n");
    int halfspan = bandwidth/2;
    gsl_vector *vout = gsl_vector_calloc(v->size - halfspan*2);
    for(size_t i=0; i < vout->size; i ++) {
        double *item = gsl_vector_ptr(vout, i);
        for (int j=-halfspan; j < halfspan+1; j ++)
            *item += gsl_vector_get(v, j+ i+ halfspan);
        *item /= halfspan*2 +1;
    }
    return vout;
}
Exemplo n.º 2
0
/** This function sorts the whole of a \c apop_data set based on one column. Sorts in place, with little additional memory used.

 Uses the \c gsl_sort_vector_index function internally, and that function just ignores NaNs; therefore this function just leaves NaNs exactly where they lay.

 \param data    The input set to be modified. (No default, must not be \c NULL.)
 \param sortby  The column of data by which the sorting will take place. As usual, -1 indicates the vector element. (default: column zero of the matrix if there is a matrix; if there's a vector but no matrix, then -1).
 \param asc   If 'd' or 'D', sort in descending order; else sort in ascending order. (Default: ascending)
 \return A pointer to the data set, so you can do things like \c apop_data_show(apop_data_sort(d, -1)).

This function uses the \ref designated syntax for inputs.
*/
APOP_VAR_HEAD apop_data * apop_data_sort(apop_data *data, int sortby, char asc){
    apop_data * apop_varad_var(data, NULL);
    apop_assert_s(data, "You gave me NULL data to sort.");
    int apop_varad_var(sortby, 0);
    if (sortby==0 && !data->matrix && data->vector) //you meant sort the vector
        sortby = -1;
    char apop_varad_var(asc, 0);
APOP_VAR_ENDHEAD
    size_t height  = (sortby==-1) ? data->vector->size: data->matrix->size1;
    size_t sorted[height];
    size_t i, *perm, start=0;
    gsl_permutation *p = gsl_permutation_alloc(height);
    memset(sorted, 0, sizeof(size_t)*height);
    if (sortby == -1)
        gsl_sort_vector_index (p, data->vector);
    else {
        APOP_COL(data, sortby, v);
        gsl_sort_vector_index (p, v);
    }
    perm = p->data;
    if (asc=='d' || asc=='D') //reverse the perm matrix.
        for (size_t j=0; j< height/2; j++){
            double t         = perm[j];
            perm[j]          = perm[height-1-j];
            perm[height-1-j] = t;
        }
    while (1){
        i     =
        start = find_min_unsorted(sorted, height, start);
        if (i==-1) 
            break;
        Apop_data_row(data, start, firstrow);
        apop_data *first_row_storage = apop_data_copy(firstrow);
        sorted[start]++;
        while (perm[i]!=start){
            //copy from perm[i] to i
            Apop_data_row(data, perm[i], onerow);
            apop_data_set_row(data, onerow, i);
            sorted[perm[i]]++;
            i = perm[i];
        }
        apop_data_set_row(data, first_row_storage, i);
        apop_data_free(first_row_storage);
    }
    gsl_permutation_free(p);
    return data;
}
Exemplo n.º 3
0
/** Return a new histogram that is the moving average of the input histogram.
 \param m A histogram, in \c apop_model form.
 \param bandwidth The number of elements to be smoothed.
 */
apop_model *apop_histogram_moving_average(apop_model *m, size_t bandwidth) {
    apop_assert_c(m && !strcmp(m->name, "Histogram"), NULL, 0, "The first argument needs to be an apop_histogram model.");
    apop_assert_s(bandwidth, "bandwidth must be an integer >=1.");
    apop_model *out = apop_model_copy(*m);
    gsl_histogram *h     = Apop_settings_get(m, apop_histogram, pdf);
    gsl_histogram *hout  = Apop_settings_get(out, apop_histogram, pdf);
    gsl_vector *bins     = apop_array_to_vector(h->bin, h->n);
    gsl_vector *smoothed = apop_vector_moving_average(bins, bandwidth);
    for (int i=0; i< h->n; i++)
        if (i < bandwidth/2 || i>= smoothed->size+bandwidth/2)
            hout->bin[i] = 0;
        else
            hout->bin[i] = gsl_vector_get(smoothed, i-bandwidth/2);
    gsl_vector_free(bins);
    gsl_vector_free(smoothed);
    return out;
}