Skip to content

Commit

Permalink
Merge pull request #8022 from takagi/cusignal-pulse_compression
Browse files Browse the repository at this point in the history
Add `cupyx.signal.pulse_compression` from cuSignal's non SciPy-compat API
  • Loading branch information
emcastillo committed Dec 20, 2023
2 parents ec763cd + 3883ef9 commit 1ebddbe
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions cupyx/signal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from cupyx.signal.radartools import pulse_compression # NOQA
1 change: 1 addition & 0 deletions cupyx/signal/radartools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from cupyx.signal.radartools._radartools import pulse_compression # NOQA
85 changes: 85 additions & 0 deletions cupyx/signal/radartools/_radartools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Some of the functions defined here were ported directly from CuSignal under
terms of the MIT license, under the following notice:
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
from cupyx.scipy.signal import windows


def pulse_compression(x, template, normalize=False, window=None, nfft=None):
"""
Pulse Compression is used to increase the range resolution and SNR
by performing matched filtering of the transmitted pulse (template)
with the received signal (x)
Parameters
----------
x : ndarray
Received signal, assume 2D array with [num_pulses, sample_per_pulse]
template : ndarray
Transmitted signal, assume 1D array
normalize : bool
Normalize transmitted signal
window : array_like, callable, string, float, or tuple, optional
Specifies the window applied to the signal in the Fourier
domain.
nfft : int, size of FFT for pulse compression. Default is number of
samples per pulse
Returns
-------
compressedIQ : ndarray
Pulse compressed output
"""
[num_pulses, samples_per_pulse] = x.shape

if nfft is None:
nfft = samples_per_pulse

if window is not None:
Nx = len(template)
if callable(window):
W = window(cp.fft.fftfreq(Nx))
elif isinstance(window, cp.ndarray):
if window.shape != (Nx,):
raise ValueError("window must have the same length as data")
W = window
else:
W = windows.get_window(window, Nx, False)

template = cp.multiply(template, W)

if normalize is True:
template = cp.divide(template, cp.linalg.norm(template))

fft_x = cp.fft.fft(x, nfft)
fft_template = cp.conj(
cp.tile(cp.fft.fft(template, nfft), (num_pulses, 1)))
compressedIQ = cp.fft.ifft(cp.multiply(fft_x, fft_template), nfft)

return compressedIQ
11 changes: 11 additions & 0 deletions docs/source/reference/ext.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ CuPy-specific functions are placed under ``cupyx`` namespace.
cupyx.zeros_pinned
cupyx.zeros_like_pinned

non-SciPy compat Signal API
---------------------------

The functions under `cupyx.signal` are non-SciPy compat signal API ported from cuSignal
through the courtesy of Nvidia cuSignal team.

.. autosummary::
:toctree: generated/

cupyx.signal.pulse_compression

Profiling utilities
-------------------

Expand Down
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions tests/cupyx_tests/signal_tests/radartools_tests/test_radartools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

import cupy
from cupyx import signal


class TestPulseCompression:

@pytest.mark.parametrize('dtype', [cupy.float32, cupy.float64])
def test_pulse_compression(self, dtype):
num_pulses = 128
num_samples_per_pulse = 9000
template_length = 1000

shape = num_pulses, num_samples_per_pulse
x = cupy.random.randn(*shape, dtype=dtype) + \
1j * cupy.random.rand(*shape, dtype=dtype)
template = cupy.random.randn(template_length, dtype=dtype) + \
1j * cupy.random.randn(template_length, dtype=dtype)

signal.pulse_compression(x, template, normalize=True, window='hamming')

0 comments on commit 1ebddbe

Please sign in to comment.