Skip to content

Commit

Permalink
Merge pull request #1 from ivanscorral/bugfix/filter-with-noise
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanscorral authored Aug 24, 2023
2 parents 0c313ff + 48ded57 commit 840866b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
16 changes: 11 additions & 5 deletions src/averages/GenericMovingAverage.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,28 @@ T GenericMovingAverage<T>::getFilterOutput()
{
using sum_t = typename conditional<is_integral<T>::value, int, T>::type;
sum_t _sum = 0;
for (uint16_t i = 0; i < _values.size(); i++)
for (uint16_t i = 0; i < _index; i++) // Iterate up to _index
{
_sum += _values[i];
}
returnValue = _sum / _values.size();
returnValue = _sum / (_index > 0 ? _index : 1); // Divide by _index if greater than 0, else divide by 1
}
return returnValue;
}


/// @brief Get the last value added to the buffer
/// @tparam T: The type of the values to be stored
/// @return T: The last value added to the buffer

template <class T>
T GenericMovingAverage<T>::getLastValue()
{
return _values.at(_index);
if(_values.size() > 0)
{
return _values.at(_index);
}
return 0;
}

/// @brief Get the buffer status
Expand All @@ -64,11 +69,11 @@ template <class T>
g_buf_status_t GenericMovingAverage<T>::getBufferStatus()
{
g_buf_status_t returnValue;
if(_index != _index % _bufSize)
if(_values.size() == _bufSize)
{
returnValue = g_buf_status_t::BUFFER_FULL;
}
else if(_index == 0)
else if(_values.empty())
{
returnValue = g_buf_status_t::BUFFER_EMPTY;
}
Expand All @@ -78,6 +83,7 @@ g_buf_status_t GenericMovingAverage<T>::getBufferStatus()
}
return returnValue;
}

/// @brief Clear the buffer
/// @tparam T: The type of the values to be stored
/// @details Clear the buffer and reset the index and count.
Expand Down
9 changes: 3 additions & 6 deletions src/util/generate_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ vector<T> generate_data::filterArrayNoise(vector<T> origin, GenericMovingAverage
{
filter->push(noiseValue);

if (filter->getBufferStatus() == g_buf_status_t::BUFFER_FULL)
{
auto lastValue = filter->getLastValue();
auto bufVal = filter->getFilterOutput();
result.push_back(bufVal);
}
// Always add the filtered value to the result
auto bufVal = filter->getFilterOutput();
result.push_back(bufVal);
}

return result;
Expand Down

0 comments on commit 840866b

Please sign in to comment.