fix filter generation

This commit is contained in:
BENEDEK László 2025-03-14 12:14:39 +01:00
parent 507c5f96df
commit 281b18b6ca
8 changed files with 63 additions and 26 deletions

View File

@ -2,6 +2,7 @@
#define FILTER_IFILTER_H
#include <cstdint>
#include <string>
#include <vector>
namespace Filter {
@ -14,6 +15,8 @@ class IFilter {
IFilter(std::size_t size);
virtual ~IFilter(){};
void Dump(const std::string& path) const;
void ConvolveIQ(const std::vector<float>& input, std::vector<float>& output);
};
} // namespace Filter

View File

@ -10,7 +10,7 @@ namespace Filter {
class LowPassFilter : public IFilter {
public:
LowPassFilter(std::size_t size, std::size_t cutoffHz, std::size_t sampleRateHz, const std::vector<float>& window);
LowPassFilter(std::int32_t size, std::size_t cutoffHz, std::size_t sampleRateHz, const std::vector<float>& window);
~LowPassFilter() override{};
};
} // namespace Filter

View File

@ -14,7 +14,9 @@ class HammingWindow : public IWindow {
HammingWindow(std::size_t width);
~HammingWindow() override {}
const std::vector<float>& getWindow() override { return window; }
void Dump(const std::string& path) const override;
const std::vector<float>& getWindow() const override { return window; }
};
} // namespace Filter

View File

@ -1,13 +1,16 @@
#ifndef FILTER_IWINDOW_H
#define FILTER_IWINDOW_H
#include <string>
#include <vector>
namespace Filter {
class IWindow {
public:
virtual ~IWindow() {}
virtual const std::vector<float>& getWindow() = 0;
virtual void Dump(const std::string& path) const = 0;
virtual const std::vector<float>& getWindow() const = 0;
};
} // namespace Filter

View File

@ -1,31 +1,43 @@
#include <cstddef>
#include <fstream>
#include <vector>
#include <filter/filter/filter.hpp>
#include <iostream>
namespace Filter {
IFilter::IFilter(std::size_t size) {
impulseResponse.reserve(size);
IFilter::IFilter(std::size_t size) : impulseResponse(size) {}
void IFilter::Dump(const std::string& path) const {
std::ofstream output;
output.open(path, std::ios::out | std::ios::binary);
output.write(reinterpret_cast<const char*>(impulseResponse.data()), sizeof(float) * impulseResponse.size());
output.close();
}
void IFilter::ConvolveIQ(const std::vector<float>& input, std::vector<float>& output) {
// TODO: SIMD
output.clear();
auto itIn = input.begin();
auto itOut = output.begin();
while (itIn != input.end()) {
float I = 0, Q = 0;
for (float h : impulseResponse) {
// I
*itOut += *itIn * h;
itIn++;
itOut++;
I += *itIn * h;
// Q
*itOut += *itIn * h;
itIn++;
itOut++;
Q += *itIn * h;
}
*(itOut + 0) = I;
*(itOut + 1) = Q;
itIn += 2;
itOut += 2;
}
}
} // namespace Filter

View File

@ -1,21 +1,26 @@
#include <cmath>
#include <cstddef>
#include <iostream>
#include <numbers>
#include <vector>
#include <filter/filter/low_pass_filter.hpp>
using std::numbers::pi;
namespace Filter {
LowPassFilter::LowPassFilter(std::size_t size, std::size_t cutoffHz, std::size_t sampleRateHz, const std::vector<float>& window) : IFilter(size) {
for (std::size_t i = 0; i < size; i++) {
std::size_t shift = std::round(size / 2);
float sampleTime = 1.0f / sampleRateHz;
LowPassFilter::LowPassFilter(std::int32_t size, std::size_t cutoffHz, std::size_t sampleRateHz, const std::vector<float>& window) : IFilter(size) {
for (std::int32_t i = 0; i < size; i++) {
int shift = size / 2.0f;
float sampleTimeS = 1.0f / sampleRateHz;
if (shift != i) {
IFilter::impulseResponse[i] = std::sin(2.0f * std::numbers::pi * sampleTime * (i - shift)) / (std::numbers::pi * sampleTime * (i - shift));
IFilter::impulseResponse[i] = std::sin(2.0f * pi * cutoffHz * sampleTimeS * (i - shift)) / (pi * sampleTimeS * (i - shift));
} else {
IFilter::impulseResponse[i] = 2.0f * cutoffHz;
}
IFilter::impulseResponse[i] *= sampleTime * window[i];
IFilter::impulseResponse[i] *= sampleTimeS * window[i];
}
}

View File

@ -9,6 +9,7 @@
#include <cxxopts.hpp>
const std::size_t BUFFER_SIZE = 256;
const std::size_t FILTER_SIZE = 64;
using namespace Filter;
@ -45,14 +46,15 @@ int main(int argc, char** argv) {
}
// create low pass filter
LowPassFilter lpf(BUFFER_SIZE, result["cutoff"].as<std::size_t>(), result["sample_rate"].as<std::size_t>(), HammingWindow(BUFFER_SIZE).getWindow());
LowPassFilter lpf(FILTER_SIZE, result["cutoff"].as<std::size_t>(), result["sample_rate"].as<std::size_t>(), HammingWindow(FILTER_SIZE).getWindow());
lpf.Dump("filter.dmp");
HammingWindow(FILTER_SIZE).Dump("window.dmp");
// read data
std::vector<float> inputBuffer;
inputBuffer.reserve(BUFFER_SIZE);
std::vector<float> inputBuffer(BUFFER_SIZE);
std::vector<float> outputBuffer;
outputBuffer.reserve(BUFFER_SIZE);
std::vector<float> outputBuffer(BUFFER_SIZE);
std::size_t len;
@ -63,6 +65,8 @@ int main(int argc, char** argv) {
}
lpf.ConvolveIQ(inputBuffer, outputBuffer);
std::fwrite(outputBuffer.data(), sizeof(float), BUFFER_SIZE, stdout);
}
return 0;

View File

@ -1,13 +1,21 @@
#include <cmath>
#include <cstdio>
#include <fstream>
#include <numbers>
#include <filter/window/hamming_window.hpp>
namespace Filter {
HammingWindow::HammingWindow(std::size_t width) {
window.reserve(width);
HammingWindow::HammingWindow(std::size_t width) : window(width) {
for (std::size_t i = 0; i < width; i++) {
window[i] = (25 / 46.0) - (21 / 46.0) * std::cos(2 * std::numbers::pi * i / width);
window[i] = (25 / 46.0f) - (21 / 46.0f) * std::cos(2.0f * std::numbers::pi * i / (float)width);
}
}
void HammingWindow::Dump(const std::string& path) const {
std::ofstream output;
output.open(path, std::ios::out | std::ios::binary);
output.write(reinterpret_cast<const char*>(getWindow().data()), sizeof(float) * getWindow().size());
output.close();
}
} // namespace Filter