# FFT in p5.js p5.sound

**URL:** <https://discourse.processing.org/t/fft-in-p5-js-p5-sound/8265>\
**Category:** Libraries\
**Created:** [February 10, 2019, 1:14pm UTC](https://discourse.processing.org/t/fft-in-p5-js-p5-sound/8265 "2019-02-10T13:14:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srinivas](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@srinivas](https://discourse.processing.org/u/srinivas)\
**Post date:** [February 10, 2019, 1:14pm UTC](https://discourse.processing.org/t/fft-in-p5-js-p5-sound/8265/1 "2019-02-10T13:14:44Z")

</div>

How is FFT done in p5.sound…  
i need fft Algorithm of p5.FFT .please…

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 10, 2019, 1:57pm UTC](https://discourse.processing.org/t/fft-in-p5-js-p5-sound/8265/2 "2019-02-10T13:57:09Z")

</div>

did you find

> <https://github.com/processing/p5.js/blob/0.7.3/lib/addons/p5.sound.js#L2810>

  
already?

---

<div class="post-metadata">

**Author:** ![srinivas](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@srinivas](https://discourse.processing.org/u/srinivas)\
**Post date:** [February 10, 2019, 2:35pm UTC](https://discourse.processing.org/t/fft-in-p5-js-p5-sound/8265/3 "2019-02-10T14:35:43Z")

</div>

No…There is no fft algorithm written there …  
I want fft algorithm used in there…

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 10, 2019, 3:31pm UTC](https://discourse.processing.org/t/fft-in-p5-js-p5-sound/8265/4 "2019-02-10T15:31:11Z")

</div>

> [@srinivas](#):
>
> There is no fft algorithm written there

correct, because they (p5.js) not do it, just send the array to…  
more digging:

> <https://github.com/processing/p5.js/blob/0.7.3/lib/addons/p5.sound.js#L3100>

> **[AnalyserNode: getByteFrequencyData() method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData)**
>
> The getByteFrequencyData() method of the AnalyserNode interface copies the current frequency data into a Uint8Array (unsigned byte array) passed into it.

[https://webaudio.github.io/web-audio-api/#current-frequency-data](https://webaudio.github.io/web-audio-api/#current-frequency-data)  
…  
[https://github.com/WebKit/webkit/blob/89c28d471fae35f1788a0f857067896a10af8974/Source/WebCore/platform/audio/FFTFrame.h](https://github.com/WebKit/webkit/blob/89c28d471fae35f1788a0f857067896a10af8974/Source/WebCore/platform/audio/FFTFrame.h)  
[https://github.com/WebKit/webkit/blob/89c28d471fae35f1788a0f857067896a10af8974/Source/WebCore/platform/audio/FFTFrame.cpp](https://github.com/WebKit/webkit/blob/89c28d471fae35f1788a0f857067896a10af8974/Source/WebCore/platform/audio/FFTFrame.cpp)

have fun!

---

<div class="post-metadata">

**Author:** ![jschornagel](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jschornagel](https://discourse.processing.org/u/jschornagel)\
**Post date:** [June 3, 2019, 1:06pm UTC](https://discourse.processing.org/t/fft-in-p5-js-p5-sound/8265/5 "2019-06-03T13:06:05Z")

</div>

I wrote a dft function specifically to process some strain gauge signals. See if this can help you. Do note that the size of four[x][] will determine the sliding window and also the max k value that will be calculated

```auto

//Sliding DFT function that calculates fourier transform
//works with even and uneven windows
void sliding_dft(float time[], float signal[], float[][] fourier) {
  //Make the dimensions of fourier array same as time
  for (int i = 0; i < fourier.length; i++) {
    fourier[i] = expand(fourier[i], time.length);
  }
  
  //Create max_k and n
  int max_k = fourier.length;
  int n = max_k * 2;
  
  //Create RE and IM arrays
  float[][] RE = new float[max_k][fourier[0].length];
  float[][] IM = new float[max_k][fourier[0].length];
  float[][] AMP = new float[max_k][fourier[0].length];
  
  int w_back = n/2;
  int w_up = n/2;
  println("w_back= " + w_back);
  println("w_up= " + w_up);

  for (int i = w_back; i < time.length - w_up; i++) {
    for (int k = 0; k < max_k; k++) {
      for (int j = 0; j < n; j++) {
        RE[k][i] += signal[i - w_back + j] * cos((-TWO_PI * float(k) * float(j))/ float(n));
        IM[k][i] += signal[i - w_back + j] * sin((-TWO_PI * float(k) * float(j))/ float(n)); 
      }
      fourier[k][i] = sqrt(pow(RE[k][i],2) + pow(IM[k][i],2));
      AMP[k][i] = (2 * fourier[k][i])/n;
    }
  }  
}

```
