Compare commits
2 Commits
384c189b82
...
507c5f96df
Author | SHA1 | Date | |
---|---|---|---|
507c5f96df | |||
ac5121e0d2 |
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -1,3 +1,7 @@
|
||||
[submodule "third-party/c-flags"]
|
||||
path = third-party/c-flags
|
||||
url = https://github.com/DieTime/c-flags.git
|
||||
[submodule "third-party/cxxopts"]
|
||||
path = third-party/cxxopts
|
||||
url = https://github.com/jarro2783/cxxopts.git
|
||||
branch = v3.2.1
|
||||
|
@ -1,10 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
|
||||
project(pico-radio C)
|
||||
project(pico-radio)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
|
||||
include_directories(include /opt/picoscope/include third-party/c-flags/single-header)
|
||||
include_directories(include
|
||||
/opt/picoscope/include
|
||||
third-party/c-flags/single-header
|
||||
third-party/cxxopts/include)
|
||||
|
||||
link_directories(/opt/picoscope/lib)
|
||||
|
||||
add_subdirectory(src/recorder)
|
||||
add_subdirectory(src/filter)
|
||||
|
21
include/filter/filter/filter.hpp
Normal file
21
include/filter/filter/filter.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef FILTER_IFILTER_H
|
||||
#define FILTER_IFILTER_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Filter {
|
||||
|
||||
class IFilter {
|
||||
protected:
|
||||
std::vector<float> impulseResponse;
|
||||
|
||||
public:
|
||||
IFilter(std::size_t size);
|
||||
virtual ~IFilter(){};
|
||||
|
||||
void ConvolveIQ(const std::vector<float>& input, std::vector<float>& output);
|
||||
};
|
||||
} // namespace Filter
|
||||
|
||||
#endif
|
18
include/filter/filter/low_pass_filter.hpp
Normal file
18
include/filter/filter/low_pass_filter.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef FILTER_LOW_PASS_FILTER_H
|
||||
#define FILTER_LOW_PASS_FILTER_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <filter/filter/filter.hpp>
|
||||
#include <filter/window/window.hpp>
|
||||
|
||||
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() override{};
|
||||
};
|
||||
} // namespace Filter
|
||||
|
||||
#endif
|
21
include/filter/window/hamming_window.hpp
Normal file
21
include/filter/window/hamming_window.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef FILTER_HAMMING_WINDOW_H
|
||||
#define FILTER_HAMMING_WINDOW_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include <filter/window/window.hpp>
|
||||
|
||||
namespace Filter {
|
||||
class HammingWindow : public IWindow {
|
||||
private:
|
||||
std::vector<float> window;
|
||||
|
||||
public:
|
||||
HammingWindow(std::size_t width);
|
||||
|
||||
~HammingWindow() override {}
|
||||
const std::vector<float>& getWindow() override { return window; }
|
||||
};
|
||||
} // namespace Filter
|
||||
|
||||
#endif
|
14
include/filter/window/window.hpp
Normal file
14
include/filter/window/window.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef FILTER_IWINDOW_H
|
||||
#define FILTER_IWINDOW_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Filter {
|
||||
class IWindow {
|
||||
public:
|
||||
virtual ~IWindow() {}
|
||||
virtual const std::vector<float>& getWindow() = 0;
|
||||
};
|
||||
} // namespace Filter
|
||||
|
||||
#endif
|
7
src/filter/CMakeLists.txt
Normal file
7
src/filter/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
||||
file(GLOB_RECURSE SOURCES "./*.cpp")
|
||||
add_executable(filter ${SOURCES})
|
||||
target_compile_options(filter PRIVATE -Wall -Wextra)
|
||||
set_target_properties(filter PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
|
||||
CXX_STANDARD 20
|
||||
)
|
31
src/filter/filter/filter.cpp
Normal file
31
src/filter/filter/filter.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include <filter/filter/filter.hpp>
|
||||
|
||||
namespace Filter {
|
||||
IFilter::IFilter(std::size_t size) {
|
||||
impulseResponse.reserve(size);
|
||||
}
|
||||
|
||||
void IFilter::ConvolveIQ(const std::vector<float>& input, std::vector<float>& output) {
|
||||
// TODO: SIMD
|
||||
|
||||
auto itIn = input.begin();
|
||||
auto itOut = output.begin();
|
||||
|
||||
while (itIn != input.end()) {
|
||||
for (float h : impulseResponse) {
|
||||
// I
|
||||
*itOut += *itIn * h;
|
||||
itIn++;
|
||||
itOut++;
|
||||
|
||||
// Q
|
||||
*itOut += *itIn * h;
|
||||
itIn++;
|
||||
itOut++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Filter
|
22
src/filter/filter/low_pass_filter.cpp
Normal file
22
src/filter/filter/low_pass_filter.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
#include <vector>
|
||||
|
||||
#include <filter/filter/low_pass_filter.hpp>
|
||||
|
||||
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;
|
||||
if (shift != i) {
|
||||
IFilter::impulseResponse[i] = std::sin(2.0f * std::numbers::pi * sampleTime * (i - shift)) / (std::numbers::pi * sampleTime * (i - shift));
|
||||
} else {
|
||||
IFilter::impulseResponse[i] = 2.0f * cutoffHz;
|
||||
}
|
||||
|
||||
IFilter::impulseResponse[i] *= sampleTime * window[i];
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Filter
|
69
src/filter/main.cpp
Normal file
69
src/filter/main.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include <filter/filter/low_pass_filter.hpp>
|
||||
#include <filter/window/hamming_window.hpp>
|
||||
|
||||
#include <cxxopts.hpp>
|
||||
|
||||
const std::size_t BUFFER_SIZE = 256;
|
||||
|
||||
using namespace Filter;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// parse arguments
|
||||
cxxopts::Options options("filter", "filter signals; stdin->stdout");
|
||||
options.add_options()("c,cutoff", "Cutoff frequency in Hz.", cxxopts::value<size_t>()->no_implicit_value())("s,sample_rate", "Sample rate of input in Hz.",
|
||||
cxxopts::value<size_t>()->no_implicit_value());
|
||||
|
||||
cxxopts::ParseResult result;
|
||||
try {
|
||||
result = options.parse(argc, argv);
|
||||
} catch (cxxopts::exceptions::exception e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (result.count("cutoff") + result.count("sample_rate") != 2) {
|
||||
std::cerr << options.help() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// prepare pipes for binary data
|
||||
std::freopen(nullptr, "rb", stdin);
|
||||
if (std::ferror(stdin)) {
|
||||
std::cerr << "Failed to reopen stdin: " << std::strerror(errno) << std::endl;
|
||||
return errno;
|
||||
}
|
||||
|
||||
std::freopen(nullptr, "wb", stdout);
|
||||
if (std::ferror(stdout)) {
|
||||
std::cerr << "Failed to reopen stdout: " << std::strerror(errno) << std::endl;
|
||||
return errno;
|
||||
}
|
||||
|
||||
// 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());
|
||||
|
||||
// read data
|
||||
std::vector<float> inputBuffer;
|
||||
inputBuffer.reserve(BUFFER_SIZE);
|
||||
|
||||
std::vector<float> outputBuffer;
|
||||
outputBuffer.reserve(BUFFER_SIZE);
|
||||
|
||||
std::size_t len;
|
||||
|
||||
while ((len = std::fread(inputBuffer.data(), sizeof(inputBuffer[0]), BUFFER_SIZE, stdin)) > 0) {
|
||||
if (std::ferror(stdin) && !std::feof(stdin)) {
|
||||
std::cerr << "Failed to read from stdin: " << std::strerror(errno) << std::endl;
|
||||
return errno;
|
||||
}
|
||||
|
||||
lpf.ConvolveIQ(inputBuffer, outputBuffer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
13
src/filter/window/hamming_window.cpp
Normal file
13
src/filter/window/hamming_window.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
|
||||
#include <filter/window/hamming_window.hpp>
|
||||
|
||||
namespace Filter {
|
||||
HammingWindow::HammingWindow(std::size_t width) {
|
||||
window.reserve(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);
|
||||
}
|
||||
}
|
||||
} // namespace Filter
|
1
third-party/cxxopts
vendored
Submodule
1
third-party/cxxopts
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 4bf61f08697b110d9e3991864650a405b3dd515d
|
Loading…
Reference in New Issue
Block a user