Giter Site home page Giter Site logo

berndporr / kiss-fft Goto Github PK

View Code? Open in Web Editor NEW
82.0 5.0 12.0 1.58 MB

A compact FFT library in C with an Android JNI wrapper

License: BSD 3-Clause "New" or "Revised" License

C++ 38.16% C 31.30% CMake 5.46% Java 25.08%
fft fft-library kiss-fft-library android android-library complex jni-android jni jni-wrapper c

kiss-fft's Introduction

KISS FFT

KISS FFT - A mixed-radix Fast Fourier Transform in C with an Android JNI wrapper.

In C

Complex to Complex FFT and IFFT

The basic usage for 1-d complex FFT is:

#include "kiss_fft.h"

kiss_fft_cfg cfg = kiss_fft_alloc( nfft, is_inverse_fft );
kiss_fft_cpx *cx_in = new kiss_fft_cpx[nfft];
kiss_fft_cpx *cx_out = new kiss_fft_cpx[nfft];
    
// put kth sample in cx_in[k].r and cx_in[k].i
            
kiss_fft( cfg , cx_in , cx_out );
            
// transformed. DC is in cx_out[0].r and cx_out[0].i 
            
free(cfg);
delete[] cx_in;
delete[] cx_out;
  • nfft is the number of samples in both time- and frequency domain
  • is_inverse is 0 for the forward transform and 1 for the inverse
  • cx_in and cx_out are arrays of nfft samples
  • Frequency domain: cx_out[0] is the DC bin of the FFT and cx_out[nfft/2] is the Nyquist bin (if exists).
  • Files: kiss_fft.h, kiss_fft.cpp and _kiss_fft_guts.h.

Real valued FFT

A real valued optimized FFT which takes real valued signals as its input is implemtned in kiss_fftr.h and kiss_fftr.cpp. It returns the positive half-spectrum: (nfft/2+1) complex frequency bins.

Real signal to complex frequencies transform

#include "kiss_fftr.h"

kiss_fftr_cfg cfg = kiss_fftr_alloc(nfft, 0);
double *cx_in = new kiss_fft_scalar[nfft];
kiss_fft_cpx *cx_out = new kiss_fft_cpx[nfft/2+1];

// put `nfft` samples in cx_in[k]

kiss_fftr(cfg, cx_in, cx_out);

// Process the spectrum `cx_out` here: We have `nfft/2+1` (!) samples.
            
free(cfg);
delete[] cx_in;
delete[] cx_out;

Complex frequencies to real signal transform

#include "kiss_fftr.h"

kiss_fftr_cfg cfg = kiss_fftr_alloc(nfft, 1);
kiss_fft_cpx *cx_in = new kiss_fft_cpx[nfft/2+1];
double *cx_out = new kiss_fft_scalar[nfft];

// put kth frequency sample in cx_in[k] up to index `nfft/2`.
// No need to populate the mirror.

kiss_fftr(cfg, cx_in, cx_out);

// Process signal `cx_out` here. It has again `nfft` samples
// and is real valued.
            
free(cfg);
delete[] cx_in;
delete[] cx_out;

Installation / Usage

The library is so small that you can directly include the sources in your project or you can pre-package it as a static library and then link it into your project. Create the static library (with the help of cmake):

cmake .
make
make install

which is installed in the usual places (e.g. /usr/local/lib and /usr/local/include) and is called libkiss-fft.a.

Android

Super-fast native FFTs under Android.

Compilation

Open this project in Android studio and run "Build". Depending on the config you'll generate a debug version of the kiss-fft library or a release version.

Installation

The Android library is in jnifft/build/outputs/. Just import it into your Android project with "New-Module-Android Library" and add the dependency with

compile project(":jnifft-release")

Complex to Complex transform

kissFastFourierTransformer = new KISSFastFourierTransformer();
Complex[] outdata = kissFastFourierTransformer.transform(indata, TransformType.FORWARD);

which transforms from Complex to Complex as defined in the apache Commons. The constant TransformType is also defined in apache Commons which determines if it's a forward or inverse transform. It can be used as a direct replacement of the apache commons FFT function.

There are also convenience functions as implemented in the apache commons library for double and Double which perform the conversion to Complex in C++. The function for the primitive type double is slightly faster than the one with Double.

Real to Complex and Complex to Real transform

For real valued sequences there are two optimised functions which directly perform the FFT on the raw double buffer without any conversion to Complex. For such real valued sequences this runs at least twice as fast as the Complex FFT functions above. The complex frequency sequence of the real sequence of length N has the length N/2+1 and then expands back to length N by the inverse transform:

