<commit_before>/* Copyright (c) 2009-2013 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * swipe.c: primary functions
 * <NAME> <<EMAIL>>
 */

#define VNUM    1.5 // current version

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>

#include <fftw3.h>   // http://www.fftw.org/
#include <sndfile.h> // http://www.mega-nerd.com/libsndfile/

#include "vector.h"  // comes with release

#define NOK      0

#define DERBS    .1 
#define POLYV    .0013028 //  1 / 12 / 64 = 1 / 768
#define DLOG2P   .0104167 // 1/96

// feel free to change these defaults
#define ST       .3  
#define DT       .001
#define MIN      100.
#define MAX      600.

#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

#ifndef NAN
    #define NAN sqrt(-1.)
#endif

#ifndef isnan
int isnan(double x) { 
    return(x != x);
}
#endif

#ifndef log2
// a base-2 log function
double log2(double x) { 
    return log(x) / log(2.);
}
#endif

#ifndef round
// rounds a double to the nearest integer value
double round(double x) { 
    return(x >= 0. ? floor(x + .5) : floor(x - .5));
}
#endif

// converts from hertz to Mel frequency
double hz2mel(double hz) { 
    return(1127.01048 * log(1. + hz / 700.));
}

// converts from hertz to ERBs
double hz2erb(double hz) { 
    return(21.4 * log10(1. + hz / 229.));
}

// converts from ERBs to hertz 
double erb2hz(double erb) { 
    return((pow(10, erb / 21.4) - 1.) * 229.);
}

// a silly function that treats NaNs as 0.
double fixnan(double x) { 
    return(isnan(x) ? 0. : x);
}

// a helper function for loudness() for individual fft slices
void La(matrix L, vector f, vector fERBs, fftw_plan plan, 
                            fftw_complex* fo, int w2, int hi, int i) {
    int j;
    fftw_execute(plan);
    vector a = makev(w2);
    for (j = 0; j < w2; j++) // this iterates over only the first half
        a.v[j] = sqrt(fo[j][0] * fo[j][0] + fo[j][1] * fo[j][1]);
    vector a2 = spline(f, a); // a2 is now the result of the cubic spline
    L.m[i][0] = fixnan(sqrt(splinv(f, a, a2, fERBs.v[0], hi)));
    for (j = 1; j < L.y; j++) { // perform a bisection query at ERB intvls
        hi = bilookv(f, fERBs.v[j], hi); 
        L.m[i][j] = fixnan(sqrt(splinv(f, a, a2, fERBs.v[j], hi)));
    }
    freev(a); 
    freev(a2);
}

// a function for populating the loudness matrix with a signal x
matrix loudness(vector x, vector fERBs, double nyquist, int w, int w2) {
    int i;
    int j; 
    int hi;
    int offset = 0;
    double td = nyquist / w2; // this is equivalent to fstep
    // testing showed this configuration of fftw to be fastest
    double* fi = fftw_malloc(sizeof(double) * w); 
    fftw_complex* fo = fftw_malloc(sizeof(fftw_complex) * w);
    fftw_plan plan = fftw_plan_dft_r2c_1d(w, fi, fo, FFTW_ESTIMATE); 
    vector hann = makev(w); // this defines the Hann[ing] window
    for (i = 0; i < w; i++) 
        hann.v[i] = .5 - (.5 * cos(2. * M_PI * ((double) i / w)));
    vector f = makev(w2);
    for (i = 0; i < w2; i++) 
        f.v[i] = i * td;
    hi = bisectv(f, fERBs.v[0]); // all calls to La() will begin here
    matrix L = makem(ceil((double) x.x / w2) + 1, fERBs.x); 
    for (j = 0; j < w2; j++) // left boundary case
        fi[j] = 0.; // more explicitly, 0. * hann.v[j]
    for (/* j = w2 */; j < w; j++) 
        fi[j] = x.v[j - w2] * hann.v[j];
    La(L, f, fERBs, plan, fo, w2, hi, 0); 
    for (i = 1; i < L.x - 2; i++) { 
        for (j = 0; j < w; j++) 
            fi[j] = x.v[j + offset] * hann.v[j];
        La(L, f, fERBs, plan, fo, w2, hi, i); 
        offset += w2;
    }
    for (/* i = L.x - 2; */; i < L.x; i++) { // right two boundary cases
        for (j = 0; j < x.x - offset; j++) // this dies at x.x + w2
            fi[j] = x.v[j + offset] * hann.v[j];
        for (/* j = x.x - offset */; j < w; j++) 
            fi[j] = 0.; // once again, 0. * hann.v[j] 
        La(L, f, fERBs, plan, fo, w2, hi, i);
        offset += w2;
    } // now L is fully valued
    freev(hann);
    freev(f);
    // L must now be normalized
    for (i = 0; i < L.x; i++) { 
        td = 0.; // td is the value of the normalization factor
        for (j = 0; j < L.y; j++) 
            td += L.m[i][j] * L.m[i][j];
        if (td != 0.) { // catches zero-division
            td = sqrt(td);
            for (j = 0; j < L.y; j++) 
                L.m[i][j] /= td;
        } // otherwise, it is already 0.
    } 
    fftw_destroy_plan(plan); 
    fftw_free(fi); 
    fftw_free(fo); 
    return(L);
}

