mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 22:35:45 +00:00
108 lines
2.1 KiB
C++
108 lines
2.1 KiB
C++
/*
|
|
Moving Extrema
|
|
|
|
Copyright 2020 Ahmet Inan <inan@aicodix.de>
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "delay.hh"
|
|
#include "deque.hh"
|
|
#include "stack.hh"
|
|
|
|
namespace DSP {
|
|
|
|
template <typename TYPE, typename EQUAL, typename COMP, int NUM>
|
|
class MovExt
|
|
{
|
|
Deque<TYPE, NUM> window;
|
|
Stack<TYPE, NUM> dispenser, refill;
|
|
EQUAL equal;
|
|
COMP comp;
|
|
public:
|
|
TYPE operator () (TYPE input)
|
|
{
|
|
if (window.full()) {
|
|
if (equal(window.front(), dispenser.top()))
|
|
dispenser.pop();
|
|
window.pop_front();
|
|
}
|
|
window.push_back(input);
|
|
|
|
while (!refill.empty() && comp(input, refill.top()))
|
|
refill.pop();
|
|
refill.push(input);
|
|
|
|
if (dispenser.empty()) {
|
|
while (!refill.empty()) {
|
|
dispenser.push(refill.top());
|
|
refill.pop();
|
|
}
|
|
return dispenser.top();
|
|
}
|
|
return comp(dispenser.top(), refill.first()) ? dispenser.top() : refill.first();
|
|
}
|
|
};
|
|
|
|
template <typename TYPE, int NUM>
|
|
class MovMin
|
|
{
|
|
Delay<TYPE, NUM> window;
|
|
Stack<TYPE, NUM> dispenser, refill;
|
|
public:
|
|
MovMin() : window(std::numeric_limits<TYPE>::max())
|
|
{
|
|
dispenser.push(std::numeric_limits<TYPE>::max());
|
|
}
|
|
TYPE operator () (TYPE input)
|
|
{
|
|
if (window(input) == dispenser.top())
|
|
dispenser.pop();
|
|
|
|
while (!refill.empty() && input < refill.top())
|
|
refill.pop();
|
|
refill.push(input);
|
|
|
|
if (dispenser.empty()) {
|
|
while (!refill.empty()) {
|
|
dispenser.push(refill.top());
|
|
refill.pop();
|
|
}
|
|
return dispenser.top();
|
|
}
|
|
return dispenser.top() < refill.first() ? dispenser.top() : refill.first();
|
|
}
|
|
};
|
|
|
|
template <typename TYPE, int NUM>
|
|
class MovMax
|
|
{
|
|
Delay<TYPE, NUM> window;
|
|
Stack<TYPE, NUM> dispenser, refill;
|
|
public:
|
|
MovMax() : window(std::numeric_limits<TYPE>::min())
|
|
{
|
|
dispenser.push(std::numeric_limits<TYPE>::min());
|
|
}
|
|
TYPE operator () (TYPE input)
|
|
{
|
|
if (window(input) == dispenser.top())
|
|
dispenser.pop();
|
|
|
|
while (!refill.empty() && input > refill.top())
|
|
refill.pop();
|
|
refill.push(input);
|
|
|
|
if (dispenser.empty()) {
|
|
while (!refill.empty()) {
|
|
dispenser.push(refill.top());
|
|
refill.pop();
|
|
}
|
|
return dispenser.top();
|
|
}
|
|
return dispenser.top() > refill.first() ? dispenser.top() : refill.first();
|
|
}
|
|
};
|
|
|
|
}
|
|
|