Main Content

dsp.NotchPeakFilter

Second-order tunable notching and peaking IIR filter

Description

The NotchPeakFilter object filters each channel of the input using IIR filter implementation. This object implements a second-order notch or peak IIR filter.

To implement a high-order notch or peak IIR filter, use the designNotchPeakIIR function. (since R2023b)

To filter each channel of the input:

  1. Create the dsp.NotchPeakFilter object and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects?

Creation

Description

npFilter = dsp.NotchPeakFilter returns a second-order notching and peaking IIR filter that independently filters each channel of the input over time, using a specified center frequency and 3 dB bandwidth.

npFilter = dsp.NotchPeakFilter(Specification='Quality factor and center frequency') specifies the quality factor (Q factor) of the notch or peak filter instead of the 3 dB bandwidth.

npFilter = dsp.NotchPeakFilter(Specification='Coefficients') specifies the coefficient values that affect bandwidth and center frequency directly, rather than specifying the design parameters in Hz. This removes the trigonometry calculations involved when the properties are tuned.

npFilter = dsp.NotchPeakFilter(Name=Value) returns a notch or peak filter with properties specified by one or more name-value pair arguments. Unspecified properties have default values.

example

Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects.

Set the specification as 'Bandwidth and center frequency', 'Quality factor and center frequency', or 'Coefficients'.

3 dB bandwidth of the filter, specified as a positive scalar in Hz or in normalized frequency units (since R2024a).

If you set the NormalizedFrequency property to:

  • false –– The value of the bandwidth is in Hz and must be less than half the SampleRate property value.

  • true –– The value of the bandwidth is in normalized frequency units. The value must be a positive scalar less than 1.0.

    When you set the NormalizedFrequency property to true while creating the object and you do not set the bandwidth, the object automatically sets the default bandwidth to normalized frequency units using the default sample rate of 44100 Hz.

    npFilter = dsp.NotchPeakFilter(NormalizedFrequency = true)
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 0.1000
            CenterFrequency: 0.5000
        NormalizedFrequency: true

    When you set the NormalizedFrequency property to true after you create the object, you must specify the bandwidth in normalized units before you run the object algorithm.

    npFilter = dsp.NotchPeakFilter
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 2205
            CenterFrequency: 11025
        NormalizedFrequency: false
                 SampleRate: 44100

    To specify the normalized frequency value, set NormalizedFrequency to true and manually convert the frequency value in Hz to the normalized value using half the input sample rate in Hz. For example, if the input sample rate Fs is 44100 Hz, the corresponding bandwidth value in normalized units is BWHz/(Fs/2).

    npFilter.NormalizedFrequency = true;
    npFilter.Bandwidth = 2205/(44100/2)
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 0.1000
            CenterFrequency: 11025
        NormalizedFrequency: true

(since R2024a)

Tunable: Yes

Dependencies

To enable this property, set Specification to 'Bandwidth and center frequency'.

Data Types: single | double

Center frequency of the filter, specified as a positive scalar in Hz or in normalized frequency units (since R2024a).

If you set the NormalizedFrequency property to:

  • false –– The value of the center frequency is in Hz and must be less than half the SampleRate property value.

  • true –– The value of the center frequency is in normalized frequency units. The value must be a positive scalar less than 1.0.

    When you set the NormalizedFrequency property to true while creating the object and you do not set the center frequency, the object automatically sets the default center frequency to normalized frequency units using the default sample rate of 44100 Hz.

    npFilter = dsp.NotchPeakFilter(NormalizedFrequency = true)
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 0.1000
            CenterFrequency: 0.5000
        NormalizedFrequency: true

    When you set the NormalizedFrequency property to true after you create the object, you must specify the center frequency in normalized units before you run the object algorithm.

    npFilter = dsp.NotchPeakFilter
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 2205
            CenterFrequency: 11025
        NormalizedFrequency: false
                 SampleRate: 44100

    To specify the normalized frequency value, set NormalizedFrequency to true and manually convert the frequency value in Hz to the normalized value using half the input sample rate in Hz. For example, if the input sample rate Fs is 44100 Hz, the corresponding center frequency value in normalized units is CFHz/(Fs/2).

    npFilter.NormalizedFrequency = true;
    npFilter.CenterFrequency = 11025/(44100/2)
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 2205
            CenterFrequency: 0.5000
        NormalizedFrequency: true

(since R2024a)

Tunable: Yes

Dependencies

To enable this property, set Specification to 'Bandwidth and center frequency' or 'Quality factor and center frequency'.

Data Types: single | double

Specify the quality factor (Q factor) for both the notch and the peak filters. The Q factor is defined as the center frequency divided by the bandwidth. A higher Q factor corresponds to a narrower notch or peak. The Q factor should be a scalar value greater than 0.

