kfrlib / kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
https://www.kfrlib.com
GNU General Public License v2.0
1.65k stars 253 forks source link

resize of output univector bug #110

Closed mipac closed 3 years ago

mipac commented 3 years ago

using pure univector and ref univector don't present the same behaviour

std::vector<float> out;
std::vector<float> in = {1, 2, 3};
auto uout = make_univector(out);
auto uin = make_univector(in);

//for example with a filter_fir<float>
myfilter.apply(uout, uin);

uout, and so out, are empty, not resized but if I use

univector<float> out;
univector<float> in = {1, 2, 3};

out.size() == in.size() is ok is it a bug, or a correct behaviour?

samuelriddle commented 3 years ago

Hi, It's expected behavior because univector created using make_univector only reads/writes data referenced and doesn't modify the container itself (std::vector in your case) because after creation it doesn't even know the original type.

It's intended mainly for avoiding redundant data copy and using kfr expressions directly on your data without creating temporary univector. Example:

void process_gain(float* data, size_t size, float gain)
{
    auto buf = make_univector(data, size); // no data copy
    buf *= gain; // operates directly on data
}

So when using any container except univector you're responsible for setting the correct size before processing.