Compare commits
No commits in common. "507c5f96dfee44517c1d9d0bed98f22b80811801" and "384c189b82c9716705f39a39be775c4e3ecf9138" have entirely different histories.
507c5f96df
...
384c189b82
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -1,7 +1,3 @@
|
|||||||
[submodule "third-party/c-flags"]
|
[submodule "third-party/c-flags"]
|
||||||
path = third-party/c-flags
|
path = third-party/c-flags
|
||||||
url = https://github.com/DieTime/c-flags.git
|
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,15 +1,10 @@
|
|||||||
cmake_minimum_required(VERSION 3.12)
|
cmake_minimum_required(VERSION 3.12)
|
||||||
|
|
||||||
project(pico-radio)
|
project(pico-radio C)
|
||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||||
|
|
||||||
include_directories(include
|
include_directories(include /opt/picoscope/include third-party/c-flags/single-header)
|
||||||
/opt/picoscope/include
|
|
||||||
third-party/c-flags/single-header
|
|
||||||
third-party/cxxopts/include)
|
|
||||||
|
|
||||||
link_directories(/opt/picoscope/lib)
|
link_directories(/opt/picoscope/lib)
|
||||||
|
|
||||||
add_subdirectory(src/recorder)
|
add_subdirectory(src/recorder)
|
||||||
add_subdirectory(src/filter)
|
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
#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
|
|
@ -1,18 +0,0 @@
|
|||||||
#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
|
|
@ -1,21 +0,0 @@
|
|||||||
#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
|
|
@ -1,14 +0,0 @@
|
|||||||
#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
|
|
@ -1,7 +0,0 @@
|
|||||||
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
|
|
||||||
)
|
|
@ -1,31 +0,0 @@
|
|||||||
#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
|
|
@ -1,22 +0,0 @@
|
|||||||
#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
|
|
@ -1,69 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
#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
1
third-party/cxxopts
vendored
@ -1 +0,0 @@
|
|||||||
Subproject commit 4bf61f08697b110d9e3991864650a405b3dd515d
|
|
Loading…
Reference in New Issue
Block a user