Tunable: Yes

Dependencies

To enable this property, set Specification to 'Quality factor and center frequency'.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fi

Specify the sample rate of the input in Hz as a finite numeric scalar.

Dependencies

To enable this property, set:

  • Specification to any accepted value except "Coefficients".

  • NormalizedFrequency to false. (since R2024a)

Data Types: single | double

Specify the value that determines the filter’s 3 dB bandwidth as a finite numeric scalar in the range [-1 1]. The value -1 corresponds to the maximum 3 dB bandwidth (SampleRate/4), and 1 corresponds to the minimum 3 dB bandwidth (0 Hz, an allpass filter).

Tunable: Yes

Dependencies

To enable this property, set Specification to 'Coefficients'.

Data Types: single | double

Specify the coefficient that determines the filter’s center frequency as a finite numeric scalar in the range [-1 1]. The value -1 corresponds to the minimum center frequency (0 Hz), and 1 corresponds to the maximum center frequency (SampleRate/2 Hz). The default is 0, which corresponds to SampleRate/4 Hz.

Tunable: Yes

Dependencies

To enable this property, set Specification to 'Coefficients'.

Data Types: single | double

Since R2024a

Option to set frequencies in normalized units, specified as one of these values:

  • true –– The 3 dB bandwidth and center frequency must be in normalized frequency units and less than 1.0.

    When you set the NormalizedFrequency property to true while creating the object and you do not set the bandwidth and center frequency, the object automatically sets the default values to normalized frequency units using the default sample rate of 44100 Hz.

    npFilter = dsp.NotchPeakFilter(NormalizedFrequency = true)
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 0.1000
            CenterFrequency: 0.5000
        NormalizedFrequency: true

    When you set the NormalizedFrequency property to true after you create the object, you must specify the bandwidth and center frequency in normalized units before you run the object algorithm.

    npFilter = dsp.NotchPeakFilter
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 2205
            CenterFrequency: 11025
        NormalizedFrequency: false
                 SampleRate: 44100

    To specify the normalized frequency value, set NormalizedFrequency to true and manually convert the frequency value in Hz to the normalized value using the input sample rate in Hz. For example, if the input sample rate Fs is 44100 Hz, the corresponding bandwidth value in normalized units is BWHz/(Fs/2) and the corresponding center frequency in normalized units is TWHz/(Fs/2).

    npFilter = dsp.NotchPeakFilter;
    npFilter.NormalizedFrequency = true;
    npFilter.Bandwidth = 2205/(44100/2);
    npFilter.CenterFrequency = 11025/(44100/2)
    npFilter = 
      dsp.NotchPeakFilter with properties:
    
              Specification: 'Bandwidth and center frequency'
                  Bandwidth: 0.1000
            CenterFrequency: 0.5000
        NormalizedFrequency: true
    

  • false –– The 3 dB bandwidth and the center frequency values are in Hz. You can specify the input sample rate through the SampleRate property.

Dependency

To enable this property, set Specification to any accepted value except "Coefficients".

Data Types: logical

Usage

Description

Y = npFilter(x) filters each channel (column) of the input signal, x, to produce the notch filter output, Y.

[Yn,Yp] = npFilter(x) filters each channel of the input signal, x, to produce the notch filter output, Yn, and peak filter output, Yp.

Input Arguments

expand all

Input signal, specified as a vector or a matrix.

Data Types: single | double
Complex Number Support: Yes

Output Arguments

expand all

Notch filter output, returned as a vector or a matrix.

Data Types: single | double
Complex Number Support: Yes

Peak filter output, returned as a vector or a matrix.

Data Types: single | double
Complex Number Support: Yes

Object Functions

To use an object function, specify the System object™ as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

release(obj)

expand all

getBandwidthGet 3 dB bandwidth
getCenterFrequencyGet center frequency
getOctaveBandwidthBandwidth in number of octaves
getQualityFactorGet quality factor
tfTransfer function
outputDelayDetermine output delay of single-rate or multirate filter
stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object

Examples

collapse all

This example shows how to use the dsp.NotchPeakFilter object as a notch filter with a center frequency of 5000 Hz and a 3 dB bandwidth of 500 Hz.

npFilter = dsp.NotchPeakFilter(CenterFrequency=5000,Bandwidth=500);
sa =  spectrumAnalyzer(SampleRate=44100,AveragingMethod='exponential',...
    ForgettingFactor=0.96,PlotAsTwoSidedSpectrum=false);
for i=1:5000
    y = npFilter(randn(1024,1));
    sa(y);
    if (i==2500)
        % Tune center frequency to 10000
        npFilter.CenterFrequency = 10000;
    end
end
release(npFilter)
release(sa)

