kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

iir.cpp (10100B)


      1 /**
      2  * KFR (https://www.kfrlib.com)
      3  * Copyright (C) 2016-2023 Dan Cazarin
      4  * See LICENSE.txt for details
      5  */
      6 
      7 #include <kfr/base.hpp>
      8 #include <kfr/dsp.hpp>
      9 #include <kfr/io.hpp>
     10 
     11 using namespace kfr;
     12 
     13 int main()
     14 {
     15     // Print the version of the KFR library being used
     16     println(library_version());
     17 
     18     // Define options for plotting DSP data
     19     const std::string options = "phaseresp=True, log_freq=True, freq_dB_lim=(-160, 10), padwidth=8192";
     20 
     21     // Define an output univector with 1024 elements
     22     univector<fbase, 1024> output;
     23 
     24     // --------------------------------------------------------------------------------------
     25     // ---------------------------- 24th-Order Bessel Lowpass Filter ------------------------
     26     // --------------------------------------------------------------------------------------
     27     {
     28         // Create a 24th-order Bessel lowpass filter with a cutoff frequency of 1 kHz and a sample rate of
     29         // 48 kHz
     30         zpk<fbase> filt = iir_lowpass(bessel<fbase>(24), 1000, 48000);
     31 
     32         // Apply the filter to a unit impulse signal to get the filter's impulse response
     33         output = iir(unitimpulse(), filt);
     34     }
     35     plot_save("bessel_lowpass24", output, options + ", title='24th-order Bessel filter, lowpass 1 kHz'");
     36 
     37     // --------------------------------------------------------------------------------------
     38     // ---------------------------- 12th-Order Bessel Lowpass Filter ------------------------
     39     // --------------------------------------------------------------------------------------
     40     {
     41         // Create a 12th-order Bessel lowpass filter with a cutoff frequency of 1 kHz and a sample rate of
     42         // 48 kHz
     43         zpk<fbase> filt = iir_lowpass(bessel<fbase>(12), 1000, 48000);
     44 
     45         // Apply the filter to a unit impulse signal to get the filter's impulse response
     46         output = iir(unitimpulse(), filt);
     47     }
     48     plot_save("bessel_lowpass12", output, options + ", title='12th-order Bessel filter, lowpass 1 kHz'");
     49 
     50     // --------------------------------------------------------------------------------------
     51     // ----------------------------- 6th-Order Bessel Lowpass Filter ------------------------
     52     // --------------------------------------------------------------------------------------
     53     {
     54         // Create a 6th-order Bessel lowpass filter with a cutoff frequency of 1 kHz and a sample rate of
     55         // 48 kHz
     56         zpk<fbase> filt = iir_lowpass(bessel<fbase>(6), 1000, 48000);
     57 
     58         // Apply the filter to a unit impulse signal to get the filter's impulse response
     59         output = iir(unitimpulse(), filt);
     60     }
     61     plot_save("bessel_lowpass6", output, options + ", title='6th-order Bessel filter, lowpass 1 kHz'");
     62 
     63     // --------------------------------------------------------------------------------------
     64     // ------------------------ 24th-Order Butterworth Lowpass Filter -----------------------
     65     // --------------------------------------------------------------------------------------
     66     {
     67         // Create a 24th-order Butterworth lowpass filter with a cutoff frequency of 1 kHz and a sample rate
     68         // of 48 kHz
     69         zpk<fbase> filt = iir_lowpass(butterworth<fbase>(24), 1000, 48000);
     70 
     71         // Apply the filter to a unit impulse signal to get the filter's impulse response
     72         output = iir(unitimpulse(), filt);
     73     }
     74     plot_save("butterworth_lowpass24", output,
     75               options + ", title='24th-order Butterworth filter, lowpass 1 kHz'");
     76 
     77     // --------------------------------------------------------------------------------------
     78     // ------------------------ 12th-Order Butterworth Lowpass Filter -----------------------
     79     // --------------------------------------------------------------------------------------
     80     {
     81         // Create a 12th-order Butterworth lowpass filter with a cutoff frequency of 1 kHz and a sample rate
     82         // of 48 kHz
     83         zpk<fbase> filt = iir_lowpass(butterworth<fbase>(12), 1000, 48000);
     84 
     85         // Apply the filter to a unit impulse signal to get the filter's impulse response
     86         output = iir(unitimpulse(), filt);
     87     }
     88     plot_save("butterworth_lowpass12", output,
     89               options + ", title='12th-order Butterworth filter, lowpass 1 kHz'");
     90 
     91     // --------------------------------------------------------------------------------------
     92     // ------------------------ 12th-Order Butterworth Highpass Filter ----------------------
     93     // --------------------------------------------------------------------------------------
     94     {
     95         // Create a 12th-order Butterworth highpass filter with a cutoff frequency of 1 kHz and a sample rate
     96         // of 48 kHz
     97         zpk<fbase> filt = iir_highpass(butterworth<fbase>(12), 1000, 48000);
     98 
     99         // Convert the filter to second-order sections (SOS).
    100         // This is an expensive operation, so keep 'iir_params' if it is reused later
    101         iir_params<fbase> bqs = to_sos(filt);
    102 
    103         // Apply the filter to a unit impulse signal to get the filter's impulse response
    104         output = iir(unitimpulse(), bqs);
    105     }
    106     plot_save("butterworth_highpass12", output,
    107               options + ", title='12th-order Butterworth filter, highpass 1 kHz'");
    108 
    109     // --------------------------------------------------------------------------------------
    110     // ---------------------- 12th-Order Butterworth Bandpass Filter ------------------------
    111     // --------------------------------------------------------------------------------------
    112     {
    113         // Create a 12th-order Butterworth bandpass filter with a passband from 0.1 to 0.2 (normalized
    114         // frequency)
    115         zpk<fbase> filt = iir_bandpass(butterworth<fbase>(12), 0.1, 0.2);
    116 
    117         // Convert the filter to second-order sections (SOS).
    118         // This is an expensive operation, so keep 'iir_params' if it is reused later
    119         iir_params<fbase> bqs = to_sos(filt);
    120 
    121         // Apply the filter to a unit impulse signal to get the filter's impulse response
    122         output = iir(unitimpulse(), bqs);
    123     }
    124     plot_save("butterworth_bandpass12", output,
    125               options + ", title='12th-order Butterworth filter, bandpass'");
    126 
    127     // --------------------------------------------------------------------------------------
    128     // ---------------------- 12th-Order Butterworth Bandstop Filter ------------------------
    129     // --------------------------------------------------------------------------------------
    130     {
    131         // Create a 12th-order Butterworth bandstop filter with a stopband from 0.1 to 0.2 (normalized
    132         // frequency)
    133         zpk<fbase> filt = iir_bandstop(butterworth<fbase>(12), 0.1, 0.2);
    134 
    135         // Convert the filter to second-order sections (SOS).
    136         // This is an expensive operation, so keep 'iir_params' if it is reused later
    137         iir_params<fbase> bqs = to_sos(filt);
    138 
    139         // Apply the filter to a unit impulse signal to get the filter's impulse response
    140         output = iir(unitimpulse(), bqs);
    141     }
    142     plot_save("butterworth_bandstop12", output,
    143               options + ", title='12th-order Butterworth filter, bandstop'");
    144 
    145     // --------------------------------------------------------------------------------------
    146     // ------------------------ 4th-Order Butterworth Bandpass Filter -----------------------
    147     // --------------------------------------------------------------------------------------
    148     {
    149         // Create a 4th-order Butterworth bandpass filter with a passband from 0.005 to 0.9 (normalized
    150         // frequency)
    151         zpk<fbase> filt = iir_bandpass(butterworth<fbase>(4), 0.005, 0.9);
    152 
    153         // Convert the filter to second-order sections (SOS).
    154         // This is an expensive operation, so keep 'iir_params' if it is reused later
    155         iir_params<fbase> bqs = to_sos(filt);
    156 
    157         // Apply the filter to a unit impulse signal to get the filter's impulse response
    158         output = iir(unitimpulse(), bqs);
    159     }
    160     plot_save("butterworth_bandpass4", output, options + ", title='4th-order Butterworth filter, bandpass'");
    161 
    162     // --------------------------------------------------------------------------------------
    163     // ------------------- 8th-Order Chebyshev Type I Lowpass Filter ------------------------
    164     // --------------------------------------------------------------------------------------
    165     {
    166         // Create an 8th-order Chebyshev Type I lowpass filter with a cutoff frequency of 0.09 (normalized
    167         // frequency) and 2 dB ripple in the passband
    168         zpk<fbase> filt = iir_lowpass(chebyshev1<fbase>(8, 2), 0.09);
    169 
    170         // Convert the filter to second-order sections (SOS).
    171         // This is an expensive operation, so keep 'iir_params' if it is reused later
    172         iir_params<fbase> bqs = to_sos(filt);
    173 
    174         // Apply the filter to a unit impulse signal to get the filter's impulse response
    175         output = iir(unitimpulse(), bqs);
    176     }
    177     plot_save("chebyshev1_lowpass8", output,
    178               options + ", title='8th-order Chebyshev Type I filter, lowpass'");
    179 
    180     // --------------------------------------------------------------------------------------
    181     // ------------------- 8th-Order Chebyshev Type II Lowpass Filter -----------------------
    182     // --------------------------------------------------------------------------------------
    183     {
    184         // Create an 8th-order Chebyshev Type II lowpass filter with a cutoff frequency of 0.09 (normalized
    185         // frequency) and 80 dB attenuation in the stopband
    186         zpk<fbase> filt = iir_lowpass(chebyshev2<fbase>(8, 80), 0.09);
    187 
    188         // Convert the filter to second-order sections (SOS).
    189         // This is an expensive operation, so keep 'iir_params' if it is reused later
    190         iir_params<fbase> bqs = to_sos(filt);
    191 
    192         // Apply the filter to a unit impulse signal to get the filter's impulse response
    193         output = iir(unitimpulse(), filt);
    194     }
    195     plot_save("chebyshev2_lowpass8", output,
    196               options + ", title='8th-order Chebyshev type II filter, lowpass'");
    197 
    198     println("SVG plots have been saved to svg directory");
    199 
    200     return 0;
    201 }