### Make Build Examples for Static Library Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Examples demonstrating how to build a static library using Make, including installing it with a specified prefix. ```bash make KISSFFT_STATIC=1 all make KISSFFT_STATIC=1 install PREFIX=/usr/local ``` -------------------------------- ### Make Install Example with Custom Prefix Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Example demonstrating how to set a custom installation prefix for KISS FFT using Make. ```bash make PREFIX=/opt/kissfft install # Installs to /opt/kissfft/include, /opt/kissfft/lib, /opt/kissfft/bin ``` -------------------------------- ### Make Build Examples for Tools Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Examples showing how to explicitly build with or without command-line tools using the KISSFFT_TOOLS Make variable. ```bash make KISSFFT_TOOLS=0 all # Build library only make KISSFFT_TOOLS=1 all # Build with tools ``` -------------------------------- ### Example: Building Shared Library with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md This example demonstrates how to build a shared library using GCC and Make with the KISS_FFT_SHARED and KISS_FFT_BUILD flags. ```bash # With Make gcc -fPIC -DKISS_FFT_SHARED -DKISS_FFT_BUILD -c kiss_fft.c gcc -shared -o libkissfft.so kiss_fft.o ``` -------------------------------- ### Example: Building Shared Library with CMake Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md This example shows how to configure the build for a shared library using CMake by setting the KISS_FFT_SHARED option to ON. ```bash # With CMake cmake -DKISS_FFT_SHARED=ON .. ``` -------------------------------- ### Install KISSFFT Targets Source: https://github.com/mborgerding/kissfft/blob/master/tools/CMakeLists.txt Configures installation paths for the compiled binaries and headers. ```cmake install(TARGETS fastconv fastconvr fft ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} PUBLIC_HEADER DESTINATION ${PKGINCLUDEDIR}) ``` -------------------------------- ### Make Build Example with OpenMP and Data Type Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Example showing how to combine OpenMP support with a specific data type (float) using Make. ```bash make KISSFFT_OPENMP=1 KISSFFT_DATATYPE=float all ``` -------------------------------- ### Make Build Examples for Data Type Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Examples demonstrating how to set the KISSFFT_DATATYPE variable in Make to build with different data types, such as int16_t or simd. ```bash make KISSFFT_DATATYPE=int16_t all make KISSFFT_DATATYPE=simd test ``` -------------------------------- ### CMake Build Example for Data Type Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Example showing how to configure the data type to int16_t using CMake and then build the project. ```bash mkdir build && cd build cmake -DKISSFFT_DATATYPE=int16_t .. make ``` -------------------------------- ### Example: Fixed-Point Build Configuration Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md This example shows how to compile with FIXED_POINT defined, resulting in kiss_fft_scalar being int16_t. This is useful for embedded systems with memory constraints. ```c // Compile with: gcc -DFIXED_POINT=16 ... #include "kiss_fft.h" int main() { // kiss_fft_scalar is now int16_t kiss_fft_cpx signal[256]; // Each component is int16_t } ``` -------------------------------- ### Conditional Logging Example with KISS FFT Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Shows how to conditionally enable logging using a verbose flag. This example includes FFT allocation, execution, and error handling, with logging for INFO and ERROR levels. ```c #include "kiss_fft_log.h" #include "kiss_fft.h" #include int verbose = 0; void my_fft_wrapper(int nfft, const kiss_fft_cpx *in, kiss_fft_cpx *out) { if (verbose) { KISS_FFT_INFO("Starting FFT size=%d", nfft); } kiss_fft_cfg cfg = kiss_fft_alloc(nfft, 0, NULL, NULL); if (!cfg) { KISS_FFT_ERROR("FFT allocation failed for size %d", nfft); return; } kiss_fft(cfg, in, out); if (verbose) { KISS_FFT_INFO("FFT completed"); } free(cfg); } int main(int argc, char *argv[]) { if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'v') { verbose = 1; } kiss_fft_cpx in[256], out[256]; my_fft_wrapper(256, in, out); return 0; } ``` -------------------------------- ### Set Installation Prefix with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Specify the installation directory prefix using the PREFIX Make variable. The default prefix is /usr/local. ```bash make PREFIX=/usr/local install ``` -------------------------------- ### 3D Real FFT Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fftndr.md This example demonstrates how to perform a 3D real FFT using kiss_fftndr. It allocates memory for input and output, fills the input volume with sample data, performs the FFT, and then frees the allocated resources. ```c #include "kiss_fftndr.h" #include #include int main() { // 3D real FFT: 64x64x64 int dims[] = {64, 64, 64}; int ndims = 3; kiss_fftndr_cfg cfg = kiss_fftndr_alloc(dims, ndims, 0, NULL, NULL); int in_total = 64 * 64 * 64; int out_total = 64 * 64 * (64/2 + 1); // Last dim is halved kiss_fft_scalar *volume = (kiss_fft_scalar*)malloc(in_total * sizeof(kiss_fft_scalar)); kiss_fft_cpx *spectrum = (kiss_fft_cpx*)malloc(out_total * sizeof(kiss_fft_cpx)); // Fill volume for (int i = 0; i < in_total; i++) { volume[i] = (kiss_fft_scalar)(i & 0xFF) / 256.0f; } // Forward 3D real FFT kiss_fftndr(cfg, volume, spectrum); printf("Input: %d real samples\n", in_total); printf("Output: %d complex values\n", out_total); free(volume); free(spectrum); free(cfg); return 0; } ``` -------------------------------- ### Example: Custom Memory Tracking Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md This example demonstrates overriding memory allocation with custom tracking functions for malloc and free. It requires defining KISS_FFT_MALLOC and KISS_FFT_FREE before including kiss_fft.h. ```c #define KISS_FFT_MALLOC(nbytes) track_malloc(__FILE__, __LINE__, nbytes) #define KISS_FFT_FREE(ptr) track_free(__FILE__, __LINE__, ptr) #include "kiss_fft.h" ``` -------------------------------- ### Example Usage of KISS_FFT_LOG_MSG Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Demonstrates how to use KISS_FFT_LOG_MSG with different severity levels and arguments. Shows compilation for debug and production builds. ```c #include "kiss_fft_log.h" #include "kiss_fft.h" #include int main() { int nfft = 256; if (nfft < 1) { KISS_FFT_LOG_MSG(ERROR, "Invalid FFT size: %d", nfft); return 1; } kiss_fft_cfg cfg = kiss_fft_alloc(nfft, 0, NULL, NULL); if (!cfg) { KISS_FFT_LOG_MSG(ERROR, "FFT allocation failed"); return 1; } return 0; } ``` ```bash # Debug build (logging enabled) gcc -o app main.c kiss_fft.c ./app # Output: [ERROR] main.c:7 Invalid FFT size: 256 # Production build (logging disabled) gcc -DNDEBUG -o app main.c kiss_fft.c ./app # No output ``` -------------------------------- ### Manual FFT Configuration Management Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kfc.md This example shows a more efficient approach using manual management of FFT configurations. It pre-allocates only the necessary configurations and uses direct indexed lookup for faster processing. ```c #include "kiss_fft.h" int main() { // Pre-allocate only needed sizes kiss_fft_cfg cfgs[10]; for (int i = 0; i < 10; i++) { int size = 100 + i * 10; cfgs[i] = kiss_fft_alloc(size, 0, NULL, NULL); } // Fast, indexed lookup for (int i = 0; i < 10; i++) { kiss_fft(cfgs[i], in, out); } for (int i = 0; i < 10; i++) { free(cfgs[i]); } } ``` -------------------------------- ### Example Usage of KISS_FFT_INFO Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Demonstrates logging informational messages about FFT configuration creation, cache operations, or computation completion. The output includes the severity, file, and line number. ```c #include "kiss_fft_log.h" KISS_FFT_INFO("Creating FFT config for size %d (inverse=%d)", nfft, inverse); KISS_FFT_INFO("FFT cached with %d existing configs", cache_size); KISS_FFT_INFO("Transform completed in %lld cycles", elapsed); ``` -------------------------------- ### KissFFT Build with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/README.md Builds KissFFT using Make, specifying the data type (e.g., float) and installing it to a specified prefix. This is useful for managing build configurations and installation. ```bash make KISSFFT_DATATYPE=float all make install PREFIX=/usr/local ``` -------------------------------- ### Example: Proper Cache Cleanup Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kfc.md Demonstrates proper cache cleanup after using multiple FFT sizes. It calls `kfc_cleanup` to free all cached configurations before exiting. ```c #include "kfc.h" #include int main() { kiss_fft_cpx *in = (kiss_fft_cpx*)malloc(1024 * sizeof(kiss_fft_cpx)); kiss_fft_cpx *out = (kiss_fft_cpx*)malloc(1024 * sizeof(kiss_fft_cpx)); // Use multiple FFT sizes kfc_fft(256, in, out); // Caches 256 config kfc_fft(512, in, out); // Caches 512 config kfc_fft(1024, in, out); // Caches 1024 config free(in); free(out); // Cleanup all cached configs kfc_cleanup(); return 0; } ``` -------------------------------- ### Compile and Run Conditional Logging Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Instructions for compiling the C code and running it with and without the verbose flag to observe logging behavior. ```bash gcc -o app app.c kiss_fft.c ./app # No logging ./app -v # With logging ``` -------------------------------- ### Set Installation Directory with CMake Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Specify the installation directory prefix using the CMAKE_INSTALL_PREFIX CMake variable. This is equivalent to Make's PREFIX variable. ```bash cmake -DCMAKE_INSTALL_PREFIX=/opt/kissfft .. make install ``` -------------------------------- ### Example: Real FFT of a Sine Wave Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fftr.md This example demonstrates the usage of kiss_fftr to compute the forward real FFT of a sine wave. It includes signal generation, FFT computation, and magnitude spectrum analysis. ```c #include "kiss_fftr.h" #include #include #include int main() { int nfft = 256; kiss_fftr_cfg cfg = kiss_fftr_alloc(nfft, 0, NULL, NULL); kiss_fft_scalar *in = (kiss_fft_scalar*)malloc(nfft * sizeof(kiss_fft_scalar)); kiss_fft_cpx *out = (kiss_fft_cpx*)malloc((nfft/2 + 1) * sizeof(kiss_fft_cpx)); // Create test signal: 100 Hz sine wave at 44.1 kHz sample rate float sample_rate = 44100.0f; float freq = 100.0f; for (int i = 0; i < nfft; i++) { in[i] = (kiss_fft_scalar)sin(2 * M_PI * freq * i / sample_rate); } // Forward real FFT kiss_fftr(cfg, in, out); // Find magnitude spectrum printf("Magnitude spectrum (first 10 bins):\n"); for (int i = 0; i < 10; i++) { float mag = sqrt(out[i].r * out[i].r + out[i].i * out[i].i); printf("Bin %d: %.4f\n", i, mag / nfft); } free(in); free(out); free(cfg); return 0; } ``` -------------------------------- ### Example: Re-using Cache After Cleanup Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kfc.md Shows how the cache is recreated after `kfc_cleanup` is called. The first call to `kfc_fft` after cleanup will create a new configuration for the specified FFT size. ```c #include "kfc.h" #include int main() { kiss_fft_cpx *in = (kiss_fft_cpx*)malloc(512 * sizeof(kiss_fft_cpx)); kiss_fft_cpx *out = (kiss_fft_cpx*)malloc(512 * sizeof(kiss_fft_cpx)); // First phase kfc_fft(512, in, out); kfc_cleanup(); // Clear cache // Second phase - will recreate cache kfc_fft(512, in, out); // Creates new 512 config kfc_cleanup(); free(in); free(out); return 0; } ``` -------------------------------- ### Full Example: Forward and Inverse Real FFT Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fftr.md Demonstrates the complete process of performing a forward real FFT and then an inverse real FFT to reconstruct the original signal. Ensure proper memory allocation and deallocation for configurations and data arrays. ```c #include "kiss_fftr.h" #include #include #include int main() { int nfft = 256; // Allocate for forward transform kiss_fftr_cfg cfg_fwd = kiss_fftr_alloc(nfft, 0, NULL, NULL); // Allocate for inverse transform kiss_fftr_cfg cfg_inv = kiss_fftr_alloc(nfft, 1, NULL, NULL); kiss_fft_scalar *original = (kiss_fft_scalar*)malloc(nfft * sizeof(kiss_fft_scalar)); kiss_fft_cpx *spectrum = (kiss_fft_cpx*)malloc((nfft/2 + 1) * sizeof(kiss_fft_cpx)); kiss_fft_scalar *reconstructed = (kiss_fft_scalar*)malloc(nfft * sizeof(kiss_fft_scalar)); // Create original signal for (int i = 0; i < nfft; i++) { original[i] = (kiss_fft_scalar)sin(2 * M_PI * 5 * i / nfft); } // Forward transform kiss_fftr(cfg_fwd, original, spectrum); // Inverse transform kiss_fftri(cfg_inv, spectrum, reconstructed); // Check reconstruction (note: may need scaling adjustment) printf("Original vs Reconstructed (first 5 samples):\n"); for (int i = 0; i < 5; i++) { printf("original[%d]=%f, reconstructed[%d]=%f\n", i, original[i], i, reconstructed[i]); } free(original); free(spectrum); free(reconstructed); free(cfg_fwd); free(cfg_inv); return 0; } ``` -------------------------------- ### Enable Debug Output Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Example demonstrating how debug logging is enabled when NDEBUG is not defined. This is useful for development and troubleshooting, as it allows logging messages to stderr. ```c // Do NOT define NDEBUG #include "kiss_fft_log.h" #include "kiss_fft.h" int main() { // Logging is enabled // Real FFT with odd size will print error kiss_fftr_cfg cfg = kiss_fftr_alloc(257, 0, NULL, NULL); // Error logged } ``` -------------------------------- ### 2D Real FFT Round-trip Transform Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fftndr.md Demonstrates a complete forward and inverse 2D real FFT round-trip. This example shows how to allocate configurations, perform the forward transform using kiss_fftndr, then the inverse transform using kiss_fftndri, and finally compares the original and reconstructed data. ```c #include "kiss_fftndr.h" #include #include #include int main() { // 2D real FFT round-trip int dims[] = {64, 64}; int ndims = 2; kiss_fftndr_cfg cfg_fwd = kiss_fftndr_alloc(dims, ndims, 0, NULL, NULL); kiss_fftndr_cfg cfg_inv = kiss_fftndr_alloc(dims, ndims, 1, NULL, NULL); int in_size = 64 * 64; int spec_size = 64 * (64/2 + 1); kiss_fft_scalar *original = (kiss_fft_scalar*)malloc(in_size * sizeof(kiss_fft_scalar)); kiss_fft_cpx *spectrum = (kiss_fft_cpx*)malloc(spec_size * sizeof(kiss_fft_cpx)); kiss_fft_scalar *reconstructed = (kiss_fft_scalar*)malloc(in_size * sizeof(kiss_fft_scalar)); // Create test pattern for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { int idx = i * 64 + j; original[idx] = (kiss_fft_scalar)(sin(2*M_PI*i/64) * cos(2*M_PI*j/64)); } } // Forward transform kiss_fftndr(cfg_fwd, original, spectrum); // Inverse transform kiss_fftndri(cfg_inv, spectrum, reconstructed); // Compare printf("Round-trip error (first element):\n"); printf("Original: %.6f, Reconstructed: %.6f\n", original[0], reconstructed[0]); free(original); free(spectrum); free(reconstructed); free(cfg_fwd); free(cfg_inv); return 0; } ``` -------------------------------- ### 3D Volume FFT Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fftnd.md Performs a 3D FFT on a 32x32x32 volume. It initializes a complex array with synthetic data, computes the FFT, and then deallocates all associated memory. ```c #include "kiss_fftnd.h" #include int main() { // 3D FFT: 32x32x32 volume int dims[] = {32, 32, 32}; int ndims = 3; kiss_fftnd_cfg cfg = kiss_fftnd_alloc(dims, ndims, 0, NULL, NULL); int total = 32 * 32 * 32; kiss_fft_cpx *volume = (kiss_fft_cpx*)malloc(total * sizeof(kiss_fft_cpx)); kiss_fft_cpx *spectrum = (kiss_fft_cpx*)malloc(total * sizeof(kiss_fft_cpx)); // Fill with synthetic data for (int i = 0; i < total; i++) { volume[i].r = (float)(i % 256) / 256.0f; volume[i].i = 0.0f; } // 3D FFT kiss_fftnd(cfg, volume, spectrum); free(volume); free(spectrum); free(cfg); return 0; } ``` -------------------------------- ### Example Usage of KISS_FFT_DEBUG Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Demonstrates how to use the KISS_FFT_DEBUG macro within a loop to log processing details and specific values like twiddle factors. Ensure kiss_fft_log.h is included. ```c #include "kiss_fft_log.h" for (int i = 0; i < nfft; i++) { KISS_FFT_DEBUG("Processing bin %d: real=%.4f, imag=%.4f", i, out[i].r, out[i].i); } KISS_FFT_DEBUG("Twiddle factor: %.6f + %.6fi", tw.r, tw.i); ``` -------------------------------- ### Simple Forward FFT Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kfc.md Demonstrates the usage of kfc_fft for a forward complex FFT. It shows the allocation of input and output arrays, filling them with a test signal, performing the FFT twice (first call allocates and caches, second reuses), and cleaning up resources. ```c #include "kfc.h" #include #include #include int main() { int nfft = 512; kiss_fft_cpx *in = (kiss_fft_cpx*)malloc(nfft * sizeof(kiss_fft_cpx)); kiss_fft_cpx *out = (kiss_fft_cpx*)malloc(nfft * sizeof(kiss_fft_cpx)); // Fill with test signal for (int i = 0; i < nfft; i++) { float t = (float)i / nfft; in[i].r = sin(2 * M_PI * 5 * t) + sin(2 * M_PI * 10 * t); in[i].i = 0.0f; } // First call: allocates and caches config kfc_fft(nfft, in, out); printf("FFT completed\n"); // Second call: reuses cached config (fast) kfc_fft(nfft, in, out); // Clean up free(in); free(out); kfc_cleanup(); return 0; } ``` -------------------------------- ### Example Usage of KISS_FFT_WARNING Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Shows how to log warnings for non-optimal FFT sizes or potential overflows in fixed-point builds. The output includes the severity level, file, and line number. ```c #include "kiss_fft_log.h" if (nfft != kiss_fft_next_fast_size(nfft)) { int fast = kiss_fft_next_fast_size(nfft); KISS_FFT_WARNING("FFT size %d is not optimal, %d is faster", nfft, fast); } #ifdef FIXED_POINT if ((a.r + b.r) > SAMP_MAX) { KISS_FFT_WARNING("overflow (%d + %d) = %ld", a.r, b.r, (long)(a.r + b.r)); } #endif ``` -------------------------------- ### 2D Image FFT Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fftnd.md Demonstrates a 2D FFT on a 256x256 gradient image. Requires kiss_fftnd.h and standard libraries. Allocates memory for input, output, and configuration, performs the FFT, prints magnitude samples, and frees allocated memory. ```c #include "kiss_fftnd.h" #include #include #include int main() { // 2D FFT of a 256x256 image int dims[] = {256, 256}; int ndims = 2; kiss_fftnd_cfg cfg = kiss_fftnd_alloc(dims, ndims, 0, NULL, NULL); int total = 256 * 256; kiss_fft_cpx *img = (kiss_fft_cpx*)malloc(total * sizeof(kiss_fft_cpx)); kiss_fft_cpx *spectrum = (kiss_fft_cpx*)malloc(total * sizeof(kiss_fft_cpx)); // Create simple gradient image for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { int idx = i * 256 + j; img[idx].r = (float)i + (float)j; // Gradient img[idx].i = 0.0f; } } // Transform kiss_fftnd(cfg, img, spectrum); // Compute magnitude at some points printf("Magnitude spectrum samples:\n"); for (int i = 0; i < 256; i += 64) { for (int j = 0; j < 256; j += 64) { int idx = i * 256 + j; float mag = sqrt(spectrum[idx].r * spectrum[idx].r + spectrum[idx].i * spectrum[idx].i); printf("(%d,%d): %.4f ", i, j, mag); } printf("\n"); } free(img); free(spectrum); free(cfg); return 0; } ``` -------------------------------- ### Declaring PKGINCLUDEDIR Source: https://github.com/mborgerding/kissfft/blob/master/CMakeLists.txt Sets the installation directory for public include files, prefixing the standard include directory with 'kissfft'. This organizes the header files. ```cmake set(PKGINCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/kissfft") message(STATUS "PKGINCLUDEDIR is ${PKGINCLUDEDIR}") ``` -------------------------------- ### Piping Data with fftutil Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/tools.md Illustrates how to use fftutil with standard input and output streams using the '-' filename. This allows chaining commands, for example, generating data, processing it with fftutil, and then analyzing the results. ```bash generate_data | fftutil -n 1024 - - | analyze_spectrum ``` -------------------------------- ### Real-time Signal Processing Pipeline with FFT and Fast Convolution Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/tools.md Demonstrates real-time signal processing pipelines using bash scripting. Includes examples for audio analysis with FFT and applying a fast convolution filter. ```bash #!/bin/bash # Record audio and pipe through FFT analysis ffmpeg -f alsa -i default -f f32le - 2>/dev/null | \ fftutil -R -n 2048 - spectrum.bin | \ some_analysis_tool spectrum.bin # Fast convolution filter sox input.wav -t raw -r 44100 -b 32 -e float - | \ fastconv -F 512 filter_coeffs.bin - - | \ sox -t raw -r 44100 -b 32 -e float - output.wav ``` -------------------------------- ### Thread-safe FFT Usage Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/README.md Demonstrates thread-safe usage of KissFFT configurations. Multiple threads can safely use the same configuration object for reading FFT operations. Configuration creation and destruction require synchronization. ```c #include #include "kiss_fft.h" kiss_fft_cfg cfg; // Shared config void* worker_thread(void *arg) { kiss_fft_cpx *in = malloc(...); kiss_fft_cpx *out = malloc(...); // Safe: multiple threads can use same cfg kiss_fft(cfg, in, out); free(in); free(out); return NULL; } int main() { cfg = kiss_fft_alloc(1024, 0, NULL, NULL); pthread_t tid; pthread_create(&tid, NULL, worker_thread, NULL); pthread_create(&tid, NULL, worker_thread, NULL); pthread_join(tid, NULL); pthread_join(tid, NULL); free(cfg); return 0; } ``` -------------------------------- ### KissFFT Build with CMake Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/README.md Builds KissFFT using CMake, configuring the build directory, specifying data types, and then installing the library. This is a standard approach for cross-platform C/C++ projects. ```bash mkdir build && cd build cmake -DKISSFFT_DATATYPE=float .. make make install ``` -------------------------------- ### Example Usage of TOSTRING Macro Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Illustrates how TOSTRING macro expands a preprocessor token like __LINE__ into its string representation, useful for embedding line numbers in logs. ```c // STRINGIFY(__LINE__) would only stringify the token name // TOSTRING(__LINE__) expands __LINE__ first, then stringifies the value const char *line_str = TOSTRING(__LINE__); // Results in "42" if on line 42 ``` -------------------------------- ### KFC Cache Performance: Slowdown with Many Sizes Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kfc.md This example demonstrates a scenario where using KFC's automatic caching leads to performance issues due to a large number of distinct FFT sizes being processed. It highlights the growing cache search time. ```c #include "kfc.h" int main() { for (int size = 100; size <= 1000; size += 10) { // Creates and searches growing cache... kfc_fft(size, in, out); } kfc_cleanup(); } ``` -------------------------------- ### Build Float FFT Static Library with CMake Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Configure a float FFT static library build without tools using CMake. Set data type, static linking, tool exclusion, and installation prefix. ```bash # CMake mkdir build && cd build cmake -DKISSFFT_DATATYPE=float -DKISSFFT_STATIC=ON -DKISSFFT_TOOLS=OFF \ -DCMAKE_INSTALL_PREFIX=/usr/local .. make all make install ``` -------------------------------- ### Build kissfft Tools with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/tools.md Demonstrates how to build the kissfft tools using the Make build system. Options include building with tools, without tools, or building only the tools. ```bash # Build with tools (default) make KISSFFT_TOOLS=1 all # Build without tools make KISSFFT_TOOLS=0 all # Build only tools make -C tools all ``` -------------------------------- ### Including GNUInstallDirs Source: https://github.com/mborgerding/kissfft/blob/master/CMakeLists.txt Includes the GNUInstallDirs module to provide standard installation directory variables, ensuring consistent installation paths across different systems. ```cmake include(GNUInstallDirs) ``` -------------------------------- ### Build KISS FFT with Make Source: https://github.com/mborgerding/kissfft/blob/master/README.md Commands to configure and build the library using the Make build system. ```bash make KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 all ``` ```bash make PREFIX=/tmp/1234 KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 install ``` -------------------------------- ### Correct Usage of kiss_fft and kiss_fftr Configuration Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/errors.md Demonstrates the correct way to allocate and use configuration structures for `kiss_fft` and `kiss_fftr` to avoid undefined behavior. It highlights the distinction between `kiss_fft_cfg` and `kiss_fftr_cfg`. ```c // WRONG kiss_fftr_cfg cfg_real = kiss_fftr_alloc(256, 0, NULL, NULL); kiss_fft_cpx in[256], out[256]; kiss_fft(cfg_real, in, out); // UB: cfg_real is wrong type // RIGHT kiss_fft_cfg cfg = kiss_fft_alloc(256, 0, NULL, NULL); kiss_fft(cfg, in, out); // OR kiss_fftr_cfg cfg_real = kiss_fftr_alloc(256, 0, NULL, NULL); kiss_fft_scalar timedata[256]; kiss_fft_cpx freqdata[256/2 + 1]; kiss_fftr(cfg_real, timedata, freqdata); ``` -------------------------------- ### Build and Test KissFFT with Make Source: https://github.com/mborgerding/kissfft/blob/master/README.md Run this command to validate the build configuration when using Make. Ensure KISSFFT_DATATYPE, KISSFFT_STATIC, and KISSFFT_OPENMP are set as needed. ```bash make KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 testsingle ``` -------------------------------- ### Forward-Inverse FFT Round-Trip Example Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kfc.md Demonstrates a forward and inverse FFT round-trip. The `kfc_fft` function caches its configuration, and `kfc_ifft` caches its own. The reconstructed signal is scaled by `nfft`. ```c #include "kfc.h" #include #include #include int main() { int nfft = 256; kiss_fft_cpx *original = (kiss_fft_cpx*)malloc(nfft * sizeof(kiss_fft_cpx)); kiss_fft_cpx *spectrum = (kiss_fft_cpx*)malloc(nfft * sizeof(kiss_fft_cpx)); kiss_fft_cpx *reconstructed = (kiss_fft_cpx*)malloc(nfft * sizeof(kiss_fft_cpx)); // Create original signal for (int i = 0; i < nfft; i++) { original[i].r = (kiss_fft_scalar)sin(2 * M_PI * 5 * i / nfft); original[i].i = 0.0f; } // Forward FFT (caches forward config) kfc_fft(nfft, original, spectrum); // Inverse FFT (caches inverse config) kfc_ifft(nfft, spectrum, reconstructed); // Note: reconstructed is scaled by nfft printf("Original[0]: %.6f\n", original[0].r); printf("Reconstructed[0]: %.6f\n", reconstructed[0].r); printf("Reconstructed[0] / nfft: %.6f\n", reconstructed[0].r / nfft); free(original); free(spectrum); free(reconstructed); kfc_cleanup(); return 0; } ``` -------------------------------- ### Example Usage of KISS_FFT_ERROR Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft_log.md Illustrates logging error messages for invalid FFT sizes or memory allocation failures. The output format includes [ERROR], filename, and line number. ```c #include "kiss_fft_log.h" if (nfft <= 0) { KISS_FFT_ERROR("FFT size must be positive, got %d", nfft); } if (!cfg) { KISS_FFT_ERROR("Memory allocation failed for size %d", nfft); } ``` -------------------------------- ### Build Fixed-Point FFT with OpenMP and Tools using Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Build a fixed-point FFT (int16_t) with OpenMP support and tools enabled using Make. Specify data type, OpenMP, and tool inclusion. ```bash # Make make KISSFFT_DATATYPE=int16_t KISSFFT_OPENMP=1 KISSFFT_TOOLS=1 all ``` -------------------------------- ### Build KISS FFT with CMake Source: https://github.com/mborgerding/kissfft/blob/master/README.md Commands to configure and build the library using the CMake build system. ```bash mkdir build && cd build cmake -DKISSFFT_DATATYPE=int16_t -DKISSFFT_STATIC=ON -DKISSFFT_OPENMP=ON .. make all ``` ```bash mkdir build && cd build cmake -DCMAKE_INSTALL_PREFIX=/tmp/1234 -DKISSFFT_DATATYPE=int16_t -DKISSFFT_STATIC=ON -DKISSFFT_OPENMP=ON .. make all make install ``` -------------------------------- ### Build Float FFT Static Library with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Build a float FFT as a static library without tools using Make. Specify data type, static linking, and tool exclusion. ```bash # Make make KISSFFT_DATATYPE=float KISSFFT_STATIC=1 KISSFFT_TOOLS=0 PREFIX=/usr/local all make install ``` -------------------------------- ### Querying and Using User-Supplied Buffer for FFT Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/errors.md Allocate a buffer of the required size by first calling kiss_fft_alloc with NULL buffer pointers. Then, create the FFT configuration within the allocated buffer. ```c kiss_fft_cfg cfg; size_t bufsize = 0; // Query required size kiss_fft_alloc(nfft, 0, NULL, &bufsize); printf("Need %zu bytes\n", bufsize); // Allocate void *buf = malloc(bufsize); // Create FFT config in provided buffer cfg = kiss_fft_alloc(nfft, 0, buf, &bufsize); if (!cfg) { fprintf(stderr, "FFT allocation in buffer failed\n"); free(buf); return 1; } // ... use cfg ... free(buf); // buf is freed, which also deallocates cfg ``` -------------------------------- ### Basic 1D Spectrum Visualization Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/tools.md Visualizes a 1D audio spectrum by computing the FFT, converting it to a power spectrum in dB, and then generating a PNG image. Requires fftutil, Python, and psdpng. ```bash # 1. Compute FFT fftutil -n 1024 audio.bin spectrum.bin # 2. Convert to power spectrum (Python) python3 <<'EOF' import struct import math spectrum = open('spectrum.bin', 'rb').read() psd = [] for i in range(0, len(spectrum), 8): r, im = struct.unpack('ff', spectrum[i:i+8]) power = (r*r + im*im) / 1024 # Normalize power_db = 10 * math.log10(max(power, 1e-10)) psd.append(struct.pack('f', power_db)) open('psd.bin', 'wb').write(b''.join(psd)) EOF # 3. Create PNG psdpng -w 512 -h 256 psd.bin spectrum.png ``` -------------------------------- ### Build SIMD Optimized FFT with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Build a SIMD optimized FFT using Make. Specify the data type as 'simd' and static linking. ```bash # Make make KISSFFT_DATATYPE=simd KISSFFT_STATIC=1 all ``` -------------------------------- ### Build Fixed-Point FFT with OpenMP and Tools using CMake Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Configure a fixed-point FFT (int16_t) build with OpenMP and tools using CMake. Set data type, OpenMP, and tool inclusion. ```bash # CMake mkdir build && cd build cmake -DKISSFFT_DATATYPE=int16_t -DKISSFFT_OPENMP=ON -DKISSFFT_TOOLS=ON .. make all ``` -------------------------------- ### Manual 2D FFT vs. kiss_fftnd Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fftnd.md Compares the manual implementation of a 2D FFT using two passes of 1D FFTs with the simplified approach using kiss_fftnd. The manual method involves applying 1D FFTs row-wise, then column-wise, requiring more complex memory management and configuration. The kiss_fftnd approach is more concise and potentially faster due to optimized cache locality. ```c #include "kiss_fft.h" #include "kiss_fftnd.h" #include #include void manual_2d_fft(int M, int N, kiss_fft_cpx *in, kiss_fft_cpx *out) { // Step 1: Apply 1D FFT to each row kiss_fft_cfg cfg_rows = kiss_fft_alloc(N, 0, NULL, NULL); kiss_fft_cpx *temp = (kiss_fft_cpx*)malloc(M * N * sizeof(kiss_fft_cpx)); for (int i = 0; i < M; i++) { kiss_fft(cfg_rows, &in[i * N], &temp[i * N]); } // Step 2: Transpose conceptually and apply 1D FFT to each column kiss_fft_cfg cfg_cols = kiss_fft_alloc(M, 0, NULL, NULL); kiss_fft_cpx *col = (kiss_fft_cpx*)malloc(M * sizeof(kiss_fft_cpx)); for (int j = 0; j < N; j++) { // Extract column j for (int i = 0; i < M; i++) { col[i] = temp[i * N + j]; } // FFT the column kiss_fft_cpx *out_col = (kiss_fft_cpx*)malloc(M * sizeof(kiss_fft_cpx)); kiss_fft(cfg_cols, col, out_col); // Place back for (int i = 0; i < M; i++) { out[i * N + j] = out_col[i]; } free(out_col); } free(col); free(temp); free(cfg_rows); free(cfg_cols); } void easy_2d_fft(int M, int N, kiss_fft_cpx *in, kiss_fft_cpx *out) { int dims[] = {M, N}; kiss_fftnd_cfg cfg = kiss_fftnd_alloc(dims, 2, 0, NULL, NULL); kiss_fftnd(cfg, in, out); free(cfg); } ``` -------------------------------- ### Build kissfft Tools with CMake Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/tools.md Shows how to build the kissfft tools using CMake. The KISSFFT_TOOLS option controls whether the tools are included in the build. ```bash # Build with tools (default) cmake -DKISSFFT_TOOLS=ON .. make # Build without tools cmake -DKISSFFT_TOOLS=OFF .. make ``` -------------------------------- ### Build Command-Line Tools with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Control the build of command-line tools like fastconv and psdpng using the KISSFFT_TOOLS Make variable. Set to 1 to build tools (default), or 0 to skip them. ```bash make KISSFFT_TOOLS=1 all # Build (default) make KISSFFT_TOOLS=0 all # Skip tools ``` -------------------------------- ### Build Static Library with Make Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Build a static library instead of a shared library by setting KISSFFT_STATIC=1 in Make. This is useful for embedding KISS FFT in static executables and avoiding runtime dependencies. ```bash make KISSFFT_STATIC=1 all ``` -------------------------------- ### Build and Test KissFFT with CMake Source: https://github.com/mborgerding/kissfft/blob/master/README.md Execute this command to test the build when using CMake. This is a simpler command compared to the Make-based testing. ```bash make test ``` -------------------------------- ### Using Correct Configuration Direction for FFT/IFFT Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/errors.md Illustrates how to correctly allocate and use FFT configurations for forward and inverse transforms. Using a forward configuration for an inverse transform (or vice versa) results in incorrect FFT output or corrupted data. ```c // WRONG kiss_fft_cfg cfg = kiss_fft_alloc(256, 0, NULL, NULL); // Forward kiss_fft(cfg, spectrum, timedata); // Attempted inverse - produces forward FFT // RIGHT: Use separate configs or same config with inverse_fft=1 kiss_fft_cfg cfg_inv = kiss_fft_alloc(256, 1, NULL, NULL); // Inverse kiss_fft(cfg_inv, spectrum, timedata); // OR: Real FFT with wrong direction kiss_fftr_cfg cfg = kiss_fftr_alloc(256, 0, NULL, NULL); // Forward real kiss_fftri(cfg, spectrum, timedata); // WRONG: cfg is forward, fftri expects inverse ``` ```c // Create separate configs for clarity kiss_fft_cfg cfg_fft = kiss_fft_alloc(NFFT, 0, NULL, NULL); kiss_fft_cfg cfg_ifft = kiss_fft_alloc(NFFT, 1, NULL, NULL); // Or use naming convention kiss_fft_cfg cfg_0_forward = ... kiss_fft_cfg cfg_1_inverse = ... ``` -------------------------------- ### Allocate kiss_fft Configuration Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/api-reference/kiss_fft.md Allocates and initializes an FFT configuration for a given FFT size. Use this function to prepare for FFT operations. Memory must be freed using kiss_fft_free or free. ```c kiss_fft_cfg kiss_fft_alloc(int nfft, int inverse_fft, void *mem, size_t *lenmem); ``` ```c #include "kiss_fft.h" #include int main() { int nfft = 1024; kiss_fft_cfg cfg = kiss_fft_alloc(nfft, 0, NULL, NULL); if (!cfg) { fprintf(stderr, "FFT alloc failed\n"); return 1; } kiss_fft_cpx *in = (kiss_fft_cpx*)malloc(nfft * sizeof(kiss_fft_cpx)); kiss_fft_cpx *out = (kiss_fft_cpx*)malloc(nfft * sizeof(kiss_fft_cpx)); // Fill in with input data... for (int i = 0; i < nfft; i++) { in[i].r = 0.0f; in[i].i = 0.0f; } // Perform FFT kiss_fft(cfg, in, out); // Clean up free(in); free(out); free(cfg); return 0; } ``` -------------------------------- ### Simple KissFFT Compilation Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/README.md Compiles the KissFFT source file into an object file and then links it with your application. Assumes kiss_fft.c is in the current directory. ```bash gcc -c kiss_fft.c gcc myapp.c kiss_fft.o -lm -o myapp ``` -------------------------------- ### Allocate KissFFT Configuration (Automatic) Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/README.md Allocates memory for the FFT configuration automatically. This is the recommended approach. Remember to free the configuration when done. ```c kiss_fft_cfg cfg = kiss_fft_alloc(nfft, 0, NULL, NULL); // ... use cfg ... free(cfg); ``` -------------------------------- ### 2D Image FFT Visualization Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/tools.md Visualizes a 2D image spectrum by computing the FFT, converting the magnitude to a power value, and generating a PNG. Requires fftutil, Python, and psdpng. ```bash # 1. Compute 2D FFT fftutil -n 256,256 image.bin spectrum_2d.bin # 2. Compute power and convert to PNG python3 <<'EOF' import struct import math spectrum = open('spectrum_2d.bin', 'rb').read() psd = [] for i in range(0, len(spectrum), 8): r, im = struct.unpack('ff', spectrum[i:i+8]) power = math.sqrt(r*r + im*im) / (256*256) psd.append(struct.pack('f', power)) open('psd.bin', 'wb').write(b''.join(psd)) EOF # 3. Create visualization psdpng -w 256 -h 256 psd.bin spectrum_viz.png ``` -------------------------------- ### Manual Compilation of kissfft Tools Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/tools.md Provides manual compilation commands for individual kissfft tools using GCC. Dependencies like libpng are noted. ```bash # Compile fftutil gcc -o fftutil tools/fftutil.c kiss_fft.c kiss_fftr.c kiss_fftnd.c kiss_fftndr.c -lm # Compile fastconv gcc -o fastconv tools/kiss_fastfir.c kiss_fft.c kiss_fftr.c -lm # Compile psdpng (requires libpng) gcc -o psdpng tools/psdpng.c kiss_fft.c -lpng -lm ``` -------------------------------- ### Allocate KissFFT Configuration (User-provided buffer) Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/README.md Allocates memory for the FFT configuration using a user-provided buffer. First, query the required buffer size, then allocate it, and finally pass it to kiss_fft_alloc. Remember to free the user-provided buffer. ```c size_t bufsize = 0; kiss_fft_alloc(nfft, 0, NULL, &bufsize); // Query required size void *buf = malloc(bufsize); kiss_fft_cfg cfg = kiss_fft_alloc(nfft, 0, buf, &bufsize); // ... use cfg ... free(buf); ``` -------------------------------- ### Build Static Library with CMake Source: https://github.com/mborgerding/kissfft/blob/master/_autodocs/configuration.md Build a static library by setting the -DKISSFFT_STATIC=ON CMake option. This is the CMake equivalent to Make's KISSFFT_STATIC=1. ```bash cmake -DKISSFFT_STATIC=ON .. ```