# FFT : spectral transformation basic example

**URL:** <https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588>\
**Category:** General JUCE discussion\
**Created:** [February 6, 2016, 12:48pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588 "2016-02-06T12:48:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptisteb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baptisteb/32/33_2.png) [@baptisteb](https://forum.juce.com/u/baptisteb)\
**Post date:** [February 6, 2016, 12:48pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/1 "2016-02-06T12:48:41Z")

</div>

Hi, I got a bit of difficulties to use the FFT plugin to manipulate the audio on the spectral domain.

I need to get the frequencies and the phase from a time signal, process them, and convert the signal back to the time domain to play it.

Could someone provide a simple example?

Thank you,

Baptiste

---

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [February 6, 2016, 2:00pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/2 "2016-02-06T14:00:33Z")

</div>

You need windowing + overlap add. (I haven't looked the FFT plug-in, but I'm guessing you would still have to implement this yourself).

Have a look at Stefan's code here:

http://blogs.zynaptiq.com/bernsee/pitch-shifting-using-the-ft/

It's hard work getting your head around, if you are not familiar with the math. I can help you out on the IRC channel if you like.

π

---

<div class="post-metadata">

**Author:** ![baptisteb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baptisteb/32/33_2.png) [@baptisteb](https://forum.juce.com/u/baptisteb)\
**Post date:** [February 8, 2016, 10:44am UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/3 "2016-02-08T10:44:23Z")

</div>

Thank you for your reply,

Indeed I will have to implement that myself, and math are not an issue. But I got trouble with the FFT plugin itself.

I would need an example of a signal transformed&nbsp;and reversed back, using the FFT plugin.

Thank you&nbsp;again,

Baptiste

---

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [February 8, 2016, 10:27pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/4 "2016-02-08T22:27:18Z")

</div>

Looking at https://www.juce.com/doc/classFFT ...

First create an order 10 forward FFT

That's going to be 2^10 = 1024 bins, or 2048 input samples

Now create float x[2048], give it a waveform -- maybe a sawtooth with only 1 tooth. So, a ramp from -1 to 1.

Throw it into performRealOnlyForwardTransform

Convert to complex: auto z = (Complex\*)x;

Now you have your frequency bins.

If you want to make a crude lowpass filter for example, set z[512 onwards] to 0

Now convert back to the time domain with performRealOnlyInverseTransform

Try doing a basic loopback and check you get back out what you put in.

Once you have that working, you are ready to work on a big waveform: you will be stepping a size-2048 window through this big waveform in steps of 512. So 4x overlap.

For each frame, you want to envelope/window it (you could Google hanning window)

&nbsp;then fft, filter, ifft

&nbsp;and then add the result to your output waveform at that location.

&nbsp;If you get stuck, post code!

π

---

<div class="post-metadata">

**Author:** ![baptisteb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baptisteb/32/33_2.png) [@baptisteb](https://forum.juce.com/u/baptisteb)\
**Post date:** [February 10, 2016, 11:29am UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/5 "2016-02-10T11:29:29Z")

</div>

Sure, this is my code. It just does an fft and reverse it. But the result is just random floats, not even normalized.

If I remove the 2 transforms the sound is right.

Really, just a couple of lines as an example of a transform and a reverse transform would be nice, even without the overlapping.

```
void MainContentComponent::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
{
    mInstrumentPlayer->getNextAudioBlock(bufferToFill); // fill the buffer with with audio

    int numSamples = bufferToFill.buffer->getNumSamples(); // 512 here

    for (int channel = 0; channel < 2; ++channel)
    {
        float* channelData = bufferToFill.buffer->getWritePointer (channel);

        FFT lFFT(9, false);

        float * freqArray = new float[numSamples * 2];
        memcpy(freqArray, channelData, numSamples * sizeof(float)); // fills the first half of the array with the signal

        lFFT.performRealOnlyForwardTransform(freqArray);
        lFFT.performRealOnlyInverseTransform(freqArray);

        bufferToFill.buffer->copyFrom(channel, 0, freqArray, numSamples);
    }
}
```

Thank you&nbsp;again,

Baptiste

---

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [February 10, 2016, 12:24pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/6 "2016-02-10T12:24:44Z")

</div>

512 samps ~ 256bins. So FFT lFFT(_ **8** ,_ ...);

The documentation should be tidied up here:

https://www.juce.com/doc/classFFT#a4351b09df7fff4082f371b7f4753bd45

_ **"The the number of points the FFT will operate on will be 2 ^ order."** _

**_^_** it should say "number of _ **complex bins** _"

&nbsp;it should also say that 2 real datapoints or 1 complex datapoint generate 1 complex bin.

&nbsp;I think!

You should first be testing on a known waveform. Only put it into the render callback once you know you have it working!

&nbsp;Also **jassert( bufferToFill.buffer-\>getNumSamples() == 512 );** would be advisable. I'm not sure this can always be relied upon.

π

---

<div class="post-metadata">

**Author:** ![baptisteb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baptisteb/32/33_2.png) [@baptisteb](https://forum.juce.com/u/baptisteb)\
**Post date:** [February 10, 2016, 1:09pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/7 "2016-02-10T13:09:33Z")

</div>

Still not working with FFT lFFT(8,&nbsp;...), or with a know wave form, like a sinus.

JUCE seems nice so far. Why is this simple processing so complicated? I will read the source code and figure things out.

Baptiste

---

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [February 10, 2016, 1:43pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/8 "2016-02-10T13:43:00Z")

</div>

There is a demo at https://github.com/julianstorer/JUCE/tree/master/examples/SimpleFFTExample/Source

Does that help?

π

PS If you can construct a minimal example demonstrating failure, I recommend posting it! Then the team can sort it out if it is a bug. Looking through the FFT source code is going to be really hard work!

---

<div class="post-metadata">

**Author:** ![baptisteb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baptisteb/32/33_2.png) [@baptisteb](https://forum.juce.com/u/baptisteb)\
**Post date:** [February 10, 2016, 3:16pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/9 "2016-02-10T15:16:38Z")

</div>

Unfortunately this example only does a forward transform.

I will post the solution here if I find it.

Thank you,

Baptiste

---

<div class="post-metadata">

**Author:** ![HectorPerez](https://avatars.discourse-cdn.com/v4/letter/h/858c86/32.png) [@HectorPerez](https://forum.juce.com/u/HectorPerez)\
**Post date:** [July 21, 2016, 9:28pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/10 "2016-07-21T21:28:26Z")

</div>

> [@\_pi](#):
>
> That’s going to be 2^10 = 1024 bins, or 2048 input samples

> [@\_pi](#):
>
> be

> [@\_pi](#):
>
> be

Hello, BaptisteB

I have this project coming up where we have to create a phase vocoder.  
I was wondering why are you using getNextAudioBlock() instead of processBlock().  
I think processBlock() applies more to a VST plugin, which is what i’m supposed to make.  
Do you think I could/ should use what you’re using?

Thank you,  
Hector

---

<div class="post-metadata">

**Author:** ![baptisteb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baptisteb/32/33_2.png) [@baptisteb](https://forum.juce.com/u/baptisteb)\
**Post date:** [July 25, 2016, 8:36am UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/11 "2016-07-25T08:36:38Z")

</div>

@HectorPerez I didn’t know about this AudioProcessor class. Looks like it is appropriate in your case.  
However, I found out how to make a fft/ifft.

See this topic: [Issue with FFT plugin - Inverse Transformation](https://forum.juce.com/t/issue-with-fft-plugin-inverse-transformation/16630/3)

---

<div class="post-metadata">

**Author:** ![voluntaryzonkey](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/voluntaryzonkey/32/6734_2.png) [@voluntaryzonkey](https://forum.juce.com/u/voluntaryzonkey)\
**Post date:** [February 17, 2021, 4:45pm UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/12 "2021-02-17T16:45:10Z")

</div>

Hi, I’ve also struggling with this and found the JUCE tutorial lacking - do you have any additional fft/ifft reading you’d recommend? Thanks!

EDIT: Sorry just saw your other thread. Still struggling but looking into it!

---

<div class="post-metadata">

**Author:** ![baptisteb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baptisteb/32/33_2.png) [@baptisteb](https://forum.juce.com/u/baptisteb)\
**Post date:** [February 18, 2021, 9:13am UTC](https://forum.juce.com/t/fft-spectral-transformation-basic-example/16588/13 "2021-02-18T09:13:20Z")

</div>

Hey, it’s a bit old but at the time i ended up using an external library called SoundTouch. You can also look at RubberBand. I just found SoundTouch easier to use.
