Skip to content

Commit

Permalink
wip: added sampling period measurement averaging using moving avg filter
Browse files Browse the repository at this point in the history
  • Loading branch information
klonyyy committed May 9, 2024
1 parent db7bd70 commit 8a9dceb
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ set(PROJECT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/TargetMemoryHandler/JlinkHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Plot/Plot.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Variable/Variable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/MovingAverage/MovingAverage.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ConfigHandler/ConfigHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/FileHandler/NFDFileHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ImguiPlugins/ImguiPlugins.cpp
Expand Down Expand Up @@ -168,6 +169,7 @@ target_include_directories(${EXECUTABLE} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/TargetMemoryHandler
${CMAKE_CURRENT_SOURCE_DIR}/src/Plot
${CMAKE_CURRENT_SOURCE_DIR}/src/ScrollingBuffer
${CMAKE_CURRENT_SOURCE_DIR}/src/MovingAverage
${CMAKE_CURRENT_SOURCE_DIR}/src/Variable
${CMAKE_CURRENT_SOURCE_DIR}/src/ConfigHandler
${CMAKE_CURRENT_SOURCE_DIR}/src/ImguiPlugins
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ void Gui::drawAcqusitionSettingsWindow(AcqusitionWindowType type)
ImGui::OpenPopup("Acqusition Settings");

ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
ImGui::SetNextWindowSize(ImVec2(950 * contentScale, 400 * contentScale));
ImGui::SetNextWindowSize(ImVec2(950 * contentScale, 500 * contentScale));
if (ImGui::BeginPopupModal("Acqusition Settings", &showAcqusitionSettingsWindow, 0))
{
if (type == AcqusitionWindowType::VARIABLE)
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/GuiPlots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void Gui::drawPlotCurve(Plot* plot, ScrollingBuffer<double>& time, std::map<std:
PlotHandler::Settings settings = plotHandler->getSettings();
ImPlot::SetupAxis(ImAxis_Y1, NULL, ImPlotAxisFlags_AutoFit);
ImPlot::SetupAxis(ImAxis_X1, "time[s]", 0);
const double viewportWidth = plotHandler->getAverageSamplingFrequency() * settings.maxViewportPoints;
const double viewportWidth = (1.0 / plotHandler->getAverageSamplingFrequency()) * settings.maxViewportPoints;
const double min = *time.getLastElement() < viewportWidth ? 0.0f : *time.getLastElement() - viewportWidth;
const double max = min == 0.0f ? *time.getLastElement() : min + viewportWidth;
ImPlot::SetupAxisLimits(ImAxis_X1, min, max, ImPlotCond_Always);
Expand Down
18 changes: 18 additions & 0 deletions src/MovingAverage/MovingAverage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include "MovingAverage.hpp"

MovingAverage::MovingAverage(size_t samples_) : samples(samples_ > maxSamples ? maxSamples : samples_)
{
}

double MovingAverage::filter(double sampleIn)
{
bufferSum -= buffer[bufferIter]; // Remove last entry from sum
buffer[bufferIter] = sampleIn; // Add new entry in place of the last one
bufferSum += buffer[bufferIter]; // Add new entry to sum
bufferIter++; // Increase iterator
if (bufferIter >= samples)
bufferIter = 0;

return bufferSum / samples;
}
20 changes: 20 additions & 0 deletions src/MovingAverage/MovingAverage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef MOVINGAVG_HPP
#define MOVINGAVG_HPP

#include <cstdint>

class MovingAverage
{
public:
MovingAverage(size_t samples_);
double filter(double sampleIn);

private:
static constexpr size_t maxSamples = 200;
size_t samples = 0;
double buffer[maxSamples] = {0};
size_t bufferIter = 0;
double bufferSum = 0;
};

#endif
12 changes: 6 additions & 6 deletions src/PlotHandler/PlotHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ void PlotHandler::dataHandler()
std::chrono::time_point<std::chrono::steady_clock> start;
uint32_t timer = 0;
double lastT = 0.0;
double sum = 0.0;

while (!done)
{
Expand Down Expand Up @@ -94,6 +93,9 @@ void PlotHandler::dataHandler()
}
plot->addTimePoint(entry.first);
}
/* filter sampling frequency */
averageSamplingPeriod = samplingPeriodFilter.filter((period - lastT));
lastT = period;
timer++;
}

Expand All @@ -120,12 +122,11 @@ void PlotHandler::dataHandler()
plot->addPoint(name, ser->var->getValue());
plot->addTimePoint(period);
}
/* filter sampling frequency */
averageSamplingPeriod = samplingPeriodFilter.filter((period - lastT));
lastT = period;
timer++;
}

sum += (period - lastT);
averageSamplingFrequency = sum / timer;
lastT = period;
}
else
std::this_thread::sleep_for(std::chrono::milliseconds(20));
Expand All @@ -140,7 +141,6 @@ void PlotHandler::dataHandler()
{
timer = 0;
lastT = 0.0;
sum = 0.0;
start = std::chrono::steady_clock::now();
}
else
Expand Down
6 changes: 4 additions & 2 deletions src/PlotHandler/PlotHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <thread>

#include "IDebugProbe.hpp"
#include "MovingAverage.hpp"
#include "Plot.hpp"
#include "PlotHandlerBase.hpp"
#include "ScrollingBuffer.hpp"
Expand Down Expand Up @@ -40,7 +41,7 @@ class PlotHandler : public PlotHandlerBase
void setProbeSettings(const IDebugProbe::DebugProbeSettings& settings);

void setDebugProbe(std::shared_ptr<IDebugProbe> probe);
double getAverageSamplingFrequency() const { return averageSamplingFrequency; }
double getAverageSamplingFrequency() const { return 1.0 / averageSamplingPeriod; }

private:
void dataHandler();
Expand All @@ -50,7 +51,8 @@ class PlotHandler : public PlotHandlerBase
std::unique_ptr<TargetMemoryHandler> varReader;
IDebugProbe::DebugProbeSettings probeSettings{};
Settings settings{};
double averageSamplingFrequency;
MovingAverage samplingPeriodFilter{100};
double averageSamplingPeriod;
};

#endif
5 changes: 1 addition & 4 deletions src/TargetMemoryHandler/StlinkHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class StlinkHandler : public IDebugProbe

std::string getLastErrorMsg() const override;
std::vector<std::string> getConnectedDevices() override;
bool requiresAlignedAccessOnRead() override
{
return true;
}
bool requiresAlignedAccessOnRead() override { return true; }

private:
stlink_t* sl = nullptr;
Expand Down

0 comments on commit 8a9dceb

Please sign in to comment.