Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/cmake-single-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
strategy:
fail-fast: false

Expand All @@ -29,7 +29,8 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt install python3-pybind11
sudo apt -y update
sudo apt -y install python3-pybind11 libfftw3-dev cmake python3-dev
python -m pip install --upgrade pip
python -m pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
compile_commands.json
build/
.vscode/
.cache/
*.so

11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ include_directories(${PYTHON_INCLUDE_DIRS})
# Find Pybind11
find_package(pybind11 REQUIRED)

pybind11_add_module(fdct src/fdct.cpp)
find_package(PkgConfig REQUIRED)
pkg_search_module(FFTW REQUIRED fftw3 IMPORTED_TARGET)
include_directories(PkgConfig::FFTW)
link_libraries(PkgConfig::FFTW)

pybind11_add_module(fdct
src/fdct_module.cpp
src/fft.cpp
src/fdct_inner.cpp
)

# Link the module with Python
target_link_libraries(fdct PRIVATE ${PYTHON_LIBRARIES})
36 changes: 36 additions & 0 deletions ref/FastDctFft.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Fast discrete cosine transform algorithms (C++)
*
* Copyright (c) 2017 Project Nayuki. (MIT License)
* https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/

#pragma once

#include <vector>


namespace FastDctFft {

void transform(std::vector<double> &vec);

void inverseTransform(std::vector<double> &vec);

}

75 changes: 75 additions & 0 deletions ref/FastDctFtt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Fast discrete cosine transform algorithms (C++)
*
* Copyright (c) 2017 Project Nayuki. (MIT License)
* https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/

#include <algorithm>
#include <cmath>
#include <cstddef>
#include "FastDctFft.hpp"
#include "FftRealPair.hpp"

using std::size_t;
using std::vector;


// DCT type II, unscaled
void FastDctFft::transform(vector<double> &vec) {
size_t len = vec.size();
size_t halfLen = len / 2;
vector<double> real(len);
for (size_t i = 0; i < halfLen; i++) {
real.at(i) = vec.at(i * 2);
real.at(len - 1 - i) = vec.at(i * 2 + 1);
}
if (len % 2 == 1)
real.at(halfLen) = vec.at(len - 1);
std::fill(vec.begin(), vec.end(), 0.0);
Fft::transform(real, vec);
for (size_t i = 0; i < len; i++) {
double temp = i * M_PI / (len * 2);
vec.at(i) = real.at(i) * std::cos(temp) + vec.at(i) * std::sin(temp);
}
}


// DCT type III, unscaled
void FastDctFft::inverseTransform(vector<double> &vec) {
size_t len = vec.size();
if (len > 0)
vec.at(0) /= 2;
vector<double> real(len);
for (size_t i = 0; i < len; i++) {
double temp = i * M_PI / (len * 2);
real.at(i) = vec.at(i) * std::cos(temp);
vec.at(i) *= -std::sin(temp);
}
Fft::transform(real, vec);

size_t halfLen = len / 2;
for (size_t i = 0; i < halfLen; i++) {
vec.at(i * 2 + 0) = real.at(i);
vec.at(i * 2 + 1) = real.at(len - 1 - i);
}
if (len % 2 == 1)
vec.at(len - 1) = real.at(halfLen);
}

149 changes: 149 additions & 0 deletions ref/FftComplex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Free FFT and convolution (C++)
*
* Copyright (c) 2021 Project Nayuki. (MIT License)
* https://www.nayuki.io/page/free-small-fft-in-multiple-languages
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/

#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <utility>
#include "FftComplex.hpp"

using std::complex;
using std::size_t;
using std::uintmax_t;
using std::vector;


// Private function prototypes
static size_t reverseBits(size_t val, int width);


void Fft::transform(vector<complex<double> > &vec, bool inverse) {
size_t n = vec.size();
if (n == 0)
return;
else if ((n & (n - 1)) == 0) // Is power of 2
transformRadix2(vec, inverse);
else // More complicated algorithm for arbitrary sizes
transformBluestein(vec, inverse);
}


