sample_rate_conversion.cpp (1767B)
1 /** 2 * KFR (https://www.kfrlib.com) 3 * Copyright (C) 2016-2023 Dan Cazarin 4 * See LICENSE.txt for details 5 */ 6 7 #include <kfr/base/math_expressions.hpp> 8 #include <kfr/dsp/oscillators.hpp> 9 #include <kfr/dsp/sample_rate_conversion.hpp> 10 #include <kfr/math/sin_cos.hpp> 11 12 namespace kfr 13 { 14 inline namespace CMT_ARCH_NAME 15 { 16 17 TEST(resampler_test) 18 { 19 const int in_sr = 44100; 20 const int out_sr = 48000; 21 const int freq = 100; 22 auto resampler = sample_rate_converter<fbase>(resample_quality::draft, out_sr, in_sr); 23 double delay = resampler.get_fractional_delay(); 24 univector<fbase> out(out_sr / 10); 25 univector<fbase> in = truncate(sin(c_pi<fbase> * phasor<fbase>(freq, in_sr, 0)), in_sr / 10); 26 univector<fbase> ref = truncate( 27 sin(c_pi<fbase> * phasor<fbase>(freq, out_sr, -delay * (static_cast<double>(freq) / out_sr))), 28 out_sr / 10); 29 resampler.process(out, in); 30 31 CHECK(rms(slice(out - ref, static_cast<size_t>(ceil(delay * 2)))) < 0.005f); 32 } 33 TEST(resampler_test_complex) 34 { 35 using type = complex<fbase>; 36 const int in_sr = 44100; 37 const int out_sr = 48000; 38 const int freq = 100; 39 auto resampler = sample_rate_converter<type>(resample_quality::draft, out_sr, in_sr); 40 double delay = resampler.get_fractional_delay(); 41 univector<type> out(out_sr / 10); 42 univector<type> in = truncate(sin(c_pi<fbase> * phasor<fbase>(freq, in_sr, 0)), in_sr / 10); 43 univector<type> ref = truncate( 44 sin(c_pi<fbase> * phasor<fbase>(freq, out_sr, -delay * (static_cast<double>(freq) / out_sr))), 45 out_sr / 10); 46 resampler.process(out, in); 47 48 CHECK(rms(cabs(slice(out - ref, static_cast<size_t>(ceil(delay * 2))))) < 0.005f); 49 } 50 } // namespace CMT_ARCH_NAME 51 } // namespace kfr