From 9bc41dbe2afa479e688f1b68cf9fada80ea1c8e5 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Fri, 6 Feb 2026 22:20:43 -0500 Subject: [PATCH] progress --- dsp/buffer.cpp | 23 +++++++++++++++++++++-- dsp/buffer.h | 6 +++--- dsp/complex.h | 2 +- dsp/processor.h | 10 ++++++++++ dsp/stream.cpp | 8 +++++++- dsp/stream.h | 2 +- dsp/window.h | 2 ++ dsp/window/all.h | 5 ----- src/main.cpp | 16 ++-------------- 9 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 dsp/processor.h delete mode 100644 dsp/window/all.h diff --git a/dsp/buffer.cpp b/dsp/buffer.cpp index b28c875..137278b 100644 --- a/dsp/buffer.cpp +++ b/dsp/buffer.cpp @@ -1,5 +1,6 @@ #include "buffer.h" #include "complex.h" +#include #include #include @@ -89,6 +90,7 @@ namespace dsp { // Allocate a new buffer buffer = (T*)volk_malloc(size * sizeof(T), volk_get_alignment()); break; + case REALLOC_KEEP: // Allocate a new buffer newbuf = (T*)volk_malloc(size * sizeof(T), volk_get_alignment()); @@ -96,9 +98,13 @@ namespace dsp { // Copy the existing data memcpy(newbuf, buffer, std::min(capacity, size)); + // Free the current buffer + volk_free(buffer); + // Replace buffer buffer = newbuf; break; + case REALLOC_ZERO: // Free the current buffer volk_free(buffer); @@ -109,9 +115,22 @@ namespace dsp { // Zero-out the new buffer memset(buffer, 0, size); break; + case REALLOC_KEEP_AND_ZERO: - // TODO - return; + // Allocate a new buffer + newbuf = (T*)volk_malloc(size * sizeof(T), volk_get_alignment()); + + // Copy the existing data + memcpy(newbuf, buffer, std::min(capacity, size)); + + // Free the current buffer + volk_free(buffer); + + // If the new buffer is larger, zero out the extra data + if (size > capacity) { memset(&newbuf[capacity], 0, size - capacity ); } + + // Replace buffer + buffer = newbuf; break; } diff --git a/dsp/buffer.h b/dsp/buffer.h index caea3af..9733b48 100644 --- a/dsp/buffer.h +++ b/dsp/buffer.h @@ -16,7 +16,7 @@ namespace dsp { // Zero out the buffer after reallocation. REALLOC_ZERO = (1 << 1), - // Keep the existing content and zero out the excess new samples. + // Keep the existing content and zero out the additional new space if there is any. REALLOC_KEEP_AND_ZERO = (REALLOC_KEEP | REALLOC_ZERO) }; @@ -60,8 +60,8 @@ namespace dsp { Buffer& operator=(Buffer&& b); /** - * Re-allocate the buffer conserving the existing data. - * @param size Number of samples. + * Re-allocate the buffer. + * @param size New number of samples. * @param behavior Specify the reallocaition behavior to either discard exiting samples, keep existing samples or zero out the buffer. */ void realloc(size_t size, ReallocBehavior behavior = REALLOC_DISCARD); diff --git a/dsp/complex.h b/dsp/complex.h index c0c1fd1..aa85837 100644 --- a/dsp/complex.h +++ b/dsp/complex.h @@ -127,7 +127,7 @@ inline constexpr dsp::Complex operator-(const dsp::Complex& a, float b) { } inline constexpr dsp::Complex operator-(float a, const dsp::Complex& b) { - return dsp::Complex{ a - b.re, b.im }; + return dsp::Complex{ a - b.re, -b.im }; } inline constexpr dsp::Complex operator*(const dsp::Complex& a, float b) { diff --git a/dsp/processor.h b/dsp/processor.h new file mode 100644 index 0000000..eadcf6e --- /dev/null +++ b/dsp/processor.h @@ -0,0 +1,10 @@ +#pragma once +#include + +namespace dsp { + template + class SISOProcessor { + public: + size_t process(const T_IN* in, T_OUT* out, size_t count); + }; +} \ No newline at end of file diff --git a/dsp/stream.cpp b/dsp/stream.cpp index f31507f..89cfa48 100644 --- a/dsp/stream.cpp +++ b/dsp/stream.cpp @@ -21,6 +21,12 @@ namespace dsp { template Stream::~Stream() { + // Free the buffers + delete[] sendSet->buffer[0]; + delete[] sendSet->buffer; + delete[] recvSet->buffer[0]; + delete[] recvSet->buffer; + // Free both send and receive buffer sets delete sendSet; delete recvSet; @@ -158,7 +164,7 @@ namespace dsp { if (stopRecv) { recvSet->samples = 0; } // Return the buffer set - return *recvSet; + return *recvSet; } template diff --git a/dsp/stream.h b/dsp/stream.h index 22a8e60..30e8687 100644 --- a/dsp/stream.h +++ b/dsp/stream.h @@ -102,7 +102,7 @@ namespace dsp { * Send a set of sample buffers. * @param count Number of valid samples in each channel buffer. * @param channels Number of valid channels channels. - * @return False if the sender thread must exist, true otherwise. + * @return False if the sender thread must exit, true otherwise. */ bool send(size_t count); diff --git a/dsp/window.h b/dsp/window.h index 66ec178..9274897 100644 --- a/dsp/window.h +++ b/dsp/window.h @@ -33,6 +33,8 @@ namespace dsp { */ inline float operator()(float x) { return def(x); } + // TODO: Maybe have the windows function override the generate and apply functions for better performance + /** * Generate a window of a given length. * @param data Samples buffer to write the window to. diff --git a/dsp/window/all.h b/dsp/window/all.h deleted file mode 100644 index 4827e0c..0000000 --- a/dsp/window/all.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include "hann.h" -#include "rectangular.h" -#include "triangular.h" -#include "welch.h" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8a2be71..27d6f8f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,9 @@ #include -#include "dsp/window.h" -#include "dsp/window/all.h" - -void testFunc(dsp::Window win) { - printf("win(0.0) = %f\n", win(0.0)); - printf("win(0.5) = %f\n", win(0.5)); - printf("win(1.0) = %f\n", win(1.0)); -} +#include int main() { try { - testFunc(dsp::window::Hann()); - - dsp::Window win = dsp::window::Triangular(); - dsp::Window win2 = dsp::window::Hann(); - - win = dsp::window::Hann(); + return 0; }