Skip to content

Commit

Permalink
Added basic functional: generate and export signal (sine wave)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilvoron committed Apr 22, 2024
1 parent 238b6de commit 713c56a
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions _build_cmake_make.bat
Original file line number Diff line number Diff line change
@@ -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...
7 changes: 7 additions & 0 deletions _build_make.bat
Original file line number Diff line number Diff line change
@@ -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...
6 changes: 6 additions & 0 deletions _clean.bat
Original file line number Diff line number Diff line change
@@ -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...
15 changes: 15 additions & 0 deletions _clean_build_cmake_make.bat
Original file line number Diff line number Diff line change
@@ -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...
16 changes: 16 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
20 changes: 20 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
45 changes: 45 additions & 0 deletions src/core/TGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "TGenerator.h"
#include <math.h>

// *************
// 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);
}
}
24 changes: 24 additions & 0 deletions src/core/TGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef TGENERATOR_H
#define TGENERATOR_H

#include <cmath>
#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
33 changes: 33 additions & 0 deletions src/core/TSignalLine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "TSignalLine.h"
#include <math.h>

// ************
// 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; }
33 changes: 33 additions & 0 deletions src/core/TSignalLine.h
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions src/io/TFileWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "TFileWriter.h"
#include <fstream>

// ************
// 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();
}
16 changes: 16 additions & 0 deletions src/io/TFileWriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TFILEWRITER_H
#define TFILEWRITER_H

#include "TSignalLine.h"
#include <string>

class TFileWriter {
public:
TFileWriter(TSignalLine* sl, std::string filePath);
void exec();
private:
TSignalLine* _sl;
std::string _filePath;
};

#endif

0 comments on commit 713c56a

Please sign in to comment.