<commit_before>// iirdes_example.c
//
// Tests infinite impulse reponse (IIR) filter design.
//

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <math.h>
#include "liquid.h"

#define OUTPUT_FILENAME "iirdes_example.m"

// print usage/help message
void usage()
{
    printf("iirdes_example [options]\n");
    printf("  u/h   : print usage\n");
    printf("  t     : filter type: [butter], cheby1, cheby2, ellip, bessel\n");
    printf("  b     : filter transformation: [LP], HP, BP, BS\n");
    printf("  n     : filter order\n");
    printf("  r     : passband ripple [dB]\n");
    printf("  s     : stopband attenuation [dB]\n");
    printf("  f     : passband cut-off [0,0.5]\n");
    printf("  c     : center frequency (BP, BS cases) [0,0.5]\n");
    printf("  o     : format [sos], tf\n");
    printf("          sos   : second-order sections form\n");
    printf("          tf    : regular transfer function form (potentially\n");
    printf("                  unstable for large orders\n");
}


int main(int argc, char*argv[]) {
    // options
    unsigned int n=6;       // filter order
    float fc = 0.25f;       // cutoff frequency (low-pass prototype)
    float f0 = 0.25f;       // center frequency (band-pass, band-stop)
    float slsl = 60.0f;     // stopband attenuation [dB]
    float ripple = 1.0f;    // passband ripple [dB]

    // filter type
    liquid_iirdes_filtertype ftype = LIQUID_IIRDES_BUTTER;

    // band type
    liquid_iirdes_bandtype btype = LIQUID_IIRDES_LOWPASS;

    // output format
    enum {  IIRDES_EXAMPLE_SOS=0,
            IIRDES_EXAMPLE_TF
    } format = 0;

    int dopt;
    while ((dopt = getopt(argc,argv,"uht:q:n:r:s:f:c:o:")) != EOF) {
        switch (dopt) {
        case 'u':
        case 'h':
            usage();
            return 0;
        case 't':
            if (strcmp(optarg,"butter")==0) {
                ftype = LIQUID_IIRDES_BUTTER;
            } else if (strcmp(optarg,"cheby1")==0) {
                ftype = LIQUID_IIRDES_CHEBY1;
            } else if (strcmp(optarg,"cheby2")==0) {
                ftype = LIQUID_IIRDES_CHEBY2;
            } else if (strcmp(optarg,"ellip")==0) {
                ftype = LIQUID_IIRDES_ELLIP;
            } else if (strcmp(optarg,"bessel")==0) {
                ftype = LIQUID_IIRDES_BESSEL;
            } else {
                fprintf(stderr,"error: iirdes_example, unknown filter type \"%s\"\n", optarg);
                usage();
                exit(1);
            }
            break;
        case 'q':
            if (strcmp(optarg,"LP")==0) {
                btype = LIQUID_IIRDES_LOWPASS;
            } else if (strcmp(optarg,"HP")==0) {
                btype = LIQUID_IIRDES_HIGHPASS;
            } else if (strcmp(optarg,"BP")==0) {
                btype = LIQUID_IIRDES_BANDPASS;
            } else if (strcmp(optarg,"BS")==0) {
                btype = LIQUID_IIRDES_BANDSTOP;
            } else {
                fprintf(stderr,"error: iirdes_example, unknown band type \"%s\"\n", optarg);
                usage();
                exit(1);
            }
            break;
        case 'n': n = atoi(optarg);         break;
        case 'r': ripple = atof(optarg);    break;
        case 's': slsl = atof(optarg);      break;
        case 'f': fc = atof(optarg);        break;
        case 'c': f0 = atof(optarg);        break;
        case 'o':
            if (strcmp(optarg,"sos")==0) {
                format = IIRDES_EXAMPLE_SOS;
            } else if (strcmp(optarg,"tf")==0) {
                format = IIRDES_EXAMPLE_TF;
            } else {
                fprintf(stderr,"error: iirdes_example, unknown output format \"%s\"\n", optarg);
                usage();
                exit(1);
            }
            break;
        default:
            fprintf(stderr,"error: iirdes_example, unknown option\n");
            usage();
            return 1;
        }
    }

    // number of analaog poles/zeros
    unsigned int npa = n;
    unsigned int nza;

    // analog poles/zeros/gain
    float complex pa[n];
    float complex za[n];
    float complex ka;

    unsigned int r = n%2;
    unsigned int L = (n-r)/2;

    unsigned int i;
    // specific filter variables
    float epsilon, Gp, Gs, ep, es;

    switch (ftype) {
    case LIQUID_IIRDES_BUTTER:
        printf("Butterworth filter design:\n");
        nza = 0;
        butter_azpkf(n,fc,za,pa,&ka);
        break;
    case LIQUID_IIRDES_CHEBY1:
        printf("Cheby-I filter design:\n");
        nza = 0;
        epsilon = sqrtf( powf(10.0f, ripple / 10.0f) - 1.0f );
        cheby1_azpkf(n,fc,epsilon,za,pa,&ka);
        break;
    case LIQUID_IIRDES_CHEBY2:<commit_msg>iirdes|example: cleaning up example<commit_after>// iirdes_example.c
//
// Tests infinite impulse reponse (IIR) filter design.
//

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <math.h>
#include "liquid.h"

#define OUTPUT_FILENAME "iirdes_example.m"

// print usage/help message
void usage()
{
    printf("iirdes_example -- infinite impulse response filter design\n");
    printf("options (default values in []):\n");
    printf("  u/h   : print usage/help\n");
    printf("  t     : filter type: [butter], cheby1, cheby2, ellip, bessel\n");
    printf("  b     : filter transformation: [LP], HP, BP, BS\n");
    printf("  n     : filter order, n > 0 [5]\n");
    printf("  r     : passband ripple in dB (cheby1, ellip), r > 0 [1.0]\n");
    printf("  s     : stopband attenuation in dB (cheby2, ellip), s > 0 [60.0]\n");
    printf("  f     : passband cut-off, 0 < f < 0.5 [0.2]\n");
    printf("  c     : center frequency (BP, BS cases), 0 < c < 0.5 [0.25]\n");
    printf("  o     : format [sos], tf\n");
    printf("          sos   : second-order sections form\n");
    printf("          tf    : regular transfer function form (potentially\n");
    printf("                  unstable for large orders\n");
}


int main(int argc, char*argv[]) {
    // options
    unsigned int n=5;       // filter order
    float fc = 0.20f;       // cutoff frequency (low-pass prototype)
    float f0 = 0.25f;       // center frequency (band-pass, band-stop)
    float slsl = 60.0f;     // stopband attenuation [dB]
    float ripple = 1.0f;    // passband ripple [dB]

    // filter type
    liquid_iirdes_filtertype ftype = LIQUID_IIRDES_BUTTER;

    // band type
    liquid_iirdes_bandtype btype = LIQUID_IIRDES_LOWPASS;

    // output format: second-order sections or transfer function
    enum {  IIRDES_EXAMPLE_SOS=0,
            IIRDES_EXAMPLE_TF
    } format = 0;

    int dopt;
    while ((dopt = getopt(argc,argv,"uht:b:n:r:s:f:c:o:")) != EOF) {
        switch (dopt) {
        case 'u':
        case 'h':
            usage();
            return 0;
        case 't':
            if (strcmp(optarg,"butter")==0) {
                ftype = LIQUID_IIRDES_BUTTER;
            } else if (strcmp(optarg,"cheby1")==0) {
                ftype = LIQUID_IIRDES_CHEBY1;
            } else if (strcmp(optarg,"cheby2")==0) {
                ftype = LIQUID_IIRDES_CHEBY2;
            } else if (strcmp(optarg,"ellip")==0) {
                ftype = LIQUID_IIRDES_ELLIP;
            } else if (strcmp(optarg,"bessel")==0) {
                ftype = LIQUID_IIRDES_BESSEL;
            } else {
                fprintf(stderr,"error: iirdes_example, unknown filter type \"%s\"\n", optarg);
                usage();
                exit(1);
            }
            break;
        case 'b':
            if (strcmp(optarg,"LP")==0) {
                btype = LIQUID_IIRDES_LOWPASS;
            } else if (strcmp(optarg,"HP")==0) {
                btype = LIQUID_IIRDES_HIGHPASS;
            } else if (strcmp(optarg,"BP")==0) {
                btype = LIQUID_IIRDES_BANDPASS;
            } else if (strcmp(optarg,"BS")==0) {
                btype = LIQUID_IIRDES_BANDSTOP;
            } else {
                fprintf(stderr,"error: iirdes_example, unknown band type \"%s\"\n", optarg);
                usage();
                exit(1);
            }
            break;
        case 'n': n = atoi(optarg);         break;
        case 'r': ripple = atof(optarg);    break;
        case 's': slsl = atof(optarg);      break;
        case 'f': fc = atof(optarg);        break;
        case 'c': f0 = atof(optarg);        break;
        case 'o':
            if (strcmp(optarg,"sos")==0) {
                format = IIRDES_EXAMPLE_SOS;
            } else if (strcmp(optarg,"tf")==0) {
                format = IIRDES_EXAMPLE_TF;
            } else {
                fprintf(stderr,"error: iirdes_example, unknown output format \"%s\"\n", optarg);
                usage();
                exit(1);
            }
            break;
        default:
            fprintf(stderr,"error: iirdes_example, unknown option\n");
            usage();
            return 1;
        }
    }

    // validate input
    if (fc <= 0 || fc >= 0.5) {
        fprintf(stderr,"error: %s, cutoff frequency out of range\n", argv[0]);
        usage();
        exit(1);
    } else if (f0 < 0 || f0 > 0.5) {
        fprintf(stderr,"error: %s, center frequency out of range\n", argv[0]);
        usage();
        exit(1);
    } else if (ripple <= 0) {
        fprintf(stderr,"error: %s, pass-band ripple out of range\n", argv[0]);
        usage();
        exit(1);
    } else if (slsl <= 0) {
        fprintf(stderr,"error: %s, stop-band ripple out of range\n", argv[0]);
        usage();
        exit(1);
    }


    // number of analaog poles/zeros
    unsigned int npa = n;
    unsigned int nza;

    // analog poles/zeros/gain
    float complex pa[n];
    float complex za[n];
    float complex ka;

    unsigned int r = n%2;
    unsigned int L = (n-r)/2;

    unsigned int i;
    // specific filter variables
    float epsilon, Gp, Gs, ep, es;

    switch (ftype) {
    case LIQUID_IIRDES_BUTTER:
        printf("Butterworth filter design:\n");
        nza = 0;
        butter_azpkf(n,fc,za,pa,&ka);
        break;
    case LIQUID_IIRDES_CHEBY1:
        printf("Cheby-I filter design:\n");
        nza = 0;
        epsilon = sqrtf( powf(10.0f, ripple / 10.0f) - 1.0f );
        cheby1_azpkf(n,fc,epsilon,za,pa,&ka);
        break;
    case LIQUID_IIRDES_CHEBY2:
{'fraction_non_alphanumeric': 0.1401933027433369, 'fraction_numerical': 0.014839402518793322, 'mean_word_length': 1.8004373974849646, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7626987', 'n_tokens_mistral': 3663, 'n_tokens_neox': 3478, 'n_words': 824}