<commit_before>/*
 * Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013 <NAME>
 *
 * This file is part of liquid.
 *
 * liquid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * liquid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with liquid.  If not, see <http://www.gnu.org/licenses/>.
 */

//
// multi-stage arbitrary resampler
//

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "liquid.internal.h"

// 
// forward declaration of internal methods
//

// execute multi-stage interpolation
void MSRESAMP(_interp_execute)(MSRESAMP()    _q,
                              TI *           _x,
                              unsigned int   _nx,
                              TO *           _y,
                              unsigned int * _num_written);

// execute multi-stage decimation
void MSRESAMP(_decim_execute)(MSRESAMP()     _q,
                              TI *           _x,
                              unsigned int   _nx,
                              TO *           _y,
                              unsigned int * _num_written);


struct MSRESAMP(_s) {
    // user-defined parameters
    float rate;                         // re-sampling rate
    float As;                           // filter stop-band attenuation [dB]

    // half-band resampler parameters
    unsigned int num_halfband_stages;   // number of halfband stages
    enum {
        MSRESAMP_HALFBAND_INTERP=0,
        MSRESAMP_HALFBAND_DECIM
    } halfband_type;                    // run half-band resamplers as interp or decim
    RESAMP2() * halfband_resamp;        // halfband decimation/interpolation objects
    float rate_halfband;                // halfband rate

    // arbitrary resampler parameters
    RESAMP() arbitrary_resamp;          // arbitrary resampling object
    float rate_arbitrary;               // clean-up resampling rate, in (0.5, 2.0)

    // internal buffers (ping-pong)
    unsigned int buffer_len;            // length of each buffer
    T * buffer0;                        // buffer[0]
    T * buffer1;                        // buffer[1]
    unsigned int buffer_index;          // index of buffer

    // scaling factor
    float zeta;
};

