Code Monkey home page Code Monkey logo

noise's Introduction

Noise

Release

Icon

A FFT computation library for Android

Noise is an Android wrapper for kissfft, a FFT implementation written in C. Noise features an api that is designed to be easy to use, and familiar for Android devs. (JNI bindings are available as well)

Sample app

Watch Noise compute FFT in real time from your microphone, the sample app is on Google Play!

Sample app preview

Get started

Add jitpack.io repo to your root build.gradle:

allprojects {
    repositories {
        //...
        maven { url "https://jitpack.io" }
    }
}

Include in Android project:

implementation 'com.github.paramsen:noise:2.0.0'

Instructions

This lib is a Kotlin wrapper for kissfft, consult the kissfft readme if you want more information about the internal FFT implementation.

Noise supports computing DFT from real and imaginary input data.

Real input

Instantiate, this example is configured to compute DFT:s on input arrays of size 4096.

Noise noise = Noise.real(4096) //input size == 4096

Invoke the FFT on some input data.

float[] src = new float[4096];
float[] dst = new float[4096 + 2]; //real output length equals src+2

// .. fill src with data

// Compute FFT:
    
float[] fft = noise.fft(src, dst);
    
// The result array has the pairs of real+imaginary floats in a one dimensional array; even indices
// are real, odd indices are imaginary. DC bin is located at index 0, 1, nyquist at index n-2, n-1
    
for(int i = 0; i < fft.length / 2; i++) {
    float real = fft[i * 2];
    float imaginary = fft[i * 2 + 1];
    
    System.out.printf("index: %d, real: %.5f, imaginary: %.5f\n", i, real, imaginary);
}

Imaginary input

This example is configured to compute DFT:s on input arrays of size 8192 (4096 [real, imaginary] pairs).

Noise noise = Noise.imaginary(8192) //input size == 8192

In order to compute a DFT from imaginary input, we need to structure our real+imaginary pairs in a flat, one dimensional array. Thus the input array has pairs of real+imaginary like; float[0] = firstReal, float[1] = firstImaginary, float[2] = secondReal, float[3] = secondImaginary..

float[] imaginaryInput = new float[8192];
    
// fill imaginaryInput with data (pairs is an array of pairs with [real, imaginary] objects):
    
for(int i = 0; i < pairs.length; i++) {
    imaginaryInput[i * 2] = pairs[i].real;
    imaginaryInput[i * 2 + 1] = pairs[i].imaginary;
}
    
// Compute the FFT with imaginaryInput:
    
float[] fft = noise.fft(realInput);
    
// The output array has the pairs of real+imaginary floats in a one dimensional array; even indices
// are real, odd indices are imaginary. DC bin is located at index 0, 1, nyquist at index n/2-2, n/2-1
    
for(int i = 0; i < fft.length / 2; i++) {
    float real = fft[i * 2];
    float imaginary = fft[i * 2 + 1];
    
    System.out.printf("index: %d, real: %.5f, imaginary: %.5f\n", i, real, imaginary);
}

Output

Both the real and imaginary implementations produce an array of real and imaginary pairs, in a flat, one dimensional structure.
Thus each even and odd index is a pair of a real and imaginary numbers, we could convert the result array to an array of pairs to better show the relation like:

float[] fft = noise.fft(input);
    
Pair<Float, Float>[] pairs = new Pair<>[fft.length / 2];
    
for(int i = 0; i < fft.length / 2; i++) {
    float real = fft[i * 2];
    float imaginary = fft[i * 2 + 1];
    
    pairs.add(new Pair(real, imaginary));
}

Sample code

I've written a sample app in Kotlin which computes FFT:s on the real time microphone signal. It features some cool Rx solutions for mic integration that might be interesting in themselves. It's on Google Play and the source can be found in the sample module.

Performance tests

The following tests measure the average FFT computation time over 1000 computations for an array of length 4096. Run on a new S8+ and an old LG G3 for comparison.

Samsung S8+:

Optimized Imaginary:   0.32ms
Optimized Real:        0.32ms
Threadsafe Imaginary:  0.38ms
Threadsafe Real:       0.48ms

LG G3:

Optimized Imaginary:   0.76ms
Optimized Real:        0.72ms
Threadsafe Imaginary:  1.02ms
Threadsafe Real:       1.33ms

