ar1st0crat / NWaves

.NET DSP library with a lot of audio processing functions
MIT License
453 stars 71 forks source link

exception on Convolver.convolve function #49

Closed Azam-Mokhtari closed 3 years ago

Azam-Mokhtari commented 3 years ago

Hello I need to call convolution function using Convolver, but always fall in exception. I have input and kernel arrays with different sizes. I tried to use fft_size with various values, but none of them worked

input array length: 7992 kernel array length: 30 fft_size values: 1024, 16, ... exception Message: "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."

exception callstack: at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count) at NWaves.Utils.MemoryOperationExtensions.FastCopyTo(Single[] source, Single[] destination, Int32 size, Int32 sourceOffset, Int32 destinationOffset) at NWaves.Operations.Convolution.Convolver.Convolve(Single[] input, Single[] kernel, Single[] output)

Could you please check this issue and tell me what is proper fft_size? Thanks

ar1st0crat commented 3 years ago

Hello,

the following code doesn't throw exceptions:


var c = new Convolver();

var input = new DiscreteSignal(1, 7992);
var kernel = new DiscreteSignal(1, 30);

var res = c.Convolve(input, kernel);

i.e., if you're not sure about the FFT size, then simply don't specify it (the library will autodeduce the size of resulting signal from input sizes as result_size=len(input)+len(kernel)-1 and FFT size as fft_size=next_power_of_two(result_size), according to DSP theory). Otherwise, it's expected that the programmer understands completely what's goin' on, and you have to guarantee that FFT size is not less than fft_size. So, minimal FFT size should be: next_power_of_two(7992 + 30 - 1) = 8192.


PS. If the reason why you set FFT size to 1024 is because you intended to convolve first 1024 samples with kernel (not the entire input signal of 7992 samples), then write this:

var res = c.Convolve(input.First(1024), kernel);
Azam-Mokhtari commented 3 years ago

Thanks a lot for your answer, This can solve the problem. Or I can use convolve function with DiscreteSignal input and DiscreteSignal kernel that have various size. Thank you so much.

ar1st0crat commented 3 years ago

Ok, I re-read your initial comment, and you're working with float arrays (providing code would be better, though).

Anyway - surely, you can still work with float arrays (there's no need to switch to DiscreteSignal) - just make sure that the length of output array is sufficient and proper minimum fft size is set in the constructor (according to my previous comment).