This commit is contained in:
AlexandreRouma
2026-02-06 22:20:43 -05:00
parent 80f2d752c1
commit 9bc41dbe2a
9 changed files with 47 additions and 27 deletions

View File

@@ -1,5 +1,6 @@
#include "buffer.h" #include "buffer.h"
#include "complex.h" #include "complex.h"
#include <string.h>
#include <volk/volk.h> #include <volk/volk.h>
#include <stdint.h> #include <stdint.h>
@@ -89,6 +90,7 @@ namespace dsp {
// Allocate a new buffer // Allocate a new buffer
buffer = (T*)volk_malloc(size * sizeof(T), volk_get_alignment()); buffer = (T*)volk_malloc(size * sizeof(T), volk_get_alignment());
break; break;
case REALLOC_KEEP: case REALLOC_KEEP:
// Allocate a new buffer // Allocate a new buffer
newbuf = (T*)volk_malloc(size * sizeof(T), volk_get_alignment()); newbuf = (T*)volk_malloc(size * sizeof(T), volk_get_alignment());
@@ -96,9 +98,13 @@ namespace dsp {
// Copy the existing data // Copy the existing data
memcpy(newbuf, buffer, std::min<size_t>(capacity, size)); memcpy(newbuf, buffer, std::min<size_t>(capacity, size));
// Free the current buffer
volk_free(buffer);
// Replace buffer // Replace buffer
buffer = newbuf; buffer = newbuf;
break; break;
case REALLOC_ZERO: case REALLOC_ZERO:
// Free the current buffer // Free the current buffer
volk_free(buffer); volk_free(buffer);
@@ -109,9 +115,22 @@ namespace dsp {
// Zero-out the new buffer // Zero-out the new buffer
memset(buffer, 0, size); memset(buffer, 0, size);
break; break;
case REALLOC_KEEP_AND_ZERO: case REALLOC_KEEP_AND_ZERO:
// TODO // Allocate a new buffer
return; newbuf = (T*)volk_malloc(size * sizeof(T), volk_get_alignment());
// Copy the existing data
memcpy(newbuf, buffer, std::min<size_t>(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; break;
} }

View File

@@ -16,7 +16,7 @@ namespace dsp {
// Zero out the buffer after reallocation. // Zero out the buffer after reallocation.
REALLOC_ZERO = (1 << 1), 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) REALLOC_KEEP_AND_ZERO = (REALLOC_KEEP | REALLOC_ZERO)
}; };
@@ -60,8 +60,8 @@ namespace dsp {
Buffer<T>& operator=(Buffer<T>&& b); Buffer<T>& operator=(Buffer<T>&& b);
/** /**
* Re-allocate the buffer conserving the existing data. * Re-allocate the buffer.
* @param size Number of samples. * @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. * @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); void realloc(size_t size, ReallocBehavior behavior = REALLOC_DISCARD);

View File

@@ -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) { 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) { inline constexpr dsp::Complex operator*(const dsp::Complex& a, float b) {

10
dsp/processor.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include <stdint.h>
namespace dsp {
template <class T_IN, class T_OUT>
class SISOProcessor {
public:
size_t process(const T_IN* in, T_OUT* out, size_t count);
};
}

View File

@@ -21,6 +21,12 @@ namespace dsp {
template <typename T> template <typename T>
Stream<T>::~Stream() { Stream<T>::~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 // Free both send and receive buffer sets
delete sendSet; delete sendSet;
delete recvSet; delete recvSet;

View File

@@ -102,7 +102,7 @@ namespace dsp {
* Send a set of sample buffers. * Send a set of sample buffers.
* @param count Number of valid samples in each channel buffer. * @param count Number of valid samples in each channel buffer.
* @param channels Number of valid channels channels. * @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); bool send(size_t count);

View File

@@ -33,6 +33,8 @@ namespace dsp {
*/ */
inline float operator()(float x) { return def(x); } 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. * Generate a window of a given length.
* @param data Samples buffer to write the window to. * @param data Samples buffer to write the window to.

View File

@@ -1,5 +0,0 @@
#pragma once
#include "hann.h"
#include "rectangular.h"
#include "triangular.h"
#include "welch.h"

View File

@@ -1,21 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include "dsp/window.h" #include <exception>
#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));
}
int main() { int main() {
try { try {
testFunc(dsp::window::Hann());
dsp::Window win = dsp::window::Triangular();
dsp::Window win2 = dsp::window::Hann();
win = dsp::window::Hann();
return 0; return 0;
} }