Tests

The implementation has been tested for compliance with the kissfft C library; for the same input, equal output is given. The tests in the Android test suite that assures that equal output is computed by loading a pre defined data set and asserting the result against a precomputed result.
The precomputed result is generated by the C test suite that runs kissfft directly in C++.

Development

Setup

Kissfft is not bundled in the source of this repository for many reasons, I have resided to let a git module script initiate it with a manual step.

Setup steps are:

  1. Run git submodule init; git submodule update in project root
  2. Check that kissfft exists in noise/src/native/kissfft

Release

There's a Gradle task that generates the README.md from template and git tags the current commit with the version number. JitPack builds on push of the tag.

Release steps are:

  1. Bump version in noise/build.gradle
  2. Run ./gradlew release in project root (generates readme)
  3. Push generated readme changes to repo
  4. Wait for JitPack to build

License

Noise is licensed under the Apache 2.0.
Kissfft is licensed under the Revised BSD License.

noise's People

Contributors

dakeks avatar paramsen 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

noise's Issues

Spectral analysis

Hello,
This is not really an issue, but rather a question about how to use your library.
We are trying to use your library to compute power spectral density on a live audio signal in an Android app. We want results per octave band, or per โ…“ octave band.
Currently we have to following Kotlin code for the octave band version. We adapted this from the code that drives the bottom graph in your demo app.

package com.paramsen.noise
import kotlin.math.log10

class SoundSpectrumUtils {

    companion object {
        private const val bands = 11
        private val lbands: Array<Int> = arrayOf(11, 22, 44, 88, 177, 355, 710, 1420, 2840, 5680, 11360);
        private val ubands = arrayOf(22, 44, 88, 177, 355, 710, 1420, 2840, 5680, 11360, 22050);
        private val bandSizes = IntArray(bands) { i -> ubands[i] - lbands[i] }

        @JvmStatic
        fun calculateBands(samples: FloatArray): FloatArray {
            val noise = Noise.real(samples.size)

            // not sure why this is, but it's in the sample app, see also https://github.com/paramsen/noise/issues/6
            for (i in samples.indices) {
                samples[i] *= 2.0f
            }

            val fft = noise.fft(samples, FloatArray(samples.size + 2))
            val resultFFT = FloatArray(samples.size);
            System.arraycopy(fft, 2, resultFFT, 0, fft.size - 2)

            val resultBands = FloatArray(bands);
            for (i in 0 until bands) {
                var accum = .0f
                for (j in 0 until bandSizes[i] step 2) {
                    //convert real and imag part to get energy
                    accum += (Math.pow(
                            resultFFT[j + lbands[i]].toDouble(),
                            2.0
                    ) + Math.pow(resultFFT[j + 1 + lbands[i]].toDouble(), 2.0)).toFloat()
                }
                accum /= bandSizes[i] / 2
                accum = 10 * log10(accum)
                resultBands[i] = accum;
            }
            return resultBands;
        }
    }
}

We have following challenges with this:

Thanks in advance for any pointers you might be able to give.

Build error

I downloaded your project and compile

CMake Error at CMakeLists.txt:3 (add_library):
  Cannot find source file:

    src/main/native/kissfft/kiss_fft.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

imaginary sample

in the readme for the imaginary input sample, I think the code should read:

Noise noise = Noise.imaginary()

Show calculation of the magic number in FFTBandView

Since the AudioRecord uses AudioFormat.ENCODING_PCM_16BIT, the range of the recorded floats are Short.MIN_VALUE..Short.MAX_VALUE.

Then in MainActivity, the energy values are doubled before applying the FFT. (I'm also curious about the reason for doubling them.)

I would therefore expect the max magnitude to be

val maxConst = (2.0 * Short.MIN_VALUE).pow(2.0) + (2.0 * Short.MAX_VALUE).pow(2.0)

which evaluates to very close to exactly 8 times bigger than what you have.

How did you come up with the value 175M? Clearly there's a problem in my reasoning, because it looks correct with 175M.

Authentication failed

Hi, How do I get an Authentication to clone the git repo into my desktop?
Because i'm getting this error "Authentication failed. You may not have permission to access the repository or the repository may have been archived."

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.