Ejemplo n.º 1
0
def high_pass(gain,
              Fs,
              freq1,
              freq2,
              passband_ripple_db,
              stopband_atten_db,
              nextra_taps=2):
    """
    Builds a high pass filter.

    Args:
        gain: Filter gain in the passband (linear)
        Fs: Sampling rate (sps)
        freq1: End of stop band (in Hz)
        freq2: Start of pass band (in Hz)
        passband_ripple_db: Pass band ripple in dB (should be small, < 1)
        stopband_atten_db: Stop band attenuation in dB (should be large, >= 60)
        nextra_taps: Extra taps to use in the filter (default=2)
    """
    passband_dev = passband_ripple_to_dev(passband_ripple_db)
    stopband_dev = stopband_atten_to_dev(stopband_atten_db)
    desired_ampls = (0, 1)
    (n, fo, ao, w) = remezord([freq1, freq2], desired_ampls,
                              [stopband_dev, passband_dev], Fs)
    # For a HPF, we need to use an odd number of taps
    # In filter.remez, ntaps = n+1, so n must be even
    if ((n + nextra_taps) % 2 == 1):
        n += 1

    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez(n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 2
0
def band_pass(gain,
              Fs,
              freq_sb1,
              freq_pb1,
              freq_pb2,
              freq_sb2,
              passband_ripple_db,
              stopband_atten_db,
              nextra_taps=2):
    """
    Builds a band pass filter.

    Args:
        gain: Filter gain in the passband (linear)
        Fs: Sampling rate (sps)
        freq_sb1: End of stop band (in Hz)
        freq_pb1: Start of pass band (in Hz)
        freq_pb2: End of pass band (in Hz)
        freq_sb2: Start of stop band (in Hz)
        passband_ripple_db: Pass band ripple in dB (should be small, < 1)
        stopband_atten_db: Stop band attenuation in dB (should be large, >= 60)
        nextra_taps: Extra taps to use in the filter (default=2)
    """
    passband_dev = passband_ripple_to_dev(passband_ripple_db)
    stopband_dev = stopband_atten_to_dev(stopband_atten_db)
    desired_ampls = (0, gain, 0)
    desired_freqs = [freq_sb1, freq_pb1, freq_pb2, freq_sb2]
    desired_ripple = [stopband_dev, passband_dev, stopband_dev]
    (n, fo, ao, w) = remezord(desired_freqs, desired_ampls, desired_ripple, Fs)
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez(n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 3
0
def high_pass (gain, Fs, freq1, freq2, passband_ripple_db, stopband_atten_db,
               nextra_taps=2):
    """
    Builds a high pass filter.
    
    Args:
        gain: Filter gain in the passband (linear)
        Fs: Sampling rate (sps)
        freq1: End of stop band (in Hz)
        freq2: Start of pass band (in Hz)
        passband_ripple_db: Pass band ripple in dB (should be small, < 1)
        stopband_atten_db: Stop band attenuation in dB (should be large, >= 60)
        nextra_taps: Extra taps to use in the filter (default=2)
    """
    passband_dev = passband_ripple_to_dev (passband_ripple_db)
    stopband_dev = stopband_atten_to_dev (stopband_atten_db)
    desired_ampls = (0, 1)
    (n, fo, ao, w) = remezord ([freq1, freq2], desired_ampls,
                               [stopband_dev, passband_dev], Fs)
    # For a HPF, we need to use an odd number of taps
    # In filter.remez, ntaps = n+1, so n must be even
    if((n+nextra_taps)%2 == 1):
        n += 1

    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez (n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 4
0
def band_pass (gain, Fs, freq_sb1, freq_pb1, freq_pb2, freq_sb2,
               passband_ripple_db, stopband_atten_db,
               nextra_taps=2):
    """
    Builds a band pass filter.
    
    Args:
        gain: Filter gain in the passband (linear)
        Fs: Sampling rate (sps)
        freq_sb1: End of stop band (in Hz)
        freq_pb1: Start of pass band (in Hz)
        freq_pb2: End of pass band (in Hz)
        freq_sb2: Start of stop band (in Hz)
        passband_ripple_db: Pass band ripple in dB (should be small, < 1)
        stopband_atten_db: Stop band attenuation in dB (should be large, >= 60)
        nextra_taps: Extra taps to use in the filter (default=2)
    """
    passband_dev = passband_ripple_to_dev (passband_ripple_db)
    stopband_dev = stopband_atten_to_dev (stopband_atten_db)
    desired_ampls = (0, gain, 0)
    desired_freqs = [freq_sb1, freq_pb1, freq_pb2, freq_sb2]
    desired_ripple = [stopband_dev, passband_dev, stopband_dev]
    (n, fo, ao, w) = remezord (desired_freqs, desired_ampls,
                               desired_ripple, Fs)
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez (n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 5
0
    def test_low_pass(self):
        gain = 1
        Fs = 1
        freq1 = 0.1
        freq2 = 0.2
        passband_ripple_db = 0.01
        stopband_atten_db = 60

        passband_dev = passband_ripple_to_dev(passband_ripple_db)
        stopband_dev = stopband_atten_to_dev(stopband_atten_db)
        desired_ampls = (gain, 0)
        (n, fo, ao, w) = remezord([freq1, freq2], desired_ampls,
                                  [passband_dev, stopband_dev], Fs)
        new_taps = filter.pm_remez(n + 2, fo, ao, w, "bandpass")

        known_taps = (
            -0.0008370135734511828, -0.0006622211673134374,
            0.0008501079576365787, 0.003059609130249229, 0.003202235537205373,
            -0.001000899296974219, -0.007589728680590891,
            -0.009790921118281865, -0.001524210202628562, 0.014373535837200111,
            0.02392881326993834, 0.011798133085019008, -0.021954446348997188,
            -0.05293436740264934, -0.04375787096766848, 0.028038890498420392,
            0.14612655590172896, 0.25738578419108626, 0.302967004188747,
            0.25738578419108626, 0.14612655590172896, 0.028038890498420392,
            -0.04375787096766848, -0.05293436740264934, -0.021954446348997188,
            0.011798133085019008, 0.02392881326993834, 0.014373535837200111,
            -0.001524210202628562, -0.009790921118281865,
            -0.007589728680590891, -0.001000899296974219, 0.003202235537205373,
            0.003059609130249229, 0.0008501079576365787,
            -0.0006622211673134374, -0.0008370135734511828)

        self.assertFloatTuplesAlmostEqual(known_taps, new_taps, 5)
Ejemplo n.º 6
0
def band_reject (gain, Fs, freq_pb1, freq_sb1, freq_sb2, freq_pb2,
                 passband_ripple_db, stopband_atten_db,
                 nextra_taps=2):
    """
    Builds a band reject filter
    spinning it up to the right center frequency
    
    Args:
        gain: Filter gain in the passband (linear)
        Fs: Sampling rate (sps)
        freq_pb1: End of pass band (in Hz)
        freq_sb1: Start of stop band (in Hz)
        freq_sb2: End of stop band (in Hz)
        freq_pb2: Start of pass band (in Hz)
        passband_ripple_db: Pass band ripple in dB (should be small, < 1)
        stopband_atten_db: Stop band attenuation in dB (should be large, >= 60)
        nextra_taps: Extra taps to use in the filter (default=2)
    """
    passband_dev = passband_ripple_to_dev (passband_ripple_db)
    stopband_dev = stopband_atten_to_dev (stopband_atten_db)
    desired_ampls = (gain, 0, gain)
    desired_freqs = [freq_pb1, freq_sb1, freq_sb2, freq_pb2]
    desired_ripple = [passband_dev, stopband_dev, passband_dev]
    (n, fo, ao, w) = remezord (desired_freqs, desired_ampls,
                               desired_ripple, Fs)
    # Make sure we use an odd number of taps
    if((n+nextra_taps)%2 == 1):
        n += 1
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez (n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 7
0
def low_pass (gain, Fs, freq1, freq2, passband_ripple_db, stopband_atten_db,
              nextra_taps=2):
    passband_dev = passband_ripple_to_dev (passband_ripple_db)
    stopband_dev = stopband_atten_to_dev (stopband_atten_db)
    desired_ampls = (gain, 0)
    (n, fo, ao, w) = remezord ([freq1, freq2], desired_ampls,
                               [passband_dev, stopband_dev], Fs)
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez (n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 8
0
def band_pass (gain, Fs, freq_sb1, freq_pb1, freq_pb2, freq_sb2,
               passband_ripple_db, stopband_atten_db,
               nextra_taps=2):
    passband_dev = passband_ripple_to_dev (passband_ripple_db)
    stopband_dev = stopband_atten_to_dev (stopband_atten_db)
    desired_ampls = (0, gain, 0)
    desired_freqs = [freq_sb1, freq_pb1, freq_pb2, freq_sb2]
    desired_ripple = [stopband_dev, passband_dev, stopband_dev]
    (n, fo, ao, w) = remezord (desired_freqs, desired_ampls,
                               desired_ripple, Fs)
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez (n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 9
0
def high_pass (gain, Fs, freq1, freq2, passband_ripple_db, stopband_atten_db,
               nextra_taps=2):
    passband_dev = passband_ripple_to_dev (passband_ripple_db)
    stopband_dev = stopband_atten_to_dev (stopband_atten_db)
    desired_ampls = (0, 1)
    (n, fo, ao, w) = remezord ([freq1, freq2], desired_ampls,
                               [stopband_dev, passband_dev], Fs)
    # For a HPF, we need to use an odd number of taps
    # In filter.remez, ntaps = n+1, so n must be even
    if((n+nextra_taps)%2 == 1):
        n += 1

    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez (n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 10
0
def low_pass(gain,
             Fs,
             freq1,
             freq2,
             passband_ripple_db,
             stopband_atten_db,
             nextra_taps=2):
    passband_dev = passband_ripple_to_dev(passband_ripple_db)
    stopband_dev = stopband_atten_to_dev(stopband_atten_db)
    desired_ampls = (gain, 0)
    (n, fo, ao, w) = remezord([freq1, freq2], desired_ampls,
                              [passband_dev, stopband_dev], Fs)
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez(n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 11
0
def band_reject (gain, Fs, freq_pb1, freq_sb1, freq_sb2, freq_pb2,
                 passband_ripple_db, stopband_atten_db,
                 nextra_taps=2):
    passband_dev = passband_ripple_to_dev (passband_ripple_db)
    stopband_dev = stopband_atten_to_dev (stopband_atten_db)
    desired_ampls = (gain, 0, gain)
    desired_freqs = [freq_pb1, freq_sb1, freq_sb2, freq_pb2]
    desired_ripple = [passband_dev, stopband_dev, passband_dev]
    (n, fo, ao, w) = remezord (desired_freqs, desired_ampls,
                               desired_ripple, Fs)
    # Make sure we use an odd number of taps
    if((n+nextra_taps)%2 == 1):
        n += 1
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez (n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 12
0
def band_pass(gain,
              Fs,
              freq_sb1,
              freq_pb1,
              freq_pb2,
              freq_sb2,
              passband_ripple_db,
              stopband_atten_db,
              nextra_taps=2):
    passband_dev = passband_ripple_to_dev(passband_ripple_db)
    stopband_dev = stopband_atten_to_dev(stopband_atten_db)
    desired_ampls = (0, gain, 0)
    desired_freqs = [freq_sb1, freq_pb1, freq_pb2, freq_sb2]
    desired_ripple = [stopband_dev, passband_dev, stopband_dev]
    (n, fo, ao, w) = remezord(desired_freqs, desired_ampls, desired_ripple, Fs)
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez(n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 13
0
def high_pass(gain,
              Fs,
              freq1,
              freq2,
              passband_ripple_db,
              stopband_atten_db,
              nextra_taps=2):
    passband_dev = passband_ripple_to_dev(passband_ripple_db)
    stopband_dev = stopband_atten_to_dev(stopband_atten_db)
    desired_ampls = (0, 1)
    (n, fo, ao, w) = remezord([freq1, freq2], desired_ampls,
                              [stopband_dev, passband_dev], Fs)
    # For a HPF, we need to use an odd number of taps
    # In filter.remez, ntaps = n+1, so n must be even
    if ((n + nextra_taps) % 2 == 1):
        n += 1

    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez(n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 14
0
def band_reject(gain,
                Fs,
                freq_pb1,
                freq_sb1,
                freq_sb2,
                freq_pb2,
                passband_ripple_db,
                stopband_atten_db,
                nextra_taps=2):
    passband_dev = passband_ripple_to_dev(passband_ripple_db)
    stopband_dev = stopband_atten_to_dev(stopband_atten_db)
    desired_ampls = (gain, 0, gain)
    desired_freqs = [freq_pb1, freq_sb1, freq_sb2, freq_pb2]
    desired_ripple = [passband_dev, stopband_dev, passband_dev]
    (n, fo, ao, w) = remezord(desired_freqs, desired_ampls, desired_ripple, Fs)
    # Make sure we use an odd number of taps
    if ((n + nextra_taps) % 2 == 1):
        n += 1
    # The remezord typically under-estimates the filter order, so add 2 taps by default
    taps = filter.pm_remez(n + nextra_taps, fo, ao, w, "bandpass")
    return taps
Ejemplo n.º 15
0
    def test_low_pass(self):
        gain = 1
        Fs = 1
        freq1 = 0.1
        freq2 = 0.2
        passband_ripple_db = 0.01
        stopband_atten_db = 60

        passband_dev = passband_ripple_to_dev(passband_ripple_db)
        stopband_dev = stopband_atten_to_dev(stopband_atten_db)
        desired_ampls = (gain, 0)
        (n, fo, ao, w) = remezord([freq1, freq2], desired_ampls,
                                  [passband_dev, stopband_dev], Fs)
        new_taps = filter.pm_remez(n + 2, fo, ao, w, "bandpass")

        known_taps = (-0.0008370135734511828, -0.0006622211673134374,
                       0.0008501079576365787, 0.003059609130249229,
                       0.003202235537205373, -0.001000899296974219,
                       -0.007589728680590891, -0.009790921118281865,
                       -0.001524210202628562, 0.014373535837200111,
                       0.02392881326993834, 0.011798133085019008,
                       -0.021954446348997188, -0.05293436740264934,
                       -0.04375787096766848, 0.028038890498420392,
                       0.14612655590172896, 0.25738578419108626,
                       0.302967004188747, 0.25738578419108626,
                       0.14612655590172896, 0.028038890498420392,
                       -0.04375787096766848, -0.05293436740264934,
                       -0.021954446348997188, 0.011798133085019008,
                       0.02392881326993834, 0.014373535837200111,
                       -0.001524210202628562, -0.009790921118281865,
                       -0.007589728680590891, -0.001000899296974219,
                       0.003202235537205373, 0.003059609130249229,
                       0.0008501079576365787, -0.0006622211673134374,
                       -0.0008370135734511828)

        self.assertFloatTuplesAlmostEqual(known_taps, new_taps, 5)