using System;
using System.Collections.Generic;
using System.Text;
namespace FftSharp
{
public static class Pad
{
///
/// Test if a number is an even power of 2
///
public static bool IsPowerOfTwo(int x) => ((x & (x - 1)) == 0) && (x > 0);
///
/// Return the input array (or a new zero-padded new one) ensuring length is a power of 2
///
/// array of any length
/// the input array or a zero-padded copy
public static Complex[] ZeroPad(Complex[] input)
{
if (IsPowerOfTwo(input.Length))
return input;
int targetLength = 1;
while (targetLength < input.Length)
targetLength *= 2;
int difference = targetLength - input.Length;
Complex[] padded = new Complex[targetLength];
Array.Copy(input, 0, padded, difference / 2, input.Length);
return padded;
}
///
/// Return the input array (or a new zero-padded new one) ensuring length is a power of 2
///
/// array of any length
/// the input array or a zero-padded copy
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;
}
///
/// Return the input array zero-padded to reach a final length
///
/// array of any length
/// pad the array with zeros a the end to achieve this final length
/// a zero-padded copy of the input array
public static Complex[] ZeroPad(Complex[] input, int finalLength)
{
int difference = finalLength - input.Length;
Complex[] padded = new Complex[finalLength];
Array.Copy(input, 0, padded, difference / 2, input.Length);
return padded;
}
///
/// Return the input array zero-padded to reach a final length
///
/// array of any length
/// pad the array with zeros a the end to achieve this final length
/// a zero-padded copy of the input array
public static double[] ZeroPad(double[] input, int finalLength)
{
int difference = finalLength - input.Length;
double[] padded = new double[finalLength];
Array.Copy(input, 0, padded, difference / 2, input.Length);
return padded;
}
}
}