Skip to content

Think DSP: Digital Signal Processing in Python - Added CUDA and RAPIDS related classes for further High Performance Computing.

Notifications You must be signed in to change notification settings

Pouria-007/ThinkDSP-CUDA-RAPIDS

 
 

Repository files navigation

ThinkDSP-CUDA-RAPIDS

Think DSP is an introduction to Digital Signal Processing in Python, now with GPU acceleration using NVIDIA CUDA, CuPy, and cuSignal.

Order Think DSP from Amazon.com.

Download Think DSP in PDF.

Read Think DSP in HTML.

What's New: GPU Acceleration

This fork adds GPU acceleration to ThinkDSP while maintaining full CPU compatibility:

  • Automatic GPU detection: Uses GPU if available, falls back to CPU
  • No API changes: All existing code works without modification
  • RAPIDS-style patterns: Efficient GPU memory management
  • Performance: 5-50x speedup for large FFTs and convolutions

See GPU Migration Guide for technical details.

Quick Start with GPU

CPU-Only Installation (Default)

conda env create -f environment.yml
conda activate ThinkDSP

GPU Installation

  1. Install CUDA libraries (if not already installed):

    # Check CUDA version
    nvidia-smi
    
    # Install CuPy and cuSignal
    # For CUDA 11.x:
    pip install cupy-cuda11x cusignal
    # For CUDA 12.x:
    pip install cupy-cuda12x cusignal
    
    # Or via conda:
    conda install -c conda-forge cupy cusignal
  2. Verify GPU availability:

    import cupy as cp
    print("CUDA available:", cp.cuda.is_available())
    print("Device:", cp.cuda.Device().id)
  3. Use ThinkDSP - GPU acceleration is automatic!

Force CPU or GPU Mode

Set environment variable to override auto-detection:

# Force CPU mode
export THINKDSP_BACKEND=cpu

# Force GPU mode (will warn if GPU unavailable)
export THINKDSP_BACKEND=gpu

The premise of this book (and the other books in the Think X series) is that if you know how to program, you can use that skill to learn other things. I am writing this book because I think the conventional approach to digital signal processing is backward: most books (and the classes that use them) present the material bottom-up, starting with mathematical abstractions like phasors.

With a programming-based approach, I can go top-down, which means I can present the most important ideas right away. By the end of the first chapter, you can decompose a sound into its harmonics, modify the harmonics, and generate new sounds.

Think DSP is a Free Book. It is available under the Creative Commons Attribution-NonCommercial 3.0 Unported License, which means that you are free to copy, distribute, and modify it, as long as you attribute the work and don't use it for commercial purposes.

Here's a notebook that previews what you will see in Chapter 1:

And if you want to see where we are headed, here's a preview of Chapter 10:

GPU Demo

Try the GPU acceleration with this simple example:

import thinkdsp
from thinkdsp_gpu.backend import get_backend
import numpy as np

# Check backend status
backend = get_backend()
print(f"Backend: {backend.name}")
if backend.use_gpu:
    import cupy as cp
    print(f"GPU Device: {cp.cuda.Device().id}")
    print(f"GPU Memory: {cp.cuda.Device().mem_info[1] / 1e9:.2f} GB")

# Create a signal
duration = 1.0
framerate = 44100
freq = 440
n = int(duration * framerate)
ts = np.arange(n) / framerate
ys = np.sin(2 * np.pi * freq * ts)

# Process on GPU (automatic)
wave = thinkdsp.Wave(ys, framerate=framerate)
spectrum = wave.make_spectrum()  # Uses GPU if available
print(f"Spectrum computed on: {backend.name}")

See code/gpu_demo.ipynb for a complete example.

Running the code

Most of the code for this book is in Jupyter notebooks. If you are not familiar with Jupyter, you can run a tutorial by clicking here. Then select "Try Classic Notebook". It will open a notebook with instructions for getting started.

To run the ThinkDSP code, you have several options:

Option 1: Run the notebooks on Google Colab (CPU only, GPU requires Colab Pro).

Option 2: Run the notebooks on Binder (CPU only).

Option 3: Use Conda to install the libraries you need and run the notebooks on your computer.

Option 4: Use poetry to install the libraries you need and run the notebooks on your computer.

The following sections explain these options in detail.

Note: GPU acceleration requires a local installation with CUDA-capable GPU. Cloud services may have GPU options (e.g., Google Colab Pro, AWS SageMaker).

Note: I have heard from a few people who tried to run the code in Spyder. Apparently there were problems, so I don't recommend it.

Option 1: Run on Colab

I have recently updated most of the notebooks in this repository so they run on Colab.