void Fft::transformRadix2(vector<complex<double> > &vec, bool inverse) {
// Length variables
size_t n = vec.size();
int levels = 0; // Compute levels = floor(log2(n))
for (size_t temp = n; temp > 1U; temp >>= 1)
levels++;
if (static_cast<size_t>(1U) << levels != n)
throw std::domain_error("Length is not a power of 2");

// Trigonometric table
vector<complex<double> > expTable(n / 2);
for (size_t i = 0; i < n / 2; i++)
expTable[i] = std::polar(1.0, (inverse ? 2 : -2) * M_PI * i / n);

// Bit-reversed addressing permutation
for (size_t i = 0; i < n; i++) {
size_t j = reverseBits(i, levels);
if (j > i)
std::swap(vec[i], vec[j]);
}

// Cooley-Tukey decimation-in-time radix-2 FFT
for (size_t size = 2; size <= n; size *= 2) {
size_t halfsize = size / 2;
size_t tablestep = n / size;
for (size_t i = 0; i < n; i += size) {
for (size_t j = i, k = 0; j < i + halfsize; j++, k += tablestep) {
complex<double> temp = vec[j + halfsize] * expTable[k];
vec[j + halfsize] = vec[j] - temp;
vec[j] += temp;
}
}
if (size == n) // Prevent overflow in 'size *= 2'
break;
}
}


void Fft::transformBluestein(vector<complex<double> > &vec, bool inverse) {
// Find a power-of-2 convolution length m such that m >= n * 2 + 1
size_t n = vec.size();
size_t m = 1;
while (m / 2 <= n) {
if (m > SIZE_MAX / 2)
throw std::length_error("Vector too large");
m *= 2;
}

// Trigonometric table
vector<complex<double> > expTable(n);
for (size_t i = 0; i < n; i++) {
uintmax_t temp = static_cast<uintmax_t>(i) * i;
temp %= static_cast<uintmax_t>(n) * 2;
double angle = (inverse ? M_PI : -M_PI) * temp / n;
expTable[i] = std::polar(1.0, angle);
}

// Temporary vectors and preprocessing
vector<complex<double> > avec(m);
for (size_t i = 0; i < n; i++)
avec[i] = vec[i] * expTable[i];
vector<complex<double> > bvec(m);
bvec[0] = expTable[0];
for (size_t i = 1; i < n; i++)
bvec[i] = bvec[m - i] = std::conj(expTable[i]);

// Convolution
vector<complex<double> > cvec = convolve(std::move(avec), std::move(bvec));

// Postprocessing
for (size_t i = 0; i < n; i++)
vec[i] = cvec[i] * expTable[i];
}


vector<complex<double> > Fft::convolve(
vector<complex<double> > xvec,
vector<complex<double> > yvec) {

size_t n = xvec.size();
if (n != yvec.size())
throw std::domain_error("Mismatched lengths");
transform(xvec, false);
transform(yvec, false);
for (size_t i = 0; i < n; i++)
xvec[i] *= yvec[i];
transform(xvec, true);
for (size_t i = 0; i < n; i++) // Scaling (because this FFT implementation omits it)
xvec[i] /= static_cast<double>(n);
return xvec;
}


static size_t reverseBits(size_t val, int width) {
size_t result = 0;
for (int i = 0; i < width; i++, val >>= 1)
result = (result << 1) | (val & 1U);
return result;
}
61 changes: 61 additions & 0 deletions ref/FftComplex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Free FFT and convolution (C++)
*
* Copyright (c) 2021 Project Nayuki. (MIT License)
* https://www.nayuki.io/page/free-small-fft-in-multiple-languages
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/

#pragma once

#include <complex>
#include <vector>
using Complex = std::complex<double>;

namespace Fft {

/*
* Computes the discrete Fourier transform (DFT) of the given complex vector, storing the result back into the vector.
* The vector can have any length. This is a wrapper function. The inverse transform does not perform scaling, so it is not a true inverse.
*/
void transform(std::vector<std::complex<double> > &vec, bool inverse);


/*
* Computes the discrete Fourier transform (DFT) of the given complex vector, storing the result back into the vector.
* The vector's length must be a power of 2. Uses the Cooley-Tukey decimation-in-time radix-2 algorithm.
*/
void transformRadix2(std::vector<std::complex<double> > &vec, bool inverse);


/*
* Computes the discrete Fourier transform (DFT) of the given complex vector, storing the result back into the vector.
* The vector can have any length. This requires the convolution function, which in turn requires the radix-2 FFT function.
* Uses Bluestein's chirp z-transform algorithm.
*/
void transformBluestein(std::vector<std::complex<double> > &vec, bool inverse);


/*
* Computes the circular convolution of the given complex vectors. Each vector's length must be the same.
*/
std::vector<std::complex<double> > convolve(
std::vector<std::complex<double> > xvec,
std::vector<std::complex<double> > yvec);

}
Loading