public Complex[] transformRealOptimisedForward(double[] v)
public double[] transformRealOptimisedInverse(Complex[] v)

Unit tests (Android vs C)

Run FFTTest and FFTRTest which compare the results to that from the apache commons FFT functions and perform an ifft(fft) test to check for rounding errors.

Attribution

This is a fork, stripped down and further debugged version of the original kiss-fft library by Mark Borgerding [email protected].

kiss-fft's People

Contributors

berndporr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kiss-fft's Issues

Unable to set FFT datatype to short

Hey,
I have been trying to set the FFT datatype to short instead of the default double.
I commented #add_definitions(-Dkiss_fft_scalar=double) in the jnifft/cmakelist.txt and instead added add_definitions(-DFIXED_POINT=16) but it produces errors when I build it. Do I have to change the wrapper as well?

Errors:
Caused by: Build command failed.
Error while executing process D:\HammadSdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build C:\Users\Confu008\Desktop\kissfft\jnifft.externalNativeBuild\cmake\debug\x86_64 --target kiss-fft-lib}
[1/4] Building CXX object CMakeFiles/kiss-fft-lib.dir/src/main/cpp/kiss-fft-lib.cpp.o
[2/4] Building C object CMakeFiles/kiss-fft-lib.dir/C_/Users/Confu008/Desktop/kissfft/kiss_fft.c.o
[3/4] Building C object CMakeFiles/kiss-fft-lib.dir/C_/Users/Confu008/Desktop/kissfft/kiss_fftr.c.o
FAILED: D:\HammadSdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=x86_64-none-linux-android21 --gcc-toolchain=D:/HammadSdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/HammadSdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -DFIXED_POINT=16 -Dkiss_fft_lib_EXPORTS -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/kiss-fft-lib.dir/src/main/cpp/kiss-fft-lib.cpp.o -MF CMakeFiles\kiss-fft-lib.dir\src\main\cpp\kiss-fft-lib.cpp.o.d -o CMakeFiles/kiss-fft-lib.dir/src/main/cpp/kiss-fft-lib.cpp.o -c C:\Users\Confu008\Desktop\kissfft\jnifft\src\main\cpp\kiss-fft-lib.cpp
C:\Users\Confu008\Desktop\kissfft\jnifft\src\main\cpp\kiss-fft-lib.cpp:122:5: error: no matching function for call to 'kiss_fftr'

kiss_fftr(cfg, values, outArray);

C:\Users\Confu008\Desktop\kissfft\jnifft\src\main\cpp/../../../../kiss_fftr.h:29:6: note: candidate function not viable: no known conversion from 'double *' to 'const int16_t *' (aka 'const short *') for 2nd argument

void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata);

C:\Users\Confu008\Desktop\kissfft\jnifft\src\main\cpp\kiss-fft-lib.cpp:190:5: error: no matching function for call to 'kiss_fftri'

kiss_fftri(cfg, inArray, outValues);

C:\Users\Confu008\Desktop\kissfft\jnifft\src\main\cpp/../../../../kiss_fftr.h:35:6: note: candidate function not viable: no known conversion from 'double *' to 'int16_t *' (aka 'short *') for 3rd argument

void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata);

2 errors generated.

ninja: build stopped: subcommand failed.

at com.android.build.gradle.internal.cxx.process.ProcessOutputJunction.execute(ProcessOutputJunction.kt:78)
at com.android.build.gradle.internal.cxx.process.ProcessOutputJunction.execute(ProcessOutputJunction.kt:119)
at com.android.build.gradle.tasks.ExternalNativeBuildTask.executeProcessBatch(ExternalNativeBuildTask.java:327)
at com.android.build.gradle.tasks.ExternalNativeBuildTask.build(ExternalNativeBuildTask.java:195)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
... 125 more

current "relation" to https://github.com/mborgerding/kissfft

You mention that:

"
This is a fork, stripped down and further debugged version of the original kiss-fft library by Mark Borgerding [email protected].
"

  • would you be confident to recommend me using your code as a new user?
  • as an end user, what do you think would be the advantages for me using this code rather than the original one?

the number of samples

Does the number of samples of frequency domain must less than the the number of samples of frequency domain.
when I Run the following program
kiss_fft_cfg cfg = kiss_fft_alloc( 1024, 0, 0, 0 );
kiss_fft_cpx *cx_in = new kiss_fft_cpx[400];
kiss_fft_cpx *cx_out = new kiss_fft_cpx[1024];
the value of ou is -nan.
sorry for My native language is not English.

in iOS Swift

Hi,
I want to calculate FFT in my project using array of doubles and length. Is kissfft is useful to calculate fft. If so then how can I install and calculate. Can you please help me. Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.