swharden/FftSharp

Input length must be an even power of 2

Closed this issue · 2 comments

I have real audio signal wave in form of double[]. it's length isn't power of 2. what should I do?

Hi @HamidLaktarash,

There's a helper function that can pad an array with zeros and always return one that is an even power of 2

double[] padded = FftSharp.Pad.ZeroPad(input);

... it's pretty simple, so you could also implement this yourself and not rely on the helper function if you don't want to

/// <summary>
/// Return the input array (or a new zero-padded new one) ensuring length is a power of 2
/// </summary>
/// <param name="input">array of any length</param>
/// <returns>the input array or a zero-padded copy</returns>
public static double[] ZeroPad(double[] input)
{
if (IsPowerOfTwo(input.Length))
return input;
int targetLength = 1;
while (targetLength < input.Length)
targetLength *= 2;
int difference = targetLength - input.Length;
double[] padded = new double[targetLength];
Array.Copy(input, 0, padded, difference / 2, input.Length);
return padded;
}