Skip to content

Instantly share code, notes, and snippets.

@geraintluff
Last active August 15, 2021 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraintluff/c55f9482dce0db6e6a1f4509129e9a2a to your computer and use it in GitHub Desktop.
Save geraintluff/c55f9482dce0db6e6a1f4509129e9a2a to your computer and use it in GitHub Desktop.
// Use like `Hadamard<8>::inPlace(data)` - size must be a power of 2
template<int size>
struct Hadamard {
static inline void inPlace(double * data) {
if (size <= 1) return;
constexpr int hSize = size/2;
// Two Hadamards of half the size
Hadamard<hSize>::inPlace(data);
Hadamard<hSize>::inPlace(data + hSize);
for (int i = 0; i < hSize; ++i) {
double a = data[i];
double b = data[i + hSize];
data[i] = (a + b);
data[i + hSize] = (a - b);
}
}
// Scaling factor required to make it orthogonal
static Sample normFactor() {
return sqrt(1.0/size);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment