Skip to content

standalone — one AWGN call, in C and in Python

Get the code

The files are listed at the bottom of this page. The tabs there show the project on the development branch; this fetches the project's folder from the latest release, which can differ from them until the next release:

curl -fsSL https://github.com/doppler-dsp/doppler/archive/refs/tags/v0.88.0.tar.gz \
  | tar -xz --strip-components=2 doppler-0.88.0/example-projects/standalone

Or browse example-projects/standalone/.

The smallest useful doppler program, written twice. main.c draws 4096 complex Gaussian samples and prints their mean and standard deviation; example.py does the same thing through the wheel. Between them they show the two different ways to have doppler.

Output, from the C side:

samples : 4096
mean    : 0.0237 + -0.0007i  (expect ≈ 0)
std dev : 0.9847 (Re)  0.9858 (Im)  (expect ≈ 1.0)

What it demonstrates

  • Building against a doppler BUILD TREE, with no install step. This is the one example here that can do that, and it is what you reach for while hacking on doppler itself.
  • Static or shared, selected at configure time, with the build-tree rpath baked so the shared binary runs without LD_LIBRARY_PATH.
  • The C and Python faces side by side. They are two distributions of one library: the wheel carries the Python extension and no C headers, a source build or an install carries the headers and the libraries. The C path does not depend on the Python one working, or the reverse.

Build and run

Against a doppler build tree — no install needed:

make DOPPLER_BUILD_DIR=../../build run

../../build is relative to this directory, so it works where the project sits in the doppler repository. Once you copy the project out, pass the build tree's absolute path instead.

Add LINK=shared for the shared library instead of the static one. Against an installed doppler:

make PREFIX=~/.local run

make help lists the knobs; make clean removes build/.

The Python sibling needs the wheel rather than the headers:

pip install doppler-dsp
make run-python

Without CMake

The core is pure C, so -lm -lpthread is the whole link line (glibc 2.34 and later fold pthread into libc, where -lpthread is harmless):

cc main.c -o awgn_example -I ../../native/inc -I ../../build/native/inc \
   ../../build/libdoppler.a -lm -lpthread

Copying it

CMakeLists.txt is the interesting file: its DOPPLER_BUILD_DIR branch is worth keeping if your project is developed alongside doppler, and the find_package(doppler) fallback is what you ship.

The files

cmake_minimum_required(VERSION 3.16)
project(awgn_example C)

# ---------------------------------------------------------------------------
# How to get doppler
#
# Option A — Python wheel (includes the Python extension; no C headers):
#   pip install doppler-dsp
#
# Option B — Build from source (gives headers, static lib, shared lib):
#   git clone https://github.com/doppler-dsp/doppler
#   cmake -B doppler/build doppler -DCMAKE_BUILD_TYPE=Release
#   cmake --build doppler/build -j
#
# Option C — Install a pre-built release artifact:
#   cmake --install doppler/build          # default: /usr/local
#   cmake --install doppler/build --prefix ~/.local
#
# ---------------------------------------------------------------------------
# Locate doppler
#
# Build-tree (B): set -DDOPPLER_BUILD_DIR=/path/to/doppler/build
# Installed (C):  cmake finds it automatically if on CMAKE_PREFIX_PATH
#
# Linking mode (build-tree only):
#   -DDOPPLER_LINK=static   link libdoppler.a  (default, no runtime dep)
#   -DDOPPLER_LINK=shared   link libdoppler.so (smaller binary, needs rpath)
#
# Either way the targets come from doppler's own package config. A build
# tree writes one too (doppler-config.cmake, doppler-targets.cmake), so this
# file never restates doppler's link interface. It used to, as an IMPORTED
# target linking only `m`, and that copy went stale the day libdoppler.a
# first needed -lpthread on glibc < 2.34 (doppler#2328).
# ---------------------------------------------------------------------------

set(DOPPLER_LINK "static" CACHE STRING "Link mode: static or shared")

if(DEFINED DOPPLER_BUILD_DIR)
    # <!-- doc-version:start -->
    find_package(doppler 0.88.0 CONFIG REQUIRED
                 PATHS "${DOPPLER_BUILD_DIR}" NO_DEFAULT_PATH)
    # <!-- doc-version:end -->
    if(DOPPLER_LINK STREQUAL "shared")
        set(_dp_target doppler::doppler)
    else()
        set(_dp_target doppler::doppler-static)
    endif()
else()
    # <!-- doc-version:start -->
    find_package(doppler 0.88.0 REQUIRED)
    # <!-- doc-version:end -->
    set(_dp_target doppler::doppler)
endif()

add_executable(awgn_example main.c)
target_link_libraries(awgn_example PRIVATE ${_dp_target})

# Bake the build-tree rpath so the binary runs without LD_LIBRARY_PATH.
if(DEFINED DOPPLER_BUILD_DIR AND DOPPLER_LINK STREQUAL "shared")
    set_target_properties(awgn_example PROPERTIES
        BUILD_RPATH "${DOPPLER_BUILD_DIR}")
endif()
# example-projects/standalone — a downstream project's own Makefile.
#
# The only example here that can build against doppler's BUILD TREE as well as
# an install, because it is what you reach for while hacking on doppler itself.
# `make help` lists everything.

