Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .noise_bandwidth() filter methods #419

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/sdr/_filter/_fir.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
import numpy.typing as npt
import scipy.integrate
import scipy.signal
from typing_extensions import Literal

Expand Down Expand Up @@ -418,6 +419,30 @@ def phase_delay(self, sample_rate: float = 1.0, N: int = 1024) -> tuple[npt.NDAr

return f, tau_phi

def noise_bandwidth(self, sample_rate: float = 1.0) -> float:
r"""
Returns the noise bandwidth $B_n$ of the FIR filter.

Arguments:
sample_rate: The sample rate $f_s$ of the filter in samples/s.

Returns:
The noise bandwidth of the FIR filter $B_n$ in Hz.

Examples:
See the :ref:`fir-filters` example.
"""
verify_scalar(sample_rate, float=True, positive=True)

# Compute the frequency response
f, H = scipy.signal.freqz(self.taps, 1, worN=2**20, whole=True, fs=sample_rate)
H = np.abs(H) ** 2

# Compute the noise bandwidth
B_n = scipy.integrate.simpson(y=H, x=f) / np.max(H)

return B_n

##############################################################################
# Properties
##############################################################################
Expand Down
25 changes: 25 additions & 0 deletions src/sdr/_filter/_iir.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
import numpy.typing as npt
import scipy.integrate
import scipy.signal
from typing_extensions import Self

Expand Down Expand Up @@ -347,6 +348,30 @@ def frequency_response(
else:
return H

def noise_bandwidth(self, sample_rate: float = 1.0) -> float:
r"""
Returns the noise bandwidth $B_n$ of the IIR filter.

Arguments:
sample_rate: The sample rate $f_s$ of the filter in samples/s.

Returns:
The noise bandwidth of the IIR filter $B_n$ in Hz.

Examples:
See the :ref:`iir-filters` example.
"""
verify_scalar(sample_rate, float=True, positive=True)

# Compute the frequency response
f, H = scipy.signal.freqz(self.b_taps, self.a_taps, worN=2**20, whole=True, fs=sample_rate)
H = np.abs(H) ** 2

# Compute the noise bandwidth
B_n = scipy.integrate.simpson(y=H, x=f) / np.max(H)

return B_n

##############################################################################
# Properties
##############################################################################
Expand Down
Empty file added tests/dsp/filter/__init__.py
Empty file.
89 changes: 89 additions & 0 deletions tests/dsp/filter/test_noise_bandwidth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
References:
- https://www.gaussianwaves.com/2020/09/equivalent-noise-bandwidth-enbw-of-window-functions/
"""

import pytest
import scipy.signal

import sdr


def test_boxcar():
n = 128
h = scipy.signal.windows.get_window("boxcar", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.000, rel=1e-3)


def test_barthann():
n = 128
h = scipy.signal.windows.get_window("barthann", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.456, rel=1e-3)


def test_bartlett():
n = 128
h = scipy.signal.windows.get_window("bartlett", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.333, rel=1e-3)


def test_blackman():
n = 128
h = scipy.signal.windows.get_window("blackman", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.727, rel=1e-3)


def test_blackmanharris():
n = 128
h = scipy.signal.windows.get_window("blackmanharris", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(2.004, rel=1e-3)


def test_bohman():
n = 128
h = scipy.signal.windows.get_window("bohman", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.786, rel=1e-3)


def test_flattop():
n = 128
h = scipy.signal.windows.get_window("flattop", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(3.770, rel=1e-3)


def test_hamming():
n = 128
h = scipy.signal.windows.get_window("hamming", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.363, rel=1e-3)


def test_hann():
n = 128
h = scipy.signal.windows.get_window("hann", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.500, rel=1e-3)


def test_nuttall():
n = 128
h = scipy.signal.windows.get_window("nuttall", n)
fir = sdr.FIR(h)
B_n = fir.noise_bandwidth(n)
assert B_n == pytest.approx(1.976, rel=1e-3)
Loading