Create a dsp.NotchPeakFilter object. Set the Specification property to 'Quality factor and center frequency' and the NormalizedFrequency property to true. The default quality factor Q is 5, and the center frequency Fc is 0.5 in normalized units.

np = dsp.NotchPeakFilter(Specification='Quality factor and center frequency',...
    NormalizedFrequency=true)
np = 
  dsp.NotchPeakFilter with properties:

          Specification: 'Quality factor and center frequency'
          QualityFactor: 5
        CenterFrequency: 0.5000
    NormalizedFrequency: true

Compute the 3 dB bandwidth of the notch peak filter using the getBandwidth function. The function computes the bandwidth as the ratio of the center frequency and the quality factor, FcQ. Since Fc is in normalized units, the function computes the 3 dB bandwidth in normalized units.

getBandwidth(np)
ans = 
0.1000

Visualize the filter response.

filterAnalyzer(np)

Create a dsp.NotchPeakFilter object with the Specification property set to 'Coefficients'.

np = dsp.NotchPeakFilter(Specification='Coefficients')
np = 
  dsp.NotchPeakFilter with properties:

                 Specification: 'Coefficients'
          BandwidthCoefficient: 0.7265
    CenterFrequencyCoefficient: 0

Determine the center frequency of the notch peak filter using the getCenterFrequency function. When the Specification is set to 'Coefficients', the center frequency is determined from the CenterFrequencyCoefficient value and the sample rate.

getCenterFrequency(np)
ans = 
0.5000

Visualize the filter response.

filterAnalyzer(np)

Create a dsp.NotchPeakFilter object with the NormalizedFrequency property set to true.

np = dsp.NotchPeakFilter(NormalizedFrequency=true)
np = 
  dsp.NotchPeakFilter with properties:

          Specification: 'Bandwidth and center frequency'
              Bandwidth: 0.1000
        CenterFrequency: 0.5000
    NormalizedFrequency: true

Determine the octave bandwidth of the filter using the getOctaveBandwidth function.

getOctaveBandwidth(np)
ans = 
0.2881

Visualize the filter response using filterAnalyzer.

filterAnalyzer(np)

Create a dsp.NotchPeakFilter object in the default configuration, where the Specification property is set to 'Bandwidth and center frequency'.

np = dsp.NotchPeakFilter
np = 
  dsp.NotchPeakFilter with properties:

          Specification: 'Bandwidth and center frequency'
              Bandwidth: 2205
        CenterFrequency: 11025
    NormalizedFrequency: false
             SampleRate: 44100

Determine the quality factor of the filter using the getQualityFactor function. The quality factor is given by the ratio of the center frequency to the bandwidth.

getQualityFactor(np)
ans = 
5

Visualize the filter response using filterAnalyzer.

filterAnalyzer(np)

Create a dsp.NotchPeakFilter System object™. Obtain the coefficients of the transfer function corresponding to the notch and peak filters.

notchpeak = dsp.NotchPeakFilter;
[Bnotch,Anotch,Bpeak,Apeak] = tf(notchpeak)
Bnotch = 1×3

    0.8633   -0.0000    0.8633

Anotch = 1×3

    1.0000   -0.0000    0.7265

Bpeak = 1×3

    0.1367         0   -0.1367

Apeak = 1×3

    1.0000   -0.0000    0.7265

Bnotch and Anotch are the vectors of numerator and denominator coefficients for the equivalent transfer function corresponding to the notch filter. Bpeak and Apeak are the vectors of numerator and denominator coefficients for the equivalent transfer function corresponding to the peak filter.

Algorithms

The design equation for the peak filter is:

H(z)=(1b)1z212bcosw0z1+(2b1)z2

The design equation for the notch filter is:

H(z)=b12cosw0z1+z212bcosw0z1+(2b1)z2

with

b=11+tan(Δw/2)

where ω0 = 2πf0/fs is the center frequency in radians/sample (f0 is the center frequency in Hz and fs is the sampling frequency in Hz). Δω = 2πΔf/fs is the 3 dB bandwidth in radians/sample (Δf is the 3 dB bandwidth in Hz). Note that the two filters are complementary:

Hnotch(z)+Hpeak(z)=1they can be written as:Hpeak(z)=12[1A(z)]Hnotch(z)=12[1+A(z)]where A(z) is a 2nd order allpass filter.A(z)=a2+a1z1+z21+a1z1+a2z2anda1=2bcosω0a2=2b1

The filter is implemented as follows:

where

G3dB=a2=2b1Gcf=a1a1a21a22=cosw0

Notice that Gcf depends only on the center frequency, and G3dB depends only on the 3 dB bandwidth.

References

[1] Orfanidis, Sophocles J. Introduction to Signal Processing. Upper Saddle River, NJ: Prentice-Hall, 1996.

Version History

Introduced in R2014a

expand all