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 "complex.h"
#include <string.h>
#include <volk/volk.h>
#include <stdint.h>
@@ -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<size_t>(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<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;
}

View File

@@ -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<T>& operator=(Buffer<T>&& 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);

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) {
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) {

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>
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
delete sendSet;
delete recvSet;
@@ -158,7 +164,7 @@ namespace dsp {
if (stopRecv) { recvSet->samples = 0; }
// Return the buffer set
return *recvSet;
return *recvSet;
}
template <typename T>

View File

@@ -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);

View File

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

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 "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 <exception>
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;
}