// populates the strength matrix using the loudness matrix
void Sadd(matrix S, matrix L, vector fERBs, vector pci, vector mu, 
                                            intvector ps, double dt, 
                                            double nyquist2, int lo, 
                                            int hi, int psz, int w2) {
    int i;
    int j;
    int k; 
    double t = 0.;
    double tp = 0.;
    double td; 
    double dtp = w2 / nyquist2;
    matrix Slocal = zerom(psz, L.x);
    for (i = 0; i < Slocal.x; i++) {
        vector q = makev(fERBs.x);
        for (j = 0; j < q.x; j++) q.v[j] = fERBs.v[j] / pci.v[i];
        vector kernel = zerov(fERBs.x); // a zero-filled kernel vector
        for (j = 0; j < ps.x; j++) {
            if PRIME(ps.v[j]) {
                for (k = 0; k < kernel.x; k++) {
                    td = fabs(q.v[k] - j - 1.); 
                    if (td < .25) // peaks<commit_msg>fix unused argument of Sadd().<commit_after>/* Copyright (c) 2009-2013 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * swipe.c: primary functions
 * <NAME> <<EMAIL>>
 */

#define VNUM    1.5 // current version

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>

#include <fftw3.h>   // http://www.fftw.org/
#include <sndfile.h> // http://www.mega-nerd.com/libsndfile/

#include "vector.h"  // comes with release

#define NOK      0

#define DERBS    .1 
#define POLYV    .0013028 //  1 / 12 / 64 = 1 / 768
#define DLOG2P   .0104167 // 1/96

// feel free to change these defaults
#define ST       .3  
#define DT       .001
#define MIN      100.
#define MAX      600.

#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

#ifndef NAN
    #define NAN sqrt(-1.)
#endif

#ifndef isnan
int isnan(double x) { 
    return(x != x);
}
#endif

#ifndef log2
// a base-2 log function
double log2(double x) { 
    return log(x) / log(2.);
}
#endif

#ifndef round
// rounds a double to the nearest integer value
double round(double x) { 
    return(x >= 0. ? floor(x + .5) : floor(x - .5));
}
#endif

// converts from hertz to Mel frequency
double hz2mel(double hz) { 
    return(1127.01048 * log(1. + hz / 700.));
}

// converts from hertz to ERBs
double hz2erb(double hz) { 
    return(21.4 * log10(1. + hz / 229.));
}

// converts from ERBs to hertz 
double erb2hz(double erb) { 
    return((pow(10, erb / 21.4) - 1.) * 229.);
}

// a silly function that treats NaNs as 0.
double fixnan(double x) { 
    return(isnan(x) ? 0. : x);
}

// a helper function for loudness() for individual fft slices
void La(matrix L, vector f, vector fERBs, fftw_plan plan, 
                            fftw_complex* fo, int w2, int hi, int i) {
    int j;
    fftw_execute(plan);
    vector a = makev(w2);
    for (j = 0; j < w2; j++) // this iterates over only the first half
        a.v[j] = sqrt(fo[j][0] * fo[j][0] + fo[j][1] * fo[j][1]);
    vector a2 = spline(f, a); // a2 is now the result of the cubic spline
    L.m[i][0] = fixnan(sqrt(splinv(f, a, a2, fERBs.v[0], hi)));
    for (j = 1; j < L.y; j++) { // perform a bisection query at ERB intvls
        hi = bilookv(f, fERBs.v[j], hi); 
        L.m[i][j] = fixnan(sqrt(splinv(f, a, a2, fERBs.v[j], hi)));
    }
    freev(a); 
    freev(a2);
}

// a function for populating the loudness matrix with a signal x
matrix loudness(vector x, vector fERBs, double nyquist, int w, int w2) {
    int i;
    int j; 
    int hi;
    int offset = 0;
    double td = nyquist / w2; // this is equivalent to fstep
    // testing showed this configuration of fftw to be fastest
    double* fi = fftw_malloc(sizeof(double) * w); 
    fftw_complex* fo = fftw_malloc(sizeof(fftw_complex) * w);
    fftw_plan plan = fftw_plan_dft_r2c_1d(w, fi, fo, FFTW_ESTIMATE); 
    vector hann = makev(w); // this defines the Hann[ing] window
    for (i = 0; i < w; i++) 
        hann.v[i] = .5 - (.5 * cos(2. * M_PI * ((double) i / w)));
    vector f = makev(w2);
    for (i = 0; i < w2; i++) 
        f.v[i] = i * td;
    hi = bisectv(f, fERBs.v[0]); // all calls to La() will begin here
    matrix L = makem(ceil((double) x.x / w2) + 1, fERBs.x); 
    for (j = 0; j < w2; j++) // left boundary case
        fi[j] = 0.; // more explicitly, 0. * hann.v[j]
    for (/* j = w2 */; j < w; j++) 
        fi[j] = x.v[j - w2] * hann.v[j];
    La(L, f, fERBs, plan, fo, w2, hi, 0); 
    for (i = 1; i < L.x - 2; i++) { 
        for (j = 0; j < w; j++) 
            fi[j] = x.v[j + offset] * hann.v[j];
        La(L, f, fERBs, plan, fo, w2, hi, i); 
        offset += w2;
    }
    for (/* i = L.x - 2; */; i < L.x; i++) { // right two boundary cases
        for (j = 0; j < x.x - offset; j++) // this dies at x.x + w2
            fi[j] = x.v[j + offset] * hann.v[j];
        for (/* j = x.x - offset */; j < w; j++) 
            fi[j] = 0.; // once again, 0. * hann.v[j] 
        La(L, f, fERBs, plan, fo, w2, hi, i);
        offset += w2;
    } // now L is fully valued
    freev(hann);
    freev(f);
    // L must now be normalized
    for (i = 0; i < L.x; i++) { 
        td = 0.; // td is the value of the normalization factor
        for (j = 0; j < L.y; j++) 
            td += L.m[i][j] * L.m[i][j];
        if (td != 0.) { // catches zero-division
            td = sqrt(td);
            for (j = 0; j < L.y; j++) 
                L.m[i][j] /= td;
        } // otherwise, it is already 0.
    } 
    fftw_destroy_plan(plan); 
    fftw_free(fi); 
    fftw_free(fo); 
    return(L);
}

// populates the strength matrix using the loudness matrix
void Sadd(matrix S, matrix L, vector fERBs, vector pci, vector mu, 
                                            intvector ps, double dt, 
                                            double nyquist2, int lo, 
                                            int psz, int w2) {
    int i;
    int j;
    int k; 
    double t = 0.;
    double tp = 0.;
    double td; 
    double dtp = w2 / nyquist2;
    matrix Slocal = zerom(psz, L.x);
    for (i = 0; i < Slocal.x; i++) {
        vector q = makev(fERBs.x);
        for (j = 0; j < q.x; j++) q.v[j] = fERBs.v[j] / pci.v[i];
        vector kernel = zerov(fERBs.x); // a zero-filled kernel vector
        for (j = 0; j < ps.x; j++) {
            if PRIME(ps.v[j]) {
                for (k = 0; k < kernel.x; k++) {
                    td = fabs(q.v[k] - j - 1.); 
                    if (td < .25) // peaks
{'fraction_non_alphanumeric': 0.1471285463026688, 'fraction_numerical': 0.028293951212051693, 'mean_word_length': 2.0627634660421545, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5224238', 'n_tokens_mistral': 5138, 'n_tokens_neox': 4711, 'n_words': 1649}