From 713c56a7d977d45e97c77e17875dfb2a87ae9167 Mon Sep 17 00:00:00 2001 From: ilvoron <46526864+ilvoron@users.noreply.github.com> Date: Mon, 22 Apr 2024 05:53:14 +0300 Subject: [PATCH] Added basic functional: generate and export signal (sine wave) --- .gitignore | 55 +++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 10 +++++++ _build_cmake_make.bat | 8 ++++++ _build_make.bat | 7 +++++ _clean.bat | 6 ++++ _clean_build_cmake_make.bat | 15 ++++++++++ main.cpp | 16 +++++++++++ src/CMakeLists.txt | 20 ++++++++++++++ src/core/TGenerator.cpp | 45 ++++++++++++++++++++++++++++++ src/core/TGenerator.h | 24 ++++++++++++++++ src/core/TSignalLine.cpp | 33 ++++++++++++++++++++++ src/core/TSignalLine.h | 33 ++++++++++++++++++++++ src/io/TFileWriter.cpp | 24 ++++++++++++++++ src/io/TFileWriter.h | 16 +++++++++++ 14 files changed, 312 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 _build_cmake_make.bat create mode 100644 _build_make.bat create mode 100644 _clean.bat create mode 100644 _clean_build_cmake_make.bat create mode 100644 main.cpp create mode 100644 src/CMakeLists.txt create mode 100644 src/core/TGenerator.cpp create mode 100644 src/core/TGenerator.h create mode 100644 src/core/TSignalLine.cpp create mode 100644 src/core/TSignalLine.h create mode 100644 src/io/TFileWriter.cpp create mode 100644 src/io/TFileWriter.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bbcf6dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,55 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +# *.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# [Custom] +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d642b68 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.5) +project(SignalProcceser) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_subdirectory(src) + +add_executable(signal main.cpp) +target_link_libraries(signal lib) \ No newline at end of file diff --git a/_build_cmake_make.bat b/_build_cmake_make.bat new file mode 100644 index 0000000..d3afa6e --- /dev/null +++ b/_build_cmake_make.bat @@ -0,0 +1,8 @@ +echo off +echo Changing directory to "build" +cd build +echo Running CMake +cmake -G "MinGW Makefiles" .. +echo Running Make +mingw32-make +set /p DUMMY=Done. Press ENTER to exit... \ No newline at end of file diff --git a/_build_make.bat b/_build_make.bat new file mode 100644 index 0000000..3aa6155 --- /dev/null +++ b/_build_make.bat @@ -0,0 +1,7 @@ +echo off +echo Changing directory to "build" +cd build +echo Running Make +set MAKE=wsl -e make +%MAKE% +set /p DUMMY=Done. Press ENTER to exit... \ No newline at end of file diff --git a/_clean.bat b/_clean.bat new file mode 100644 index 0000000..ab6d8e4 --- /dev/null +++ b/_clean.bat @@ -0,0 +1,6 @@ +echo off +echo Cleaning "build" directory... +rmdir /S /Q build +echo Making "build" directory... +mkdir build +set /p DUMMY=Done. Press ENTER to exit... \ No newline at end of file diff --git a/_clean_build_cmake_make.bat b/_clean_build_cmake_make.bat new file mode 100644 index 0000000..c91f1f7 --- /dev/null +++ b/_clean_build_cmake_make.bat @@ -0,0 +1,15 @@ +echo off + +echo Cleaning "build" directory... +rmdir /S /Q build +echo Making "build" directory... +mkdir build + +echo Changing directory to "build" +cd build +echo Running CMake +cmake -G "MinGW Makefiles" .. +echo Running Make +mingw32-make + +set /p DUMMY=Done. Press ENTER to exit... \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..99aec5b --- /dev/null +++ b/main.cpp @@ -0,0 +1,16 @@ +#include "TFileWriter.h" +#include "TGenerator.h" + +int main() +{ + double time = 3; + double oscillationFreq = 2; + double samplingFreq = 200; + double amplitude = 3.5; + double initPhase = 0; + double offsetY = 0; + TGenerator gen(time, oscillationFreq, initPhase, offsetY, amplitude, samplingFreq); + gen.exec(); + TFileWriter file(gen.sl(), "signal.txt"); + file.exec(); +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e611e5e --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.5) + +file(GLOB_RECURSE CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/core/*.cpp") +file(GLOB_RECURSE IO_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/io/*.cpp") +file(GLOB_RECURSE ANALYSIS_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/analysis/*.cpp") + +file(GLOB_RECURSE CORE_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/core/*.h") +file(GLOB_RECURSE IO_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/io/*.h") +file(GLOB_RECURSE ANALYSIS_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/analysis/*.h") + +add_library(lib + ${CORE_SOURCES} ${IO_SOURCES} ${ANALYSIS_SOURCES} + ${CORE_HEADERS} ${IO_HEADERS} ${ANALYSIS_HEADERS} +) + +target_include_directories(lib PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/core + ${CMAKE_CURRENT_SOURCE_DIR}/io + ${CMAKE_CURRENT_SOURCE_DIR}/analysis +) \ No newline at end of file diff --git a/src/core/TGenerator.cpp b/src/core/TGenerator.cpp new file mode 100644 index 0000000..38b9529 --- /dev/null +++ b/src/core/TGenerator.cpp @@ -0,0 +1,45 @@ +#include "TGenerator.h" +#include + +// ************* +// PRIVATE +// ************* + +int TGenerator::_randInt(int from, int to) { + return rand() % (to - from + 1) + from; +} + +// ************ +// PUBLIC +// ************ + +TGenerator::TGenerator(double time, double oscillationFreq, double initPhase, double offsetY, double amplitude, double samplingFreq) { + _time = time; + _oscillationFreq = oscillationFreq; + _initPhase = initPhase; + _offsetY = offsetY; + _amplitude = amplitude; + _samplingFreq = samplingFreq; + _sl = new TSignalLine(time, oscillationFreq, initPhase, offsetY, amplitude, samplingFreq); +} + +TSignalLine *TGenerator::sl() const { return _sl; } + +void TGenerator::exec() { + unsigned int pointsCount = _sl->pointsCount(); + double x, y; + for (int i = 0; i < pointsCount; i++) { + x = i / _samplingFreq; + y = _amplitude * sin((2 * i * M_PI * _oscillationFreq) / _samplingFreq + _initPhase) + _offsetY; + _sl->set(i, x, y); + } +} + +// TODO: MAKE "TRUE" NOISE WITH TOTAL ABS EQUALS TO 0 + REFACTOR THE FUNCTION +void TGenerator::addNoise(double dispersion){ + for (int i = 0;i<_sl->pointsCount();i++){ + double yOffset = _randInt(1, 1000000) / 1000000. * dispersion; + int modif = _randInt(0, 2) - 1; + _sl->set(i,_sl->at(i).x,_sl->at(i).y + modif * yOffset); + } +} \ No newline at end of file diff --git a/src/core/TGenerator.h b/src/core/TGenerator.h new file mode 100644 index 0000000..c23262f --- /dev/null +++ b/src/core/TGenerator.h @@ -0,0 +1,24 @@ +#ifndef TGENERATOR_H +#define TGENERATOR_H + +#include +#include "TSignalLine.h" + +class TGenerator{ + public: + TGenerator(double time, double oscillationFreq, double initPhase = 0, double offsetY = 0, double amplitude = 1, double samplingFreq = 1000); + TSignalLine *sl() const; + void exec(); + void addNoise(double dispersion); + private: + TSignalLine* _sl; + double _time; + double _oscillationFreq; + double _initPhase; + double _offsetY; + double _amplitude; + double _samplingFreq; + int _randInt(int from, int to); +}; + +#endif \ No newline at end of file diff --git a/src/core/TSignalLine.cpp b/src/core/TSignalLine.cpp new file mode 100644 index 0000000..44bd591 --- /dev/null +++ b/src/core/TSignalLine.cpp @@ -0,0 +1,33 @@ +#include "TSignalLine.h" +#include + +// ************ +// PUBLIC +// ************ + +TSignalLine::TSignalLine(double time, double oscillationFreq, double initPhase, double offsetY, double amplitude, double samplingFreq) { + _time = time; + _oscillationFreq = oscillationFreq; + _initPhase = initPhase; + _offsetY = offsetY; + _amplitude = amplitude; + _samplingFreq = samplingFreq; + _pointsCount = ceil(time * samplingFreq); + _points = new Point[_pointsCount]; +} + +TSignalLine::~TSignalLine(){ delete _points; } + +void TSignalLine::set(unsigned int index,double x, double y){ + _points[index].x = x; + _points[index].y = y; +} + +Point TSignalLine::at(unsigned int index){ return _points[index]; } +unsigned int TSignalLine::pointsCount() { return _pointsCount; } +double TSignalLine::time() { return _time; } +double TSignalLine::oscillationFreq() { return _oscillationFreq; } +double TSignalLine::initPhase() { return _initPhase; } +double TSignalLine::offsetY() { return _offsetY; } +double TSignalLine::amplitude() { return _amplitude; } +double TSignalLine::samplingFreq() { return _samplingFreq; } \ No newline at end of file diff --git a/src/core/TSignalLine.h b/src/core/TSignalLine.h new file mode 100644 index 0000000..509cf03 --- /dev/null +++ b/src/core/TSignalLine.h @@ -0,0 +1,33 @@ +#ifndef TSIGNALLINE_H +#define TSIGNALLINE_H + +struct Point{ + double x; + double y; +}; + +class TSignalLine{ + public: + TSignalLine(double time, double oscillationFreq, double initPhase = 0, double offsetY = 0, double amplitude = 1, double samplingFreq = 1000); + ~TSignalLine(); + void set(unsigned int index, double x, double y); + Point at(unsigned int index); + unsigned int pointsCount(); + double time(); + double oscillationFreq(); + double initPhase(); + double offsetY(); + double amplitude(); + double samplingFreq(); + private: + Point* _points; + unsigned int _pointsCount; + double _time; + double _oscillationFreq; + double _initPhase; + double _offsetY; + double _amplitude; + double _samplingFreq; +}; + +#endif \ No newline at end of file diff --git a/src/io/TFileWriter.cpp b/src/io/TFileWriter.cpp new file mode 100644 index 0000000..28f9435 --- /dev/null +++ b/src/io/TFileWriter.cpp @@ -0,0 +1,24 @@ +#include "TFileWriter.h" +#include + +// ************ +// PUBLIC +// ************ + +TFileWriter::TFileWriter(TSignalLine* sl, std::string filePath) { + _sl = sl; + _filePath = filePath; +} + +void TFileWriter::exec() { + std::ofstream file; + file.open(_filePath); + unsigned int pointsCount = _sl->pointsCount(); + double x, y; + for (int i = 0; i < pointsCount; i++) { + x = _sl->at(i).x; + y = _sl->at(i).y; + file << x << '\t' << y << '\n'; + } + file.close(); +} \ No newline at end of file diff --git a/src/io/TFileWriter.h b/src/io/TFileWriter.h new file mode 100644 index 0000000..9be9d57 --- /dev/null +++ b/src/io/TFileWriter.h @@ -0,0 +1,16 @@ +#ifndef TFILEWRITER_H +#define TFILEWRITER_H + +#include "TSignalLine.h" +#include + +class TFileWriter { + public: + TFileWriter(TSignalLine* sl, std::string filePath); + void exec(); + private: + TSignalLine* _sl; + std::string _filePath; +}; + +#endif \ No newline at end of file