A combination of some frequently used filters
Embed:
(wiki syntax)
Show/hide line numbers
CIRCULAR_BUFFER_VECTOR.cpp
00001 #include "CIRCULAR_BUFFER_VECTOR.h" 00002 00003 // First type of constructer 00004 CIRCULAR_BUFFER_VECTOR::CIRCULAR_BUFFER_VECTOR(void): 00005 buffer(0,vector<float>(0)) 00006 { 00007 // 00008 buffer_size = 0; 00009 vector_size = 0; 00010 idx = 0; 00011 } 00012 // Second type of constructer 00013 CIRCULAR_BUFFER_VECTOR::CIRCULAR_BUFFER_VECTOR(size_t buffer_size_in, size_t vector_size_in): 00014 buffer_size(buffer_size_in), 00015 vector_size(vector_size_in), 00016 buffer(buffer_size_in,vector<float>(vector_size_in,0.0)) 00017 { 00018 idx = 0; 00019 } 00020 // 00021 void CIRCULAR_BUFFER_VECTOR::Init(size_t buffer_size_in, const vector<float> &initial_value){ // If using the first type of constructer, this function helps 00022 // 00023 buffer_size = buffer_size_in; 00024 vector_size = initial_value.size(); 00025 idx = 0; 00026 // 00027 buffer.assign(buffer_size_in, initial_value); 00028 } 00029 void CIRCULAR_BUFFER_VECTOR::Reset(const vector<float> &value){ // Reset all the element without changeing the buffer size 00030 // 00031 buffer.assign(buffer_size, value); 00032 idx = 0; 00033 } 00034 // Element-wise operation 00035 vector<float> CIRCULAR_BUFFER_VECTOR::Get(int i){ // Get the element that is i samples ago 00036 int idx_e = idx - i; 00037 while (idx_e < 0){ 00038 idx_e += buffer_size; 00039 } 00040 return buffer[idx_e]; 00041 } 00042 void CIRCULAR_BUFFER_VECTOR::Set(int i, const vector<float> &value){ // Set the element that is i samples ago to the "value" 00043 // 00044 int idx_e = idx - i; 00045 while (idx_e < 0){ 00046 idx_e += buffer_size; 00047 } 00048 // 00049 buffer[idx_e] = value; 00050 } 00051 void CIRCULAR_BUFFER_VECTOR::Increase(int i, const vector<float> &increase_value, bool is_minus){ // vi += (or -=) increase_value 00052 // 00053 int idx_e = idx - i; 00054 while (idx_e < 0){ 00055 idx_e += buffer_size; 00056 } 00057 // 00058 Get_VectorIncrement(buffer[idx_e], increase_value, is_minus); 00059 } 00060 // Iterate the buffer 00061 void CIRCULAR_BUFFER_VECTOR::Insert(const vector<float> &x_new){ // Pop the oldest element and push a new element 00062 // 00063 idx++; 00064 while (idx >= buffer_size){ 00065 idx -= (int)buffer_size; 00066 } 00067 buffer[idx] = x_new; 00068 } 00069 // Utilities 00070 // Increment 00071 void CIRCULAR_BUFFER_VECTOR::Get_VectorIncrement(vector<float> &v_a, const vector<float> &v_b, bool is_minus){ // v_a += (or -=) v_b 00072 // Size check 00073 if (v_a.size() != v_b.size()){ 00074 v_a.resize(v_b.size()); 00075 } 00076 // 00077 if (is_minus){ // -= 00078 for (size_t i = 0; i < v_b.size(); ++i){ 00079 v_a[i] -= v_b[i]; 00080 } 00081 }else{ // += 00082 for (size_t i = 0; i < v_b.size(); ++i){ 00083 v_a[i] += v_b[i]; 00084 } 00085 } 00086 00087 }
Generated on Fri Jul 15 2022 14:57:41 by
1.7.2