// create msresamp object
//  TODO: add signal bandwidth parameter?
//  TODO: add center frequency for resamp_ccct? facilitates DDS object synthesis
//  _r              :   resampling rate [output/input]
//  _As             :   stop-band attenuation
MSRESAMP() MSRESAMP(_create)(float _r,
                             float _As)
{
    // validate input
    if (_r <= 0.0f) {
        fprintf(stderr,"error: msresamp_%s_create(), resampling rate must be greater than zero\n", EXTENSION_FULL);
        exit(1);
    }

    // create object
    MSRESAMP() q = (MSRESAMP()) malloc(sizeof(struct MSRESAMP(_s)));
    q->rate = _r;
    q->As   = _As;

    // decimation or interpolation?
    q->halfband_type = (q->rate > 1.0f) ? MSRESAMP_HALFBAND_INTERP : MSRESAMP_HALFBAND_DECIM;

    // compute derived values
    q->rate_arbitrary = q->rate;
    q->rate_halfband  = 1.0f;
    q->num_halfband_stages = 0;
    switch(q->halfband_type) {
        case MSRESAMP_HALFBAND_INTERP:
            while (q->rate_arbitrary > 2.0f) {
                q->num_halfband_stages++;
                q->rate_halfband  *= 2.0f;
                q->rate_arbitrary *= 0.5f;
            }
            break;
        case MSRESAMP_HALFBAND_DECIM:
            while (q->rate_arbitrary < 0.5f) {
                q->num_halfband_stages++;
                q->rate_halfband  *= 0.5f;
                q->rate_arbitrary *= 2.0f;
            }
            break;
        default:;
    }

    // allocate memory for buffers
    q->buffer_len = 1<<q->num_halfband_stages;
    q->buffer0 = (T*) malloc( q->buffer_len*sizeof(T) );
    q->buffer1 = (T*) malloc( q->buffer_len*sizeof(T) );

    // create half-band resampler objects
    q->halfband_resamp = (RESAMP2()*) malloc(q->num_halfband_stages*sizeof(RESAMP()));
    unsigned int i;
    for (i=0; i<q->num_halfband_stages; i++) {<commit_msg>msresamp: fixing bug with allocating half-band resamplers<commit_after>/*
 * Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013 <NAME>
 *
 * This file is part of liquid.
 *
 * liquid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * liquid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with liquid.  If not, see <http://www.gnu.org/licenses/>.
 */

//
// multi-stage arbitrary resampler
//

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "liquid.internal.h"

// 
// forward declaration of internal methods
//

// execute multi-stage interpolation
void MSRESAMP(_interp_execute)(MSRESAMP()    _q,
                              TI *           _x,
                              unsigned int   _nx,
                              TO *           _y,
                              unsigned int * _num_written);

// execute multi-stage decimation
void MSRESAMP(_decim_execute)(MSRESAMP()     _q,
                              TI *           _x,
                              unsigned int   _nx,
                              TO *           _y,
                              unsigned int * _num_written);


struct MSRESAMP(_s) {
    // user-defined parameters
    float rate;                         // re-sampling rate
    float As;                           // filter stop-band attenuation [dB]

    // half-band resampler parameters
    unsigned int num_halfband_stages;   // number of halfband stages
    enum {
        MSRESAMP_HALFBAND_INTERP=0,
        MSRESAMP_HALFBAND_DECIM
    } halfband_type;                    // run half-band resamplers as interp or decim
    RESAMP2() * halfband_resamp;        // halfband decimation/interpolation objects
    float rate_halfband;                // halfband rate

    // arbitrary resampler parameters
    RESAMP() arbitrary_resamp;          // arbitrary resampling object
    float rate_arbitrary;               // clean-up resampling rate, in (0.5, 2.0)

    // internal buffers (ping-pong)
    unsigned int buffer_len;            // length of each buffer
    T * buffer0;                        // buffer[0]
    T * buffer1;                        // buffer[1]
    unsigned int buffer_index;          // index of buffer

    // scaling factor
    float zeta;
};

// create msresamp object
//  TODO: add signal bandwidth parameter?
//  TODO: add center frequency for resamp_ccct? facilitates DDS object synthesis
//  _r              :   resampling rate [output/input]
//  _As             :   stop-band attenuation
MSRESAMP() MSRESAMP(_create)(float _r,
                             float _As)
{
    // validate input
    if (_r <= 0.0f) {
        fprintf(stderr,"error: msresamp_%s_create(), resampling rate must be greater than zero\n", EXTENSION_FULL);
        exit(1);
    }

    // create object
    MSRESAMP() q = (MSRESAMP()) malloc(sizeof(struct MSRESAMP(_s)));
    q->rate = _r;
    q->As   = _As;

    // decimation or interpolation?
    q->halfband_type = (q->rate > 1.0f) ? MSRESAMP_HALFBAND_INTERP : MSRESAMP_HALFBAND_DECIM;

    // compute derived values
    q->rate_arbitrary = q->rate;
    q->rate_halfband  = 1.0f;
    q->num_halfband_stages = 0;
    switch(q->halfband_type) {
        case MSRESAMP_HALFBAND_INTERP:
            while (q->rate_arbitrary > 2.0f) {
                q->num_halfband_stages++;
                q->rate_halfband  *= 2.0f;
                q->rate_arbitrary *= 0.5f;
            }
            break;
        case MSRESAMP_HALFBAND_DECIM:
            while (q->rate_arbitrary < 0.5f) {
                q->num_halfband_stages++;
                q->rate_halfband  *= 0.5f;
                q->rate_arbitrary *= 2.0f;
            }
            break;
        default:;
    }

    // allocate memory for buffers
    q->buffer_len = 1<<q->num_halfband_stages;
    q->buffer0 = (T*) malloc( q->buffer_len*sizeof(T) );
    q->buffer1 = (T*) malloc( q->buffer_len*sizeof(T) );

    // create half-band resampler objects
    q->halfband_resamp = (RESAMP2()*) malloc(q->num_halfband_stages*sizeof(RESAMP2()));
    unsigned int i;
    for (i=0; i<q->num_halfband_stages; i++) {
{'fraction_non_alphanumeric': 0.10007732243455209, 'fraction_numerical': 0.01424942008174086, 'mean_word_length': 1.8400250941028857, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5970600', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2661, 'n_words': 756}