You can open any of them by clicking on the links below. If you want to modify and save any of them, you can use Colab to save a copy in a Google Drive or your own GitHub repo, or on your computer.

Option 2: Run on Binder

To run the code for this book on Binder, press this button:

Binder

It takes a minute or so to start up, but then you should see the Jupyter home page with a list of files. Click on code to open the folder with the notebooks, then click on one of the notebooks (with the .ipynb extension).

Option 3: Install Python+Jupyter

First, download the files from this repository. If you are a Git user, you can run

git clone --depth 1 https://github.com/AllenDowney/ThinkDSP.git

Otherwise you can download this Zip file and unzip it. Either way, you should end up with a directory called ThinkDSP.

Now, if you don't already have Jupyter, I highly recommend installing Anaconda, which is a Python distribution that contains everything you need to run the ThinkDSP code. It is easy to install on Windows, Mac, and Linux, and because it does a user-level install, it will not interfere with other Python installations.

Information about installing Anaconda is here.

If you have the choice of Python 2 or 3, choose Python 3.

There are two ways to get the packages you need for ThinkDSP. You can install them by hand or create a Conda environment.

To install them by hand run

conda install jupyter numpy scipy pandas matplotlib seaborn

Or, to create a conda environment, run

cd ThinkDSP
conda env create -f environment.yml
conda activate ThinkDSP

Option 4: Use poetry to manage the project on your computer or notebook locally.

First, download the files from this repository. If you are a Git user, you can run

git clone --depth 1 https://github.com/AllenDowney/ThinkDSP.git

Then, assuming you have poetry installed on your machine, run

cd ThinkDSP
poetry install

to install the libraries you need in a virtual environment. To activate the environment, run

poetry shell

Then you can run Jupyter.

Run Jupyter

To start Jupyter, run:

jupyter notebook

Jupyter should launch your default browser or open a tab in an existing browser window. If not, the Jupyter server should print a URL you can use. For example, when I launch Jupyter, I get

~/ThinkComplexity2$ jupyter notebook
[I 10:03:20.115 NotebookApp] Serving notebooks from local directory: /home/downey/ThinkDSP
[I 10:03:20.115 NotebookApp] 0 active kernels
[I 10:03:20.115 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 10:03:20.115 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

In this case, the URL is http://localhost:8888. When you start your server, you might get a different URL. Whatever it is, if you paste it into a browser, you should see a home page with a list of directories.

Click on code to open the folder with the notebooks, then click on one of the notebooks (with the .ipynb extension).

Select the cell with the import statements and press "Shift-Enter" to run the code in the cell. If it works and you get no error messages, you are all set.

If you get error messages about missing packages, you can install the packages you need using your package manager, or install Anaconda.

If you run into problems with these instructions, let me know and I will make corrections. Good luck!

Freesound

Special thanks to Freesound (http://freesound.org), which is the source of many of the sound samples I use in this book, and to the Freesound users who uploaded those sounds. I include some of their wave files in the GitHub repository for this book, using the original file names, so it should be easy to find their sources.

Unfortunately, most Freesound users don't make their real names available, so I can only thank them using their user names. Samples used in this book were contributed by Freesound users: iluppai, wcfl10, thirsk, docquesting, kleeb, landup, zippi1, themusicalnomad, bcjordan, rockwehrmann, marchascon7, jcveliz. Thank you all!

Here are links to the sources:

http://www.freesound.org/people/iluppai/sounds/100475/

http://www.freesound.org/people/wcfl10/sounds/105977/

http://www.freesound.org/people/Thirsk/sounds/120994/

http://www.freesound.org/people/ciccarelli/sounds/132736/

http://www.freesound.org/people/Kleeb/sounds/180960/

http://www.freesound.org/people/zippi1/sounds/18871/

http://www.freesound.org/people/themusicalnomad/sounds/253887/

http://www.freesound.org/people/bcjordan/sounds/28042/

http://www.freesound.org/people/rockwehrmann/sounds/72475/

http://www.freesound.org/people/marcgascon7/sounds/87778/

http://www.freesound.org/people/jcveliz/sounds/92002/


Disclaimer

THIS 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.

The GPU acceleration features have not been tested on all hardware configurations, operating systems, or use cases. The software may contain bugs, errors, or incompatibilities. Use at your own risk. The maintainers and contributors accept no responsibility or liability for any damages, data loss, or other issues that may arise from the use of this software.

About

Think DSP: Digital Signal Processing in Python - Added CUDA and RAPIDS related classes for further High Performance Computing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 99.1%
  • Other 0.9%