Line data Source code
1 : /** 2 : * \file CachedCosinusGeneratorFilter.cpp 3 : */ 4 : 5 : #include "CachedCosinusGeneratorFilter.h" 6 : 7 : #include <boost/math/constants/constants.hpp> 8 : 9 : #include <cmath> 10 : #include <cstdint> 11 : #include <cstring> 12 : 13 : namespace ATK 14 : { 15 : template<typename DataType_> 16 5 : CachedCosinusGeneratorFilter<DataType_>::CachedCosinusGeneratorFilter(int periods, int seconds) 17 5 : :Parent(0, 1), periods(periods), seconds(seconds) 18 : { 19 5 : } 20 : 21 : template<typename DataType_> 22 2 : void CachedCosinusGeneratorFilter<DataType_>::set_frequency(int periods, int seconds) 23 : { 24 2 : if(periods <= 0) 25 : { 26 1 : throw std::out_of_range("Periods must be strictly positive"); 27 : } 28 1 : this->periods = periods; 29 1 : this->seconds = seconds; 30 1 : setup(); 31 1 : } 32 : 33 : template<typename DataType_> 34 2 : std::pair<int, int> CachedCosinusGeneratorFilter<DataType_>::get_frequency() const 35 : { 36 2 : return std::make_pair(periods, seconds); 37 : } 38 : 39 : template<typename DataType_> 40 1 : void CachedCosinusGeneratorFilter<DataType_>::set_volume(DataType_ volume) 41 : { 42 1 : this->volume = volume; 43 1 : } 44 : 45 : template<typename DataType_> 46 1 : DataType_ CachedCosinusGeneratorFilter<DataType_>::get_volume() const 47 : { 48 1 : return volume; 49 : } 50 : 51 : template<typename DataType_> 52 1 : void CachedCosinusGeneratorFilter<DataType_>::set_offset(DataType_ offset) 53 : { 54 1 : this->offset = offset; 55 1 : } 56 : 57 : template<typename DataType_> 58 1 : DataType_ CachedCosinusGeneratorFilter<DataType_>::get_offset() const 59 : { 60 1 : return offset; 61 : } 62 : 63 : template<typename DataType_> 64 3 : void CachedCosinusGeneratorFilter<DataType_>::setup() 65 : { 66 3 : indice = 0; 67 3 : cache.resize(output_sampling_rate * seconds); 68 960003 : for(gsl::index i = 0; i < cache.size(); ++i) 69 : { 70 960000 : cache[i] = static_cast<DataType>(std::cos(2 * boost::math::constants::pi<double>() * (i+1) * periods / seconds / output_sampling_rate)); 71 : } 72 3 : } 73 : 74 : template<typename DataType_> 75 1 : void CachedCosinusGeneratorFilter<DataType_>::process_impl(gsl::index size) const 76 : { 77 1 : DataType* ATK_RESTRICT output = outputs[0]; 78 1 : gsl::index processed = 0; 79 2 : while(processed < size) 80 : { 81 1 : auto to_copy = std::min(size - processed, static_cast<gsl::index>(cache.size()) - indice); 82 1 : memcpy(reinterpret_cast<void*>(output + processed), reinterpret_cast<const void*>(cache.data() + indice), to_copy * sizeof(DataType_)); 83 1 : indice += to_copy; 84 1 : processed += to_copy; 85 1 : if(indice >= cache.size()) 86 : { 87 0 : indice = 0; 88 : } 89 : } 90 65537 : for(gsl::index i = 0; i < size; ++i) 91 : { 92 65536 : output[i] = static_cast<DataType>(offset + volume * output[i]); 93 : } 94 1 : } 95 : 96 : #if ATK_ENABLE_INSTANTIATION 97 : template class CachedCosinusGeneratorFilter<float>; 98 : #endif 99 : template class CachedCosinusGeneratorFilter<double>; 100 : }