├── .gitignore ├── .travis.yml ├── CHANGELOG ├── CMakeLists.txt ├── COPYING ├── LICENSES ├── BSD-3-Clause └── Unlicense ├── Makefile ├── README.md ├── README.simd ├── TIPS ├── _kiss_fft_guts.h ├── cmake └── JoinPaths.cmake ├── kfc.c ├── kfc.h ├── kiss_fft.c ├── kiss_fft.h ├── kiss_fft_log.h ├── kiss_fftnd.c ├── kiss_fftnd.h ├── kiss_fftndr.c ├── kiss_fftndr.h ├── kiss_fftr.c ├── kiss_fftr.h ├── kissfft-config.cmake.in ├── kissfft.hh ├── kissfft.pc.in ├── kissfft_i32.hh ├── test ├── CMakeLists.txt ├── Makefile ├── benchfftw.c ├── benchkiss.c ├── doit.c ├── kissfft-testsuite.sh ├── pstats.c ├── pstats.h ├── test_real.c ├── test_simd.c ├── testcpp.cc ├── testkiss.py └── twotonetest.c └── tools ├── CMakeLists.txt ├── Makefile ├── fftutil.c ├── kiss_fastfir.c └── psdpng.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.swp 3 | *.so 4 | *.so.* 5 | *.a 6 | *.dylib 7 | test/testcpp 8 | test/bm_fftw_double 9 | test/bm_fftw_float 10 | test/bm_fftw_int16_t 11 | test/bm_fftw_int32_t 12 | test/bm_fftw_simd 13 | test/bm_kiss_double 14 | test/bm_kiss_float 15 | test/bm_kiss_int16_t 16 | test/bm_kiss_int32_t 17 | test/bm_kiss_simd 18 | test/st_double 19 | test/st_float 20 | test/st_int16_t 21 | test/st_int32_t 22 | test/st_simd 23 | test/tkfc_double 24 | test/tkfc_float 25 | test/tkfc_int16_t 26 | test/tkfc_int32_t 27 | test/tkfc_simd 28 | test/tr_double 29 | test/tr_float 30 | test/tr_int16_t 31 | test/tr_int32_t 32 | test/tr_simd 33 | tools/fastconv_double 34 | tools/fastconv_float 35 | tools/fastconv_int16_t 36 | tools/fastconv_int32_t 37 | tools/fastconv_simd 38 | tools/fastconvr_double 39 | tools/fastconvr_float 40 | tools/fastconvr_int16_t 41 | tools/fastconvr_int32_t 42 | tools/fastconvr_simd 43 | tools/fft_double 44 | tools/fft_float 45 | tools/fft_int16_t 46 | tools/fft_int32_t 47 | tools/fft_simd 48 | test/test_simd 49 | build 50 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | python: 4 | - "3.7" 5 | 6 | dist: focal 7 | 8 | before_install: 9 | - sudo apt-get install -y libfftw3-dev 10 | 11 | addons: 12 | apt: 13 | update: true 14 | 15 | install: true 16 | 17 | jobs: 18 | include: 19 | - name: "build (make)" 20 | script: 21 | - make all 22 | - make testall 23 | - name: "build (cmake)" 24 | script: 25 | - mkdir build && cd build 26 | - cmake .. 27 | - make 28 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | 1.3.0 2012-07-18 2 | removed non-standard malloc.h from kiss_fft.h 3 | 4 | moved -lm to end of link line 5 | 6 | checked various return values 7 | 8 | converted python Numeric code to NumPy 9 | 10 | fixed test of int32_t on 64 bit OS 11 | 12 | added padding in a couple of places to allow SIMD alignment of structs 13 | 14 | 1.2.9 2010-05-27 15 | threadsafe ( including OpenMP ) 16 | 17 | first edition of kissfft.hh the C++ template fft engine 18 | 19 | 1.2.8 20 | Changed memory.h to string.h -- apparently more standard 21 | 22 | Added openmp extensions. This can have fairly linear speedups for larger FFT sizes. 23 | 24 | 1.2.7 25 | Shrank the real-fft memory footprint. Thanks to Galen Seitz. 26 | 27 | 1.2.6 (Nov 14, 2006) The "thanks to GenArts" release. 28 | Added multi-dimensional real-optimized FFT, see tools/kiss_fftndr 29 | Thanks go to GenArts, Inc. for sponsoring the development. 30 | 31 | 1.2.5 (June 27, 2006) The "release for no good reason" release. 32 | Changed some harmless code to make some compilers' warnings go away. 33 | Added some more digits to pi -- why not. 34 | Added kiss_fft_next_fast_size() function to help people decide how much to pad. 35 | Changed multidimensional test from 8 dimensions to only 3 to avoid testing 36 | problems with fixed point (sorry Buckaroo Banzai). 37 | 38 | 1.2.4 (Oct 27, 2005) The "oops, inverse fixed point real fft was borked" release. 39 | Fixed scaling bug for inverse fixed point real fft -- also fixed test code that should've been failing. 40 | Thanks to Jean-Marc Valin for bug report. 41 | 42 | Use sys/types.h for more portable types than short,int,long => int16_t,int32_t,int64_t 43 | If your system does not have these, you may need to define them -- but at least it breaks in a 44 | loud and easily fixable way -- unlike silently using the wrong size type. 45 | 46 | Hopefully tools/psdpng.c is fixed -- thanks to Steve Kellog for pointing out the weirdness. 47 | 48 | 1.2.3 (June 25, 2005) The "you want to use WHAT as a sample" release. 49 | Added ability to use 32 bit fixed point samples -- requires a 64 bit intermediate result, a la 'long long' 50 | 51 | Added ability to do 4 FFTs in parallel by using SSE SIMD instructions. This is accomplished by 52 | using the __m128 (vector of 4 floats) as kiss_fft_scalar. Define USE_SIMD to use this. 53 | 54 | I know, I know ... this is drifting a bit from the "kiss" principle, but the speed advantages 55 | make it worth it for some. Also recent gcc makes it SOO easy to use vectors of 4 floats like a POD type. 56 | 57 | 1.2.2 (May 6, 2005) The Matthew release 58 | Replaced fixed point division with multiply&shift. Thanks to Jean-Marc Valin for 59 | discussions regarding. Considerable speedup for fixed-point. 60 | 61 | Corrected overflow protection in real fft routines when using fixed point. 62 | Finder's Credit goes to Robert Oschler of robodance for pointing me at the bug. 63 | This also led to the CHECK_OVERFLOW_OP macro. 64 | 65 | 1.2.1 (April 4, 2004) 66 | compiles cleanly with just about every -W warning flag under the sun 67 | 68 | reorganized kiss_fft_state so it could be read-only/const. This may be useful for embedded systems 69 | that are willing to predeclare twiddle factors, factorization. 70 | 71 | Fixed C_MUL,S_MUL on 16-bit platforms. 72 | 73 | tmpbuf will only be allocated if input & output buffers are same 74 | scratchbuf will only be allocated for ffts that are not multiples of 2,3,5 75 | 76 | NOTE: The tmpbuf,scratchbuf changes may require synchronization code for multi-threaded apps. 77 | 78 | 79 | 1.2 (Feb 23, 2004) 80 | interface change -- cfg object is forward declaration of struct instead of void* 81 | This maintains type saftey and lets the compiler warn/error about stupid mistakes. 82 | (prompted by suggestion from Erik de Castro Lopo) 83 | 84 | small speed improvements 85 | 86 | added psdpng.c -- sample utility that will create png spectrum "waterfalls" from an input file 87 | ( not terribly useful yet) 88 | 89 | 1.1.1 (Feb 1, 2004 ) 90 | minor bug fix -- only affects odd rank, in-place, multi-dimensional FFTs 91 | 92 | 1.1 : (Jan 30,2004) 93 | split sample_code/ into test/ and tools/ 94 | 95 | Removed 2-D fft and added N-D fft (arbitrary) 96 | 97 | modified fftutil.c to allow multi-d FFTs 98 | 99 | Modified core fft routine to allow an input stride via kiss_fft_stride() 100 | (eased support of multi-D ffts) 101 | 102 | Added fast convolution filtering (FIR filtering using overlap-scrap method, with tail scrap) 103 | 104 | Add kfc.[ch]: the KISS FFT Cache. It takes care of allocs for you ( suggested by Oscar Lesta ). 105 | 106 | 1.0.1 (Dec 15, 2003) 107 | fixed bug that occurred when nfft==1. Thanks to Steven Johnson. 108 | 109 | 1.0 : (Dec 14, 2003) 110 | changed kiss_fft function from using a single buffer, to two buffers. 111 | If the same buffer pointer is supplied for both in and out, kiss will 112 | manage the buffer copies. 113 | 114 | added kiss_fft2d and kiss_fftr as separate source files (declarations in kiss_fft.h ) 115 | 116 | 0.4 :(Nov 4,2003) optimized for radix 2,3,4,5 117 | 118 | 0.3 :(Oct 28, 2003) woops, version 2 didn't actually factor out any radices other than 2. 119 | Thanks to Steven Johnson for finding this one. 120 | 121 | 0.2 :(Oct 27, 2003) added mixed radix, only radix 2,4 optimized versions 122 | 123 | 0.1 :(May 19 2003) initial release, radix 2 only 124 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Directory for easier includes 2 | # Anywhere you see include(...) you can check /cmake for that file 3 | set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) 4 | 5 | # 6 | # Extract version from Makefile 7 | # 8 | 9 | file(READ Makefile _MAKEFILE_CONTENTS) 10 | 11 | string(REGEX MATCH "KFVER_MAJOR = ([0-9]+)\n" KFVER_MAJOR_MATCH "${_MAKEFILE_CONTENTS}") 12 | if(NOT KFVER_MAJOR_MATCH) 13 | message(FATAL_ERROR "Cannot extract major (ABI) version from Makefile") 14 | endif() 15 | set(KFVER_MAJOR "${CMAKE_MATCH_1}") 16 | 17 | string(REGEX MATCH "KFVER_MINOR = ([0-9]+)\n" KFVER_MINOR_MATCH "${_MAKEFILE_CONTENTS}") 18 | if(NOT KFVER_MINOR_MATCH) 19 | message(FATAL_ERROR "Cannot extract minor version from Makefile") 20 | endif() 21 | set(KFVER_MINOR "${CMAKE_MATCH_1}") 22 | 23 | string(REGEX MATCH "KFVER_PATCH = ([0-9]+)\n" KFVER_PATCH_MATCH "${_MAKEFILE_CONTENTS}") 24 | if(NOT KFVER_PATCH_MATCH) 25 | message(FATAL_ERROR "Cannot extract patch version from Makefile") 26 | endif() 27 | set(KFVER_PATCH "${CMAKE_MATCH_1}") 28 | 29 | set(MAKEFILE_EXTRACTED_VERSION "${KFVER_MAJOR}.${KFVER_MINOR}.${KFVER_PATCH}") 30 | 31 | # 32 | # Declare CMake project 33 | # 34 | 35 | cmake_minimum_required(VERSION 3.6) 36 | project(kissfft VERSION "${MAKEFILE_EXTRACTED_VERSION}") 37 | 38 | # 39 | # CMake configuration options 40 | # 41 | 42 | # Principal datatype: double, float (default), int16_t, int32_t, simd 43 | 44 | set(KISSFFT_DATATYPE "float" CACHE STRING "Principal datatype of kissfft: double, float (default), int16_t, int32_t, simd") 45 | 46 | # Additional options 47 | 48 | option(KISSFFT_OPENMP "Build kissfft with OpenMP support" OFF) 49 | option(KISSFFT_PKGCONFIG "Build pkg-config files" ON) 50 | option(KISSFFT_STATIC "Build kissfft as static (ON) or shared library (OFF)" OFF) 51 | option(KISSFFT_TEST "Build kissfft tests" ON) 52 | option(KISSFFT_TOOLS "Build kissfft command-line tools" ON) 53 | option(KISSFFT_USE_ALLOCA "Use alloca instead of malloc" OFF) 54 | 55 | # 56 | # Validate datatype 57 | # 58 | 59 | if (NOT KISSFFT_DATATYPE MATCHES "^double$" AND 60 | NOT KISSFFT_DATATYPE MATCHES "^float$" AND 61 | NOT KISSFFT_DATATYPE MATCHES "^int16_t$" AND 62 | NOT KISSFFT_DATATYPE MATCHES "^int32_t$" AND 63 | NOT KISSFFT_DATATYPE MATCHES "^simd$") 64 | message(FATAL_ERROR "Incorrect value of KISSFFT_DATATYPE! It can be one of: double, float, int16_t, int32_t, simd") 65 | endif() 66 | 67 | # 68 | # Print principal datatype 69 | # 70 | 71 | message(STATUS "Building KissFFT with datatype: ${KISSFFT_DATATYPE}") 72 | set(KISSFFT_OUTPUT_NAME "kissfft-${KISSFFT_DATATYPE}") 73 | 74 | # 75 | # Validate KISSFFT_STATIC 76 | # 77 | 78 | if (BUILD_SHARED_LIBS AND KISSFFT_STATIC) 79 | message(FATAL_ERROR "Conflicting CMake configuration: -DBUILD_SHARED_LIBS=ON and -DKISSFFT_STATIC=ON") 80 | endif() 81 | 82 | # 83 | # Enable BUILD_SHARED_LIBS for shared library build before 84 | # kissfft library is declared 85 | # 86 | 87 | if (NOT KISSFFT_STATIC) 88 | set(BUILD_SHARED_LIBS ON) 89 | message(STATUS "Building shared library") 90 | else() 91 | message(STATUS "Building static library") 92 | endif() 93 | 94 | # 95 | # Detect C compiler and pass appropriate flags 96 | # 97 | 98 | if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang|AppleClang") 99 | add_compile_options(-ffast-math -fomit-frame-pointer 100 | -W -Wall -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings 101 | "$<$:-Wstrict-prototypes;-Wmissing-prototypes;-Wnested-externs;-Wbad-function-cast>") 102 | endif() 103 | 104 | # 105 | # Add GNUInstallDirs for GNU infrastructure before target)include_directories 106 | # 107 | 108 | if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$" AND NOT CMAKE_CROSSCOMPILING) 109 | include(GNUInstallDirs) 110 | endif() 111 | 112 | # 113 | # Declare PKGINCLUDEDIR for kissfft include path 114 | # 115 | 116 | set(PKGINCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/kissfft") 117 | message(STATUS "PKGINCLUDEDIR is ${PKGINCLUDEDIR}") 118 | 119 | # 120 | # Declare kissfft library ( libkissfft.a / libkissfft-${KISSFFT_DATATYPE}.so.${MAKEFILE_EXTRACTED_VERSION} ) 121 | # 122 | 123 | add_library(kissfft 124 | kiss_fft.c 125 | kfc.c 126 | kiss_fftnd.c 127 | kiss_fftndr.c 128 | kiss_fftr.c) 129 | 130 | target_include_directories(kissfft PUBLIC 131 | $ 132 | $) 133 | 134 | # 135 | # Set compile definitions based on datatype and additional support flags 136 | # 137 | 138 | set(KISSFFT_COMPILE_DEFINITIONS) 139 | 140 | # 141 | # double / float 142 | # 143 | 144 | if(KISSFFT_DATATYPE MATCHES "^float$" OR KISSFFT_DATATYPE MATCHES "^double$") 145 | list(APPEND KISSFFT_COMPILE_DEFINITIONS kiss_fft_scalar=${KISSFFT_DATATYPE}) 146 | else() 147 | 148 | # 149 | # int16_t 150 | # 151 | 152 | if(KISSFFT_DATATYPE MATCHES "^int16_t$") 153 | list(APPEND KISSFFT_COMPILE_DEFINITIONS FIXED_POINT=16) 154 | else() 155 | 156 | # 157 | # int32_t 158 | # 159 | 160 | if(KISSFFT_DATATYPE MATCHES "^int32_t$") 161 | list(APPEND KISSFFT_COMPILE_DEFINITIONS FIXED_POINT=32) 162 | else() 163 | 164 | # 165 | # simd 166 | # 167 | 168 | if(KISSFFT_DATATYPE MATCHES "^simd$") 169 | list(APPEND KISSFFT_COMPILE_DEFINITIONS USE_SIMD) 170 | if (NOT MSVC) 171 | target_compile_options(kissfft PRIVATE -msse) 172 | else() 173 | target_compile_options(kissfft PRIVATE "/arch:SSE") 174 | endif() 175 | endif() 176 | endif() 177 | endif() 178 | endif() 179 | 180 | # 181 | # OpenMP support 182 | # 183 | 184 | if(KISSFFT_OPENMP) 185 | if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang|AppleClang") 186 | if (NOT MSVC) 187 | target_compile_options(kissfft PRIVATE -fopenmp) 188 | if(${CMAKE_VERSION} VERSION_LESS "3.13.0") 189 | target_link_libraries(kissfft PRIVATE "-fopenmp") 190 | else() 191 | target_link_options(kissfft PRIVATE -fopenmp) 192 | endif() 193 | else() 194 | target_compile_options(kissfft PRIVATE "/openmp") 195 | if(${CMAKE_VERSION} VERSION_LESS "3.13.0") 196 | target_link_libraries(kissfft PRIVATE "/openmp") 197 | else() 198 | target_link_options(kissfft PRIVATE "/openmp") 199 | endif() 200 | endif() 201 | set(KISSFFT_EXPORT_SUFFIX "-openmp") 202 | set(KISSFFT_OUTPUT_NAME "kissfft-${KISSFFT_DATATYPE}-openmp") 203 | else() 204 | message(FATAL_ERROR "Don't know how to enable OpenMP for this compiler") 205 | endif() 206 | endif() 207 | 208 | # 209 | # Shared / static library 210 | # 211 | 212 | if(NOT KISSFFT_STATIC) 213 | list(APPEND KISSFFT_COMPILE_DEFINITIONS KISS_FFT_SHARED) 214 | set_target_properties(kissfft PROPERTIES 215 | C_VISIBILITY_PRESET hidden) 216 | set(KISSFFT_EXPORT_SUFFIX "${KISSFFT_EXPORT_SUFFIX}-shared") 217 | else() 218 | set(KISSFFT_EXPORT_SUFFIX "${KISSFFT_EXPORT_SUFFIX}-static") 219 | endif() 220 | 221 | # 222 | # Alloca support 223 | # 224 | 225 | if(KISSFFT_USE_ALLOCA) 226 | list(APPEND KISSFFT_COMPILE_DEFINITIONS KISS_FFT_USE_ALLOCA) 227 | endif() 228 | 229 | # Set library name, version, soversion and aliases 230 | 231 | target_compile_definitions(kissfft PUBLIC ${KISSFFT_COMPILE_DEFINITIONS}) 232 | set_target_properties(kissfft PROPERTIES 233 | OUTPUT_NAME "${KISSFFT_OUTPUT_NAME}" 234 | DEFINE_SYMBOL KISS_FFT_BUILD 235 | EXPORT_NAME "${KISSFFT_OUTPUT_NAME}" 236 | VERSION ${PROJECT_VERSION} 237 | SOVERSION ${KFVER_MAJOR}) 238 | add_library(kissfft::kissfft ALIAS kissfft) 239 | add_library(kissfft::kissfft-${KISSFFT_DATATYPE} ALIAS kissfft) 240 | 241 | # 242 | # Build with libm (-lm) on Linux and kFreeBSD 243 | # 244 | 245 | if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$" AND NOT CMAKE_CROSSCOMPILING) 246 | target_link_libraries(kissfft PRIVATE m) 247 | endif() 248 | 249 | # 250 | # Define a helper function to define executable file 251 | # 252 | 253 | function(add_kissfft_executable NAME) 254 | add_executable(${NAME} ${ARGN}) 255 | target_link_libraries(${NAME} PRIVATE kissfft::kissfft) 256 | 257 | # 258 | # Build with libm (-lm) on Linux and kFreeBSD 259 | # 260 | 261 | if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$" AND NOT CMAKE_CROSSCOMPILING) 262 | target_link_libraries(${NAME} PRIVATE m) 263 | endif() 264 | 265 | if (NOT KISSFFT_OPENMP) 266 | set_target_properties(${NAME} PROPERTIES 267 | OUTPUT_NAME "${NAME}-${KISSFFT_DATATYPE}") 268 | else() 269 | if (NOT MSVC) 270 | target_compile_options(${NAME} PRIVATE -fopenmp) 271 | if(${CMAKE_VERSION} VERSION_LESS "3.13.0") 272 | target_link_libraries(${NAME} PRIVATE "-fopenmp") 273 | else() 274 | target_link_options(${NAME} PRIVATE -fopenmp) 275 | endif() 276 | else() 277 | target_compile_options(${NAME} PRIVATE "/openmp") 278 | if(${CMAKE_VERSION} VERSION_LESS "3.13.0") 279 | target_link_libraries(${NAME} PRIVATE "/openmp") 280 | else() 281 | target_link_options(${NAME} PRIVATE "/openmp") 282 | endif() 283 | endif() 284 | set_target_properties(${NAME} PROPERTIES 285 | OUTPUT_NAME "${NAME}-${KISSFFT_DATATYPE}-openmp") 286 | endif() 287 | endfunction() 288 | 289 | # 290 | # Perform installation of kissfft library and development files 291 | # 292 | 293 | install(TARGETS kissfft EXPORT kissfft 294 | ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 295 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 296 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") 297 | 298 | install(FILES kiss_fft.h 299 | kissfft.hh 300 | kiss_fftnd.h 301 | kiss_fftndr.h 302 | kiss_fftr.h 303 | DESTINATION "${PKGINCLUDEDIR}") 304 | 305 | set(KISSFFT_INSTALL_CMAKE "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 306 | CACHE FILEPATH "Install destination of kissfft cmake modules") 307 | mark_as_advanced(KISSFFT_INSTALL_CMAKE) 308 | 309 | install(EXPORT kissfft DESTINATION "${KISSFFT_INSTALL_CMAKE}" 310 | NAMESPACE "kissfft::" 311 | FILE "${PROJECT_NAME}-${KISSFFT_DATATYPE}${KISSFFT_EXPORT_SUFFIX}-targets.cmake") 312 | include(CMakePackageConfigHelpers) 313 | configure_package_config_file(kissfft-config.cmake.in kissfft-config.cmake 314 | INSTALL_DESTINATION "${KISSFFT_INSTALL_CMAKE}") 315 | write_basic_package_version_file(kissfft-config-version.cmake COMPATIBILITY AnyNewerVersion) 316 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/kissfft-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/kissfft-config-version.cmake" 317 | DESTINATION "${KISSFFT_INSTALL_CMAKE}") 318 | 319 | set(PKG_KISSFFT_DEFS) 320 | foreach(_def ${KISSFFT_COMPILE_DEFINITIONS}) 321 | set(PKG_KISSFFT_DEFS "${PKG_KISSFFT_DEFS} -D${_def}") 322 | endforeach() 323 | if (KISSFFT_PKGCONFIG) 324 | include(JoinPaths) 325 | set(PKGCONFIG_KISSFFT_PKGINCLUDEDIR "\${includedir}/kissfft") 326 | set(PKGCONFIG_KISSFFT_PREFIX "${CMAKE_INSTALL_PREFIX}") 327 | set(PKGCONFIG_KISSFFT_VERSION "${kissfft_VERSION}") 328 | join_paths(PKGCONFIG_KISSFFT_LIBDIR "\${prefix}" "${CMAKE_INSTALL_LIBDIR}") 329 | join_paths(PKGCONFIG_KISSFFT_INCLUDEDIR "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") 330 | if(KISSFFT_DATATYPE MATCHES "^simd$") 331 | list(APPEND KISSFFT_COMPILE_DEFINITIONS USE_SIMD) 332 | if (NOT MSVC) 333 | set(PKG_KISSFFT_DEFS "${PKG_KISSFFT_DEFS} -msse") 334 | else() 335 | set(PKG_KISSFFT_DEFS "${PKG_KISSFFT_DEFS} /ARCH:SSE") 336 | endif() 337 | endif() 338 | if (NOT KISSFFT_OPENMP) 339 | configure_file(kissfft.pc.in "kissfft-${KISSFFT_DATATYPE}.pc" @ONLY) 340 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/kissfft-${KISSFFT_DATATYPE}.pc" 341 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 342 | else() 343 | if (NOT MSVC) 344 | set(PKG_OPENMP "-fopenmp") 345 | set(PKG_KISSFFT_DEFS "${PKG_KISSFFT_DEFS} -fopenmp") 346 | else() 347 | set(PKG_KISSFFT_DEFS "${PKG_KISSFFT_DEFS} /openmp") 348 | set(PKG_OPENMP "/openmp") 349 | endif() 350 | configure_file(kissfft.pc.in "kissfft-${KISSFFT_DATATYPE}-openmp.pc" @ONLY) 351 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/kissfft-${KISSFFT_DATATYPE}-openmp.pc" 352 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 353 | endif() 354 | endif() 355 | 356 | # 357 | # Build and install tools if requested by user 358 | # 359 | 360 | if(KISSFFT_TOOLS) 361 | add_subdirectory(tools) 362 | endif() 363 | 364 | # 365 | # Build and run tests if requested by user 366 | # 367 | 368 | if(KISSFFT_TEST) 369 | enable_testing() 370 | add_subdirectory(test) 371 | endif() 372 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2003-2010 Mark Borgerding . All rights reserved. 2 | 3 | KISS FFT is provided under: 4 | 5 | SPDX-License-Identifier: BSD-3-Clause 6 | 7 | Being under the terms of the BSD 3-clause "New" or "Revised" License, 8 | according with: 9 | 10 | LICENSES/BSD-3-Clause 11 | 12 | -------------------------------------------------------------------------------- /LICENSES/BSD-3-Clause: -------------------------------------------------------------------------------- 1 | Valid-License-Identifier: BSD-3-Clause 2 | SPDX-URL: https://spdx.org/licenses/BSD-3-Clause.html 3 | Usage-Guide: 4 | To use the BSD 3-clause "New" or "Revised" License put the following SPDX 5 | tag/value pair into a comment according to the placement guidelines in 6 | the licensing rules documentation: 7 | SPDX-License-Identifier: BSD-3-Clause 8 | License-Text: 9 | 10 | Copyright (c) . All rights reserved. 11 | 12 | Redistribution and use in source and binary forms, with or without modification, 13 | are permitted provided that the following conditions are met: 14 | 15 | 1. Redistributions of source code must retain the above copyright notice, 16 | this list of conditions and the following disclaimer. 17 | 18 | 2. Redistributions in binary form must reproduce the above copyright notice, 19 | this list of conditions and the following disclaimer in the documentation 20 | and/or other materials provided with the distribution. 21 | 22 | 3. Neither the name of the copyright holder nor the names of its contributors 23 | may be used to endorse or promote products derived from this software without 24 | specific prior written permission. 25 | 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 30 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 32 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 33 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 34 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 35 | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | -------------------------------------------------------------------------------- /LICENSES/Unlicense: -------------------------------------------------------------------------------- 1 | Valid-License-Identifier: Unlicense 2 | SPDX-URL: https://spdx.org/licenses/Unlicense.html 3 | Usage-Guide: 4 | To use the Unlicense put the following SPDX tag/value pair into a 5 | comment according to the placement guidelines in the licensing rules 6 | documentation: 7 | SPDX-License-Identifier: Unlicense 8 | License-Text: 9 | 10 | This is free and unencumbered software released into the public domain. 11 | 12 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute 13 | this software, either in source code form or as a compiled binary, for any 14 | purpose, commercial or non-commercial, and by any means. 15 | 16 | In jurisdictions that recognize copyright laws, the author or authors of this 17 | software dedicate any and all copyright interest in the software to the public 18 | domain. We make this dedication for the benefit of the public at large and 19 | to the detriment of our heirs and successors. We intend this dedication to be 20 | an overt act of relinquishment in perpetuity of all present and future rights 21 | to this software under copyright law. 22 | 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 25 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 26 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 28 | THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | 30 | For more information, please refer to 31 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Semantic versioning 3 | # 4 | # KFVER_MAJOR denotes the ABI version. 5 | # 6 | # - It must be bumped only if API public members are removed or 7 | # changed in the incompatible 8 | # 9 | # KFVER_MINOR denotes the minor version within a compatible ABI. 10 | # 11 | # - It should be bumped if new API public members are added 12 | # (but not removed!) so programs linked against the same library 13 | # version continue operating properly 14 | # 15 | # KFVER_PATCH denotes bugfix count since the last minor update. 16 | # 17 | # - It should be bumped whenever a bug fix is pushed. 18 | # 19 | 20 | export KFVER_MAJOR = 131 21 | export KFVER_MINOR = 1 22 | export KFVER_PATCH = 0 23 | 24 | # 25 | # Data type (float / int16_t / int32_t / simd) 26 | # 27 | 28 | export KISSFFT_DATATYPE ?= float 29 | 30 | # 31 | # Default options 32 | # 33 | 34 | export KISSFFT_OPENMP ?= 0 35 | export KISSFFT_STATIC ?= 0 36 | export KISSFFT_TOOLS ?= 1 37 | export KISSFFT_USE_ALLOCA ?= 0 38 | 39 | # 40 | # Installation directories 41 | # 42 | 43 | PREFIX ?= /usr/local 44 | export ABS_PREFIX = $(abspath $(PREFIX)) 45 | 46 | BINDIR ?= $(ABS_PREFIX)/bin 47 | export ABS_BINDIR = $(abspath $(BINDIR)) 48 | 49 | INCLUDEDIR ?= $(ABS_PREFIX)/include 50 | export ABS_INCLUDEDIR = $(abspath $(INCLUDEDIR)) 51 | export ABS_PKGINCLUDEDIR = $(ABS_INCLUDEDIR)/kissfft 52 | 53 | # 54 | # Override LIBDIR with lib64 following CMake's 55 | # GNUInstallDirs logic: 56 | # 57 | 58 | CANDIDATE_LIBDIR_NAME = lib 59 | 60 | ifneq ($(MAKECMDGOALS),clean) 61 | ifeq ($(shell uname -s),Linux) 62 | _UNAME_ARCH = $(shell uname -i) 63 | 64 | ifeq (,$(_UNAME_ARCH)) 65 | _UNAME_ARCH = $(shell uname -m) 66 | 67 | ifeq (,$(_UNAME_ARCH)) 68 | $(warning WARNING: Can not detect system architecture!) 69 | endif 70 | endif 71 | 72 | ifeq ($(_UNAME_ARCH),x86_64) 73 | CANDIDATE_LIBDIR_NAME = lib64 74 | endif 75 | endif 76 | endif 77 | 78 | CANDIDATE_LIBDIR = $(PREFIX)/$(CANDIDATE_LIBDIR_NAME) 79 | LIBDIR ?= $(CANDIDATE_LIBDIR) 80 | 81 | export ABS_LIBDIR = $(abspath $(LIBDIR)) 82 | 83 | export INSTALL ?= install 84 | 85 | # 86 | # Library name and version 87 | # 88 | 89 | ifeq ($(KISSFFT_OPENMP), 1) 90 | KISSFFTLIB_SHORTNAME = kissfft-$(KISSFFT_DATATYPE)-openmp 91 | KISSFFT_PKGCONFIG = kissfft-$(KISSFFT_DATATYPE)-openmp.pc 92 | KISSFFTLIB_FLAGS = -fopenmp 93 | TYPEFLAGS = -fopenmp 94 | PKGCONFIG_OPENMP = -fopenmp 95 | else 96 | KISSFFTLIB_SHORTNAME = kissfft-$(KISSFFT_DATATYPE) 97 | KISSFFT_PKGCONFIG = kissfft-$(KISSFFT_DATATYPE).pc 98 | TYPEFLAGS = 99 | PKGCONFIG_OPENMP = 100 | endif 101 | 102 | ifeq ($(KISSFFT_STATIC), 1) 103 | KISSFFTLIB_NAME = lib$(KISSFFTLIB_SHORTNAME).a 104 | KISSFFTLIB_FLAGS += -static 105 | else ifeq ($(shell uname -s),Darwin) 106 | KISSFFTLIB_NAME = lib$(KISSFFTLIB_SHORTNAME).dylib 107 | KISSFFTLIB_FLAGS += -shared -Wl,-install_name,$(KISSFFTLIB_NAME) 108 | else 109 | KISSFFTLIB_SODEVELNAME = lib$(KISSFFTLIB_SHORTNAME).so 110 | KISSFFTLIB_SONAME = $(KISSFFTLIB_SODEVELNAME).$(KFVER_MAJOR) 111 | KISSFFTLIB_NAME = $(KISSFFTLIB_SONAME).$(KFVER_MINOR).$(KFVER_PATCH) 112 | KISSFFTLIB_FLAGS += -shared -Wl,-soname,$(KISSFFTLIB_SONAME) 113 | endif 114 | 115 | export KISSFFTLIB_SHORTNAME 116 | 117 | # 118 | # Compile-time definitions by datatype 119 | # 120 | # 121 | # Note: -DKISS_FFT_BUILD and -DKISS_FFT_SHARED control 122 | # C symbol visibility. 123 | # 124 | 125 | ifeq "$(KISSFFT_DATATYPE)" "int32_t" 126 | TYPEFLAGS += -DFIXED_POINT=32 127 | else ifeq "$(KISSFFT_DATATYPE)" "int16_t" 128 | TYPEFLAGS += -DFIXED_POINT=16 129 | else ifeq "$(KISSFFT_DATATYPE)" "simd" 130 | TYPEFLAGS += -DUSE_SIMD=1 -msse 131 | else ifeq "$(KISSFFT_DATATYPE)" "float" 132 | TYPEFLAGS += -Dkiss_fft_scalar=$(KISSFFT_DATATYPE) 133 | else ifeq "$(KISSFFT_DATATYPE)" "double" 134 | TYPEFLAGS += -Dkiss_fft_scalar=$(KISSFFT_DATATYPE) 135 | else 136 | $(error ERROR: KISSFFT_DATATYPE must be one of: float double int16_t int32_t simd) 137 | endif 138 | 139 | ifneq ($(KISSFFT_STATIC), 1) 140 | TYPEFLAGS += -DKISS_FFT_SHARED 141 | endif 142 | 143 | ifeq ($(KISSFFT_USE_ALLOCA), 1) 144 | TYPEFLAGS += -DKISS_FFT_USE_ALLOCA=1 145 | endif 146 | 147 | # 148 | # Compile-time definitions 149 | # 150 | 151 | # 152 | # Save pkgconfig variables before appending 153 | # -DKISS_FFT_BUILD to TYPEFLAGS 154 | # 155 | 156 | ifneq ($(shell uname -s),Darwin) 157 | PKGCONFIG_KISSFFT_VERSION = $(KFVER_MAJOR).$(KFVER_MINOR).$(KFVER_PATCH) 158 | PKGCONFIG_KISSFFT_OUTPUT_NAME = $(KISSFFTLIB_SHORTNAME) 159 | PKGCONFIG_PKG_KISSFFT_DEFS = $(TYPEFLAGS) 160 | PKGCONFIG_KISSFFT_PREFIX = $(ABS_PREFIX) 161 | ifeq ($(ABS_INCLUDEDIR),$(ABS_PREFIX)/include) 162 | PKGCONFIG_KISSFFT_INCLUDEDIR = $${prefix}/include 163 | else 164 | PKGCONFIG_KISSFFT_INCLUDEDIR = $(ABS_INCLUDEDIR) 165 | 166 | endif 167 | ifeq ($(ABS_LIBDIR),$(ABS_PREFIX)/$(CANDIDATE_LIBDIR_NAME)) 168 | PKGCONFIG_KISSFFT_LIBDIR = $${prefix}/$(CANDIDATE_LIBDIR_NAME) 169 | else 170 | PKGCONFIG_KISSFFT_LIBDIR = $(ABS_LIBDIR) 171 | endif 172 | PKGCONFIG_KISSFFT_PKGINCLUDEDIR = $${includedir}/kissfft 173 | endif 174 | 175 | export TYPEFLAGS 176 | 177 | # Compile .c into .o 178 | # 179 | 180 | # 181 | # -DKISS_FFT_BUILD is used for library artifacts, so 182 | # consumer executable in 'test' and 'tools' do _NOT_ 183 | # need it. pkg-config output does not need it either. 184 | # 185 | 186 | %.c.o: %.c 187 | $(CC) -Wall -fPIC \ 188 | -o $@ \ 189 | $(CFLAGS) $(TYPEFLAGS) -DKISS_FFT_BUILD \ 190 | -c $< 191 | 192 | # 193 | # Target: "make all" 194 | # 195 | 196 | all: kfc.c.o kiss_fft.c.o kiss_fftnd.c.o kiss_fftndr.c.o kiss_fftr.c.o 197 | ifneq ($(KISSFFT_STATIC), 1) 198 | $(CC) $(KISSFFTLIB_FLAGS) -o $(KISSFFTLIB_NAME) $^ 199 | ifneq ($(shell uname -s),Darwin) 200 | ln -sf $(KISSFFTLIB_NAME) $(KISSFFTLIB_SONAME) 201 | ln -sf $(KISSFFTLIB_NAME) $(KISSFFTLIB_SODEVELNAME) 202 | endif 203 | else 204 | $(AR) crus $(KISSFFTLIB_NAME) $^ 205 | endif 206 | ifneq ($(KISSFFT_TOOLS), 0) 207 | $(MAKE) -C tools CFLAGADD="$(CFLAGADD)" all 208 | endif 209 | 210 | # 211 | # Target: "make install" 212 | # 213 | 214 | install: all 215 | $(INSTALL) -Dt $(ABS_PKGINCLUDEDIR) -m 644 \ 216 | kiss_fft.h \ 217 | kissfft.hh \ 218 | kiss_fftnd.h \ 219 | kiss_fftndr.h \ 220 | kiss_fftr.h 221 | $(INSTALL) -Dt $(ABS_LIBDIR) -m 644 $(KISSFFTLIB_NAME) 222 | ifneq ($(KISSFFT_STATIC), 1) 223 | ifneq ($(shell uname -s),Darwin) 224 | cd $(LIBDIR) && \ 225 | ln -sf $(KISSFFTLIB_NAME) $(KISSFFTLIB_SONAME) && \ 226 | ln -sf $(KISSFFTLIB_NAME) $(KISSFFTLIB_SODEVELNAME) 227 | endif 228 | endif 229 | ifneq ($(shell uname -s),Darwin) 230 | mkdir "$(ABS_LIBDIR)/pkgconfig" 231 | sed \ 232 | -e 's+@PKGCONFIG_KISSFFT_VERSION@+$(PKGCONFIG_KISSFFT_VERSION)+' \ 233 | -e 's+@KISSFFT_OUTPUT_NAME@+$(PKGCONFIG_KISSFFT_OUTPUT_NAME)+' \ 234 | -e 's+@PKG_KISSFFT_DEFS@+$(PKGCONFIG_PKG_KISSFFT_DEFS)+' \ 235 | -e 's+@PKG_OPENMP@+$(PKGCONFIG_OPENMP)+' \ 236 | -e 's+@PKGCONFIG_KISSFFT_PREFIX@+$(PKGCONFIG_KISSFFT_PREFIX)+' \ 237 | -e 's+@PKGCONFIG_KISSFFT_INCLUDEDIR@+$(PKGCONFIG_KISSFFT_INCLUDEDIR)+' \ 238 | -e 's+@PKGCONFIG_KISSFFT_LIBDIR@+$(PKGCONFIG_KISSFFT_LIBDIR)+' \ 239 | -e 's+@PKGCONFIG_KISSFFT_PKGINCLUDEDIR@+$(PKGCONFIG_KISSFFT_PKGINCLUDEDIR)+' \ 240 | kissfft.pc.in 1>"$(ABS_LIBDIR)/pkgconfig/$(KISSFFT_PKGCONFIG)" 241 | endif 242 | ifneq ($(KISSFFT_TOOLS), 0) 243 | $(MAKE) -C tools install 244 | endif 245 | 246 | # 247 | # Target: "make doc" 248 | # 249 | 250 | doc: 251 | $(warning Start by reading the README file. If you want to build and test lots of stuff, do a 'make testall') 252 | $(warning but be aware that 'make testall' has dependencies that the basic kissfft software does not.) 253 | $(warning It is generally unneeded to run these tests yourself, unless you plan on changing the inner workings) 254 | $(warning of kissfft and would like to make use of its regression tests.) 255 | 256 | # 257 | # Target: "make testsingle" 258 | # 259 | 260 | testsingle: 261 | $(MAKE) clean 262 | $(MAKE) all 263 | $(MAKE) -C test CFLAGADD="$(CFLAGADD)" test testcpp 264 | 265 | # 266 | # Target: "make testall" 267 | # 268 | 269 | testall: 270 | # Shared libraries 271 | $(MAKE) KISSFFT_DATATYPE=double testsingle 272 | $(MAKE) KISSFFT_DATATYPE=float testsingle 273 | $(MAKE) KISSFFT_DATATYPE=int16_t testsingle 274 | # The simd and int32_t types may or may not work on your machine 275 | $(MAKE) KISSFFT_DATATYPE=int32_t testsingle 276 | $(MAKE) KISSFFT_DATATYPE=simd testsingle 277 | # Static libraries 278 | $(MAKE) KISSFFT_DATATYPE=double KISSFFT_STATIC=1 testsingle 279 | $(MAKE) KISSFFT_DATATYPE=float KISSFFT_STATIC=1 testsingle 280 | $(MAKE) KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 testsingle 281 | # The simd and int32_t types may or may not work on your machine 282 | $(MAKE) KISSFFT_DATATYPE=int32_t KISSFFT_STATIC=1 testsingle 283 | $(MAKE) KISSFFT_DATATYPE=simd KISSFFT_STATIC=1 testsingle 284 | # OpenMP libraries 285 | $(MAKE) KISSFFT_DATATYPE=double KISSFFT_OPENMP=1 testsingle 286 | $(MAKE) KISSFFT_DATATYPE=float KISSFFT_OPENMP=1 testsingle 287 | $(MAKE) KISSFFT_DATATYPE=int16_t KISSFFT_OPENMP=1 testsingle 288 | # The simd and int32_t types may or may not work on your machine 289 | $(MAKE) KISSFFT_DATATYPE=int32_t KISSFFT_OPENMP=1 testsingle 290 | $(MAKE) KISSFFT_DATATYPE=simd KISSFFT_OPENMP=1 testsingle 291 | $(warning All tests passed!) 292 | 293 | # 294 | # Target: "make tarball" 295 | # 296 | 297 | tarball: clean 298 | git archive --prefix=kissfft/ -o kissfft$(KFVER).tar.gz v$(KFVER) 299 | git archive --prefix=kissfft/ -o kissfft$(KFVER).zip v$(KFVER) 300 | 301 | # 302 | # Target: "make clean" 303 | # 304 | 305 | clean: 306 | rm -f *.o *.a *.so *.so.* 307 | cd test && $(MAKE) clean 308 | cd tools && $(MAKE) clean 309 | rm -f kiss_fft*.tar.gz *~ *.pyc kiss_fft*.zip 310 | 311 | # 312 | # Target: "make asm" 313 | # 314 | 315 | asm: kiss_fft.s 316 | 317 | # TODO: Sort out if we should add kfc / other C headers 318 | 319 | kiss_fft.s: kiss_fft.c kiss_fft.h _kiss_fft_guts.h 320 | [ -e kiss_fft.s ] && mv kiss_fft.s kiss_fft.s~ || true 321 | $(CC) -S kiss_fft.c -O3 -mtune=native -ffast-math -fomit-frame-pointer -unroll-loops -dA -fverbose-asm 322 | $(CC) -o kiss_fft_short.s -S kiss_fft.c -O3 -mtune=native -ffast-math -fomit-frame-pointer -dA -fverbose-asm -DFIXED_POINT 323 | [ -e kiss_fft.s~ ] && diff kiss_fft.s~ kiss_fft.s || true 324 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KISS FFT [![Build Status](https://travis-ci.com/mborgerding/kissfft.svg?branch=master)](https://travis-ci.com/mborgerding/kissfft) 2 | 3 | KISS FFT - A mixed-radix Fast Fourier Transform based up on the principle, 4 | "Keep It Simple, Stupid." 5 | 6 | There are many great fft libraries already around. Kiss FFT is not trying 7 | to be better than any of them. It only attempts to be a reasonably efficient, 8 | moderately useful FFT that can use fixed or floating data types and can be 9 | incorporated into someone's C program in a few minutes with trivial licensing. 10 | 11 | ## USAGE: 12 | 13 | The basic usage for 1-d complex FFT is: 14 | 15 | ```c 16 | #include "kiss_fft.h" 17 | kiss_fft_cfg cfg = kiss_fft_alloc( nfft ,is_inverse_fft ,0,0 ); 18 | while ... 19 | 20 | ... // put kth sample in cx_in[k].r and cx_in[k].i 21 | 22 | kiss_fft( cfg , cx_in , cx_out ); 23 | 24 | ... // transformed. DC is in cx_out[0].r and cx_out[0].i 25 | 26 | kiss_fft_free(cfg); 27 | ``` 28 | - **Note**: frequency-domain data is stored from dc up to 2pi. 29 | so cx_out[0] is the dc bin of the FFT 30 | and cx_out[nfft/2] is the Nyquist bin (if exists) 31 | 32 | Declarations are in "kiss_fft.h", along with a brief description of the 33 | functions you'll need to use. 34 | 35 | Code definitions for 1d complex FFTs are in kiss_fft.c. 36 | 37 | You can do other cool stuff with the extras you'll find in tools/ 38 | > - multi-dimensional FFTs 39 | > - real-optimized FFTs (returns the positive half-spectrum: 40 | (nfft/2+1) complex frequency bins) 41 | > - fast convolution FIR filtering (not available for fixed point) 42 | > - spectrum image creation 43 | 44 | The core fft and most tools/ code can be compiled to use float, double, 45 | Q15 short or Q31 samples. The default is float. 46 | 47 | ## BUILDING: 48 | 49 | There are two functionally-equivalent build systems supported by kissfft: 50 | 51 | - Make (traditional Makefiles for Unix / Linux systems) 52 | - CMake (more modern and feature-rich build system developed by Kitware) 53 | 54 | To build kissfft, the following build environment can be used: 55 | 56 | - GNU build environment with GCC, Clang and GNU Make or CMake (>= 3.6) 57 | - Microsoft Visual C++ (MSVC) with CMake (>= 3.6) 58 | 59 | Additional libraries required to build and test kissfft include: 60 | 61 | - libpng for psdpng tool, 62 | - libfftw3 to validate kissfft results against it, 63 | - python 2/3 with Numpy to validate kissfft results against it. 64 | - OpenMP supported by GCC, Clang or MSVC for multi-core FFT transformations 65 | 66 | Environments like Cygwin and MinGW can be highly likely used to build kissfft 67 | targeting Windows platform, but no tests were performed to the date. 68 | 69 | Both Make and CMake builds are easily configurable: 70 | 71 | - `KISSFFT_DATATYPE=` (for Make) or `-DKISSFFT_DATATYPE=` 72 | (for CMake) denote the principal datatype used by kissfft. It can be one 73 | of the following: 74 | 75 | - float (default) 76 | - double 77 | - int16_t 78 | - int32_t 79 | - SIMD (requires SSE instruction set support on target CPU) 80 | 81 | - `KISSFFT_OPENMP=1` (for Make) or `-DKISSFFT_OPENMP=ON` (for CMake) builds kissfft 82 | with OpenMP support. Please note that a supported compiler is required and this 83 | option is turned off by default. 84 | 85 | - `KISSFFT_STATIC=1` (for Make) or `-DKISSFFT_STATIC=ON` (for CMake) instructs 86 | the builder to create static library ('.lib' for Windows / '.a' for Unix or Linux). 87 | By default, this option is turned off and the shared library is created 88 | ('.dll' for Windows, '.so' for Linux or Unix, '.dylib' for Mac OSX) 89 | 90 | - `-DKISSFFT_TEST=OFF` (for CMake) disables building tests for kissfft. On Make, 91 | building tests is done separately by 'make testall' or 'make testsingle', so 92 | no specific setting is required. 93 | 94 | - `KISSFFT_TOOLS=0` (for Make) or `-DKISSFFT_TOOLS=OFF` (for CMake) builds kissfft 95 | without command-line tools like 'fastconv'. By default the tools are built. 96 | 97 | - `KISSFFT_USE_ALLOCA=1` (for Make) or `-DKISSFFT_USE_ALLOCA=ON` (for CMake) 98 | build kissfft with 'alloca' usage instead of 'malloc' / 'free'. 99 | 100 | - `PREFIX=/full/path/to/installation/prefix/directory` (for Make) or 101 | `-DCMAKE_INSTALL_PREFIX=/full/path/to/installation/prefix/directory` (for CMake) 102 | specifies the prefix directory to install kissfft into. 103 | 104 | For example, to build kissfft as a static library with 'int16_t' datatype and 105 | OpenMP support using Make, run the command from kissfft source tree: 106 | 107 | ``` 108 | make KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 all 109 | ``` 110 | 111 | The same configuration for CMake is: 112 | 113 | ``` 114 | mkdir build && cd build 115 | cmake -DKISSFFT_DATATYPE=int16_t -DKISSFFT_STATIC=ON -DKISSFFT_OPENMP=ON .. 116 | make all 117 | ``` 118 | 119 | To specify '/tmp/1234' as installation prefix directory, run: 120 | 121 | 122 | ``` 123 | make PREFIX=/tmp/1234 KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 install 124 | ``` 125 | 126 | or 127 | 128 | ``` 129 | mkdir build && cd build 130 | cmake -DCMAKE_INSTALL_PREFIX=/tmp/1234 -DKISSFFT_DATATYPE=int16_t -DKISSFFT_STATIC=ON -DKISSFFT_OPENMP=ON .. 131 | make all 132 | make install 133 | ``` 134 | 135 | ## TESTING: 136 | 137 | To validate the build configured as an example above, run the following command from 138 | kissfft source tree: 139 | 140 | ``` 141 | make KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 testsingle 142 | ``` 143 | 144 | if using Make, or: 145 | 146 | ``` 147 | make test 148 | ``` 149 | 150 | if using CMake. 151 | 152 | To test all possible build configurations, please run an extended testsuite from 153 | kissfft source tree: 154 | 155 | ``` 156 | sh test/kissfft-testsuite.sh 157 | ``` 158 | 159 | Please note that the extended testsuite takes around 20-40 minutes depending on device 160 | it runs on. This testsuite is useful for reporting bugs or testing the pull requests. 161 | 162 | ## BACKGROUND 163 | 164 | I started coding this because I couldn't find a fixed point FFT that didn't 165 | use assembly code. I started with floating point numbers so I could get the 166 | theory straight before working on fixed point issues. In the end, I had a 167 | little bit of code that could be recompiled easily to do ffts with short, float 168 | or double (other types should be easy too). 169 | 170 | Once I got my FFT working, I was curious about the speed compared to 171 | a well respected and highly optimized fft library. I don't want to criticize 172 | this great library, so let's call it FFT_BRANDX. 173 | During this process, I learned: 174 | 175 | > 1. FFT_BRANDX has more than 100K lines of code. The core of kiss_fft is about 500 lines (cpx 1-d). 176 | > 2. It took me an embarrassingly long time to get FFT_BRANDX working. 177 | > 3. A simple program using FFT_BRANDX is 522KB. A similar program using kiss_fft is 18KB (without optimizing for size). 178 | > 4. FFT_BRANDX is roughly twice as fast as KISS FFT in default mode. 179 | 180 | It is wonderful that free, highly optimized libraries like FFT_BRANDX exist. 181 | But such libraries carry a huge burden of complexity necessary to extract every 182 | last bit of performance. 183 | 184 | **Sometimes simpler is better, even if it's not better.** 185 | 186 | ## FREQUENTLY ASKED QUESTIONS: 187 | > Q: Can I use kissfft in a project with a ___ license?
188 | > A: Yes. See LICENSE below. 189 | 190 | > Q: Why don't I get the output I expect?
191 | > A: The two most common causes of this are 192 | > 1) scaling : is there a constant multiplier between what you got and what you want? 193 | > 2) mixed build environment -- all code must be compiled with same preprocessor 194 | > definitions for FIXED_POINT and kiss_fft_scalar 195 | 196 | > Q: Will you write/debug my code for me?
197 | > A: Probably not unless you pay me. I am happy to answer pointed and topical questions, but 198 | > I may refer you to a book, a forum, or some other resource. 199 | 200 | 201 | ## PERFORMANCE 202 | (on Athlon XP 2100+, with gcc 2.96, float data type) 203 | 204 | Kiss performed 10000 1024-pt cpx ffts in .63 s of cpu time. 205 | For comparison, it took md5sum twice as long to process the same amount of data. 206 | Transforming 5 minutes of CD quality audio takes less than a second (nfft=1024). 207 | 208 | **DO NOT:** 209 | - use Kiss if you need the Fastest Fourier Transform in the World 210 | - ask me to add features that will bloat the code 211 | 212 | ## UNDER THE HOOD 213 | 214 | Kiss FFT uses a time decimation, mixed-radix, out-of-place FFT. If you give it an input buffer 215 | and output buffer that are the same, a temporary buffer will be created to hold the data. 216 | 217 | No static data is used. The core routines of kiss_fft are thread-safe (but not all of the tools directory).[ 218 | 219 | No scaling is done for the floating point version (for speed). 220 | Scaling is done both ways for the fixed-point version (for overflow prevention). 221 | 222 | Optimized butterflies are used for factors 2,3,4, and 5. 223 | 224 | The real (i.e. not complex) optimization code only works for even length ffts. It does two half-length 225 | FFTs in parallel (packed into real&imag), and then combines them via twiddling. The result is 226 | nfft/2+1 complex frequency bins from DC to Nyquist. If you don't know what this means, search the web. 227 | 228 | The fast convolution filtering uses the overlap-scrap method, slightly 229 | modified to put the scrap at the tail. 230 | 231 | ## LICENSE 232 | Revised BSD License, see COPYING for verbiage. 233 | Basically, "free to use&change, give credit where due, no guarantees" 234 | Note this license is compatible with GPL at one end of the spectrum and closed, commercial software at 235 | the other end. See http://www.fsf.org/licensing/licenses 236 | 237 | ## TODO 238 | - Add real optimization for odd length FFTs 239 | - Document/revisit the input/output fft scaling 240 | - Make doc describing the overlap (tail) scrap fast convolution filtering in kiss_fastfir.c 241 | - Test all the ./tools/ code with fixed point (kiss_fastfir.c doesn't work, maybe others) 242 | 243 | ## AUTHOR 244 | Mark Borgerding 245 | Mark@Borgerding.net 246 | -------------------------------------------------------------------------------- /README.simd: -------------------------------------------------------------------------------- 1 | If you are reading this, it means you think you may be interested in using the SIMD extensions in kissfft 2 | to do 4 *separate* FFTs at once. 3 | 4 | Beware! Beyond here there be dragons! 5 | 6 | This API is not easy to use, is not well documented, and breaks the KISS principle. 7 | 8 | 9 | Still reading? Okay, you may get rewarded for your patience with a considerable speedup 10 | (2-3x) on intel x86 machines with SSE if you are willing to jump through some hoops. 11 | 12 | The basic idea is to use the packed 4 float __m128 data type as a scalar element. 13 | This means that the format is pretty convoluted. It performs 4 FFTs per fft call on signals A,B,C,D. 14 | 15 | For complex data, the data is interlaced as follows: 16 | rA0,rB0,rC0,rD0, iA0,iB0,iC0,iD0, rA1,rB1,rC1,rD1, iA1,iB1,iC1,iD1 ... 17 | where "rA0" is the real part of the zeroth sample for signal A 18 | 19 | Real-only data is laid out: 20 | rA0,rB0,rC0,rD0, rA1,rB1,rC1,rD1, ... 21 | 22 | Compile with gcc flags something like 23 | -O3 -mpreferred-stack-boundary=4 -DUSE_SIMD=1 -msse 24 | 25 | Be aware of SIMD alignment. This is the most likely cause of segfaults. 26 | The code within kissfft uses scratch variables on the stack. 27 | With SIMD, these must have addresses on 16 byte boundaries. 28 | Search on "SIMD alignment" for more info. 29 | 30 | 31 | 32 | Robin at Divide Concept was kind enough to share his code for formatting to/from the SIMD kissfft. 33 | I have not run it -- use it at your own risk. It appears to do 4xN and Nx4 transpositions 34 | (out of place). 35 | 36 | void SSETools::pack128(float* target, float* source, unsigned long size128) 37 | { 38 | __m128* pDest = (__m128*)target; 39 | __m128* pDestEnd = pDest+size128; 40 | float* source0=source; 41 | float* source1=source0+size128; 42 | float* source2=source1+size128; 43 | float* source3=source2+size128; 44 | 45 | while(pDest 20 | 21 | #define MAXFACTORS 32 22 | /* e.g. an fft of length 128 has 4 factors 23 | as far as kissfft is concerned 24 | 4*4*4*2 25 | */ 26 | 27 | struct kiss_fft_state{ 28 | int nfft; 29 | int inverse; 30 | int factors[2*MAXFACTORS]; 31 | kiss_fft_cpx twiddles[1]; 32 | }; 33 | 34 | /* 35 | Explanation of macros dealing with complex math: 36 | 37 | C_MUL(m,a,b) : m = a*b 38 | C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise 39 | C_SUB( res, a,b) : res = a - b 40 | C_SUBFROM( res , a) : res -= a 41 | C_ADDTO( res , a) : res += a 42 | * */ 43 | #ifdef FIXED_POINT 44 | #include 45 | #if (FIXED_POINT==32) 46 | # define FRACBITS 31 47 | # define SAMPPROD int64_t 48 | #define SAMP_MAX INT32_MAX 49 | #define SAMP_MIN INT32_MIN 50 | #else 51 | # define FRACBITS 15 52 | # define SAMPPROD int32_t 53 | #define SAMP_MAX INT16_MAX 54 | #define SAMP_MIN INT16_MIN 55 | #endif 56 | 57 | #if defined(CHECK_OVERFLOW) 58 | # define CHECK_OVERFLOW_OP(a,op,b) \ 59 | if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ 60 | KISS_FFT_WARNING("overflow (%d " #op" %d) = %ld", (a),(b),(SAMPPROD)(a) op (SAMPPROD)(b)); } 61 | #endif 62 | 63 | 64 | # define smul(a,b) ( (SAMPPROD)(a)*(b) ) 65 | # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) 66 | 67 | # define S_MUL(a,b) sround( smul(a,b) ) 68 | 69 | # define C_MUL(m,a,b) \ 70 | do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ 71 | (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) 72 | 73 | # define DIVSCALAR(x,k) \ 74 | (x) = sround( smul( x, SAMP_MAX/k ) ) 75 | 76 | # define C_FIXDIV(c,div) \ 77 | do { DIVSCALAR( (c).r , div); \ 78 | DIVSCALAR( (c).i , div); }while (0) 79 | 80 | # define C_MULBYSCALAR( c, s ) \ 81 | do{ (c).r = sround( smul( (c).r , s ) ) ;\ 82 | (c).i = sround( smul( (c).i , s ) ) ; }while(0) 83 | 84 | #else /* not FIXED_POINT*/ 85 | 86 | # define S_MUL(a,b) ( (a)*(b) ) 87 | #define C_MUL(m,a,b) \ 88 | do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ 89 | (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) 90 | # define C_FIXDIV(c,div) /* NOOP */ 91 | # define C_MULBYSCALAR( c, s ) \ 92 | do{ (c).r *= (s);\ 93 | (c).i *= (s); }while(0) 94 | #endif 95 | 96 | #ifndef CHECK_OVERFLOW_OP 97 | # define CHECK_OVERFLOW_OP(a,op,b) /* noop */ 98 | #endif 99 | 100 | #define C_ADD( res, a,b)\ 101 | do { \ 102 | CHECK_OVERFLOW_OP((a).r,+,(b).r)\ 103 | CHECK_OVERFLOW_OP((a).i,+,(b).i)\ 104 | (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ 105 | }while(0) 106 | #define C_SUB( res, a,b)\ 107 | do { \ 108 | CHECK_OVERFLOW_OP((a).r,-,(b).r)\ 109 | CHECK_OVERFLOW_OP((a).i,-,(b).i)\ 110 | (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ 111 | }while(0) 112 | #define C_ADDTO( res , a)\ 113 | do { \ 114 | CHECK_OVERFLOW_OP((res).r,+,(a).r)\ 115 | CHECK_OVERFLOW_OP((res).i,+,(a).i)\ 116 | (res).r += (a).r; (res).i += (a).i;\ 117 | }while(0) 118 | 119 | #define C_SUBFROM( res , a)\ 120 | do {\ 121 | CHECK_OVERFLOW_OP((res).r,-,(a).r)\ 122 | CHECK_OVERFLOW_OP((res).i,-,(a).i)\ 123 | (res).r -= (a).r; (res).i -= (a).i; \ 124 | }while(0) 125 | 126 | 127 | #ifdef FIXED_POINT 128 | # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) 129 | # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) 130 | # define HALF_OF(x) ((x)>>1) 131 | #elif defined(USE_SIMD) 132 | # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) 133 | # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) 134 | # define HALF_OF(x) ((x)*_mm_set1_ps(.5)) 135 | #else 136 | # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) 137 | # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) 138 | # define HALF_OF(x) ((x)*((kiss_fft_scalar).5)) 139 | #endif 140 | 141 | #define kf_cexp(x,phase) \ 142 | do{ \ 143 | (x)->r = KISS_FFT_COS(phase);\ 144 | (x)->i = KISS_FFT_SIN(phase);\ 145 | }while(0) 146 | 147 | 148 | /* a debugging function */ 149 | #define pcpx(c)\ 150 | KISS_FFT_DEBUG("%g + %gi\n",(double)((c)->r),(double)((c)->i)) 151 | 152 | 153 | #ifdef KISS_FFT_USE_ALLOCA 154 | // define this to allow use of alloca instead of malloc for temporary buffers 155 | // Temporary buffers are used in two case: 156 | // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 157 | // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. 158 | #include 159 | #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) 160 | #define KISS_FFT_TMP_FREE(ptr) 161 | #else 162 | #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) 163 | #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) 164 | #endif 165 | 166 | #endif /* _kiss_fft_guts_h */ 167 | 168 | -------------------------------------------------------------------------------- /cmake/JoinPaths.cmake: -------------------------------------------------------------------------------- 1 | # This module provides function for joining paths 2 | # known from most languages 3 | # 4 | # SPDX-License-Identifier: (MIT OR CC0-1.0) 5 | # Copyright 2020 Jan Tojnar 6 | # https://github.com/jtojnar/cmake-snips 7 | # 8 | # Modelled after Python’s os.path.join 9 | # https://docs.python.org/3.7/library/os.path.html#os.path.join 10 | # Windows not supported 11 | function(join_paths joined_path first_path_segment) 12 | set(temp_path "${first_path_segment}") 13 | foreach(current_segment IN LISTS ARGN) 14 | if(NOT ("${current_segment}" STREQUAL "")) 15 | if(IS_ABSOLUTE "${current_segment}") 16 | set(temp_path "${current_segment}") 17 | else() 18 | set(temp_path "${temp_path}/${current_segment}") 19 | endif() 20 | endif() 21 | endforeach() 22 | set(${joined_path} "${temp_path}" PARENT_SCOPE) 23 | endfunction() 24 | -------------------------------------------------------------------------------- /kfc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #include "kfc.h" 10 | 11 | typedef struct cached_fft *kfc_cfg; 12 | 13 | struct cached_fft 14 | { 15 | int nfft; 16 | int inverse; 17 | kiss_fft_cfg cfg; 18 | kfc_cfg next; 19 | }; 20 | 21 | static kfc_cfg cache_root=NULL; 22 | static int ncached=0; 23 | 24 | static kiss_fft_cfg find_cached_fft(int nfft,int inverse) 25 | { 26 | size_t len; 27 | kfc_cfg cur=cache_root; 28 | kfc_cfg prev=NULL; 29 | while ( cur ) { 30 | if ( cur->nfft == nfft && inverse == cur->inverse ) 31 | break;/*found the right node*/ 32 | prev = cur; 33 | cur = prev->next; 34 | } 35 | if (cur== NULL) { 36 | /* no cached node found, need to create a new one*/ 37 | kiss_fft_alloc(nfft,inverse,0,&len); 38 | #ifdef USE_SIMD 39 | int padding = (16-sizeof(struct cached_fft)) & 15; 40 | // make sure the cfg aligns on a 16 byte boundary 41 | len += padding; 42 | #endif 43 | cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len )); 44 | if (cur == NULL) 45 | return NULL; 46 | cur->cfg = (kiss_fft_cfg)(cur+1); 47 | #ifdef USE_SIMD 48 | cur->cfg = (kiss_fft_cfg) ((char*)(cur+1)+padding); 49 | #endif 50 | kiss_fft_alloc(nfft,inverse,cur->cfg,&len); 51 | cur->nfft=nfft; 52 | cur->inverse=inverse; 53 | cur->next = NULL; 54 | if ( prev ) 55 | prev->next = cur; 56 | else 57 | cache_root = cur; 58 | ++ncached; 59 | } 60 | return cur->cfg; 61 | } 62 | 63 | void kfc_cleanup(void) 64 | { 65 | kfc_cfg cur=cache_root; 66 | kfc_cfg next=NULL; 67 | while (cur){ 68 | next = cur->next; 69 | free(cur); 70 | cur=next; 71 | } 72 | ncached=0; 73 | cache_root = NULL; 74 | } 75 | void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout) 76 | { 77 | kiss_fft( find_cached_fft(nfft,0),fin,fout ); 78 | } 79 | 80 | void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout) 81 | { 82 | kiss_fft( find_cached_fft(nfft,1),fin,fout ); 83 | } 84 | 85 | #ifdef KFC_TEST 86 | static void check(int nc) 87 | { 88 | if (ncached != nc) { 89 | fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached); 90 | exit(1); 91 | } 92 | } 93 | 94 | int main(void) 95 | { 96 | kiss_fft_cpx buf1[1024],buf2[1024]; 97 | memset(buf1,0,sizeof(buf1)); 98 | check(0); 99 | kfc_fft(512,buf1,buf2); 100 | check(1); 101 | kfc_fft(512,buf1,buf2); 102 | check(1); 103 | kfc_ifft(512,buf1,buf2); 104 | check(2); 105 | kfc_cleanup(); 106 | check(0); 107 | return 0; 108 | } 109 | #endif 110 | -------------------------------------------------------------------------------- /kfc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #ifndef KFC_H 10 | #define KFC_H 11 | #include "kiss_fft.h" 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | /* 18 | KFC -- Kiss FFT Cache 19 | 20 | Not needing to deal with kiss_fft_alloc and a config 21 | object may be handy for a lot of programs. 22 | 23 | KFC uses the underlying KISS FFT functions, but caches the config object. 24 | The first time kfc_fft or kfc_ifft for a given FFT size, the cfg 25 | object is created for it. All subsequent calls use the cached 26 | configuration object. 27 | 28 | NOTE: 29 | You should probably not use this if your program will be using a lot 30 | of various sizes of FFTs. There is a linear search through the 31 | cached objects. If you are only using one or two FFT sizes, this 32 | will be negligible. Otherwise, you may want to use another method 33 | of managing the cfg objects. 34 | 35 | There is no automated cleanup of the cached objects. This could lead 36 | to large memory usage in a program that uses a lot of *DIFFERENT* 37 | sized FFTs. If you want to force all cached cfg objects to be freed, 38 | call kfc_cleanup. 39 | 40 | */ 41 | 42 | /*forward complex FFT */ 43 | void KISS_FFT_API kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout); 44 | /*reverse complex FFT */ 45 | void KISS_FFT_API kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout); 46 | 47 | /*free all cached objects*/ 48 | void KISS_FFT_API kfc_cleanup(void); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /kiss_fft.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | 10 | #include "_kiss_fft_guts.h" 11 | /* The guts header contains all the multiplication and addition macros that are defined for 12 | fixed or floating point complex numbers. It also delares the kf_ internal functions. 13 | */ 14 | 15 | static void kf_bfly2( 16 | kiss_fft_cpx * Fout, 17 | const size_t fstride, 18 | const kiss_fft_cfg st, 19 | int m 20 | ) 21 | { 22 | kiss_fft_cpx * Fout2; 23 | kiss_fft_cpx * tw1 = st->twiddles; 24 | kiss_fft_cpx t; 25 | Fout2 = Fout + m; 26 | do{ 27 | C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); 28 | 29 | C_MUL (t, *Fout2 , *tw1); 30 | tw1 += fstride; 31 | C_SUB( *Fout2 , *Fout , t ); 32 | C_ADDTO( *Fout , t ); 33 | ++Fout2; 34 | ++Fout; 35 | }while (--m); 36 | } 37 | 38 | static void kf_bfly4( 39 | kiss_fft_cpx * Fout, 40 | const size_t fstride, 41 | const kiss_fft_cfg st, 42 | const size_t m 43 | ) 44 | { 45 | kiss_fft_cpx *tw1,*tw2,*tw3; 46 | kiss_fft_cpx scratch[6]; 47 | size_t k=m; 48 | const size_t m2=2*m; 49 | const size_t m3=3*m; 50 | 51 | 52 | tw3 = tw2 = tw1 = st->twiddles; 53 | 54 | do { 55 | C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); 56 | 57 | C_MUL(scratch[0],Fout[m] , *tw1 ); 58 | C_MUL(scratch[1],Fout[m2] , *tw2 ); 59 | C_MUL(scratch[2],Fout[m3] , *tw3 ); 60 | 61 | C_SUB( scratch[5] , *Fout, scratch[1] ); 62 | C_ADDTO(*Fout, scratch[1]); 63 | C_ADD( scratch[3] , scratch[0] , scratch[2] ); 64 | C_SUB( scratch[4] , scratch[0] , scratch[2] ); 65 | C_SUB( Fout[m2], *Fout, scratch[3] ); 66 | tw1 += fstride; 67 | tw2 += fstride*2; 68 | tw3 += fstride*3; 69 | C_ADDTO( *Fout , scratch[3] ); 70 | 71 | if(st->inverse) { 72 | Fout[m].r = scratch[5].r - scratch[4].i; 73 | Fout[m].i = scratch[5].i + scratch[4].r; 74 | Fout[m3].r = scratch[5].r + scratch[4].i; 75 | Fout[m3].i = scratch[5].i - scratch[4].r; 76 | }else{ 77 | Fout[m].r = scratch[5].r + scratch[4].i; 78 | Fout[m].i = scratch[5].i - scratch[4].r; 79 | Fout[m3].r = scratch[5].r - scratch[4].i; 80 | Fout[m3].i = scratch[5].i + scratch[4].r; 81 | } 82 | ++Fout; 83 | }while(--k); 84 | } 85 | 86 | static void kf_bfly3( 87 | kiss_fft_cpx * Fout, 88 | const size_t fstride, 89 | const kiss_fft_cfg st, 90 | size_t m 91 | ) 92 | { 93 | size_t k=m; 94 | const size_t m2 = 2*m; 95 | kiss_fft_cpx *tw1,*tw2; 96 | kiss_fft_cpx scratch[5]; 97 | kiss_fft_cpx epi3; 98 | epi3 = st->twiddles[fstride*m]; 99 | 100 | tw1=tw2=st->twiddles; 101 | 102 | do{ 103 | C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); 104 | 105 | C_MUL(scratch[1],Fout[m] , *tw1); 106 | C_MUL(scratch[2],Fout[m2] , *tw2); 107 | 108 | C_ADD(scratch[3],scratch[1],scratch[2]); 109 | C_SUB(scratch[0],scratch[1],scratch[2]); 110 | tw1 += fstride; 111 | tw2 += fstride*2; 112 | 113 | Fout[m].r = Fout->r - HALF_OF(scratch[3].r); 114 | Fout[m].i = Fout->i - HALF_OF(scratch[3].i); 115 | 116 | C_MULBYSCALAR( scratch[0] , epi3.i ); 117 | 118 | C_ADDTO(*Fout,scratch[3]); 119 | 120 | Fout[m2].r = Fout[m].r + scratch[0].i; 121 | Fout[m2].i = Fout[m].i - scratch[0].r; 122 | 123 | Fout[m].r -= scratch[0].i; 124 | Fout[m].i += scratch[0].r; 125 | 126 | ++Fout; 127 | }while(--k); 128 | } 129 | 130 | static void kf_bfly5( 131 | kiss_fft_cpx * Fout, 132 | const size_t fstride, 133 | const kiss_fft_cfg st, 134 | int m 135 | ) 136 | { 137 | kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; 138 | int u; 139 | kiss_fft_cpx scratch[13]; 140 | kiss_fft_cpx * twiddles = st->twiddles; 141 | kiss_fft_cpx *tw; 142 | kiss_fft_cpx ya,yb; 143 | ya = twiddles[fstride*m]; 144 | yb = twiddles[fstride*2*m]; 145 | 146 | Fout0=Fout; 147 | Fout1=Fout0+m; 148 | Fout2=Fout0+2*m; 149 | Fout3=Fout0+3*m; 150 | Fout4=Fout0+4*m; 151 | 152 | tw=st->twiddles; 153 | for ( u=0; ur += scratch[7].r + scratch[8].r; 168 | Fout0->i += scratch[7].i + scratch[8].i; 169 | 170 | scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); 171 | scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); 172 | 173 | scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); 174 | scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); 175 | 176 | C_SUB(*Fout1,scratch[5],scratch[6]); 177 | C_ADD(*Fout4,scratch[5],scratch[6]); 178 | 179 | scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); 180 | scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); 181 | scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); 182 | scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); 183 | 184 | C_ADD(*Fout2,scratch[11],scratch[12]); 185 | C_SUB(*Fout3,scratch[11],scratch[12]); 186 | 187 | ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; 188 | } 189 | } 190 | 191 | /* perform the butterfly for one stage of a mixed radix FFT */ 192 | static void kf_bfly_generic( 193 | kiss_fft_cpx * Fout, 194 | const size_t fstride, 195 | const kiss_fft_cfg st, 196 | int m, 197 | int p 198 | ) 199 | { 200 | int u,k,q1,q; 201 | kiss_fft_cpx * twiddles = st->twiddles; 202 | kiss_fft_cpx t; 203 | int Norig = st->nfft; 204 | 205 | kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); 206 | if (scratch == NULL){ 207 | KISS_FFT_ERROR("Memory allocation failed."); 208 | return; 209 | } 210 | 211 | for ( u=0; u=Norig) twidx-=Norig; 226 | C_MUL(t,scratch[q] , twiddles[twidx] ); 227 | C_ADDTO( Fout[ k ] ,t); 228 | } 229 | k += m; 230 | } 231 | } 232 | KISS_FFT_TMP_FREE(scratch); 233 | } 234 | 235 | static 236 | void kf_work( 237 | kiss_fft_cpx * Fout, 238 | const kiss_fft_cpx * f, 239 | const size_t fstride, 240 | int in_stride, 241 | int * factors, 242 | const kiss_fft_cfg st 243 | ) 244 | { 245 | kiss_fft_cpx * Fout_beg=Fout; 246 | const int p=*factors++; /* the radix */ 247 | const int m=*factors++; /* stage's fft length/p */ 248 | const kiss_fft_cpx * Fout_end = Fout + p*m; 249 | 250 | #ifdef _OPENMP 251 | // use openmp extensions at the 252 | // top-level (not recursive) 253 | if (fstride==1 && p<=5 && m!=1) 254 | { 255 | int k; 256 | 257 | // execute the p different work units in different threads 258 | # pragma omp parallel for 259 | for (k=0;k floor_sqrt) 322 | p = n; /* no more factors, skip to end */ 323 | } 324 | n /= p; 325 | *facbuf++ = p; 326 | *facbuf++ = n; 327 | } while (n > 1); 328 | } 329 | 330 | /* 331 | * 332 | * User-callable function to allocate all necessary storage space for the fft. 333 | * 334 | * The return value is a contiguous block of memory, allocated with malloc. As such, 335 | * It can be freed with free(), rather than a kiss_fft-specific function. 336 | * */ 337 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem ) 338 | { 339 | KISS_FFT_ALIGN_CHECK(mem) 340 | 341 | kiss_fft_cfg st=NULL; 342 | size_t memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fft_state) 343 | + sizeof(kiss_fft_cpx)*(nfft-1)); /* twiddle factors*/ 344 | 345 | if ( lenmem==NULL ) { 346 | st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); 347 | }else{ 348 | if (mem != NULL && *lenmem >= memneeded) 349 | st = (kiss_fft_cfg)mem; 350 | *lenmem = memneeded; 351 | } 352 | if (st) { 353 | int i; 354 | st->nfft=nfft; 355 | st->inverse = inverse_fft; 356 | 357 | for (i=0;iinverse) 361 | phase *= -1; 362 | kf_cexp(st->twiddles+i, phase ); 363 | } 364 | 365 | kf_factor(nfft,st->factors); 366 | } 367 | return st; 368 | } 369 | 370 | 371 | void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) 372 | { 373 | if (fin == fout) { 374 | //NOTE: this is not really an in-place FFT algorithm. 375 | //It just performs an out-of-place FFT into a temp buffer 376 | if (fout == NULL){ 377 | KISS_FFT_ERROR("fout buffer NULL."); 378 | return; 379 | } 380 | 381 | kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); 382 | if (tmpbuf == NULL){ 383 | KISS_FFT_ERROR("Memory allocation error."); 384 | return; 385 | } 386 | 387 | 388 | 389 | kf_work(tmpbuf,fin,1,in_stride, st->factors,st); 390 | memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); 391 | KISS_FFT_TMP_FREE(tmpbuf); 392 | }else{ 393 | kf_work( fout, fin, 1,in_stride, st->factors,st ); 394 | } 395 | } 396 | 397 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) 398 | { 399 | kiss_fft_stride(cfg,fin,fout,1); 400 | } 401 | 402 | 403 | void kiss_fft_cleanup(void) 404 | { 405 | // nothing needed any more 406 | } 407 | 408 | int kiss_fft_next_fast_size(int n) 409 | { 410 | while(1) { 411 | int m=n; 412 | while ( (m%2) == 0 ) m/=2; 413 | while ( (m%3) == 0 ) m/=3; 414 | while ( (m%5) == 0 ) m/=5; 415 | if (m<=1) 416 | break; /* n is completely factorable by twos, threes, and fives */ 417 | n++; 418 | } 419 | return n; 420 | } 421 | -------------------------------------------------------------------------------- /kiss_fft.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #ifndef KISS_FFT_H 10 | #define KISS_FFT_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | // Define KISS_FFT_SHARED macro to properly export symbols 18 | #ifdef KISS_FFT_SHARED 19 | # ifdef _WIN32 20 | # ifdef KISS_FFT_BUILD 21 | # define KISS_FFT_API __declspec(dllexport) 22 | # else 23 | # define KISS_FFT_API __declspec(dllimport) 24 | # endif 25 | # else 26 | # define KISS_FFT_API __attribute__ ((visibility ("default"))) 27 | # endif 28 | #else 29 | # define KISS_FFT_API 30 | #endif 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | /* 37 | ATTENTION! 38 | If you would like a : 39 | -- a utility that will handle the caching of fft objects 40 | -- real-only (no imaginary time component ) FFT 41 | -- a multi-dimensional FFT 42 | -- a command-line utility to perform ffts 43 | -- a command-line utility to perform fast-convolution filtering 44 | 45 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c 46 | in the tools/ directory. 47 | */ 48 | 49 | /* User may override KISS_FFT_MALLOC and/or KISS_FFT_FREE. */ 50 | #ifdef USE_SIMD 51 | # include 52 | # define kiss_fft_scalar __m128 53 | # ifndef KISS_FFT_MALLOC 54 | # define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) 55 | # define KISS_FFT_ALIGN_CHECK(ptr) 56 | # define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) 57 | # endif 58 | # ifndef KISS_FFT_FREE 59 | # define KISS_FFT_FREE _mm_free 60 | # endif 61 | #else 62 | # define KISS_FFT_ALIGN_CHECK(ptr) 63 | # define KISS_FFT_ALIGN_SIZE_UP(size) (size) 64 | # ifndef KISS_FFT_MALLOC 65 | # define KISS_FFT_MALLOC malloc 66 | # endif 67 | # ifndef KISS_FFT_FREE 68 | # define KISS_FFT_FREE free 69 | # endif 70 | #endif 71 | 72 | 73 | #ifdef FIXED_POINT 74 | #include 75 | # if (FIXED_POINT == 32) 76 | # define kiss_fft_scalar int32_t 77 | # else 78 | # define kiss_fft_scalar int16_t 79 | # endif 80 | #else 81 | # ifndef kiss_fft_scalar 82 | /* default is float */ 83 | # define kiss_fft_scalar float 84 | # endif 85 | #endif 86 | 87 | typedef struct { 88 | kiss_fft_scalar r; 89 | kiss_fft_scalar i; 90 | }kiss_fft_cpx; 91 | 92 | typedef struct kiss_fft_state* kiss_fft_cfg; 93 | 94 | /* 95 | * kiss_fft_alloc 96 | * 97 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. 98 | * 99 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); 100 | * 101 | * The return value from fft_alloc is a cfg buffer used internally 102 | * by the fft routine or NULL. 103 | * 104 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. 105 | * The returned value should be free()d when done to avoid memory leaks. 106 | * 107 | * The state can be placed in a user supplied buffer 'mem': 108 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, 109 | * then the function places the cfg in mem and the size used in *lenmem 110 | * and returns mem. 111 | * 112 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), 113 | * then the function returns NULL and places the minimum cfg 114 | * buffer size in *lenmem. 115 | * */ 116 | 117 | kiss_fft_cfg KISS_FFT_API kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); 118 | 119 | /* 120 | * kiss_fft(cfg,in_out_buf) 121 | * 122 | * Perform an FFT on a complex input buffer. 123 | * for a forward FFT, 124 | * fin should be f[0] , f[1] , ... ,f[nfft-1] 125 | * fout will be F[0] , F[1] , ... ,F[nfft-1] 126 | * Note that each element is complex and can be accessed like 127 | f[k].r and f[k].i 128 | * */ 129 | void KISS_FFT_API kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); 130 | 131 | /* 132 | A more generic version of the above function. It reads its input from every Nth sample. 133 | * */ 134 | void KISS_FFT_API kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); 135 | 136 | /* If kiss_fft_alloc allocated a buffer, it is one contiguous 137 | buffer and can be simply free()d when no longer needed*/ 138 | #define kiss_fft_free KISS_FFT_FREE 139 | 140 | /* 141 | Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up 142 | your compiler output to call this before you exit. 143 | */ 144 | void KISS_FFT_API kiss_fft_cleanup(void); 145 | 146 | 147 | /* 148 | * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) 149 | */ 150 | int KISS_FFT_API kiss_fft_next_fast_size(int n); 151 | 152 | /* for real ffts, we need an even size */ 153 | #define kiss_fftr_next_fast_size_real(n) \ 154 | (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) 155 | 156 | #ifdef __cplusplus 157 | } 158 | #endif 159 | 160 | #endif 161 | -------------------------------------------------------------------------------- /kiss_fft_log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #ifndef kiss_fft_log_h 10 | #define kiss_fft_log_h 11 | 12 | #define ERROR 1 13 | #define WARNING 2 14 | #define INFO 3 15 | #define DEBUG 4 16 | 17 | #define STRINGIFY(x) #x 18 | #define TOSTRING(x) STRINGIFY(x) 19 | 20 | #if defined(NDEBUG) 21 | # define KISS_FFT_LOG_MSG(severity, ...) ((void)0) 22 | #else 23 | # define KISS_FFT_LOG_MSG(severity, ...) \ 24 | fprintf(stderr, "[" #severity "] " __FILE__ ":" TOSTRING(__LINE__) " "); \ 25 | fprintf(stderr, __VA_ARGS__); \ 26 | fprintf(stderr, "\n") 27 | #endif 28 | 29 | #define KISS_FFT_ERROR(...) KISS_FFT_LOG_MSG(ERROR, __VA_ARGS__) 30 | #define KISS_FFT_WARNING(...) KISS_FFT_LOG_MSG(WARNING, __VA_ARGS__) 31 | #define KISS_FFT_INFO(...) KISS_FFT_LOG_MSG(INFO, __VA_ARGS__) 32 | #define KISS_FFT_DEBUG(...) KISS_FFT_LOG_MSG(DEBUG, __VA_ARGS__) 33 | 34 | 35 | 36 | #endif /* kiss_fft_log_h */ 37 | -------------------------------------------------------------------------------- /kiss_fftnd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #include "kiss_fftnd.h" 10 | #include "_kiss_fft_guts.h" 11 | 12 | struct kiss_fftnd_state{ 13 | int dimprod; /* dimsum would be mighty tasty right now */ 14 | int ndims; 15 | int *dims; 16 | kiss_fft_cfg *states; /* cfg states for each dimension */ 17 | kiss_fft_cpx * tmpbuf; /*buffer capable of hold the entire input */ 18 | }; 19 | 20 | kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem) 21 | { 22 | KISS_FFT_ALIGN_CHECK(mem) 23 | 24 | kiss_fftnd_cfg st = NULL; 25 | int i; 26 | int dimprod=1; 27 | size_t memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftnd_state)); 28 | char * ptr = NULL; 29 | 30 | for (i=0;istates[i] */ 34 | dimprod *= dims[i]; 35 | } 36 | memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(int) * ndims);/* st->dims */ 37 | memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(void*) * ndims);/* st->states */ 38 | memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); /* st->tmpbuf */ 39 | 40 | if (lenmem == NULL) {/* allocate for the caller*/ 41 | ptr = (char *) malloc (memneeded); 42 | } else { /* initialize supplied buffer if big enough */ 43 | if (*lenmem >= memneeded) 44 | ptr = (char *) mem; 45 | *lenmem = memneeded; /*tell caller how big struct is (or would be) */ 46 | } 47 | if (!ptr) 48 | return NULL; /*malloc failed or buffer too small */ 49 | 50 | st = (kiss_fftnd_cfg) ptr; 51 | st->dimprod = dimprod; 52 | st->ndims = ndims; 53 | ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftnd_state)); 54 | 55 | st->states = (kiss_fft_cfg *)ptr; 56 | ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(void*) * ndims); 57 | 58 | st->dims = (int*)ptr; 59 | ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(int) * ndims); 60 | 61 | st->tmpbuf = (kiss_fft_cpx*)ptr; 62 | ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); 63 | 64 | for (i=0;idims[i] = dims[i]; 67 | kiss_fft_alloc (st->dims[i], inverse_fft, NULL, &len); 68 | st->states[i] = kiss_fft_alloc (st->dims[i], inverse_fft, ptr,&len); 69 | ptr += len; 70 | } 71 | /* 72 | Hi there! 73 | 74 | If you're looking at this particular code, it probably means you've got a brain-dead bounds checker 75 | that thinks the above code overwrites the end of the array. 76 | 77 | It doesn't. 78 | 79 | -- Mark 80 | 81 | P.S. 82 | The below code might give you some warm fuzzies and help convince you. 83 | */ 84 | if ( ptr - (char*)st != (int)memneeded ) { 85 | fprintf(stderr, 86 | "################################################################################\n" 87 | "Internal error! Memory allocation miscalculation\n" 88 | "################################################################################\n" 89 | ); 90 | } 91 | return st; 92 | } 93 | 94 | /* 95 | This works by tackling one dimension at a time. 96 | 97 | In effect, 98 | Each stage starts out by reshaping the matrix into a DixSi 2d matrix. 99 | A Di-sized fft is taken of each column, transposing the matrix as it goes. 100 | 101 | Here's a 3-d example: 102 | Take a 2x3x4 matrix, laid out in memory as a contiguous buffer 103 | [ [ [ a b c d ] [ e f g h ] [ i j k l ] ] 104 | [ [ m n o p ] [ q r s t ] [ u v w x ] ] ] 105 | 106 | Stage 0 ( D=2): treat the buffer as a 2x12 matrix 107 | [ [a b ... k l] 108 | [m n ... w x] ] 109 | 110 | FFT each column with size 2. 111 | Transpose the matrix at the same time using kiss_fft_stride. 112 | 113 | [ [ a+m a-m ] 114 | [ b+n b-n] 115 | ... 116 | [ k+w k-w ] 117 | [ l+x l-x ] ] 118 | 119 | Note fft([x y]) == [x+y x-y] 120 | 121 | Stage 1 ( D=3) treats the buffer (the output of stage D=2) as an 3x8 matrix, 122 | [ [ a+m a-m b+n b-n c+o c-o d+p d-p ] 123 | [ e+q e-q f+r f-r g+s g-s h+t h-t ] 124 | [ i+u i-u j+v j-v k+w k-w l+x l-x ] ] 125 | 126 | And perform FFTs (size=3) on each of the columns as above, transposing 127 | the matrix as it goes. The output of stage 1 is 128 | (Legend: ap = [ a+m e+q i+u ] 129 | am = [ a-m e-q i-u ] ) 130 | 131 | [ [ sum(ap) fft(ap)[0] fft(ap)[1] ] 132 | [ sum(am) fft(am)[0] fft(am)[1] ] 133 | [ sum(bp) fft(bp)[0] fft(bp)[1] ] 134 | [ sum(bm) fft(bm)[0] fft(bm)[1] ] 135 | [ sum(cp) fft(cp)[0] fft(cp)[1] ] 136 | [ sum(cm) fft(cm)[0] fft(cm)[1] ] 137 | [ sum(dp) fft(dp)[0] fft(dp)[1] ] 138 | [ sum(dm) fft(dm)[0] fft(dm)[1] ] ] 139 | 140 | Stage 2 ( D=4) treats this buffer as a 4*6 matrix, 141 | [ [ sum(ap) fft(ap)[0] fft(ap)[1] sum(am) fft(am)[0] fft(am)[1] ] 142 | [ sum(bp) fft(bp)[0] fft(bp)[1] sum(bm) fft(bm)[0] fft(bm)[1] ] 143 | [ sum(cp) fft(cp)[0] fft(cp)[1] sum(cm) fft(cm)[0] fft(cm)[1] ] 144 | [ sum(dp) fft(dp)[0] fft(dp)[1] sum(dm) fft(dm)[0] fft(dm)[1] ] ] 145 | 146 | Then FFTs each column, transposing as it goes. 147 | 148 | The resulting matrix is the 3d FFT of the 2x3x4 input matrix. 149 | 150 | Note as a sanity check that the first element of the final 151 | stage's output (DC term) is 152 | sum( [ sum(ap) sum(bp) sum(cp) sum(dp) ] ) 153 | , i.e. the summation of all 24 input elements. 154 | 155 | */ 156 | void kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) 157 | { 158 | int i,k; 159 | const kiss_fft_cpx * bufin=fin; 160 | kiss_fft_cpx * bufout; 161 | 162 | /*arrange it so the last bufout == fout*/ 163 | if ( st->ndims & 1 ) { 164 | bufout = fout; 165 | if (fin==fout) { 166 | memcpy( st->tmpbuf, fin, sizeof(kiss_fft_cpx) * st->dimprod ); 167 | bufin = st->tmpbuf; 168 | } 169 | }else 170 | bufout = st->tmpbuf; 171 | 172 | for ( k=0; k < st->ndims; ++k) { 173 | int curdim = st->dims[k]; 174 | int stride = st->dimprod / curdim; 175 | 176 | for ( i=0 ; istates[k], bufin+i , bufout+i*curdim, stride ); 178 | 179 | /*toggle back and forth between the two buffers*/ 180 | if (bufout == st->tmpbuf){ 181 | bufout = fout; 182 | bufin = st->tmpbuf; 183 | }else{ 184 | bufout = st->tmpbuf; 185 | bufin = fout; 186 | } 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /kiss_fftnd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #ifndef KISS_FFTND_H 10 | #define KISS_FFTND_H 11 | 12 | #include "kiss_fft.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | typedef struct kiss_fftnd_state * kiss_fftnd_cfg; 19 | 20 | kiss_fftnd_cfg KISS_FFT_API kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem); 21 | void KISS_FFT_API kiss_fftnd(kiss_fftnd_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | #endif 27 | -------------------------------------------------------------------------------- /kiss_fftndr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #include "kiss_fftndr.h" 10 | #include "_kiss_fft_guts.h" 11 | #define MAX(x,y) ( ( (x)<(y) )?(y):(x) ) 12 | 13 | struct kiss_fftndr_state 14 | { 15 | int dimReal; 16 | int dimOther; 17 | kiss_fftr_cfg cfg_r; 18 | kiss_fftnd_cfg cfg_nd; 19 | void * tmpbuf; 20 | }; 21 | 22 | static int prod(const int *dims, int ndims) 23 | { 24 | int x=1; 25 | while (ndims--) 26 | x *= *dims++; 27 | return x; 28 | } 29 | 30 | kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem) 31 | { 32 | KISS_FFT_ALIGN_CHECK(mem) 33 | 34 | kiss_fftndr_cfg st = NULL; 35 | size_t nr=0 , nd=0,ntmp=0; 36 | int dimReal = dims[ndims-1]; 37 | int dimOther = prod(dims,ndims-1); 38 | size_t memneeded; 39 | char * ptr = NULL; 40 | 41 | (void)kiss_fftr_alloc(dimReal,inverse_fft,NULL,&nr); 42 | (void)kiss_fftnd_alloc(dims,ndims-1,inverse_fft,NULL,&nd); 43 | ntmp = 44 | MAX( 2*dimOther , dimReal+2) * sizeof(kiss_fft_scalar) // freq buffer for one pass 45 | + dimOther*(dimReal+2) * sizeof(kiss_fft_scalar); // large enough to hold entire input in case of in-place 46 | 47 | memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof( struct kiss_fftndr_state )) + KISS_FFT_ALIGN_SIZE_UP(nr) + KISS_FFT_ALIGN_SIZE_UP(nd) + KISS_FFT_ALIGN_SIZE_UP(ntmp); 48 | 49 | if (lenmem==NULL) { 50 | ptr = (char*) malloc(memneeded); 51 | }else{ 52 | if (*lenmem >= memneeded) 53 | ptr = (char *)mem; 54 | *lenmem = memneeded; 55 | } 56 | if (ptr==NULL) 57 | return NULL; 58 | 59 | st = (kiss_fftndr_cfg) ptr; 60 | memset( st , 0 , memneeded); 61 | ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftndr_state)); 62 | 63 | st->dimReal = dimReal; 64 | st->dimOther = dimOther; 65 | st->cfg_r = kiss_fftr_alloc( dimReal,inverse_fft,ptr,&nr); 66 | ptr += KISS_FFT_ALIGN_SIZE_UP(nr); 67 | st->cfg_nd = kiss_fftnd_alloc(dims,ndims-1,inverse_fft, ptr,&nd); 68 | ptr += KISS_FFT_ALIGN_SIZE_UP(nd); 69 | st->tmpbuf = ptr; 70 | 71 | return st; 72 | } 73 | 74 | void kiss_fftndr(kiss_fftndr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) 75 | { 76 | int k1,k2; 77 | int dimReal = st->dimReal; 78 | int dimOther = st->dimOther; 79 | int nrbins = dimReal/2+1; 80 | 81 | kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf; 82 | kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther); 83 | 84 | // timedata is N0 x N1 x ... x Nk real 85 | 86 | // take a real chunk of data, fft it and place the output at correct intervals 87 | for (k1=0;k1cfg_r, timedata + k1*dimReal , tmp1 ); // tmp1 now holds nrbins complex points 89 | for (k2=0;k2cfg_nd, tmp2+k2*dimOther, tmp1); // tmp1 now holds dimOther complex points 95 | for (k1=0;k1dimReal; 104 | int dimOther = st->dimOther; 105 | int nrbins = dimReal/2+1; 106 | kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf; 107 | kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther); 108 | 109 | for (k2=0;k2cfg_nd, tmp1, tmp2+k2*dimOther); 113 | } 114 | 115 | for (k1=0;k1cfg_r,tmp1,timedata + k1*dimReal); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /kiss_fftndr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #ifndef KISS_NDR_H 10 | #define KISS_NDR_H 11 | 12 | #include "kiss_fft.h" 13 | #include "kiss_fftr.h" 14 | #include "kiss_fftnd.h" 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | typedef struct kiss_fftndr_state *kiss_fftndr_cfg; 21 | 22 | 23 | kiss_fftndr_cfg KISS_FFT_API kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem); 24 | /* 25 | dims[0] must be even 26 | 27 | If you don't care to allocate space, use mem = lenmem = NULL 28 | */ 29 | 30 | 31 | void KISS_FFT_API kiss_fftndr( 32 | kiss_fftndr_cfg cfg, 33 | const kiss_fft_scalar *timedata, 34 | kiss_fft_cpx *freqdata); 35 | /* 36 | input timedata has dims[0] X dims[1] X ... X dims[ndims-1] scalar points 37 | output freqdata has dims[0] X dims[1] X ... X dims[ndims-1]/2+1 complex points 38 | */ 39 | 40 | void KISS_FFT_API kiss_fftndri( 41 | kiss_fftndr_cfg cfg, 42 | const kiss_fft_cpx *freqdata, 43 | kiss_fft_scalar *timedata); 44 | /* 45 | input and output dimensions are the exact opposite of kiss_fftndr 46 | */ 47 | 48 | 49 | #define kiss_fftndr_free free 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /kiss_fftr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #include "kiss_fftr.h" 10 | #include "_kiss_fft_guts.h" 11 | 12 | struct kiss_fftr_state{ 13 | kiss_fft_cfg substate; 14 | kiss_fft_cpx * tmpbuf; 15 | kiss_fft_cpx * super_twiddles; 16 | #ifdef USE_SIMD 17 | void * pad; 18 | #endif 19 | }; 20 | 21 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem) 22 | { 23 | KISS_FFT_ALIGN_CHECK(mem) 24 | 25 | int i; 26 | kiss_fftr_cfg st = NULL; 27 | size_t subsize = 0, memneeded; 28 | 29 | if (nfft & 1) { 30 | KISS_FFT_ERROR("Real FFT optimization must be even."); 31 | return NULL; 32 | } 33 | nfft >>= 1; 34 | 35 | kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); 36 | memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); 37 | 38 | if (lenmem == NULL) { 39 | st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); 40 | } else { 41 | if (*lenmem >= memneeded) 42 | st = (kiss_fftr_cfg) mem; 43 | *lenmem = memneeded; 44 | } 45 | if (!st) 46 | return NULL; 47 | 48 | st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */ 49 | st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize); 50 | st->super_twiddles = st->tmpbuf + nfft; 51 | kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize); 52 | 53 | for (i = 0; i < nfft/2; ++i) { 54 | double phase = 55 | -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5); 56 | if (inverse_fft) 57 | phase *= -1; 58 | kf_cexp (st->super_twiddles+i,phase); 59 | } 60 | return st; 61 | } 62 | 63 | void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) 64 | { 65 | /* input buffer timedata is stored row-wise */ 66 | int k,ncfft; 67 | kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; 68 | 69 | if ( st->substate->inverse) { 70 | KISS_FFT_ERROR("kiss fft usage error: improper alloc"); 71 | return;/* The caller did not call the correct function */ 72 | } 73 | 74 | ncfft = st->substate->nfft; 75 | 76 | /*perform the parallel fft of two real signals packed in real,imag*/ 77 | kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf ); 78 | /* The real part of the DC element of the frequency spectrum in st->tmpbuf 79 | * contains the sum of the even-numbered elements of the input time sequence 80 | * The imag part is the sum of the odd-numbered elements 81 | * 82 | * The sum of tdc.r and tdc.i is the sum of the input time sequence. 83 | * yielding DC of input time sequence 84 | * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... 85 | * yielding Nyquist bin of input time sequence 86 | */ 87 | 88 | tdc.r = st->tmpbuf[0].r; 89 | tdc.i = st->tmpbuf[0].i; 90 | C_FIXDIV(tdc,2); 91 | CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i); 92 | CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); 93 | freqdata[0].r = tdc.r + tdc.i; 94 | freqdata[ncfft].r = tdc.r - tdc.i; 95 | #ifdef USE_SIMD 96 | freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); 97 | #else 98 | freqdata[ncfft].i = freqdata[0].i = 0; 99 | #endif 100 | 101 | for ( k=1;k <= ncfft/2 ; ++k ) { 102 | fpk = st->tmpbuf[k]; 103 | fpnk.r = st->tmpbuf[ncfft-k].r; 104 | fpnk.i = - st->tmpbuf[ncfft-k].i; 105 | C_FIXDIV(fpk,2); 106 | C_FIXDIV(fpnk,2); 107 | 108 | C_ADD( f1k, fpk , fpnk ); 109 | C_SUB( f2k, fpk , fpnk ); 110 | C_MUL( tw , f2k , st->super_twiddles[k-1]); 111 | 112 | freqdata[k].r = HALF_OF(f1k.r + tw.r); 113 | freqdata[k].i = HALF_OF(f1k.i + tw.i); 114 | freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r); 115 | freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i); 116 | } 117 | } 118 | 119 | void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata) 120 | { 121 | /* input buffer timedata is stored row-wise */ 122 | int k, ncfft; 123 | 124 | if (st->substate->inverse == 0) { 125 | KISS_FFT_ERROR("kiss fft usage error: improper alloc"); 126 | return;/* The caller did not call the correct function */ 127 | } 128 | 129 | ncfft = st->substate->nfft; 130 | 131 | st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; 132 | st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; 133 | C_FIXDIV(st->tmpbuf[0],2); 134 | 135 | for (k = 1; k <= ncfft / 2; ++k) { 136 | kiss_fft_cpx fk, fnkc, fek, fok, tmp; 137 | fk = freqdata[k]; 138 | fnkc.r = freqdata[ncfft - k].r; 139 | fnkc.i = -freqdata[ncfft - k].i; 140 | C_FIXDIV( fk , 2 ); 141 | C_FIXDIV( fnkc , 2 ); 142 | 143 | C_ADD (fek, fk, fnkc); 144 | C_SUB (tmp, fk, fnkc); 145 | C_MUL (fok, tmp, st->super_twiddles[k-1]); 146 | C_ADD (st->tmpbuf[k], fek, fok); 147 | C_SUB (st->tmpbuf[ncfft - k], fek, fok); 148 | #ifdef USE_SIMD 149 | st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0); 150 | #else 151 | st->tmpbuf[ncfft - k].i *= -1; 152 | #endif 153 | } 154 | kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata); 155 | } 156 | -------------------------------------------------------------------------------- /kiss_fftr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #ifndef KISS_FTR_H 10 | #define KISS_FTR_H 11 | 12 | #include "kiss_fft.h" 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | 18 | /* 19 | 20 | Real optimized version can save about 45% cpu time vs. complex fft of a real seq. 21 | 22 | 23 | 24 | */ 25 | 26 | typedef struct kiss_fftr_state *kiss_fftr_cfg; 27 | 28 | 29 | kiss_fftr_cfg KISS_FFT_API kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); 30 | /* 31 | nfft must be even 32 | 33 | If you don't care to allocate space, use mem = lenmem = NULL 34 | */ 35 | 36 | 37 | void KISS_FFT_API kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata); 38 | /* 39 | input timedata has nfft scalar points 40 | output freqdata has nfft/2+1 complex points 41 | */ 42 | 43 | void KISS_FFT_API kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata); 44 | /* 45 | input freqdata has nfft/2+1 complex points 46 | output timedata has nfft scalar points 47 | */ 48 | 49 | #define kiss_fftr_free KISS_FFT_FREE 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | #endif 55 | -------------------------------------------------------------------------------- /kissfft-config.cmake.in: -------------------------------------------------------------------------------- 1 | # kissfft-config.ccmake accept the following components: 2 | # 3 | # SHARED/STATIC: 4 | # This components allows one to choose a shared/static kissfft library. 5 | # The default is selected by BUILD_SHARED_LIBS. 6 | # They are to be used exclusively. Using them together is an error. 7 | # 8 | # example: 9 | # find_package(kissfft CONFIG REQUIRED COMPONENTS STATIC) 10 | # 11 | # simd/int16/int32/float/double: 12 | # This components allows one to choose the datatype. 13 | # When using this component, the target kissfft::kissfft becomes available. 14 | # When not using this component, you will have to choose the correct kissfft target. 15 | # 16 | # example: 17 | # find_package(kissfft CONFIG REQUIRED) 18 | # # - kissfft::kissfft-float, kissfft::kissfft-int32_t/ ... are available (if they are installed) 19 | # # - kissfft::kissfft is not available, 20 | # 21 | # find_package(kissfft CONFIG REQUIRED COMPONENTS int32_t) 22 | # # - kissfft::kissfft-float, kissfft::kissfft-int32_t/ ... are available (if they are installed) 23 | # # - kissfft::kissfft is available (as an alias for kissfft::kissfft-int32_t), 24 | 25 | @PACKAGE_INIT@ 26 | 27 | cmake_minimum_required(VERSION 3.3) 28 | 29 | # Set include glob of config files using SHARED/static component, BUILD_SHARED_LIBS by default 30 | set(_kissfft_shared_detected OFF) 31 | set(_kissfft_shared ${BUILD_SHARED_LIBS}) 32 | if("SHARED" IN_LIST kissfft_FIND_COMPONENTS) 33 | set(_kissfft_shared_detected ON) 34 | set(_kissfft_shared ON) 35 | endif() 36 | if("STATIC" IN_LIST kissfft_FIND_COMPONENTS) 37 | if(_kissfft_shared_detected) 38 | message(FATAL_ERROR "SHARED and STATIC components cannot be used together") 39 | endif() 40 | set(_kissfft_shared_detected ON) 41 | set(_kissfft_shared OFF) 42 | endif() 43 | 44 | if(_kissfft_shared) 45 | set(_kissfft_config_glob "kissfft-*-shared-targets.cmake") 46 | else() 47 | set(_kissfft_config_glob "kissfft-*-static-targets.cmake") 48 | endif() 49 | 50 | # Load information for all configured kissfft 51 | get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) 52 | file(GLOB CONFIG_FILES "${_DIR}/${_kissfft_config_glob}") 53 | foreach(f ${CONFIG_FILES}) 54 | include(${f}) 55 | endforeach() 56 | 57 | # If a datatype component is passed, create kissfft::kissfft 58 | set(_kissfft_datatype_detected) 59 | foreach(_kissfft_datatype simd int16 int32 float double) 60 | if(_kissfft_datatype IN_LIST kissfft_FIND_COMPONENTS) 61 | if(_kissfft_datatype_detected) 62 | message(FATAL_ERROR "Cannot define datatype COMPONENT twice: ${_kissfft_datatype_detected} and ${_kissfft_datatype}") 63 | endif() 64 | set(_kissfft_datatype_detected ${_kissfft_datatype}) 65 | endif() 66 | endforeach() 67 | 68 | if(_kissfft_datatype_detected) 69 | if(NOT TARGET kissfft::kissfft-${_kissfft_datatype_detected}) 70 | message(FATAL_ERROR "kissfft with datatype=${_kissfft_datatype_detected} is not installed") 71 | endif() 72 | if(TARGET kissfft::kissfft) 73 | message(SEND_ERROR "kissfft::kissfft already exists. You cannot use 2 find_package's with datatype that are visible to eachother.") 74 | else() 75 | add_library(kissfft::kissfft INTERFACE IMPORTED) 76 | set_property(TARGET kissfft::kissfft PROPERTY INTERFACE_LINK_LIBRARIES kissfft::kissfft-${_kissfft_datatype_detected}) 77 | endif() 78 | endif() 79 | 80 | set(kissfft_FOUND ON) 81 | set(KISSFFT_VERSION @kissfft_VERSION@) 82 | 83 | -------------------------------------------------------------------------------- /kissfft.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #ifndef KISSFFT_CLASS_HH 10 | #define KISSFFT_CLASS_HH 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | template 17 | class kissfft 18 | { 19 | public: 20 | 21 | typedef std::complex cpx_t; 22 | 23 | kissfft( const std::size_t nfft, 24 | const bool inverse ) 25 | :_nfft(nfft) 26 | ,_inverse(inverse) 27 | { 28 | // fill twiddle factors 29 | _twiddles.resize(_nfft); 30 | const scalar_t phinc = (_inverse?2:-2)* std::acos( (scalar_t) -1) / _nfft; 31 | for (std::size_t i=0;i<_nfft;++i) 32 | _twiddles[i] = std::exp( cpx_t(0,i*phinc) ); 33 | 34 | //factorize 35 | //start factoring out 4's, then 2's, then 3,5,7,9,... 36 | std::size_t n= _nfft; 37 | std::size_t p=4; 38 | do { 39 | while (n % p) { 40 | switch (p) { 41 | case 4: p = 2; break; 42 | case 2: p = 3; break; 43 | default: p += 2; break; 44 | } 45 | if (p*p>n) 46 | p = n;// no more factors 47 | } 48 | n /= p; 49 | _stageRadix.push_back(p); 50 | _stageRemainder.push_back(n); 51 | }while(n>1); 52 | } 53 | 54 | 55 | /// Changes the FFT-length and/or the transform direction. 56 | /// 57 | /// @post The @c kissfft object will be in the same state as if it 58 | /// had been newly constructed with the passed arguments. 59 | /// However, the implementation may be faster than constructing a 60 | /// new fft object. 61 | void assign( const std::size_t nfft, 62 | const bool inverse ) 63 | { 64 | if ( nfft != _nfft ) 65 | { 66 | kissfft tmp( nfft, inverse ); // O(n) time. 67 | std::swap( tmp, *this ); // this is O(1) in C++11, O(n) otherwise. 68 | } 69 | else if ( inverse != _inverse ) 70 | { 71 | // conjugate the twiddle factors. 72 | for ( typename std::vector::iterator it = _twiddles.begin(); 73 | it != _twiddles.end(); ++it ) 74 | it->imag( -it->imag() ); 75 | } 76 | } 77 | 78 | /// Calculates the complex Discrete Fourier Transform. 79 | /// 80 | /// The size of the passed arrays must be passed in the constructor. 81 | /// The sum of the squares of the absolute values in the @c dst 82 | /// array will be @c N times the sum of the squares of the absolute 83 | /// values in the @c src array, where @c N is the size of the array. 84 | /// In other words, the l_2 norm of the resulting array will be 85 | /// @c sqrt(N) times as big as the l_2 norm of the input array. 86 | /// This is also the case when the inverse flag is set in the 87 | /// constructor. Hence when applying the same transform twice, but with 88 | /// the inverse flag changed the second time, then the result will 89 | /// be equal to the original input times @c N. 90 | void transform(const cpx_t * fft_in, cpx_t * fft_out, const std::size_t stage = 0, const std::size_t fstride = 1, const std::size_t in_stride = 1) const 91 | { 92 | const std::size_t p = _stageRadix[stage]; 93 | const std::size_t m = _stageRemainder[stage]; 94 | cpx_t * const Fout_beg = fft_out; 95 | cpx_t * const Fout_end = fft_out + p*m; 96 | 97 | if (m==1) { 98 | do{ 99 | *fft_out = *fft_in; 100 | fft_in += fstride*in_stride; 101 | }while(++fft_out != Fout_end ); 102 | }else{ 103 | do{ 104 | // recursive call: 105 | // DFT of size m*p performed by doing 106 | // p instances of smaller DFTs of size m, 107 | // each one takes a decimated version of the input 108 | transform(fft_in, fft_out, stage+1, fstride*p,in_stride); 109 | fft_in += fstride*in_stride; 110 | }while( (fft_out += m) != Fout_end ); 111 | } 112 | 113 | fft_out=Fout_beg; 114 | 115 | // recombine the p smaller DFTs 116 | switch (p) { 117 | case 2: kf_bfly2(fft_out,fstride,m); break; 118 | case 3: kf_bfly3(fft_out,fstride,m); break; 119 | case 4: kf_bfly4(fft_out,fstride,m); break; 120 | case 5: kf_bfly5(fft_out,fstride,m); break; 121 | default: kf_bfly_generic(fft_out,fstride,m,p); break; 122 | } 123 | } 124 | 125 | /// Calculates the Discrete Fourier Transform (DFT) of a real input 126 | /// of size @c 2*N. 127 | /// 128 | /// The 0-th and N-th value of the DFT are real numbers. These are 129 | /// stored in @c dst[0].real() and @c dst[0].imag() respectively. 130 | /// The remaining DFT values up to the index N-1 are stored in 131 | /// @c dst[1] to @c dst[N-1]. 132 | /// The other half of the DFT values can be calculated from the 133 | /// symmetry relation 134 | /// @code 135 | /// DFT(src)[2*N-k] == conj( DFT(src)[k] ); 136 | /// @endcode 137 | /// The same scaling factors as in @c transform() apply. 138 | /// 139 | /// @note For this to work, the types @c scalar_t and @c cpx_t 140 | /// must fulfill the following requirements: 141 | /// 142 | /// For any object @c z of type @c cpx_t, 143 | /// @c reinterpret_cast(z)[0] is the real part of @c z and 144 | /// @c reinterpret_cast(z)[1] is the imaginary part of @c z. 145 | /// For any pointer to an element of an array of @c cpx_t named @c p 146 | /// and any valid array index @c i, @c reinterpret_cast(p)[2*i] 147 | /// is the real part of the complex number @c p[i], and 148 | /// @c reinterpret_cast(p)[2*i+1] is the imaginary part of the 149 | /// complex number @c p[i]. 150 | /// 151 | /// Since C++11, these requirements are guaranteed to be satisfied for 152 | /// @c scalar_ts being @c float, @c double or @c long @c double 153 | /// together with @c cpx_t being @c std::complex. 154 | void transform_real( const scalar_t * const src, 155 | cpx_t * const dst ) const 156 | { 157 | const std::size_t N = _nfft; 158 | if ( N == 0 ) 159 | return; 160 | 161 | // perform complex FFT 162 | transform( reinterpret_cast(src), dst ); 163 | 164 | // post processing for k = 0 and k = N 165 | dst[0] = cpx_t( dst[0].real() + dst[0].imag(), 166 | dst[0].real() - dst[0].imag() ); 167 | 168 | // post processing for all the other k = 1, 2, ..., N-1 169 | const scalar_t pi = std::acos( (scalar_t) -1); 170 | const scalar_t half_phi_inc = ( _inverse ? pi : -pi ) / N; 171 | const cpx_t twiddle_mul = std::exp( cpx_t(0, half_phi_inc) ); 172 | for ( std::size_t k = 1; 2*k < N; ++k ) 173 | { 174 | const cpx_t w = (scalar_t)0.5 * cpx_t( 175 | dst[k].real() + dst[N-k].real(), 176 | dst[k].imag() - dst[N-k].imag() ); 177 | const cpx_t z = (scalar_t)0.5 * cpx_t( 178 | dst[k].imag() + dst[N-k].imag(), 179 | -dst[k].real() + dst[N-k].real() ); 180 | const cpx_t twiddle = 181 | k % 2 == 0 ? 182 | _twiddles[k/2] : 183 | _twiddles[k/2] * twiddle_mul; 184 | dst[ k] = w + twiddle * z; 185 | dst[N-k] = std::conj( w - twiddle * z ); 186 | } 187 | if ( N % 2 == 0 ) 188 | dst[N/2] = std::conj( dst[N/2] ); 189 | } 190 | 191 | private: 192 | 193 | void kf_bfly2( cpx_t * Fout, const size_t fstride, const std::size_t m) const 194 | { 195 | for (std::size_t k=0;k _scratchbuf.size()) _scratchbuf.resize(p); 331 | 332 | for ( std::size_t u=0; u=_nfft) 346 | twidx-=_nfft; 347 | Fout[ k ] += _scratchbuf[q] * twiddles[twidx]; 348 | } 349 | k += m; 350 | } 351 | } 352 | } 353 | 354 | std::size_t _nfft; 355 | bool _inverse; 356 | std::vector _twiddles; 357 | std::vector _stageRadix; 358 | std::vector _stageRemainder; 359 | mutable std::vector _scratchbuf; 360 | }; 361 | #endif 362 | -------------------------------------------------------------------------------- /kissfft.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@PKGCONFIG_KISSFFT_PREFIX@ 2 | libdir=@PKGCONFIG_KISSFFT_LIBDIR@ 3 | includedir=@PKGCONFIG_KISSFFT_INCLUDEDIR@ 4 | 5 | Name: kissfft 6 | Description: A Fast Fourier Transform (FFT) library that tries to Keep it Simple, Stupid 7 | Version: @PKGCONFIG_KISSFFT_VERSION@ 8 | 9 | Libs: @PKG_OPENMP@ -L${libdir} -l@KISSFFT_OUTPUT_NAME@ 10 | Cflags: -I@PKGCONFIG_KISSFFT_PKGINCLUDEDIR@ @PKG_KISSFFT_DEFS@ 11 | -------------------------------------------------------------------------------- /kissfft_i32.hh: -------------------------------------------------------------------------------- 1 | #ifndef KISSFFT_I32_CLASS_HH 2 | #define KISSFFT_I32_CLASS_HH 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // TODO1: substitute complex (behaviour not defined for nonfloats), should be faster 9 | // TODO2: use std:: namespace 10 | // TODO3: make unittests for all ffts (c, cpp, i32) 11 | 12 | template 13 | struct complex_s 14 | { 15 | DType real; 16 | DType imag; 17 | }; 18 | 19 | class kissfft_i32 20 | { 21 | private: 22 | 23 | using scalar_type = int32_t; 24 | using cpx_type = complex; 25 | 26 | scalar_type _scale_factor; 27 | std::size_t _nfft; 28 | bool _inverse; 29 | std::vector _twiddles; 30 | std::vector _stageRadix; 31 | std::vector _stageRemainder; 32 | 33 | public: 34 | 35 | // scale_factor: upscale twiddle-factors otherwise they lie between 0..1 (out of range for integer) --> fixed point math 36 | kissfft_i32(const std::size_t nfft, const bool inverse, const double scale_factor = 1024.0) 37 | : _scale_factor(scalar_type(scale_factor)), _nfft(nfft), _inverse(inverse) 38 | { 39 | // fill twiddle factors 40 | _twiddles.resize(_nfft); 41 | const double phinc = (_inverse ? 2 : -2) * acos(-1.0) / _nfft; 42 | for (std::size_t i = 0; i < _nfft; ++i) 43 | { 44 | _twiddles[i] = scale_factor * exp(complex(0, i * phinc)); 45 | } 46 | //factorize 47 | //start factoring out 4's, then 2's, then 3,5,7,9,... 48 | std::size_t n = _nfft; 49 | std::size_t p = 4; 50 | do 51 | { 52 | while (n % p) 53 | { 54 | switch (p) 55 | { 56 | case 4: 57 | p = 2; 58 | break; 59 | case 2: 60 | p = 3; 61 | break; 62 | default: 63 | p += 2; 64 | break; 65 | } 66 | if (p * p > n) p = n;// no more factors 67 | } 68 | n /= p; 69 | _stageRadix.push_back(p); 70 | _stageRemainder.push_back(n); 71 | } while (n > 1); 72 | } 73 | 74 | /// Calculates the complex Discrete Fourier Transform. 75 | /// 76 | /// The size of the passed arrays must be passed in the constructor. 77 | /// The sum of the squares of the absolute values in the @c dst 78 | /// array will be @c N times the sum of the squares of the absolute 79 | /// values in the @c src array, where @c N is the size of the array. 80 | /// In other words, the l_2 norm of the resulting array will be 81 | /// @c sqrt(N) times as big as the l_2 norm of the input array. 82 | /// This is also the case when the inverse flag is set in the 83 | /// constructor. Hence when applying the same transform twice, but with 84 | /// the inverse flag changed the second time, then the result will 85 | /// be equal to the original input times @c N. 86 | void transform(const cpx_type * FSrc, 87 | cpx_type * FDst, 88 | const std::size_t stage = 0, 89 | const std::size_t fstride = 1, 90 | const std::size_t in_stride = 1) const 91 | { 92 | const std::size_t p = _stageRadix[stage]; 93 | const std::size_t m = _stageRemainder[stage]; 94 | cpx_type *const Fout_beg = FDst; 95 | cpx_type *const Fout_end = FDst + p * m; 96 | 97 | if (m == 1) 98 | { 99 | do 100 | { 101 | *FDst = *FSrc; 102 | FSrc += fstride * in_stride; 103 | } while (++FDst != Fout_end); 104 | } 105 | else 106 | { 107 | do 108 | { 109 | // recursive call: 110 | // DFT of size m*p performed by doing 111 | // p instances of smaller DFTs of size m, 112 | // each one takes a decimated version of the input 113 | transform(FSrc, FDst, stage + 1, fstride * p, in_stride); 114 | FSrc += fstride * in_stride; 115 | } while ((FDst += m) != Fout_end); 116 | } 117 | 118 | FDst = Fout_beg; 119 | 120 | // recombine the p smaller DFTs 121 | switch (p) 122 | { 123 | case 2: 124 | kf_bfly2(FDst, fstride, m); 125 | break; 126 | case 3: 127 | kf_bfly3(FDst, fstride, m); 128 | break; 129 | case 4: 130 | kf_bfly4(FDst, fstride, m); 131 | break; 132 | case 5: 133 | kf_bfly5(FDst, fstride, m); 134 | break; 135 | default: 136 | kf_bfly_generic(FDst, fstride, m, p); 137 | break; 138 | } 139 | } 140 | 141 | private: 142 | 143 | void kf_bfly2(cpx_type *const Fout, const size_t fstride, const std::size_t m) const 144 | { 145 | for (std::size_t k = 0; k < m; ++k) 146 | { 147 | const cpx_type t = (Fout[m + k] * _twiddles[k * fstride]) / _scale_factor; 148 | Fout[m + k] = Fout[k] - t; 149 | Fout[k] += t; 150 | } 151 | } 152 | 153 | void kf_bfly3(cpx_type *Fout, const std::size_t fstride, const std::size_t m) const 154 | { 155 | std::size_t k = m; 156 | const std::size_t m2 = 2 * m; 157 | const cpx_type *tw1, *tw2; 158 | cpx_type scratch[5]; 159 | const cpx_type epi3 = _twiddles[fstride * m]; 160 | 161 | tw1 = tw2 = &_twiddles[0]; 162 | 163 | do 164 | { 165 | scratch[1] = (Fout[m] * *tw1) / _scale_factor; 166 | scratch[2] = (Fout[m2] * *tw2) / _scale_factor; 167 | 168 | scratch[3] = scratch[1] + scratch[2]; 169 | scratch[0] = scratch[1] - scratch[2]; 170 | tw1 += fstride; 171 | tw2 += fstride * 2; 172 | 173 | Fout[m] = Fout[0] - (scratch[3] / 2); 174 | scratch[0] *= epi3.imag(); 175 | scratch[0] /= _scale_factor; 176 | 177 | Fout[0] += scratch[3]; 178 | 179 | Fout[m2] = cpx_type(Fout[m].real() + scratch[0].imag(), Fout[m].imag() - scratch[0].real()); 180 | 181 | Fout[m] += cpx_type(-scratch[0].imag(), scratch[0].real()); 182 | ++Fout; 183 | } while (--k); 184 | } 185 | 186 | void kf_bfly4(cpx_type *const Fout, const std::size_t fstride, const std::size_t m) const 187 | { 188 | cpx_type scratch[7]; 189 | const scalar_type negative_if_inverse = _inverse ? -1 : +1; 190 | 191 | for (std::size_t k = 0; k < m; ++k) 192 | { 193 | scratch[0] = (Fout[k + m] * _twiddles[k * fstride]) / _scale_factor; 194 | scratch[1] = (Fout[k + 2 * m] * _twiddles[k * fstride * 2]) / _scale_factor; 195 | scratch[2] = (Fout[k + 3 * m] * _twiddles[k * fstride * 3]) / _scale_factor; 196 | scratch[5] = Fout[k] - scratch[1]; 197 | 198 | Fout[k] += scratch[1]; 199 | scratch[3] = scratch[0] + scratch[2]; 200 | scratch[4] = scratch[0] - scratch[2]; 201 | scratch[4] = cpx_type(scratch[4].imag() * negative_if_inverse, 202 | -scratch[4].real() * negative_if_inverse); 203 | 204 | Fout[k + 2 * m] = Fout[k] - scratch[3]; 205 | Fout[k] += scratch[3]; 206 | Fout[k + m] = scratch[5] + scratch[4]; 207 | Fout[k + 3 * m] = scratch[5] - scratch[4]; 208 | } 209 | } 210 | 211 | void kf_bfly5(cpx_type *const Fout, const std::size_t fstride, const std::size_t m) const 212 | { 213 | cpx_type *Fout0, *Fout1, *Fout2, *Fout3, *Fout4; 214 | cpx_type scratch[13]; 215 | const cpx_type ya = _twiddles[fstride * m]; 216 | const cpx_type yb = _twiddles[fstride * 2 * m]; 217 | 218 | Fout0 = Fout; 219 | Fout1 = Fout0 + m; 220 | Fout2 = Fout0 + 2 * m; 221 | Fout3 = Fout0 + 3 * m; 222 | Fout4 = Fout0 + 4 * m; 223 | 224 | for (std::size_t u = 0; u < m; ++u) 225 | { 226 | scratch[0] = *Fout0; 227 | 228 | scratch[1] = (*Fout1 * _twiddles[u * fstride]) / _scale_factor; 229 | scratch[2] = (*Fout2 * _twiddles[2 * u * fstride]) / _scale_factor; 230 | scratch[3] = (*Fout3 * _twiddles[3 * u * fstride]) / _scale_factor; 231 | scratch[4] = (*Fout4 * _twiddles[4 * u * fstride]) / _scale_factor; 232 | 233 | scratch[7] = scratch[1] + scratch[4]; 234 | scratch[10] = scratch[1] - scratch[4]; 235 | scratch[8] = scratch[2] + scratch[3]; 236 | scratch[9] = scratch[2] - scratch[3]; 237 | 238 | *Fout0 += scratch[7]; 239 | *Fout0 += scratch[8]; 240 | 241 | scratch[5] = scratch[0] + (cpx_type( 242 | scratch[7].real() * ya.real() + scratch[8].real() * yb.real(), 243 | scratch[7].imag() * ya.real() + scratch[8].imag() * yb.real() ) / _scale_factor); 244 | 245 | scratch[6] = cpx_type( 246 | scratch[10].imag() * ya.imag() + scratch[9].imag() * yb.imag(), 247 | -scratch[10].real() * ya.imag() - scratch[9].real() * yb.imag() ) / _scale_factor; 248 | 249 | *Fout1 = scratch[5] - scratch[6]; 250 | *Fout4 = scratch[5] + scratch[6]; 251 | 252 | scratch[11] = scratch[0] + (cpx_type( 253 | scratch[7].real() * yb.real() + scratch[8].real() * ya.real(), 254 | scratch[7].imag() * yb.real() + scratch[8].imag() * ya.real() ) / _scale_factor); 255 | 256 | scratch[12] = cpx_type( 257 | -scratch[10].imag() * yb.imag() + scratch[9].imag() * ya.imag(), 258 | scratch[10].real() * yb.imag() - scratch[9].real() * ya.imag() ) / _scale_factor; 259 | 260 | *Fout2 = scratch[11] + scratch[12]; 261 | *Fout3 = scratch[11] - scratch[12]; 262 | 263 | ++Fout0; 264 | ++Fout1; 265 | ++Fout2; 266 | ++Fout3; 267 | ++Fout4; 268 | } 269 | } 270 | 271 | /* perform the butterfly for one stage of a mixed radix FFT */ 272 | void kf_bfly_generic(cpx_type * const Fout, const size_t fstride, const std::size_t m, const std::size_t p) const 273 | { 274 | const cpx_type *twiddles = &_twiddles[0]; 275 | cpx_type scratchbuf[p]; 276 | 277 | for (std::size_t u = 0; u < m; ++u) 278 | { 279 | std::size_t k = u; 280 | for (std::size_t q1 = 0; q1 < p; ++q1) 281 | { 282 | scratchbuf[q1] = Fout[k]; 283 | k += m; 284 | } 285 | 286 | k = u; 287 | for (std::size_t q1 = 0; q1 < p; ++q1) 288 | { 289 | std::size_t twidx = 0; 290 | Fout[k] = scratchbuf[0]; 291 | for (std::size_t q = 1; q < p; ++q) 292 | { 293 | twidx += fstride * k; 294 | if (twidx >= _nfft) 295 | twidx -= _nfft; 296 | Fout[k] += (scratchbuf[q] * twiddles[twidx]) / _scale_factor; 297 | } 298 | k += m; 299 | } 300 | } 301 | } 302 | }; 303 | 304 | #endif 305 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | function(add_kissfft_test_executable NAME) 2 | add_kissfft_executable(${NAME} ${ARGN}) 3 | target_include_directories(${NAME} PRIVATE ..) 4 | 5 | add_test(NAME ${NAME} COMMAND ${NAME}) 6 | set_tests_properties(${NAME} PROPERTIES TIMEOUT 3600) 7 | endfunction() 8 | 9 | set(KISSFFT_TEST_NUMFFTS 10000) 10 | 11 | # 12 | # Add tools-independent fastfilt_* (../tools/fft_*) executable without adding a test 13 | # 14 | 15 | add_kissfft_executable(fastfilt ../tools/fftutil.c) 16 | target_include_directories(fastfilt PRIVATE ..) 17 | 18 | # 19 | # Add test executables and define tests 20 | # 21 | 22 | add_kissfft_test_executable(bm_kiss benchkiss.c pstats.c) 23 | # add_test(NAME benchmar COMMAND ${NAME}) 24 | # set_tests_properties(${NAME} PROPERTIES TIMEOUT 3600) 25 | 26 | include(FindPkgConfig) 27 | if(KISSFFT_FLOAT) 28 | set(fftw3_pkg fftw3f) 29 | else() 30 | set(fftw3_pkg fftw3) 31 | endif() 32 | pkg_check_modules(fftw3 REQUIRED IMPORTED_TARGET ${fftw3_pkg}) 33 | add_kissfft_test_executable(bm_fftw benchfftw.c pstats.c) 34 | target_link_libraries(bm_fftw PRIVATE PkgConfig::fftw3) 35 | 36 | add_kissfft_test_executable(st twotonetest.c) 37 | 38 | add_kissfft_test_executable(tkfc twotonetest.c) 39 | target_compile_definitions(tkfc PRIVATE KFC_TEST) 40 | 41 | add_kissfft_test_executable(ffr twotonetest.c) 42 | add_kissfft_test_executable(tr test_real.c) 43 | 44 | add_kissfft_test_executable(testcpp testcpp.cc) 45 | 46 | if(KISSFFT_DATATYPE MATCHES "^simd$") 47 | add_kissfft_test_executable(tsimd test_simd.c) 48 | target_compile_definitions(tsimd PRIVATE USE_SIMD) 49 | if (NOT MSVC) 50 | target_compile_options(kissfft PRIVATE -msse) 51 | else() 52 | target_compile_options(kissfft PRIVATE "/arch:SSE") 53 | endif() 54 | endif() 55 | 56 | find_package(PythonInterp REQUIRED) 57 | add_test(NAME testkiss.py COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/testkiss.py") 58 | list(APPEND TESTKISS_PY_ENV "KISSFFT_DATATYPE=${KISSFFT_DATATYPE}") 59 | list(APPEND TESTKISS_PY_ENV "KISSFFT_OPENMP=${KISSFFT_OPENMP}") 60 | set_tests_properties(testkiss.py PROPERTIES 61 | TIMEOUT 3600 62 | ENVIRONMENT "${TESTKISS_PY_ENV}" 63 | WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") 64 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Warnings 3 | # 4 | 5 | WARNINGS = -W -Wall -Wstrict-prototypes -Wmissing-prototypes \ 6 | -Wcast-align -Wcast-qual -Wnested-externs -Wshadow -Wbad-function-cast \ 7 | -Wwrite-strings 8 | 9 | # 10 | # Compile-time definitions 11 | # 12 | 13 | CFLAGS = -O3 -I.. -I../tools $(WARNINGS) 14 | CFLAGS += -ffast-math -fomit-frame-pointer 15 | #CFLAGS += -funroll-loops 16 | #CFLAGS += -march=prescott 17 | #CFLAGS += -mtune=native 18 | # TIP: try adding -openmp or -fopenmp to enable OPENMP directives and use of multiple cores 19 | #CFLAGS += -fopenmp 20 | CFLAGS += $(CFLAGADD) 21 | 22 | CXXFLAGS = -O3 -ffast-math -fomit-frame-pointer -I.. -W -Wall -march=native -mtune=native 23 | 24 | # 25 | # Count of FFT runs tested 26 | # 27 | 28 | ifeq "$(NFFT)" "" 29 | NFFT = 1800 30 | endif 31 | ifeq "$(NUMFFTS)" "" 32 | NUMFFTS = 10000 33 | endif 34 | 35 | # 36 | # Test binary executable names 37 | # 38 | 39 | SELFTESTSRC = twotonetest.c 40 | 41 | ifneq ($(KISSFFT_OPENMP),1) 42 | BENCHKISS = bm-kiss-$(KISSFFT_DATATYPE) 43 | BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE) 44 | SELFTEST = st-$(KISSFFT_DATATYPE) 45 | TESTREAL = tr-$(KISSFFT_DATATYPE) 46 | TESTKFC = tkfc-$(KISSFFT_DATATYPE) 47 | TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE) 48 | TESTCPP = testcpp-$(KISSFFT_DATATYPE) 49 | TESTSIMD = testsimd 50 | else 51 | BENCHKISS = bm-kiss-$(KISSFFT_DATATYPE)-openmp 52 | BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE)-openmp 53 | SELFTEST = st-$(KISSFFT_DATATYPE)-openmp 54 | TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp 55 | TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp 56 | TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp 57 | TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp 58 | TESTSIMD = testsimd-openmp 59 | CFLAGS += -fopenmp 60 | CXXFLAGS += -fopenmp 61 | endif 62 | 63 | ifeq "$(KISSFFT_DATATYPE)" "float" 64 | # fftw needs to be built with --enable-float to build this lib 65 | FFTWLIB = -lfftw3f 66 | else 67 | FFTWLIB = -lfftw3 68 | endif 69 | 70 | FFTWLIBDIR ?= $(ABS_LIBDIR) 71 | ABS_FFTWLIBDIR = $(abspath $(FFTWLIBDIR)) 72 | 73 | # 74 | # Check missing external libraries 75 | # 76 | 77 | ifneq ($(MAKECMDGOALS),clean) 78 | LIBFFTW_MISSING = $(shell echo "int main(){return 0;}" > _test_library_dummy.c; \ 79 | $(CC) -o _test_library_dummy _test_library_dummy.c $(FFTWLIB) -L$(ABS_FFTWLIBDIR); \ 80 | echo $$?; \ 81 | rm -f _test_library_dummy.c _test_library_dummy) 82 | endif 83 | 84 | # 85 | # Find Python interpreter 86 | # 87 | 88 | ifneq ($(MAKECMDGOALS),clean) 89 | PYTHON_INTERPRETER = $(shell python --version) 90 | ifeq ($(PYTHON_INTERPRETER), ) 91 | PYTHON_INTERPRETER = $(shell python2 --version) 92 | ifeq ($(PYTHON_INTERPRETER), ) 93 | PYTHON_INTERPRETER = $(shell python3 --version) 94 | ifeq ($(PYTHON_INTERPRETER), ) 95 | $(error ERROR: Can not find Python interpreter!) 96 | else 97 | PYTHON_INTERPRETER = "python3" 98 | endif 99 | else 100 | PYTHON_INTERPRETER = "python2" 101 | endif 102 | else 103 | PYTHON_INTERPRETER = "python" 104 | endif 105 | endif 106 | 107 | # 108 | # Target: "make all" 109 | # 110 | 111 | all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTKFC) $(TESTFASTFILT) 112 | 113 | # 114 | # Individual test make rules 115 | # 116 | 117 | $(SELFTEST): $(SELFTESTSRC) 118 | $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 119 | 120 | $(TESTKFC): ../kfc.c 121 | $(CC) -o $@ $(CFLAGS) -DKFC_TEST $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 122 | 123 | $(TESTREAL): test_real.c 124 | $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 125 | 126 | $(BENCHKISS): benchkiss.c pstats.c 127 | $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 128 | 129 | $(TESTFASTFILT): ../tools/fftutil.c 130 | $(CC) -o $@ $(CFLAGS) -DKFC_TEST $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 131 | 132 | $(BENCHFFTW): benchfftw.c pstats.c 133 | $(warning ======attempting to build FFTW benchmark) 134 | ifeq ($(LIBFFTW_MISSING), 0) 135 | $(CC) -o $@ $(CFLAGS) -DDATATYPE$(KISSFFT_DATATYPE) $^ $(FFTWLIB) -L$(ABS_FFTWLIBDIR) -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 136 | else 137 | $(warning WARNING: No FFTW development files found! FFTW not available for comparison!) 138 | endif 139 | 140 | # 141 | # Test SSE 142 | # 143 | 144 | $(TESTSIMD): test_simd.c 145 | ifeq "$(KISSFFT_DATATYPE)" "simd" 146 | $(CC) -o $@ -g $(CFLAGS) -DUSE_SIMD=1 -msse $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 147 | else 148 | $(error ERROR: This test makes sense only with KISSFFT_DATATYPE=simd) 149 | endif 150 | 151 | testsse: $(TESTSIMD) 152 | LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTSIMD) 153 | 154 | # 155 | # Test C++ 156 | # 157 | 158 | $(TESTCPP): testcpp.cc ../kissfft.hh 159 | $(CXX) -o $@ $(CXXFLAGS) testcpp.cc -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 160 | 161 | testcpp: $(TESTCPP) 162 | LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTCPP) 163 | 164 | # 165 | # Target: "make test" 166 | # 167 | 168 | test: all 169 | ifeq "$(KISSFFT_DATATYPE)" "simd" 170 | $(MAKE) testsse 171 | endif 172 | @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTKFC) 173 | $(warning ======1d & 2-d complex fft self test (type= $(KISSFFT_DATATYPE) )) 174 | @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(SELFTEST) 175 | $(warning ======real FFT (type= $(KISSFFT_DATATYPE) )) 176 | @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTREAL) 177 | $(warning ======timing test (type=$(KISSFFT_DATATYPE))) 178 | @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHKISS) -x $(NUMFFTS) -n $(NFFT) 179 | @[ -x ./$(BENCHFFTW) ] && LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHFFTW) -x $(NUMFFTS) -n $(NFFT) || true 180 | $(warning ======higher dimensions (type=$(KISSFFT_DATATYPE))) 181 | @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." $(PYTHON_INTERPRETER) ./testkiss.py 182 | 183 | # 184 | # Target: "make clean" 185 | # 186 | 187 | clean: 188 | rm -f *~ bm-* st-* tr-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c 189 | -------------------------------------------------------------------------------- /test/benchfftw.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "pstats.h" 13 | 14 | #ifdef DATATYPEdouble 15 | 16 | #define CPXTYPE fftw_complex 17 | #define PLAN fftw_plan 18 | #define FFTMALLOC fftw_malloc 19 | #define MAKEPLAN fftw_plan_dft_1d 20 | #define DOFFT fftw_execute 21 | #define DESTROYPLAN fftw_destroy_plan 22 | #define FFTFREE fftw_free 23 | 24 | #elif defined(DATATYPEfloat) 25 | 26 | #define CPXTYPE fftwf_complex 27 | #define PLAN fftwf_plan 28 | #define FFTMALLOC fftwf_malloc 29 | #define MAKEPLAN fftwf_plan_dft_1d 30 | #define DOFFT fftwf_execute 31 | #define DESTROYPLAN fftwf_destroy_plan 32 | #define FFTFREE fftwf_free 33 | 34 | #endif 35 | 36 | #ifndef CPXTYPE 37 | int main(void) 38 | { 39 | fprintf(stderr,"Datatype not available in FFTW\n" ); 40 | return 0; 41 | } 42 | #else 43 | int main(int argc,char ** argv) 44 | { 45 | int nfft=1024; 46 | int isinverse=0; 47 | int numffts=1000,i; 48 | 49 | CPXTYPE * in=NULL; 50 | CPXTYPE * out=NULL; 51 | PLAN p; 52 | 53 | pstats_init(); 54 | 55 | while (1) { 56 | int c = getopt (argc, argv, "n:ix:h"); 57 | if (c == -1) 58 | break; 59 | switch (c) { 60 | case 'n': 61 | nfft = atoi (optarg); 62 | break; 63 | case 'x': 64 | numffts = atoi (optarg); 65 | break; 66 | case 'i': 67 | isinverse = 1; 68 | break; 69 | case 'h': 70 | case '?': 71 | default: 72 | fprintf(stderr,"options:\n-n N: complex fft length\n-i: inverse\n-x N: number of ffts to compute\n" 73 | ""); 74 | } 75 | } 76 | 77 | in=FFTMALLOC(sizeof(CPXTYPE) * nfft); 78 | out=FFTMALLOC(sizeof(CPXTYPE) * nfft); 79 | for (i=0;i 9 | #include 10 | #include 11 | #include 12 | #include "kiss_fft.h" 13 | #include "kiss_fftr.h" 14 | #include "kiss_fftnd.h" 15 | #include "kiss_fftndr.h" 16 | 17 | #include "pstats.h" 18 | 19 | static 20 | int getdims(int * dims, char * arg) 21 | { 22 | char *s; 23 | int ndims=0; 24 | while ( (s=strtok( arg , ",") ) ) { 25 | dims[ndims++] = atoi(s); 26 | //printf("%s=%d\n",s,dims[ndims-1]); 27 | arg=NULL; 28 | } 29 | return ndims; 30 | } 31 | 32 | int main(int argc,char ** argv) 33 | { 34 | int k; 35 | int nfft[32]; 36 | int ndims = 1; 37 | int isinverse=0; 38 | int numffts=1000,i; 39 | kiss_fft_cpx * buf; 40 | kiss_fft_cpx * bufout; 41 | int real = 0; 42 | 43 | nfft[0] = 1024;// default 44 | 45 | while (1) { 46 | int c = getopt (argc, argv, "n:ix:r"); 47 | if (c == -1) 48 | break; 49 | switch (c) { 50 | case 'r': 51 | real = 1; 52 | break; 53 | case 'n': 54 | ndims = getdims(nfft, optarg ); 55 | if (nfft[0] != kiss_fft_next_fast_size(nfft[0]) ) { 56 | int ng = kiss_fft_next_fast_size(nfft[0]); 57 | fprintf(stderr,"warning: %d might be a better choice for speed than %d\n",ng,nfft[0]); 58 | } 59 | break; 60 | case 'x': 61 | numffts = atoi (optarg); 62 | break; 63 | case 'i': 64 | isinverse = 1; 65 | break; 66 | } 67 | } 68 | int nbytes = sizeof(kiss_fft_cpx); 69 | for (k=0;k 12 | 13 | #include "kiss_fft.h" 14 | #include "kiss_fftnd.h" 15 | #include "kiss_fftr.h" 16 | 17 | BEGIN_BENCH_DOC 18 | BENCH_DOC("name", "kissfft") 19 | BENCH_DOC("version", "1.0.1") 20 | BENCH_DOC("year", "2004") 21 | BENCH_DOC("author", "Mark Borgerding") 22 | BENCH_DOC("language", "C") 23 | BENCH_DOC("url", "http://sourceforge.net/projects/kissfft/") 24 | BENCH_DOC("copyright", 25 | "Copyright (c) 2003,4 Mark Borgerding\n" 26 | "\n" 27 | "All rights reserved.\n" 28 | "\n" 29 | "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n" 30 | "\n" 31 | " * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n" 32 | " * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n" 33 | " * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n" 34 | "\n" 35 | "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n") 36 | END_BENCH_DOC 37 | 38 | int can_do(struct problem *p) 39 | { 40 | if (p->rank == 1) { 41 | if (p->kind == PROBLEM_REAL) { 42 | return (p->n[0] & 1) == 0; /* only even real is okay */ 43 | } else { 44 | return 1; 45 | } 46 | } else { 47 | return p->kind == PROBLEM_COMPLEX; 48 | } 49 | } 50 | 51 | static kiss_fft_cfg cfg=NULL; 52 | static kiss_fftr_cfg cfgr=NULL; 53 | static kiss_fftnd_cfg cfgnd=NULL; 54 | 55 | #define FAILIF( c ) \ 56 | if ( c ) do {\ 57 | fprintf(stderr,\ 58 | "kissfft: " #c " (file=%s:%d errno=%d %s)\n",\ 59 | __FILE__,__LINE__ , errno,strerror( errno ) ) ;\ 60 | exit(1);\ 61 | }while(0) 62 | 63 | 64 | 65 | void setup(struct problem *p) 66 | { 67 | size_t i; 68 | 69 | /* 70 | fprintf(stderr,"%s %s %d-d ", 71 | (p->sign == 1)?"Inverse":"Forward", 72 | p->kind == PROBLEM_COMPLEX?"Complex":"Real", 73 | p->rank); 74 | */ 75 | if (p->rank == 1) { 76 | if (p->kind == PROBLEM_COMPLEX) { 77 | cfg = kiss_fft_alloc (p->n[0], (p->sign == 1), 0, 0); 78 | FAILIF(cfg==NULL); 79 | }else{ 80 | cfgr = kiss_fftr_alloc (p->n[0], (p->sign == 1), 0, 0); 81 | FAILIF(cfgr==NULL); 82 | } 83 | }else{ 84 | int dims[5]; 85 | for (i=0;irank;++i){ 86 | dims[i] = p->n[i]; 87 | } 88 | /* multi-dimensional */ 89 | if (p->kind == PROBLEM_COMPLEX) { 90 | cfgnd = kiss_fftnd_alloc( dims , p->rank, (p->sign == 1), 0, 0 ); 91 | FAILIF(cfgnd==NULL); 92 | } 93 | } 94 | } 95 | 96 | void doit(int iter, struct problem *p) 97 | { 98 | int i; 99 | void *in = p->in; 100 | void *out = p->out; 101 | 102 | if (p->in_place) 103 | out = p->in; 104 | 105 | if (p->rank == 1) { 106 | if (p->kind == PROBLEM_COMPLEX){ 107 | for (i = 0; i < iter; ++i) 108 | kiss_fft (cfg, in, out); 109 | } else { 110 | /* PROBLEM_REAL */ 111 | if (p->sign == -1) /* FORWARD */ 112 | for (i = 0; i < iter; ++i) 113 | kiss_fftr (cfgr, in, out); 114 | else 115 | for (i = 0; i < iter; ++i) 116 | kiss_fftri (cfgr, in, out); 117 | } 118 | }else{ 119 | /* multi-dimensional */ 120 | for (i = 0; i < iter; ++i) 121 | kiss_fftnd(cfgnd,in,out); 122 | } 123 | } 124 | 125 | void done(struct problem *p) 126 | { 127 | free(cfg); 128 | cfg=NULL; 129 | free(cfgr); 130 | cfgr=NULL; 131 | free(cfgnd); 132 | cfgnd=NULL; 133 | UNUSED(p); 134 | } 135 | -------------------------------------------------------------------------------- /test/kissfft-testsuite.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Test suite for kissfft 5 | # 6 | # Copyright (c) 2021, Vasyl Gello. 7 | # This file is part of KISS FFT - https://github.com/mborgerding/kissfft 8 | # 9 | # SPDX-License-Identifier: BSD-3-Clause 10 | # See COPYING file for more information. 11 | # 12 | 13 | if [ ! -f CHANGELOG ] && [ ! -f kiss_fft.h ]; then 14 | echo "ERROR: Please run this testsuite from top level of kissfft source tree!" >&2 15 | return 1 16 | fi 17 | 18 | TESTSUITEOUTDIR="$2" 19 | 20 | if [ -z "$TESTSUITEOUTDIR" ]; then 21 | TESTSUITEOUTDIR="/tmp/kissfft-testsuite" 22 | fi 23 | 24 | if ! mkdir -p "$TESTSUITEOUTDIR"; then 25 | echo "ERROR: Can not create directory '$TESTSUITEOUTDIR'!" >&2 26 | return 1 27 | fi 28 | 29 | # 30 | # Test runner function 31 | # 32 | # Parameters: 33 | # 34 | # $1 - Action: "test" or "install" 35 | # $2 - Build type: "make" or "cmake" 36 | # $3 - Data type: "float" "double" "int16_t" "int32_t" "simd" 37 | # $4 - library type: "shared" or "static" 38 | # $5 - Include tools: "yes" or "no" 39 | # $6 - Install root dir: "existing writable directory" 40 | # 41 | 42 | test_runner() { 43 | _ACTION="$1" 44 | _BUILD_TYPE="$2" 45 | _DATA_TYPE="$3" 46 | _LIB_TYPE="$4" 47 | _OPENMP="$5" 48 | _INCLUDE_TOOLS="$6" 49 | _INSTALL_ROOT_DIR="$7" 50 | 51 | _CMAKE_OPTS="" 52 | _MAKE_OPTS="" 53 | 54 | # Prepare install directory name without "$_OPENMP" and "$_INCLUDE_TOOLS" 55 | 56 | _INSTALL_DIR="$_INSTALL_ROOT_DIR/$_BUILD_TYPE/$_DATA_TYPE/$_LIB_TYPE" 57 | 58 | # Prepare log file without "$_OPENMP" and "$_INCLUDE_TOOLS" 59 | 60 | _LOG_FILE="$_INSTALL_ROOT_DIR/$_ACTION-$_BUILD_TYPE-$_DATA_TYPE-$_LIB_TYPE" 61 | 62 | # Validate parameters 63 | 64 | # Create install root directory 65 | 66 | if [ -z "$_INSTALL_ROOT_DIR" ]; then 67 | echo "" >&2 68 | echo "ERROR: Empty path to writeable directory" >&2 69 | echo "" >&2 70 | return 1 71 | fi 72 | 73 | if [ ! -d "$_INSTALL_ROOT_DIR" ]; then 74 | if ! mkdir -p "$_INSTALL_ROOT_DIR"; then 75 | echo "" >&2 76 | echo "ERROR: Can not create directory '$_INSTALL_ROOT_DIR'" >&2 77 | echo "" >&2 78 | return 1 79 | fi 80 | fi 81 | 82 | if [ "$_BUILD_TYPE" != "make" ] && [ "$_BUILD_TYPE" != "cmake" ]; then 83 | echo "ERROR: Build type must be one of: cmake make" >&2 84 | echo "" >&2 85 | return 1 86 | fi 87 | 88 | if [ "$_DATA_TYPE" != "double" ] && 89 | [ "$_DATA_TYPE" != "float" ] && 90 | [ "$_DATA_TYPE" != "int16_t" ] && 91 | [ "$_DATA_TYPE" != "int32_t" ] && 92 | [ "$_DATA_TYPE" != "simd" ]; 93 | then 94 | echo "ERROR: Data type must be one of: double float int16_t int32_t simd" >&2 95 | echo "" >&2 96 | return 1 97 | else 98 | _MAKE_OPTS="$_MAKE_OPTS KISSFFT_DATATYPE=$_DATA_TYPE" 99 | _CMAKE_OPTS="$_CMAKE_OPTS -DKISSFFT_DATATYPE=$_DATA_TYPE" 100 | fi 101 | 102 | if [ "$_LIB_TYPE" != "shared" ] && [ "$_LIB_TYPE" != "static" ]; then 103 | echo "ERROR: Library type must be one of: shared static" >&2 104 | echo "" >&2 105 | return 1 106 | fi 107 | 108 | case "$_LIB_TYPE" in 109 | "shared") 110 | ;; 111 | "static") 112 | _MAKE_OPTS="$_MAKE_OPTS KISSFFT_STATIC=1" 113 | _CMAKE_OPTS="$_CMAKE_OPTS -DKISSFFT_STATIC=ON" 114 | ;; 115 | "*") 116 | echo "ERROR: OpenMP inclusion must be one of: no yes" >&2 117 | echo "" >&2 118 | return 1 119 | ;; 120 | esac 121 | 122 | case "$_OPENMP" in 123 | "yes") 124 | _INSTALL_DIR="$_INSTALL_DIR/openmp" 125 | _LOG_FILE="$_LOG_FILE-openmp" 126 | _MAKE_OPTS="$_MAKE_OPTS KISSFFT_OPENMP=1" 127 | _CMAKE_OPTS="$_CMAKE_OPTS -DKISSFFT_OPENMP=ON" 128 | ;; 129 | "no") 130 | _INSTALL_DIR="$_INSTALL_DIR/noopenmp" 131 | _LOG_FILE="$_LOG_FILE-noopenmp" 132 | ;; 133 | "*") 134 | echo "ERROR: OpenMP inclusion must be one of: no yes" >&2 135 | echo "" >&2 136 | return 1 137 | ;; 138 | esac 139 | 140 | case "$_INCLUDE_TOOLS" in 141 | "yes") 142 | _INSTALL_DIR="$_INSTALL_DIR/tools" 143 | _LOG_FILE="$_LOG_FILE-tools" 144 | ;; 145 | "no") 146 | _INSTALL_DIR="$_INSTALL_DIR/notools" 147 | _LOG_FILE="$_LOG_FILE-notools" 148 | _MAKE_OPTS="$_MAKE_OPTS KISSFFT_TOOLS=0" 149 | _CMAKE_OPTS="$_CMAKE_OPTS -DKISSFFT_TOOLS=OFF" 150 | ;; 151 | "*") 152 | echo "ERROR: Tools inclusion must be one of: no yes" >&2 153 | echo "" >&2 154 | return 1 155 | ;; 156 | esac 157 | 158 | # Clean kissfft 159 | 160 | rm -rf build 1>/dev/null 2>/dev/null 161 | make clean 1>/dev/null 2>&1 162 | 163 | # Prepare status line 164 | 165 | _STATUS_LINE="Running: $(printf "% 10s" "$_ACTION") |" 166 | _STATUS_LINE="$_STATUS_LINE Build Type: $(printf "% 7s" "$_BUILD_TYPE") |" 167 | _STATUS_LINE="$_STATUS_LINE Data Type: $(printf "% 7s" "$_DATA_TYPE") |" 168 | _STATUS_LINE="$_STATUS_LINE Lib Type: $(printf "% 7s" "$_LIB_TYPE") |" 169 | _STATUS_LINE="$_STATUS_LINE OpenMP: $(printf "% 3s" "$_OPENMP") |" 170 | _STATUS_LINE="$_STATUS_LINE Tools: $(printf "% 3s" "$_INCLUDE_TOOLS") |" 171 | 172 | # Skip tests with tools not installed as they are same as with tools 173 | 174 | if [ "$_ACTION" = "test" ] && [ "$_INCLUDE_TOOLS" = "no" ]; then 175 | return 2 176 | fi 177 | 178 | # Run selected action 179 | 180 | echo "$_STATUS_LINE" 181 | 182 | case "$_ACTION" in 183 | "test") 184 | _MAKE_OPTS="$_MAKE_OPTS PREFIX=$_INSTALL_DIR" 185 | _CMAKE_OPTS="$_CMAKE_OPTS -DCMAKE_INSTALL_PREFIX=$_INSTALL_DIR" 186 | 187 | case "$_BUILD_TYPE" in 188 | "make") 189 | make $_MAKE_OPTS all 1>>"$_LOG_FILE" 2>&1 && 190 | make $_MAKE_OPTS testsingle 1>>"$_LOG_FILE" 2>&1 && 191 | _RET=$? 192 | ;; 193 | "cmake") 194 | mkdir build 1>/dev/null 2>&1 && 195 | cd build && 196 | cmake $_CMAKE_OPTS .. 1>"$_LOG_FILE" 2>&1 && 197 | make all 1>>"$_LOG_FILE" 2>&1 && 198 | make test 1>>"$_LOG_FILE" 2>&1 199 | _RET=$? 200 | cd .. 201 | ;; 202 | esac 203 | ;; 204 | "install") 205 | _MAKE_OPTS="$_MAKE_OPTS PREFIX=$_INSTALL_DIR" 206 | _CMAKE_OPTS="$_CMAKE_OPTS -DCMAKE_INSTALL_PREFIX=$_INSTALL_DIR" 207 | 208 | case "$_BUILD_TYPE" in 209 | "make") 210 | make $_MAKE_OPTS install 1>>"$_LOG_FILE" 2>&1 211 | _RET=$? 212 | ;; 213 | "cmake") 214 | mkdir build 1>/dev/null 2>&1 && 215 | cd build && 216 | cmake $_CMAKE_OPTS .. 1>"$_LOG_FILE" 2>&1 && 217 | make all 1>>"$_LOG_FILE" 2>&1 && 218 | make install 1>>"$_LOG_FILE" 2>&1 219 | _RET=$? 220 | cd .. 221 | ;; 222 | esac 223 | ;; 224 | *) 225 | echo "ERROR: Action must be one of: test install" >&2 226 | echo "" >&2 227 | return 1 228 | ;; 229 | esac 230 | 231 | # Clean kissfft 232 | 233 | rm -rf build 1>/dev/null 2>/dev/null 234 | make clean 1>/dev/null 2>&1 235 | 236 | # Return result 237 | 238 | [ $_RET -eq 0 ] && return 0 || return 1 239 | } 240 | 241 | # Main script 242 | 243 | for ACTION in test install; do 244 | for BUILD_TYPE in make cmake; do 245 | for DATA_TYPE in float double int16_t int32_t simd; do 246 | for LIB_TYPE in shared static; do 247 | for OPENMP in no yes; do 248 | for INCLUDE_TOOLS in no yes; do 249 | test_runner \ 250 | "$ACTION" \ 251 | "$BUILD_TYPE" \ 252 | "$DATA_TYPE" \ 253 | "$LIB_TYPE" \ 254 | "$OPENMP" \ 255 | "$INCLUDE_TOOLS" \ 256 | "$TESTSUITEOUTDIR" 257 | 258 | case $? in 259 | 0) 260 | echo "Result: OK" 261 | ;; 262 | 1) 263 | echo "Result: FAIL" 264 | ;; 265 | 2) 266 | # Ignore it 267 | echo "Result: IGNORE" 1>/dev/null 268 | ;; 269 | esac 270 | done 271 | done 272 | done 273 | done 274 | done 275 | done 2>&1 | tee "$TESTSUITEOUTDIR/all-tests.log" 276 | -------------------------------------------------------------------------------- /test/pstats.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "pstats.h" 15 | 16 | static struct tms tms_beg; 17 | static struct tms tms_end; 18 | static int has_times = 0; 19 | 20 | 21 | void pstats_init(void) 22 | { 23 | has_times = times(&tms_beg) != -1; 24 | } 25 | 26 | static void tms_report(void) 27 | { 28 | double cputime; 29 | if (! has_times ) 30 | return; 31 | times(&tms_end); 32 | cputime = ( ((float)tms_end.tms_utime + tms_end.tms_stime + tms_end.tms_cutime + tms_end.tms_cstime ) - 33 | ((float)tms_beg.tms_utime + tms_beg.tms_stime + tms_beg.tms_cutime + tms_beg.tms_cstime ) ) 34 | / sysconf(_SC_CLK_TCK); 35 | fprintf(stderr,"\tcputime=%.3f\n" , cputime); 36 | } 37 | 38 | static void ps_report(void) 39 | { 40 | char buf[1024]; 41 | #ifdef __APPLE__ /* MAC OS X */ 42 | sprintf(buf,"ps -o command,majflt,minflt,rss,pagein,vsz -p %d 1>&2",getpid() ); 43 | #else /* GNU/Linux */ 44 | sprintf(buf,"ps -o comm,majflt,minflt,rss,drs,pagein,sz,trs,vsz %d 1>&2",getpid() ); 45 | #endif 46 | if (system( buf )==-1) { 47 | perror("system call to ps failed"); 48 | } 49 | } 50 | 51 | void pstats_report() 52 | { 53 | ps_report(); 54 | tms_report(); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /test/pstats.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | #ifndef PSTATS_H 9 | #define PSTATS_H 10 | 11 | void pstats_init(void); 12 | void pstats_report(void); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /test/test_real.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | #include "kiss_fftr.h" 9 | #include "_kiss_fft_guts.h" 10 | #include 11 | #include 12 | #include 13 | 14 | static double cputime(void) 15 | { 16 | struct tms t; 17 | times(&t); 18 | return (double)(t.tms_utime + t.tms_stime)/ sysconf(_SC_CLK_TCK) ; 19 | } 20 | 21 | static 22 | kiss_fft_scalar rand_scalar(void) 23 | { 24 | #ifdef USE_SIMD 25 | return _mm_set1_ps(rand()-RAND_MAX/2); 26 | #else 27 | kiss_fft_scalar s = (kiss_fft_scalar)(rand() -RAND_MAX/2); 28 | return s/2; 29 | #endif 30 | } 31 | 32 | static 33 | double snr_compare( kiss_fft_cpx * vec1,kiss_fft_cpx * vec2, int n) 34 | { 35 | int k; 36 | double sigpow=1e-10,noisepow=1e-10,err,snr,scale=0; 37 | 38 | #ifdef USE_SIMD 39 | float *fv1 = (float*)vec1; 40 | float *fv2 = (float*)vec2; 41 | for (k=0;k<8*n;++k) { 42 | sigpow += *fv1 * *fv1; 43 | err = *fv1 - *fv2; 44 | noisepow += err*err; 45 | ++fv1; 46 | ++fv2; 47 | } 48 | #else 49 | for (k=0;k1) 81 | nfft = atoi(argv[1]); 82 | kiss_fft_cpx cin[nfft]; 83 | kiss_fft_cpx cout[nfft]; 84 | kiss_fft_cpx sout[nfft]; 85 | kiss_fft_cfg kiss_fft_state; 86 | kiss_fftr_cfg kiss_fftr_state; 87 | 88 | kiss_fft_scalar rin[nfft+2]; 89 | kiss_fft_scalar rout[nfft+2]; 90 | kiss_fft_scalar zero; 91 | memset(&zero,0,sizeof(zero) ); // ugly way of setting short,int,float,double, or __m128 to zero 92 | 93 | srand(time(0)); 94 | 95 | for (i=0;i 2 | 3 | static void test1(void) 4 | { 5 | int is_inverse = 1; 6 | int n[2] = {256,256}; 7 | size_t nbytes = sizeof(kiss_fft_cpx)*n[0]*n[1]; 8 | 9 | kiss_fft_cpx * inbuf = _mm_malloc(nbytes,16); 10 | kiss_fft_cpx * outbuf = _mm_malloc(nbytes,16); 11 | memset(inbuf,0,nbytes); 12 | memset(outbuf,0,nbytes); 13 | 14 | kiss_fftnd_cfg cfg = kiss_fftnd_alloc(n,2,is_inverse,0,0); 15 | kiss_fftnd(cfg,inbuf,outbuf); 16 | kiss_fft_free(cfg); 17 | _mm_free(inbuf); 18 | _mm_free(outbuf); 19 | } 20 | 21 | int main(void) 22 | { 23 | test1(); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /test/testcpp.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | #include "kissfft.hh" 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | static inline 15 | double curtime(void) 16 | { 17 | struct timeval tv; 18 | gettimeofday(&tv, NULL); 19 | return (double)tv.tv_sec + (double)tv.tv_usec*.000001; 20 | } 21 | 22 | using namespace std; 23 | 24 | template 25 | void dotest(int nfft) 26 | { 27 | typedef kissfft FFT; 28 | typedef std::complex cpx_type; 29 | 30 | cout << "type:" << typeid(T).name() << " nfft:" << nfft; 31 | 32 | FFT fft(nfft,false); 33 | 34 | vector inbuf(nfft); 35 | vector outbuf(nfft); 36 | for (int k=0;k acc = 0; 46 | long double phinc = 2*k0* M_PIl / nfft; 47 | for (int k1=0;k1 x(inbuf[k1].real(),inbuf[k1].imag()); 49 | acc += x * exp( complex(0,-k1*phinc) ); 50 | } 51 | totalpower += norm(acc); 52 | complex x(outbuf[k0].real(),outbuf[k0].imag()); 53 | complex dif = acc - x; 54 | difpower += norm(dif); 55 | } 56 | cout << " RMSE:" << sqrt(difpower/totalpower) << "\t"; 57 | 58 | double t0 = curtime(); 59 | int nits=20e6/nfft; 60 | for (int k=0;k1) { 70 | for (int k=1;k(nfft); dotest(nfft); dotest(nfft); 73 | } 74 | }else{ 75 | dotest(32); dotest(32); dotest(32); 76 | dotest(1024); dotest(1024); dotest(1024); 77 | dotest(840); dotest(840); dotest(840); 78 | } 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /test/testkiss.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright (c) 2003-2019, Mark Borgerding. All rights reserved. 3 | # This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | # 5 | # SPDX-License-Identifier: BSD-3-Clause 6 | # See COPYING file for more information. 7 | from __future__ import absolute_import, division, print_function 8 | import math 9 | import sys 10 | import os 11 | import random 12 | import struct 13 | import getopt 14 | import numpy as np 15 | 16 | po = math.pi 17 | e = math.e 18 | do_real = False 19 | datatype = os.environ.get('KISSFFT_DATATYPE', 'float') 20 | openmp = os.environ.get('KISSFFT_OPENMP', 'float') 21 | 22 | util = './fastfilt-' + datatype 23 | 24 | if openmp == '1' or openmp == 'ON': 25 | util = util + '-openmp' 26 | 27 | minsnr = 90 28 | if datatype == 'double': 29 | dtype = np.float64 30 | elif datatype == 'float': 31 | dtype = np.float32 32 | elif datatype == 'int16_t': 33 | dtype = np.int16 34 | minsnr = 10 35 | elif datatype == 'int32_t': 36 | dtype = np.int32 37 | elif datatype == 'simd': 38 | sys.stderr.write('testkiss.py does not yet test simd') 39 | sys.exit(0) 40 | else: 41 | sys.stderr.write('unrecognized datatype {0}\n'.format(datatype)) 42 | sys.exit(1) 43 | 44 | def dopack(x): 45 | if np.iscomplexobj(x): 46 | x = x.astype(np.complex128).view(np.float64) 47 | else: 48 | x = x.astype(np.float64) 49 | return x.astype(dtype).tobytes() 50 | 51 | def dounpack(x, cpx): 52 | x = np.frombuffer(x, dtype).astype(np.float64) 53 | if cpx: 54 | x = x[::2] + 1j * x[1::2] 55 | return x 56 | 57 | def make_random(shape): 58 | 'create random uniform (-1,1) data of the given shape' 59 | if do_real: 60 | return np.random.uniform(-1, 1, shape) 61 | else: 62 | return (np.random.uniform(-1, 1, shape) + 1j * np.random.uniform(-1, 1, shape)) 63 | 64 | def randmat(ndim): 65 | 'create a random multidimensional array in range (-1,1)' 66 | dims = np.random.randint(2, 5, ndim) 67 | if do_real: 68 | dims[-1] = (dims[-1] // 2) * 2 # force even last dimension if real 69 | return make_random(dims) 70 | 71 | def test_fft(ndim): 72 | x = randmat(ndim) 73 | 74 | if do_real: 75 | xver = np.fft.rfftn(x) 76 | else: 77 | xver = np.fft.fftn(x) 78 | 79 | x2 = dofft(x, do_real) 80 | err = xver - x2 81 | errf = err.ravel() 82 | xverf = xver.ravel() 83 | errpow = np.vdot(errf, errf) + 1e-10 84 | sigpow = np.vdot(xverf, xverf) + 1e-10 85 | snr = 10 * math.log10(abs(sigpow / errpow)) 86 | print('SNR (compared to NumPy) : {0:.1f}dB'.format(float(snr))) 87 | 88 | if snr < minsnr: 89 | print('xver=', xver) 90 | print('x2=', x2) 91 | print('err', err) 92 | sys.exit(1) 93 | 94 | def dofft(x, isreal): 95 | dims = list(np.shape(x)) 96 | x = x.ravel() 97 | 98 | scale = 1 99 | if datatype == 'int16_t': 100 | x = 32767 * x 101 | scale = len(x) / 32767.0 102 | elif datatype == 'int32_t': 103 | x = 2147483647.0 * x 104 | scale = len(x) / 2147483647.0 105 | 106 | cmd = util + ' -n ' 107 | cmd += ','.join([str(d) for d in dims]) 108 | if do_real: 109 | cmd += ' -R ' 110 | 111 | print(cmd) 112 | 113 | from subprocess import Popen, PIPE 114 | p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) 115 | 116 | p.stdin.write(dopack(x)) 117 | p.stdin.close() 118 | 119 | res = dounpack(p.stdout.read(), 1) 120 | if do_real: 121 | dims[-1] = (dims[-1] // 2) + 1 122 | 123 | res = scale * res 124 | 125 | p.wait() 126 | return np.reshape(res, dims) 127 | 128 | def main(): 129 | opts, args = getopt.getopt(sys.argv[1:], 'r') 130 | opts = dict(opts) 131 | global do_real 132 | do_real = '-r' in opts 133 | if do_real: 134 | print('Testing multi-dimensional real FFTs') 135 | else: 136 | print('Testing multi-dimensional FFTs') 137 | 138 | for dim in range(1, 4): 139 | test_fft(dim) 140 | 141 | 142 | if __name__ == "__main__": 143 | main() 144 | -------------------------------------------------------------------------------- /test/twotonetest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include "kiss_fft.h" 12 | #include "kiss_fftr.h" 13 | #include 14 | 15 | 16 | static 17 | double two_tone_test( int nfft, int bin1,int bin2) 18 | { 19 | kiss_fftr_cfg cfg = NULL; 20 | kiss_fft_cpx *kout = NULL; 21 | kiss_fft_scalar *tbuf = NULL; 22 | 23 | int i; 24 | double f1 = bin1*2*M_PI/nfft; 25 | double f2 = bin2*2*M_PI/nfft; 26 | double sigpow=0; 27 | double noisepow=0; 28 | #if FIXED_POINT==32 29 | long maxrange = LONG_MAX; 30 | #else 31 | long maxrange = SHRT_MAX;/* works fine for float too*/ 32 | #endif 33 | 34 | cfg = kiss_fftr_alloc(nfft , 0, NULL, NULL); 35 | tbuf = KISS_FFT_MALLOC(nfft * sizeof(kiss_fft_scalar)); 36 | kout = KISS_FFT_MALLOC(nfft * sizeof(kiss_fft_cpx)); 37 | 38 | /* generate a signal with two tones*/ 39 | for (i = 0; i < nfft; i++) { 40 | #ifdef USE_SIMD 41 | tbuf[i] = _mm_set1_ps( (maxrange>>1)*cos(f1*i) 42 | + (maxrange>>1)*cos(f2*i) ); 43 | #else 44 | tbuf[i] = (maxrange>>1)*cos(f1*i) 45 | + (maxrange>>1)*cos(f2*i); 46 | #endif 47 | } 48 | 49 | kiss_fftr(cfg, tbuf, kout); 50 | 51 | for (i=0;i < (nfft/2+1);++i) { 52 | #ifdef USE_SIMD 53 | double tmpr = (double)*(float*)&kout[i].r / (double)maxrange; 54 | double tmpi = (double)*(float*)&kout[i].i / (double)maxrange; 55 | #else 56 | double tmpr = (double)kout[i].r / (double)maxrange; 57 | double tmpi = (double)kout[i].i / (double)maxrange; 58 | #endif 59 | double mag2 = tmpr*tmpr + tmpi*tmpi; 60 | if (i!=0 && i!= nfft/2) 61 | mag2 *= 2; /* all bins except DC and Nyquist have symmetric counterparts implied*/ 62 | 63 | /* if there is power in one of the expected bins, it is signal, otherwise noise*/ 64 | if ( i!=bin1 && i != bin2 ) 65 | noisepow += mag2; 66 | else 67 | sigpow += mag2; 68 | } 69 | kiss_fft_cleanup(); 70 | /*printf("TEST %d,%d,%d noise @ %fdB\n",nfft,bin1,bin2,10*log10(noisepow/sigpow +1e-30) );*/ 71 | return 10*log10(sigpow/(noisepow+1e-50) ); 72 | } 73 | 74 | int main(int argc,char ** argv) 75 | { 76 | int nfft = 4*2*2*3*5; 77 | if (argc>1) nfft = atoi(argv[1]); 78 | 79 | int i,j; 80 | double minsnr = 500; 81 | double maxsnr = -500; 82 | double snr; 83 | for (i=0;i>4)+1) { 84 | for (j=i;j>4)+7) { 85 | snr = two_tone_test(nfft,i,j); 86 | if (snrmaxsnr) { 90 | maxsnr=snr; 91 | } 92 | } 93 | } 94 | snr = two_tone_test(nfft,nfft/2,nfft/2); 95 | if (snrmaxsnr) maxsnr=snr; 97 | 98 | printf("TwoToneTest: snr ranges from %ddB to %ddB\n",(int)minsnr,(int)maxsnr); 99 | printf("sizeof(kiss_fft_scalar) = %d\n",(int)sizeof(kiss_fft_scalar) ); 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /tools/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_kissfft_executable(fastconvr kiss_fastfir.c) 2 | target_compile_definitions(fastconvr PRIVATE REAL_FASTFIR FAST_FILT_UTIL) 3 | 4 | add_kissfft_executable(fastconv kiss_fastfir.c) 5 | target_compile_definitions(fastconv PRIVATE FAST_FILT_UTIL) 6 | 7 | add_kissfft_executable(fft fftutil.c) 8 | 9 | install(TARGETS fastconv fastconvr fft 10 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 11 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 12 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 13 | PUBLIC_HEADER DESTINATION ${PKGINCLUDEDIR}) 14 | 15 | # psdpng does not build with "simd" datatype 16 | if(NOT KISSFFT_DATATYPE MATCHES "simd") 17 | include(FindPkgConfig) 18 | pkg_check_modules(libpng REQUIRED IMPORTED_TARGET libpng) 19 | add_kissfft_executable(psdpng psdpng.c) 20 | target_link_libraries(psdpng PRIVATE PkgConfig::libpng) 21 | install(TARGETS psdpng 22 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 23 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 24 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 25 | PUBLIC_HEADER DESTINATION ${PKGINCLUDEDIR}) 26 | endif() 27 | 28 | #FIXME: dumphdr.c is not available 29 | #add_kissfft_executable(dumphdr dumphdr.c) 30 | -------------------------------------------------------------------------------- /tools/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Warnings 3 | # 4 | 5 | WARNINGS = -W -Wall -Wstrict-prototypes -Wmissing-prototypes \ 6 | -Wcast-align -Wcast-qual -Wnested-externs -Wshadow -Wbad-function-cast \ 7 | -Wwrite-strings 8 | 9 | # 10 | # Compile-time definitions 11 | # 12 | 13 | CFLAGS = -Wall -O3 $(WARNINGS) 14 | #CFLAGS = -Wall -O3 -pedantic -march=pentiumpro -ffast-math -fomit-frame-pointer $(WARNINGS) 15 | # If the above flags do not work, try the following 16 | # tip: try -openmp or -fopenmp to use multiple cores 17 | 18 | CFLAGS += $(CFLAGADD) 19 | 20 | # 21 | # Check missing external libraries 22 | # 23 | 24 | ifneq ($(MAKECMDGOALS),clean) 25 | LIBPNG_MISSING = $(shell echo "int main(){return 0;}" > _test_library_dummy.c; \ 26 | $(CC) -o _test_library_dummy _test_library_dummy.c -lpng; \ 27 | echo $$?; \ 28 | rm -f _test_library_dummy.c _test_library_dummy) 29 | endif 30 | 31 | # 32 | # Tool names 33 | # 34 | 35 | ifneq ($(KISSFFT_OPENMP),1) 36 | FFTUTIL = fft-$(KISSFFT_DATATYPE) 37 | FASTFILT = fastconv-$(KISSFFT_DATATYPE) 38 | FASTFILTREAL = fastconvr-$(KISSFFT_DATATYPE) 39 | PSDPNG = psdpng-$(KISSFFT_DATATYPE) 40 | DUMPHDR = dumphdr-$(KISSFFT_DATATYPE) 41 | else 42 | FFTUTIL = fft-$(KISSFFT_DATATYPE)-openmp 43 | FASTFILT = fastconv-$(KISSFFT_DATATYPE)-openmp 44 | FASTFILTREAL = fastconvr-$(KISSFFT_DATATYPE)-openmp 45 | PSDPNG = psdpng-$(KISSFFT_DATATYPE)-openmp 46 | DUMPHDR = dumphdr-$(KISSFFT_DATATYPE)-openmp 47 | endif 48 | 49 | # 50 | # Target: "make all" 51 | # 52 | 53 | all: $(FFTUTIL) $(FASTFILT) $(FASTFILTREAL) $(PSDPNG) 54 | # $(DUMPHDR) 55 | 56 | # 57 | # Individual tool make rules 58 | # 59 | 60 | $(FASTFILTREAL): kiss_fastfir.c 61 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) -DREAL_FASTFIR $< -DFAST_FILT_UTIL -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 62 | 63 | $(FASTFILT): kiss_fastfir.c 64 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $< -DFAST_FILT_UTIL -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 65 | 66 | $(FFTUTIL): fftutil.c 67 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 68 | 69 | $(PSDPNG): psdpng.c 70 | ifeq "$(KISSFFT_DATATYPE)" "simd" 71 | $(warning WARNING: psdpng can not utilize SIMD!) 72 | else ifeq ($(LIBPNG_MISSING), 0) 73 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lpng -lm 74 | else 75 | $(error ERROR: no libpng development files found!) 76 | endif 77 | 78 | $(DUMPHDR): dumphdr.c 79 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm 80 | 81 | # 82 | # Target: "make install" 83 | # 84 | 85 | install: all 86 | $(INSTALL) -Dt $(ABS_BINDIR) -m 755 \ 87 | $(FFTUTIL) \ 88 | $(FASTFILT) \ 89 | $(FASTFILTREAL) 90 | 91 | ifneq "$(KISSFFT_DATATYPE)" "simd" 92 | $(INSTALL) -Dt $(ABS_BINDIR) -m 755 \ 93 | $(PSDPNG) 94 | endif 95 | 96 | # 97 | # Target: "make clean" 98 | # 99 | 100 | clean: 101 | rm -f *~ fft fft-* fastconv fastconv-* fastconvr fastconvr-* psdpng psdpng-* _test_library_dummy _test_library_dummy.c 102 | -------------------------------------------------------------------------------- /tools/fftutil.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "kiss_fft.h" 16 | #include "kiss_fftndr.h" 17 | 18 | static 19 | void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse) 20 | { 21 | kiss_fft_cfg st; 22 | kiss_fft_cpx * buf; 23 | kiss_fft_cpx * bufout; 24 | 25 | buf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft ); 26 | bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft ); 27 | st = kiss_fft_alloc( nfft ,isinverse ,0,0); 28 | 29 | while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) { 30 | kiss_fft( st , buf ,bufout); 31 | fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout ); 32 | } 33 | free(st); 34 | free(buf); 35 | free(bufout); 36 | } 37 | 38 | static 39 | void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse) 40 | { 41 | kiss_fftnd_cfg st; 42 | kiss_fft_cpx *buf; 43 | int dimprod=1,i; 44 | for (i=0;i 0) { 51 | kiss_fftnd (st, buf, buf); 52 | fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout); 53 | } 54 | free (st); 55 | free (buf); 56 | } 57 | 58 | 59 | 60 | static 61 | void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse) 62 | { 63 | int dimprod=1,i; 64 | kiss_fftndr_cfg st; 65 | void *ibuf; 66 | void *obuf; 67 | int insize,outsize; // size in bytes 68 | 69 | for (i=0;i 0) { 85 | if (isinverse) { 86 | kiss_fftndri(st, 87 | (kiss_fft_cpx*)ibuf, 88 | (kiss_fft_scalar*)obuf); 89 | }else{ 90 | kiss_fftndr(st, 91 | (kiss_fft_scalar*)ibuf, 92 | (kiss_fft_cpx*)obuf); 93 | } 94 | fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout); 95 | } 96 | free(st); 97 | free(ibuf); 98 | free(obuf); 99 | } 100 | 101 | static 102 | void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse) 103 | { 104 | kiss_fftr_cfg st; 105 | kiss_fft_scalar * rbuf; 106 | kiss_fft_cpx * cbuf; 107 | 108 | rbuf = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar) * nfft ); 109 | cbuf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * (nfft/2+1) ); 110 | st = kiss_fftr_alloc( nfft ,isinverse ,0,0); 111 | 112 | if (isinverse==0) { 113 | while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) { 114 | kiss_fftr( st , rbuf ,cbuf); 115 | fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout ); 116 | } 117 | }else{ 118 | while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) { 119 | kiss_fftri( st , cbuf ,rbuf); 120 | fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout ); 121 | } 122 | } 123 | free(st); 124 | free(rbuf); 125 | free(cbuf); 126 | } 127 | 128 | static 129 | int get_dims(char * arg,int * dims) 130 | { 131 | char *p0; 132 | int ndims=0; 133 | 134 | do{ 135 | p0 = strchr(arg,','); 136 | if (p0) 137 | *p0++ = '\0'; 138 | dims[ndims++] = atoi(arg); 139 | // fprintf(stderr,"dims[%d] = %d\n",ndims-1,dims[ndims-1]); 140 | arg = p0; 141 | }while (p0); 142 | return ndims; 143 | } 144 | 145 | int main(int argc,char ** argv) 146 | { 147 | int isinverse=0; 148 | int isreal=0; 149 | FILE *fin=stdin; 150 | FILE *fout=stdout; 151 | int ndims=1; 152 | int dims[32]; 153 | dims[0] = 1024; /*default fft size*/ 154 | 155 | while (1) { 156 | int c=getopt(argc,argv,"n:iR"); 157 | if (c==-1) break; 158 | switch (c) { 159 | case 'n': 160 | ndims = get_dims(optarg,dims); 161 | break; 162 | case 'i':isinverse=1;break; 163 | case 'R':isreal=1;break; 164 | case '?': 165 | fprintf(stderr,"usage options:\n" 166 | "\t-n d1[,d2,d3...]: fft dimension(s)\n" 167 | "\t-i : inverse\n" 168 | "\t-R : real input samples, not complex\n"); 169 | exit (1); 170 | default:fprintf(stderr,"bad %c\n",c);break; 171 | } 172 | } 173 | 174 | if ( optind < argc ) { 175 | if (strcmp("-",argv[optind]) !=0) 176 | fin = fopen(argv[optind],"rb"); 177 | ++optind; 178 | } 179 | 180 | if ( optind < argc ) { 181 | if ( strcmp("-",argv[optind]) !=0 ) 182 | fout = fopen(argv[optind],"wb"); 183 | ++optind; 184 | } 185 | 186 | if (ndims==1) { 187 | if (isreal) 188 | fft_file_real(fin,fout,dims[0],isinverse); 189 | else 190 | fft_file(fin,fout,dims[0],isinverse); 191 | }else{ 192 | if (isreal) 193 | fft_filend_real(fin,fout,dims,ndims,isinverse); 194 | else 195 | fft_filend(fin,fout,dims,ndims,isinverse); 196 | } 197 | 198 | if (fout!=stdout) fclose(fout); 199 | if (fin!=stdin) fclose(fin); 200 | 201 | return 0; 202 | } 203 | -------------------------------------------------------------------------------- /tools/kiss_fastfir.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #include "_kiss_fft_guts.h" 10 | 11 | 12 | /* 13 | Some definitions that allow real or complex filtering 14 | */ 15 | #ifdef REAL_FASTFIR 16 | #define MIN_FFT_LEN 2048 17 | #include "kiss_fftr.h" 18 | typedef kiss_fft_scalar kffsamp_t; 19 | typedef kiss_fftr_cfg kfcfg_t; 20 | #define FFT_ALLOC kiss_fftr_alloc 21 | #define FFTFWD kiss_fftr 22 | #define FFTINV kiss_fftri 23 | #else 24 | #define MIN_FFT_LEN 1024 25 | typedef kiss_fft_cpx kffsamp_t; 26 | typedef kiss_fft_cfg kfcfg_t; 27 | #define FFT_ALLOC kiss_fft_alloc 28 | #define FFTFWD kiss_fft 29 | #define FFTINV kiss_fft 30 | #endif 31 | 32 | typedef struct kiss_fastfir_state *kiss_fastfir_cfg; 33 | 34 | 35 | 36 | kiss_fastfir_cfg kiss_fastfir_alloc(const kffsamp_t * imp_resp,size_t n_imp_resp, 37 | size_t * nfft,void * mem,size_t*lenmem); 38 | 39 | /* see do_file_filter for usage */ 40 | size_t kiss_fastfir( kiss_fastfir_cfg cfg, kffsamp_t * inbuf, kffsamp_t * outbuf, size_t n, size_t *offset); 41 | 42 | 43 | 44 | static int verbose=0; 45 | 46 | 47 | struct kiss_fastfir_state{ 48 | size_t nfft; 49 | size_t ngood; 50 | kfcfg_t fftcfg; 51 | kfcfg_t ifftcfg; 52 | kiss_fft_cpx * fir_freq_resp; 53 | kiss_fft_cpx * freqbuf; 54 | size_t n_freq_bins; 55 | kffsamp_t * tmpbuf; 56 | }; 57 | 58 | 59 | kiss_fastfir_cfg kiss_fastfir_alloc( 60 | const kffsamp_t * imp_resp,size_t n_imp_resp, 61 | size_t *pnfft, /* if <= 0, an appropriate size will be chosen */ 62 | void * mem,size_t*lenmem) 63 | { 64 | kiss_fastfir_cfg st = NULL; 65 | size_t len_fftcfg,len_ifftcfg; 66 | size_t memneeded = sizeof(struct kiss_fastfir_state); 67 | char * ptr; 68 | size_t i; 69 | size_t nfft=0; 70 | float scale; 71 | int n_freq_bins; 72 | if (pnfft) 73 | nfft=*pnfft; 74 | 75 | if (nfft<=0) { 76 | /* determine fft size as next power of two at least 2x 77 | the impulse response length*/ 78 | i=n_imp_resp-1; 79 | nfft=2; 80 | do{ 81 | nfft<<=1; 82 | }while (i>>=1); 83 | #ifdef MIN_FFT_LEN 84 | if ( nfft < MIN_FFT_LEN ) 85 | nfft=MIN_FFT_LEN; 86 | #endif 87 | } 88 | if (pnfft) 89 | *pnfft = nfft; 90 | 91 | #ifdef REAL_FASTFIR 92 | n_freq_bins = nfft/2 + 1; 93 | #else 94 | n_freq_bins = nfft; 95 | #endif 96 | /*fftcfg*/ 97 | FFT_ALLOC (nfft, 0, NULL, &len_fftcfg); 98 | memneeded += len_fftcfg; 99 | /*ifftcfg*/ 100 | FFT_ALLOC (nfft, 1, NULL, &len_ifftcfg); 101 | memneeded += len_ifftcfg; 102 | /* tmpbuf */ 103 | memneeded += sizeof(kffsamp_t) * nfft; 104 | /* fir_freq_resp */ 105 | memneeded += sizeof(kiss_fft_cpx) * n_freq_bins; 106 | /* freqbuf */ 107 | memneeded += sizeof(kiss_fft_cpx) * n_freq_bins; 108 | 109 | if (lenmem == NULL) { 110 | st = (kiss_fastfir_cfg) malloc (memneeded); 111 | } else { 112 | if (*lenmem >= memneeded) 113 | st = (kiss_fastfir_cfg) mem; 114 | *lenmem = memneeded; 115 | } 116 | if (!st) 117 | return NULL; 118 | 119 | st->nfft = nfft; 120 | st->ngood = nfft - n_imp_resp + 1; 121 | st->n_freq_bins = n_freq_bins; 122 | ptr=(char*)(st+1); 123 | 124 | st->fftcfg = (kfcfg_t)ptr; 125 | ptr += len_fftcfg; 126 | 127 | st->ifftcfg = (kfcfg_t)ptr; 128 | ptr += len_ifftcfg; 129 | 130 | st->tmpbuf = (kffsamp_t*)ptr; 131 | ptr += sizeof(kffsamp_t) * nfft; 132 | 133 | st->freqbuf = (kiss_fft_cpx*)ptr; 134 | ptr += sizeof(kiss_fft_cpx) * n_freq_bins; 135 | 136 | st->fir_freq_resp = (kiss_fft_cpx*)ptr; 137 | ptr += sizeof(kiss_fft_cpx) * n_freq_bins; 138 | 139 | FFT_ALLOC (nfft,0,st->fftcfg , &len_fftcfg); 140 | FFT_ALLOC (nfft,1,st->ifftcfg , &len_ifftcfg); 141 | 142 | memset(st->tmpbuf,0,sizeof(kffsamp_t)*nfft); 143 | /*zero pad in the middle to left-rotate the impulse response 144 | This puts the scrap samples at the end of the inverse fft'd buffer */ 145 | st->tmpbuf[0] = imp_resp[ n_imp_resp - 1 ]; 146 | for (i=0;itmpbuf[ nfft - n_imp_resp + 1 + i ] = imp_resp[ i ]; 148 | } 149 | 150 | FFTFWD(st->fftcfg,st->tmpbuf,st->fir_freq_resp); 151 | 152 | /* TODO: this won't work for fixed point */ 153 | scale = 1.0 / st->nfft; 154 | 155 | for ( i=0; i < st->n_freq_bins; ++i ) { 156 | #ifdef USE_SIMD 157 | st->fir_freq_resp[i].r *= _mm_set1_ps(scale); 158 | st->fir_freq_resp[i].i *= _mm_set1_ps(scale); 159 | #else 160 | st->fir_freq_resp[i].r *= scale; 161 | st->fir_freq_resp[i].i *= scale; 162 | #endif 163 | } 164 | return st; 165 | } 166 | 167 | static void fastconv1buf(const kiss_fastfir_cfg st,const kffsamp_t * in,kffsamp_t * out) 168 | { 169 | size_t i; 170 | /* multiply the frequency response of the input signal by 171 | that of the fir filter*/ 172 | FFTFWD( st->fftcfg, in , st->freqbuf ); 173 | for ( i=0; in_freq_bins; ++i ) { 174 | kiss_fft_cpx tmpsamp; 175 | C_MUL(tmpsamp,st->freqbuf[i],st->fir_freq_resp[i]); 176 | st->freqbuf[i] = tmpsamp; 177 | } 178 | 179 | /* perform the inverse fft*/ 180 | FFTINV(st->ifftcfg,st->freqbuf,out); 181 | } 182 | 183 | /* n : the size of inbuf and outbuf in samples 184 | return value: the number of samples completely processed 185 | n-retval samples should be copied to the front of the next input buffer */ 186 | static size_t kff_nocopy( 187 | kiss_fastfir_cfg st, 188 | const kffsamp_t * inbuf, 189 | kffsamp_t * outbuf, 190 | size_t n) 191 | { 192 | size_t norig=n; 193 | while (n >= st->nfft ) { 194 | fastconv1buf(st,inbuf,outbuf); 195 | inbuf += st->ngood; 196 | outbuf += st->ngood; 197 | n -= st->ngood; 198 | } 199 | return norig - n; 200 | } 201 | 202 | static 203 | size_t kff_flush(kiss_fastfir_cfg st,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n) 204 | { 205 | size_t zpad=0,ntmp; 206 | 207 | ntmp = kff_nocopy(st,inbuf,outbuf,n); 208 | n -= ntmp; 209 | inbuf += ntmp; 210 | outbuf += ntmp; 211 | 212 | zpad = st->nfft - n; 213 | memset(st->tmpbuf,0,sizeof(kffsamp_t)*st->nfft ); 214 | memcpy(st->tmpbuf,inbuf,sizeof(kffsamp_t)*n ); 215 | 216 | fastconv1buf(st,st->tmpbuf,st->tmpbuf); 217 | 218 | memcpy(outbuf,st->tmpbuf,sizeof(kffsamp_t)*( st->ngood - zpad )); 219 | return ntmp + st->ngood - zpad; 220 | } 221 | 222 | size_t kiss_fastfir( 223 | kiss_fastfir_cfg vst, 224 | kffsamp_t * inbuf, 225 | kffsamp_t * outbuf, 226 | size_t n_new, 227 | size_t *offset) 228 | { 229 | size_t ntot = n_new + *offset; 230 | if (n_new==0) { 231 | return kff_flush(vst,inbuf,outbuf,ntot); 232 | }else{ 233 | size_t nwritten = kff_nocopy(vst,inbuf,outbuf,ntot); 234 | *offset = ntot - nwritten; 235 | /*save the unused or underused samples at the front of the input buffer */ 236 | memcpy( inbuf , inbuf+nwritten , *offset * sizeof(kffsamp_t) ); 237 | return nwritten; 238 | } 239 | } 240 | 241 | #ifdef FAST_FILT_UTIL 242 | #include 243 | #include 244 | #include 245 | #include 246 | 247 | static 248 | void direct_file_filter( 249 | FILE * fin, 250 | FILE * fout, 251 | const kffsamp_t * imp_resp, 252 | size_t n_imp_resp) 253 | { 254 | size_t nlag = n_imp_resp - 1; 255 | 256 | const kffsamp_t *tmph; 257 | kffsamp_t *buf, *circbuf; 258 | kffsamp_t outval; 259 | size_t nread; 260 | size_t nbuf; 261 | size_t oldestlag = 0; 262 | size_t k, tap; 263 | #ifndef REAL_FASTFIR 264 | kffsamp_t tmp; 265 | #endif 266 | 267 | nbuf = 4096; 268 | buf = (kffsamp_t *) malloc ( sizeof (kffsamp_t) * nbuf); 269 | circbuf = (kffsamp_t *) malloc (sizeof (kffsamp_t) * nlag); 270 | if (!circbuf || !buf) { 271 | perror("circbuf allocation"); 272 | exit(1); 273 | } 274 | 275 | if ( fread (circbuf, sizeof (kffsamp_t), nlag, fin) != nlag ) { 276 | perror ("insufficient data to overcome transient"); 277 | exit (1); 278 | } 279 | 280 | do { 281 | nread = fread (buf, sizeof (kffsamp_t), nbuf, fin); 282 | if (nread <= 0) 283 | break; 284 | 285 | for (k = 0; k < nread; ++k) { 286 | tmph = imp_resp+nlag; 287 | #ifdef REAL_FASTFIR 288 | # ifdef USE_SIMD 289 | outval = _mm_set1_ps(0); 290 | #else 291 | outval = 0; 292 | #endif 293 | for (tap = oldestlag; tap < nlag; ++tap) 294 | outval += circbuf[tap] * *tmph--; 295 | for (tap = 0; tap < oldestlag; ++tap) 296 | outval += circbuf[tap] * *tmph--; 297 | outval += buf[k] * *tmph; 298 | #else 299 | # ifdef USE_SIMD 300 | outval.r = outval.i = _mm_set1_ps(0); 301 | #else 302 | outval.r = outval.i = 0; 303 | #endif 304 | for (tap = oldestlag; tap < nlag; ++tap){ 305 | C_MUL(tmp,circbuf[tap],*tmph); 306 | --tmph; 307 | C_ADDTO(outval,tmp); 308 | } 309 | 310 | for (tap = 0; tap < oldestlag; ++tap) { 311 | C_MUL(tmp,circbuf[tap],*tmph); 312 | --tmph; 313 | C_ADDTO(outval,tmp); 314 | } 315 | C_MUL(tmp,buf[k],*tmph); 316 | C_ADDTO(outval,tmp); 317 | #endif 318 | 319 | circbuf[oldestlag++] = buf[k]; 320 | buf[k] = outval; 321 | 322 | if (oldestlag == nlag) 323 | oldestlag = 0; 324 | } 325 | 326 | if (fwrite (buf, sizeof (buf[0]), nread, fout) != nread) { 327 | perror ("short write"); 328 | exit (1); 329 | } 330 | } while (nread); 331 | free (buf); 332 | free (circbuf); 333 | } 334 | 335 | static 336 | void do_file_filter( 337 | FILE * fin, 338 | FILE * fout, 339 | const kffsamp_t * imp_resp, 340 | size_t n_imp_resp, 341 | size_t nfft ) 342 | { 343 | int fdout; 344 | size_t n_samps_buf; 345 | 346 | kiss_fastfir_cfg cfg; 347 | kffsamp_t *inbuf,*outbuf; 348 | int nread,nwrite; 349 | size_t idx_inbuf; 350 | 351 | fdout = fileno(fout); 352 | 353 | cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0); 354 | 355 | /* use length to minimize buffer shift*/ 356 | n_samps_buf = 8*4096/sizeof(kffsamp_t); 357 | n_samps_buf = nfft + 4*(nfft-n_imp_resp+1); 358 | 359 | if (verbose) fprintf(stderr,"bufsize=%d\n",(int)(sizeof(kffsamp_t)*n_samps_buf) ); 360 | 361 | 362 | /*allocate space and initialize pointers */ 363 | inbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf); 364 | outbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf); 365 | 366 | idx_inbuf=0; 367 | do{ 368 | /* start reading at inbuf[idx_inbuf] */ 369 | nread = fread( inbuf + idx_inbuf, sizeof(kffsamp_t), n_samps_buf - idx_inbuf,fin ); 370 | 371 | /* If nread==0, then this is a flush. 372 | The total number of samples in input is idx_inbuf + nread . */ 373 | nwrite = kiss_fastfir(cfg, inbuf, outbuf,nread,&idx_inbuf) * sizeof(kffsamp_t); 374 | /* kiss_fastfir moved any unused samples to the front of inbuf and updated idx_inbuf */ 375 | 376 | if ( write(fdout, outbuf, nwrite) != nwrite ) { 377 | perror("short write"); 378 | exit(1); 379 | } 380 | }while ( nread ); 381 | free(cfg); 382 | free(inbuf); 383 | free(outbuf); 384 | } 385 | 386 | int main(int argc,char**argv) 387 | { 388 | kffsamp_t * h; 389 | int use_direct=0; 390 | size_t nh,nfft=0; 391 | FILE *fin=stdin; 392 | FILE *fout=stdout; 393 | FILE *filtfile=NULL; 394 | while (1) { 395 | int c=getopt(argc,argv,"n:h:i:o:vd"); 396 | if (c==-1) break; 397 | switch (c) { 398 | case 'v': 399 | verbose=1; 400 | break; 401 | case 'n': 402 | nfft=atoi(optarg); 403 | break; 404 | case 'i': 405 | fin = fopen(optarg,"rb"); 406 | if (fin==NULL) { 407 | perror(optarg); 408 | exit(1); 409 | } 410 | break; 411 | case 'o': 412 | fout = fopen(optarg,"w+b"); 413 | if (fout==NULL) { 414 | perror(optarg); 415 | exit(1); 416 | } 417 | break; 418 | case 'h': 419 | filtfile = fopen(optarg,"rb"); 420 | if (filtfile==NULL) { 421 | perror(optarg); 422 | exit(1); 423 | } 424 | break; 425 | case 'd': 426 | use_direct=1; 427 | break; 428 | case '?': 429 | fprintf(stderr,"usage options:\n" 430 | "\t-n nfft: fft size to use\n" 431 | "\t-d : use direct FIR filtering, not fast convolution\n" 432 | "\t-i filename: input file\n" 433 | "\t-o filename: output(filtered) file\n" 434 | "\t-n nfft: fft size to use\n" 435 | "\t-h filename: impulse response\n"); 436 | exit (1); 437 | default:fprintf(stderr,"bad %c\n",c);break; 438 | } 439 | } 440 | if (filtfile==NULL) { 441 | fprintf(stderr,"You must supply the FIR coeffs via -h\n"); 442 | exit(1); 443 | } 444 | fseek(filtfile,0,SEEK_END); 445 | nh = ftell(filtfile) / sizeof(kffsamp_t); 446 | if (verbose) fprintf(stderr,"%d samples in FIR filter\n",(int)nh); 447 | h = (kffsamp_t*)malloc(sizeof(kffsamp_t)*nh); 448 | fseek(filtfile,0,SEEK_SET); 449 | if (fread(h,sizeof(kffsamp_t),nh,filtfile) != nh) 450 | fprintf(stderr,"short read on filter file\n"); 451 | 452 | fclose(filtfile); 453 | 454 | if (use_direct) 455 | direct_file_filter( fin, fout, h,nh); 456 | else 457 | do_file_filter( fin, fout, h,nh,nfft); 458 | 459 | if (fout!=stdout) fclose(fout); 460 | if (fin!=stdin) fclose(fin); 461 | 462 | return 0; 463 | } 464 | #endif 465 | -------------------------------------------------------------------------------- /tools/psdpng.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. 3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 | * 5 | * SPDX-License-Identifier: BSD-3-Clause 6 | * See COPYING file for more information. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "kiss_fft.h" 17 | #include "kiss_fftr.h" 18 | 19 | int nfft=1024; 20 | FILE * fin=NULL; 21 | FILE * fout=NULL; 22 | 23 | int navg=20; 24 | int remove_dc=0; 25 | int nrows=0; 26 | float * vals=NULL; 27 | int stereo=0; 28 | 29 | static 30 | void config(int argc,char** argv) 31 | { 32 | while (1) { 33 | int c = getopt (argc, argv, "n:r:as"); 34 | if (c == -1) 35 | break; 36 | switch (c) { 37 | case 'n': nfft=(int)atoi(optarg);break; 38 | case 'r': navg=(int)atoi(optarg);break; 39 | case 'a': remove_dc=1;break; 40 | case 's': stereo=1;break; 41 | case '?': 42 | fprintf (stderr, "usage options:\n" 43 | "\t-n d: fft dimension(s) [1024]\n" 44 | "\t-r d: number of rows to average [20]\n" 45 | "\t-a : remove average from each fft buffer\n" 46 | "\t-s : input is stereo, channels will be combined before fft\n" 47 | "16 bit machine format real input is assumed\n" 48 | ); 49 | break; 50 | default: 51 | fprintf (stderr, "bad %c\n", c); 52 | exit (1); 53 | break; 54 | } 55 | } 56 | if ( optind < argc ) { 57 | if (strcmp("-",argv[optind]) !=0) 58 | fin = fopen(argv[optind],"rb"); 59 | ++optind; 60 | } 61 | 62 | if ( optind < argc ) { 63 | if ( strcmp("-",argv[optind]) !=0 ) 64 | fout = fopen(argv[optind],"wb"); 65 | ++optind; 66 | } 67 | if (fin==NULL) 68 | fin=stdin; 69 | if (fout==NULL) 70 | fout=stdout; 71 | } 72 | 73 | #define CHECKNULL(p) if ( (p)==NULL ) do { fprintf(stderr,"CHECKNULL failed @ %s(%d): %s\n",__FILE__,__LINE__,#p );exit(1);} while(0) 74 | 75 | typedef struct 76 | { 77 | png_byte r; 78 | png_byte g; 79 | png_byte b; 80 | } rgb_t; 81 | 82 | static 83 | void val2rgb(float x,rgb_t *p) 84 | { 85 | const double pi = 3.14159265358979; 86 | p->g = (int)(255*sin(x*pi)); 87 | p->r = (int)(255*abs(sin(x*pi*3/2))); 88 | p->b = (int)(255*abs(sin(x*pi*5/2))); 89 | //fprintf(stderr,"%.2f : %d,%d,%d\n",x,(int)p->r,(int)p->g,(int)p->b); 90 | } 91 | 92 | static 93 | void cpx2pixels(rgb_t * res,const float * fbuf,size_t n) 94 | { 95 | size_t i; 96 | float minval,maxval,valrange; 97 | minval=maxval=fbuf[0]; 98 | 99 | for (i = 0; i < n; ++i) { 100 | if (fbuf[i] > maxval) maxval = fbuf[i]; 101 | if (fbuf[i] < minval) minval = fbuf[i]; 102 | } 103 | 104 | fprintf(stderr,"min ==%f,max=%f\n",minval,maxval); 105 | valrange = maxval-minval; 106 | if (valrange == 0) { 107 | fprintf(stderr,"min == max == %f\n",minval); 108 | exit (1); 109 | } 110 | 111 | for (i = 0; i < n; ++i) 112 | val2rgb( (fbuf[i] - minval)/valrange , res+i ); 113 | } 114 | 115 | static 116 | void transform_signal(void) 117 | { 118 | short *inbuf; 119 | kiss_fftr_cfg cfg=NULL; 120 | kiss_fft_scalar *tbuf; 121 | kiss_fft_cpx *fbuf; 122 | float *mag2buf; 123 | int i; 124 | int n; 125 | int avgctr=0; 126 | 127 | int nfreqs=nfft/2+1; 128 | 129 | CHECKNULL( cfg=kiss_fftr_alloc(nfft,0,0,0) ); 130 | CHECKNULL( inbuf=(short*)malloc(sizeof(short)*2*nfft ) ); 131 | CHECKNULL( tbuf=(kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*nfft ) ); 132 | CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) ); 133 | CHECKNULL( mag2buf=(float*)calloc(nfreqs,sizeof(float) ) ); 134 | 135 | while (1) { 136 | if (stereo) { 137 | n = fread(inbuf,sizeof(short)*2,nfft,fin); 138 | if (n != nfft ) 139 | break; 140 | for (i=0;i