even more stuff

This commit is contained in:
AlexandreRouma
2022-06-15 16:08:54 +02:00
parent 343ec6ca1c
commit d1318d3a0f
156 changed files with 24826 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
#pragma once
#include "../types.h"
#include "windowed_sinc.h"
#include "estimate_tap_count.h"
#include "../window/nuttall.h"
#include "../math/phasor.h"
#include "../math/freq_to_omega.h"
namespace dsp::taps {
template<class T>
inline tap<T> bandPass(double bandStart, double bandStop, double transWidth, double sampleRate) {
assert(bandStop > bandStart);
float offsetOmega = math::freqToOmega((bandStart + bandStop) / 2.0, sampleRate);
return windowedSinc<T>(estimateTapCount(transWidth, sampleRate), (bandStop - bandStart) / 2.0, sampleRate, [=](double n, double N) {
if constexpr (std::is_same_v<T, float>) {
return cosf(offsetOmega * (float)n) * window::nuttall(n, N);
}
if constexpr (std::is_same_v<T, complex_t>) {
return math::phasor(offsetOmega * (float)n) * window::nuttall(n, N);
}
});
}
}

View File

@@ -0,0 +1,7 @@
#pragma once
namespace dsp::taps {
inline int estimateTapCount(double transWidth, double samplerate) {
return 3.8 * samplerate / transWidth;
}
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include "tap.h"
#include "../math/sinc.h"
#include "../math/freq_to_omega.h"
#include "../window/nuttall.h"
namespace dsp::taps {
template<class T>
inline tap<T> fromArray(int count, const T* taps) {
// Allocate taps
tap<T> _taps = taps::alloc<T>(count);
// Copy data
memcpy(_taps.taps, taps, count * sizeof(T));
return _taps;
}
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include "windowed_sinc.h"
#include "estimate_tap_count.h"
#include "../window/nuttall.h"
namespace dsp::taps {
inline tap<float> highPass(double cutoff, double transWidth, double sampleRate) {
return windowedSinc<float>(estimateTapCount(transWidth, sampleRate), (sampleRate / 2.0) - cutoff, sampleRate, [=](double n, double N){
return window::nuttall(n, N) * (((int)round(n) % 2) ? -1.0f : 1.0f);
});
}
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include "windowed_sinc.h"
#include "estimate_tap_count.h"
#include "../window/nuttall.h"
namespace dsp::taps {
inline tap<float> lowPass(double cutoff, double transWidth, double sampleRate) {
return windowedSinc<float>(estimateTapCount(transWidth, sampleRate), cutoff, sampleRate, window::nuttall);
}
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include <math.h>
#include "tap.h"
#include "../math/constants.h"
#include "../math/sinc.h"
namespace dsp::taps {
template<class T>
inline tap<T> raisedCosine(int count, double beta, double Ts) {
// Allocate taps
tap<T> taps = taps::alloc<T>(count);
// Generate taps
double half = (double)count / 2.0;
double limit = Ts / (2.0 * beta);
for (int i = 0; i < count; i++) {
double t = (double)i - half + 0.5;
if (t == limit || t == -limit) {
taps.taps[i] = math::sinc(1.0 / (2.0*beta)) * DB_M_PI / (4.0*Ts);
}
else {
taps.taps[i] = math::sinc(t / Ts) * DB_M_PI / (4.0*Ts);
}
}
return taps;
}
template<class T>
inline tap<T> raisedCosine(int count, double beta, double symbolrate, double samplerate) {
return raisedCosine<T>(count, beta, samplerate / symbolrate);
}
}

View File

@@ -0,0 +1,35 @@
#pragma once
#include <math.h>
#include "tap.h"
#include "../math/constants.h"
namespace dsp {
template<class T>
inline tap<T> rootRaisedCosine(int count, double beta, double Ts) {
// Allocate taps
tap<T> taps = taps::alloc<T>(count);
// Generate taps
double half = (double)count / 2.0;
double limit = Ts / (4.0 * beta);
for (int i = 0; i < count; i++) {
double t = (double)i - half + 0.5;
if (t == 0.0) {
taps.taps[i] = (1.0 + beta*(4.0/DB_M_PI - 1.0)) / Ts;
}
else if (t == limit || t == -limit) {
taps.taps[i] = ((1.0 + 2.0/DB_M_PI)*sin(DB_M_PI/(4.0*beta)) + (1.0 - 2.0/DB_M_PI)*cos(DB_M_PI/(4.0*beta))) * beta/(Ts*DB_M_SQRT2);
}
else {
taps.taps[i] = ((sin((1.0 - beta)*DB_M_PI*t/Ts) + cos((1.0 + beta)*DB_M_PI*t/Ts)*4.0*beta*t/Ts) / ((1.0 - (4.0*beta*t/Ts)*(4.0*beta*t/Ts))*DB_M_PI*t/Ts)) / Ts;
}
}
return taps;
}
template<class T>
inline tap<T> rootRaisedCosine(int count, double beta, double symbolrate, double samplerate) {
return rootRaisedCosine<T>(count, beta, samplerate / symbolrate);
}
}

29
core/src/dsp/taps/tap.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <volk/volk.h>
namespace dsp {
template<class T>
class tap {
public:
T* taps = NULL;
unsigned int size = 0;
};
namespace taps {
template<class T>
inline tap<T> alloc(int count) {
tap<T> taps;
taps.size = count;
taps.taps = buffer::alloc<T>(count);
return taps;
}
template<class T>
inline void free(tap<T>& taps) {
if (!taps.taps) { return; }
buffer::free(taps.taps);
taps.taps = NULL;
taps.size = 0;
}
}
}

View File

@@ -0,0 +1,35 @@
#pragma once
#include "tap.h"
#include "../math/sinc.h"
#include "../math/freq_to_omega.h"
#include "../window/nuttall.h"
namespace dsp::taps {
template<class T, typename Func>
inline tap<T> windowedSinc(int count, double omega, Func window, double norm = 1.0) {
// Allocate taps
tap<T> taps = taps::alloc<T>(count);
// Generate using window
double half = (double)count / 2.0;
double corr = norm * omega / DB_M_PI;
for (int i = 0; i < count; i++) {
double t = (double)i - half + 0.5;
if constexpr (std::is_same_v<T, float>) {
taps.taps[i] = math::sinc(t * omega) * window(t - half, count) * corr;
}
if constexpr (std::is_same_v<T, complex_t>) {
complex_t cplx = { math::sinc(t * omega), 0.0f };
taps.taps[i] = cplx * window(t - half, count) * corr;
}
}
return taps;
}
template<class T, typename Func>
inline tap<T> windowedSinc(int count, double cutoff, double samplerate, Func window, double norm = 1.0) {
return windowedSinc<T>(count, math::freqToOmega(cutoff, samplerate), window, norm);
}
}