BUILD_DIR        ?= build
CMAKE_BUILD_TYPE ?= Release
# Where doppler is installed (a `cmake --install` tree or an extracted release
# tarball). Empty means "a system install CMake can already find".
PREFIX           ?=
# ...or point at a doppler BUILD tree instead, which needs no install step:
#   make DOPPLER_BUILD_DIR=../../build run
DOPPLER_BUILD_DIR ?=
# Build-tree only: static (default, no runtime dependency) or shared.
LINK             ?= static

CMAKE_FLAGS := -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)
ifneq ($(strip $(DOPPLER_BUILD_DIR)),)
CMAKE_FLAGS += -DDOPPLER_BUILD_DIR=$(abspath $(DOPPLER_BUILD_DIR)) -DDOPPLER_LINK=$(LINK)
else ifneq ($(strip $(PREFIX)),)
CMAKE_FLAGS += -DCMAKE_PREFIX_PATH=$(abspath $(PREFIX))
endif

.PHONY: all build run run-python clean help
.DEFAULT_GOAL := build

all: build

# Configure every time rather than gating on CMakeCache.txt: re-running
# configure is cheap and idempotent, while a cached one silently ignores a
# changed PREFIX or DOPPLER_BUILD_DIR — the mistake this target prevents.
build:
    cmake -S . -B $(BUILD_DIR) $(CMAKE_FLAGS)
    cmake --build $(BUILD_DIR)

run: build
    $(BUILD_DIR)/awgn_example

# The same thing in Python, for comparison. It needs the wheel
# (`pip install doppler-dsp`) rather than the headers this project compiles
# against — two different ways to have doppler, and the C path above does not
# depend on this one working.
run-python:
    python3 example.py

clean:
    rm -rf $(BUILD_DIR)

help:
    @echo "standalone — one AWGN call, in C and in Python"
    @echo
    @echo "  make build                 configure and compile"
    @echo "  make run                   build, then run awgn_example"
    @echo "  make run-python            run example.py (needs the wheel)"
    @echo "  make clean                 remove $(BUILD_DIR)/"
    @echo
    @echo "  DOPPLER_BUILD_DIR=<dir>    build against a doppler build tree"
    @echo "  LINK=static|shared         build-tree link mode (default static)"
    @echo "  PREFIX=<dir>               ...or where doppler is installed"
    @echo "  CMAKE_BUILD_TYPE=<t>       default Release"
    @echo "  BUILD_DIR=<dir>            default build"
"""example.py — minimal doppler Python example.

Install:
    pip install doppler-dsp

Run:
    python example-projects/standalone/example.py
"""

import numpy as np

from doppler.source import awgn

noise = awgn(4096, amplitude=1.0, seed=42)

mean = noise.mean()
std_re = float(np.std(np.real(noise)))
std_im = float(np.std(np.imag(noise)))

print(f"samples : {len(noise)}")
print(f"mean    : {mean.real:.4f} + {mean.imag:.4f}i  (expect ≈ 0)")
print(f"std dev : {std_re:.4f} (Re)  {std_im:.4f} (Im)  (expect ≈ 1.0)")
/**
 * standalone/main.c — minimal doppler example.
 *
 * Generates 4096 complex AWGN samples with the one-shot dp_awgn() function,
 * then prints the empirical mean and per-component standard deviation.
 *
 * Build from source tree (doppler built but not installed):
 *   cmake -B build -DBUILD_PYTHON=OFF && cmake --build build -j
 *   cmake -B example-projects/standalone/build example-projects/standalone \
 *         -DDOPPLER_BUILD_DIR=$(pwd)/build
 *   cmake --build example-projects/standalone/build
 *   ./example-projects/standalone/build/awgn_example
 *
 * Build from installed artifact (after cmake --install):
 *   cmake -B example-projects/standalone/build example-projects/standalone
 *   cmake --build example-projects/standalone/build
 *   ./example-projects/standalone/build/awgn_example
 *
 * Or with plain gcc against the build tree (the core is pure C — the whole
 * link line is -lm -lpthread):
 *   gcc -o awgn_example example-projects/standalone/main.c \
 *       -Inative/inc -Ibuild/native/inc \
 *       build/libdoppler.a -lm -lpthread
 */

#include <complex.h>
#include <doppler/awgn/awgn_core.h>
#include <doppler/clib_common.h>
#include <math.h>
#include <stdio.h>

#define N 4096

int
main (void)
{
  float complex out[N];

  /* One-shot: no persistent state needed. */
  if (dp_awgn (/*seed=*/42, /*amplitude=*/1.0f, N, out) != DP_OK)
    {
      fprintf (stderr, "awgn: allocation failed\n");
      return 1;
    }

  /* Compute mean and per-component standard deviation. */
  double sum_re = 0, sum_im = 0;
  for (int i = 0; i < N; i++)
    {
      sum_re += crealf (out[i]);
      sum_im += cimagf (out[i]);
    }
  double mean_re = sum_re / N;
  double mean_im = sum_im / N;

  double var_re = 0, var_im = 0;
  for (int i = 0; i < N; i++)
    {
      double dr = crealf (out[i]) - mean_re;
      double di = cimagf (out[i]) - mean_im;
      var_re += dr * dr;
      var_im += di * di;
    }
  double std_re = sqrt (var_re / N);
  double std_im = sqrt (var_im / N);

  printf ("samples : %d\n", N);
  printf ("mean    : %.4f + %.4fi  (expect ≈ 0)\n", mean_re, mean_im);
  printf ("std dev : %.4f (Re)  %.4f (Im)  (expect ≈ 1.0)\n", std_re, std_im);
  return 0;
}