Skip to content

Commit

Permalink
Merge pull request cupy#8077 from asi1024/mvdr
Browse files Browse the repository at this point in the history
Add `cupyx.signal.mvdr`
  • Loading branch information
asi1024 authored and chainer-ci committed Jan 14, 2025
1 parent dbcc86a commit 7a5f5de
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions cupyx/signal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from cupyx.signal._radartools import pulse_doppler # NOQA
from cupyx.signal._radartools import cfar_alpha # NOQA
from cupyx.signal._radartools import ca_cfar # NOQA
from cupyx.signal._radartools import mvdr # NOQA
1 change: 1 addition & 0 deletions cupyx/signal/_radartools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from cupyx.signal._radartools._beamformers import mvdr # NOQA
from cupyx.signal._radartools._radartools import pulse_compression # NOQA
from cupyx.signal._radartools._radartools import pulse_doppler # NOQA
from cupyx.signal._radartools._radartools import cfar_alpha # NOQA
Expand Down
60 changes: 60 additions & 0 deletions cupyx/signal/_radartools/_beamformers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) 2019-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import cupy as cp


def mvdr(x, sv, calc_cov=True):
"""
Minimum variance distortionless response (MVDR) beamformer weights
Parameters
----------
x : ndarray
Received signal or input covariance matrix, assume 2D array with
size [num_sensors, num_samples]
sv: ndarray
Steering vector, assume 1D array with size [num_sensors, 1]
calc_cov : bool
Determine whether to calculate covariance matrix. Simply put, calc_cov
defines whether x input is made of sensor/observation data or is
a precalculated covariance matrix
Note: Unlike MATLAB where input matrix x is of size MxN where N represents
the number of array elements, we assume row-major formatted data where each
row is assumed to be complex-valued data from a given sensor (i.e. NxM)
"""
if x.shape[0] > x.shape[1]:
raise ValueError(
"Matrix has more sensors than samples. Consider \
transposing and remember cuSignal is row-major, unlike MATLAB"
)

if x.shape[0] != sv.shape[0]:
raise ValueError("Steering Vector and input data do not align")

if calc_cov:
x = cp.cov(x, dtype=x.dtype)

wB = cp.linalg.inv(x).dot(sv)
wA = sv.conj().dot(wB) # 1x1 scalar
return wB / wA
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,12 @@ def test_ca_cfar2d(xp, dtype, shape, gc, rc):
def test_ca_cfar2d_failures(shape, gc, rc):
with pytest.raises(ValueError):
_, _ = signal.ca_cfar(cupy.zeros(shape), gc, rc)


@testing.for_float_dtypes(no_float16=True)
def test_mvdr(dtype):
x = cupy.array([[1, 2, 3], [4, 5, 7]], dtype=dtype)
sv = cupy.array([1, 2], dtype=dtype)
out = signal.mvdr(x, sv)
assert out.dtype == dtype
testing.assert_allclose(out, [-2, 1.5])

0 comments on commit 7a5f5de

Please sign in to comment.