# FFT in Processing

**URL:** https://discourse.processing.org/t/fft-in-processing/38463
**Category:** Electronics (Arduino, etc.)
**Created:** [August 23, 2022, 10:31am UTC](https://discourse.processing.org/t/fft-in-processing/38463 "2022-08-23T10:31:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![onelectron](https://avatars.discourse-cdn.com/v4/letter/o/7993a0/32.png) [@onelectron](https://discourse.processing.org/u/onelectron)
#### Post date: [August 23, 2022, 10:31am UTC](https://discourse.processing.org/t/fft-in-processing/38463/1 "2022-08-23T10:31:03Z")

</div>

Hello,  
I am trying to send data from an accelerometer to the Processing through UART. It’s working. My final goal is to use the sensor data to create an FFT visual interface to visualize the vibration frequency. I have seen the “sound” library which has a FFT function. But I am not sure how to use it with the UART data.

I can create an array of 512 samples from the data received over UART. I was thinking of using that array to draw the FFT. So, in a way, the program will collect a packet of data points (512 data points) from the UART. After collecting the data, it will run the FFT function using that data, display the FFT result through processing, and update the next frame with the next set of data coming from the UART and it will continuously perform that operation.

Any suggestions on how I can do it?

Thank you.

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [August 25, 2022, 10:56am UTC](https://discourse.processing.org/t/fft-in-processing/38463/2 "2022-08-25T10:56:07Z")

</div>

Hi @onelectron, An interesting project but I think you won’t get much help with the question as it is. The forum works better with specific questions. “It’s working” - what’s working? What data is coming out? Where is the data now? In an array? what type? dimensions? Maybe you are asking “How do I push this data into the sound library FFT function?” I think that should be in libraries section of the forum, better chance of someone familiar seeing it.

---

<div class="post-metadata">

### Author: ![onelectron](https://avatars.discourse-cdn.com/v4/letter/o/7993a0/32.png) [@onelectron](https://discourse.processing.org/u/onelectron)
#### Post date: [August 25, 2022, 11:12am UTC](https://discourse.processing.org/t/fft-in-processing/38463/3 "2022-08-25T11:12:24Z")

</div>

Thank you @RichardDL for the suggestion. I’ll modify the question and repost it to the libraries section of the forum.

---

<div class="post-metadata">

### Author: ![onelectron](https://avatars.discourse-cdn.com/v4/letter/o/7993a0/32.png) [@onelectron](https://discourse.processing.org/u/onelectron)
#### Post date: [August 25, 2022, 11:25am UTC](https://discourse.processing.org/t/fft-in-processing/38463/4 "2022-08-25T11:25:40Z")

</div>

> [@RichardDL](#):
>
> How do I push this data into the sound library FFT function?

Here is the link of the new post:

> [@How do I push UART data into the sound library's FFT function?](https://discourse.processing.org/t/how-do-i-push-uart-data-into-the-sound-librarys-fft-function/38484):
>
> Hello, I am trying to send data from an accelerometer to the Processing through UART. I was able to accomplish that portion of my project. My final goal is to use the sensor data to create an FFT visual interface to visualize the vibration frequency. I have seen the “sound” library which has a FFT function. But I am not sure how to use it with the UART data. Is there a way to pass the uart data to the FFT function of the sound library so that other than talking samples from an audio file, the F…

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [August 25, 2022, 2:01pm UTC](https://discourse.processing.org/t/fft-in-processing/38463/5 "2022-08-25T14:01:38Z")

</div>

Simple FFT if yo don’t need sound library

```auto

/ ***************************************************************
	 * fft.c
	 * Douglas L. Jones
	 * University of Illinois at Urbana-Champaign
	 * January 19, 1992
	 * http://cnx.rice.edu/content/m12016/latest/
	 *
	 * fft: in-place radix-2 DIT DFT of a complex input
	 *
	 * input:
	 * n: length of FFT: must be a power of two
	 * m: n = 2**m
	 * input/output
	 * x: double array of length n with real part of data
	 * y: double array of length n with imag part of data
	 *
	 * Permission to copy and use this program is granted
	 * as long as this header is included.
	 **************************************************************** /
	public void fft(float[] x, float[] y)
	{
		int i,j,k,n1,n2,a;
		float c,s,e,t1,t2;

		// Bit-reverse
		j = 0;
		n2 = n/2;
		for (i=1; i < n - 1; i++) {
			n1 = n2;
			while ( j >= n1 ) {
				j = j - n1;
				n1 = n1/2;
			}
			j = j + n1;

			if (i < j) {
				t1 = x[i];
				x[i] = x[j];
				x[j] = t1;
				t1 = y[i];
				y[i] = y[j];
				y[j] = t1;
			}
		}

		// FFT
		n1 = 0;
		n2 = 1;

		for (i=0; i < m; i++) {
			n1 = n2;
			n2 = n2 + n2;
			a = 0;

			for (j=0; j < n1; j++) {
				c = cos[a];
				s = sin[a];
				a += 1 << (m-i-1);

				for (k=j; k < n; k=k+n2) {
					t1 = c*x[k+n1] - s*y[k+n1];
					t2 = s*x[k+n1] + c*y[k+n1];
					x[k+n1] = x[k] - t1;
					y[k+n1] = y[k] - t2;
					x[k] = x[k] + t1;
					y[k] = y[k] + t2;
				}
			}
		}
	}
}

```

Another example here and you can find the above one also

[https://raw.githubusercontent.com/demantz/RFAnalyzer/master/app/src/main/java/com/mantz\_it/rfanalyzer/FFT.java](https://raw.githubusercontent.com/demantz/RFAnalyzer/master/app/src/main/java/com/mantz_it/rfanalyzer/FFT.java)
