/**
* This sketch demonstrates very simply how you might use the inverse FFT to modify an audio signal.
* Press 'f' to perform the forward FFT, then press 's' to set one of the frequency bands to 150.
* Now press 'd' to take the inverse FFT. You will see that the wave form now looks like two sine waves that have
* been added together. In fact, this is exactly the case. The sine wave that has been added has the
* same frequency as the frequency band that we artificially changed the value of.
*
* You might wonder what the actual frequency added to the spectrum is.
* That frequency is a fraction of the sampling rate, which can be found with the formula f = i/N
* where f is the fraction of the sampling rate, i is the index of the frequency band,
* and N is the time-domain size of the FFT. In this case we have a 512 point FFT and we are
* changing the frequency band at index 20. So in our case f = 20/512 = 0.0390625
* Our sampling rate is 44100 Hz, a value passed in the Sine constructor,
* so the frequency in Hz that is being added to the spectrum is 44100 * 0.0390625 = 1722.65625 Hz
*
*/
import ddf.minim.analysis.*;
import ddf.minim.signals.*;
FFT fft;
SineWave sine;
float[] buffer;
int bsize = 512;
void setup()
{
size(512, 300, P3D);
// create an FFT with a time-domain size the same as the size of buffer
// it is required that these two values be the same
// and also that the value is a power of two
fft = new FFT(bsize, 44100);
sine = new SineWave(600, 1, 44100);
buffer = new float[bsize];
// fill the buffer with a sine wave
sine.generate(buffer);
}
void draw()
{
background(0);
noStroke();
fill(255, 128);
// draw the waveform
for(int i = 0; i < buffer.length; i++)
{
ellipse(i, 50 + buffer[i]*10, 2, 2);
}
noFill();
stroke(255);
// draw the spectrum
for(int i = 0; i < fft.specSize(); i++)
{
line(i, height, i, height - fft.getBand(i));
}
stroke(255, 0, 0);
line(width/2, height, width/2, 0);
}
void keyReleased()
{
if ( key == 'f' )
{
println("Performing a Forward FFT on buffer.");
fft.forward(buffer);
}
if ( key == 'd' )
{
println("Performing an Inverse FFT and putting the result in buffer.");
fft.inverse(buffer);
}
if ( key == 's' )
{
// by setting frequency band 20 to a high value, we are basically mixing in a sine wave at that frequency
// after setting the frequency band and then taking the inverse FFT, you will see the waveform change
println("Setting frequency band 20 to 150.");
fft.setBand(20, 150);
}
}