|
| 1 | +//===- KFRFir.cpp ---------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// This file implements the benchmark for KFR Fir function. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +#include <benchmark/benchmark.h> |
| 22 | +#include <kfr/base.hpp> |
| 23 | +#include <kfr/dft.hpp> |
| 24 | +#include <kfr/dsp.hpp> |
| 25 | +#include <kfr/io.hpp> |
| 26 | + |
| 27 | +using namespace kfr; |
| 28 | + |
| 29 | +univector<fbase, 1023> taps127; |
| 30 | +univector<float, 2000000> aud; |
| 31 | +univector<float> result; |
| 32 | + |
| 33 | +// Initialize univector. |
| 34 | +void initializeKFRFir() { |
| 35 | + expression_pointer<fbase> kaiser = |
| 36 | + to_pointer(window_kaiser(taps127.size(), 3.0)); |
| 37 | + fir_lowpass(taps127, 0.2, kaiser, true); |
| 38 | + audio_reader_wav<float> reader(open_file_for_reading( |
| 39 | + "../../benchmarks/AudioProcessing/Audios/NASA_Mars.wav")); |
| 40 | + reader.read(aud.data(), aud.size()); |
| 41 | +} |
| 42 | + |
| 43 | +// Benchmarking function. |
| 44 | +static void KFR_FIR(benchmark::State &state) { |
| 45 | + for (auto _ : state) { |
| 46 | + for (int i = 0; i < state.range(0); ++i) { |
| 47 | + result = kfr::fir(aud, taps127); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Register benchmarking function. |
| 53 | +BENCHMARK(KFR_FIR)->Arg(1); |
| 54 | + |
| 55 | +// Generate result wav file. |
| 56 | +void generateResultKFRFir() { |
| 57 | + univector<float> generateResult = kfr::fir(aud, taps127); |
| 58 | + |
| 59 | + audio_writer_wav<float> writer(open_file_for_writing("./ResultKFRFir.wav"), |
| 60 | + audio_format{1 /* channel */, |
| 61 | + audio_sample_type::i24, |
| 62 | + 100000 /* sample rate */}); |
| 63 | + writer.write(generateResult.data(), generateResult.size()); |
| 64 | + println("Sample Rate = ", writer.format().samplerate); |
| 65 | + println("Channels = ", writer.format().channels); |
| 66 | + println("Length = ", writer.format().length); |
| 67 | + println("Duration (s) = ", |
| 68 | + writer.format().length / writer.format().samplerate); |
| 69 | + println("Bit depth = ", audio_sample_bit_depth(writer.format().type)); |
| 70 | +} |
0 commit comments