Skip to content

Commit

Permalink
Add FIR.noise_bandwidth() and IIR.noise_bandwidth()
Browse files Browse the repository at this point in the history
Fixes #399
  • Loading branch information
mhostetter committed Jul 19, 2024
1 parent 801caa7 commit 4102cfb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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

0 comments on commit 4102cfb

Please sign in to comment.