univector<T> correlate(const univector_ref<const T>& src1, const univector_ref<const T>& src2) { const size_t size = next_poweroftwo(src1.size() + src2.size() - 1); univector<complex<T>> src1padded = src1; univector<complex<T>> src2padded = reverse(src2); src1padded.resize(size, 0); src2padded.resize(size, 0); dft_plan_ptr<T> dft = dft_cache::instance().get(ctype_t<T>(), size); univector<u8> temp(dft->temp_size); dft->execute(src1padded, src1padded, temp); dft->execute(src2padded, src2padded, temp); src1padded = src1padded * src2padded; dft->execute(src1padded, src1padded, temp, true); const T invsize = reciprocal<T>(size); return truncate(real(src1padded), src1.size() + src2.size() - 1) * invsize; }
size_t process(univector<T, Tag>& output, univector_ref<const T> input) { const itype required_input_size = input_size_for_output(output.size()); const itype input_size = input.size(); for (size_t i = 0; i < output.size(); i++) { const itype intermediate_index = output_position_to_intermediate(static_cast<itype>(i) + output_position); const itype intermediate_start = intermediate_index - taps + 1; const std::lldiv_t input_pos = floor_div(intermediate_start + interpolation_factor - 1, interpolation_factor); const itype input_start = input_pos.quot; // first input sample const itype tap_start = interpolation_factor - 1 - input_pos.rem; const univector_ref<T> tap_ptr = filter.slice(static_cast<size_t>(tap_start * depth)); if (input_start >= input_position + input_size) { output[i] = T(0); } else if (input_start >= input_position) { output[i] = dotproduct(input.slice(input_start - input_position, depth), tap_ptr); } else { const itype prev_count = input_position - input_start; output[i] = dotproduct(delay.slice(size_t(depth - prev_count)), tap_ptr) + dotproduct(input.slice(0, size_t(depth - prev_count)), tap_ptr.slice(size_t(prev_count), size_t(depth - prev_count))); } } if (required_input_size >= depth) { delay.slice(0, delay.size()) = padded(input.slice(size_t(required_input_size - depth))); } else { delay.truncate(size_t(depth - required_input_size)) = delay.slice(size_t(required_input_size)); delay.slice(size_t(depth - required_input_size)) = padded(input); } input_position += required_input_size; output_position += output.size(); return required_input_size; }