TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24350 14
Migrating from Python to C++ for performance critical code

I maintain *Spectre*, an open-source program for recording radio spectrograms using software-defined radios.

At its core, the program applies many, many Fast Fourier Transforms to an incoming stream of complex-valued samples. The samples come in quickly - up to tens of millions of samples a second. The main use case is for the application to run on Raspberry Pis, so performance is important.

While we make use of optimised libraries such as NumPy and pyFFTW for the heavy lifting, the core of the program is written in Python. Until now, I've been content with Python's advantages:

It is swifter to develop, convenient to maintain and more accessible to new developers.
The optimised libraries are already very good.

We're considering migrating to a custom C++ core. Is it worth it? Have you done it? What trade-offs would you consider?

As an example of the type of code we're looking at:

# Initialise an empty array, into which we'll copy the spectrums computed by fftw.
dynamicspectra = np.empty((windowsize, numspectrums), dtype=np.float32)

for n in range(num
spectrums):
# Center the window for the current frame
center = windowhop * n
start = center - window
size // 2
stop = start + windowsize

# The window is fully inside the signal.
if start >= 0 and stop <= signal
size:
buffer: = signalstart:stop window

# The window partially overlaps with the signal.
else:
# Zero the buffer and apply the window only to valid signal samples
signal_indices = np.arange(start, stop)
valid_mask = (signal_indices >= 0) & (signal_indices < signal_size)
buffer[:] = 0.0
buffer[valid_mask] = signal[signal_indices[valid_mask]]
windowvalid_mask

# Compute the DFT in-place, to produce the spectrum.
fftwobj.execute()

# Copy the spectrum into the spectrogram.
dynamic
spectra:, n = np.abs(buffer)


If you're curious, the program is hosted on GitHub. The performance-critical digital-signal processing is implemented in this module and this module.

https://redd.it/1p6l69u
@r_cpp
GitHub GitHub - jcfitzpatrick12/spectre: An SDR-agnostic program for recording radio signals and spectrograms (waterfall displays). An SDR-agnostic program for recording radio signals and spectrograms (waterfall displays). - jcfitzpatrick12/spectre
More from @r_cpp
  1. Oct 11, 2026wxWidgets 3.2.12 and 3.3.4 Released https://wxwidgets.org/news/2026/10/wxwidgets-3.2.12-an…
  2. Oct 11, 2026VKRT NES Remaster — an open-source C++20 NES emulator with an observation-driven remasteri…
  3. Oct 11, 2026Right way to test async code https://redd.it/1x2wqgz @r_cpp
  4. Oct 10, 2026Yantra: an LALR(1) parser generator for C++23 Yantra: an LALR(1) parser generator for C++2…
  5. Oct 10, 2026Opting in, and back out again https://dryperspective.github.io/posts/opting-in/ https://re…
  6. Oct 10, 2026I’m Building Bursztyn OS a Polish Operating System From Scratch I’m building Bursztyn OS,…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →